-
Notifications
You must be signed in to change notification settings - Fork 9
/
run.py
129 lines (115 loc) · 4.2 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import schedule
import time
import logging
import sys
import argparse
from argparse import RawTextHelpFormatter
from cisco import dnac
"""cisco-dnac-network-devices-cfg-backup-s3 Console Script.
Copyright (c) 2020 Cisco and/or its affiliates.
This software is licensed to you under the terms of the Cisco Sample
Code License, Version 1.1 (the "License"). You may obtain a copy of the
License at
https://developer.cisco.com/docs/licenses
All use of the material herein must be in accordance with the terms of
the License. All rights not expressly granted by the License are
reserved. Unless required by applicable law or agreed to separately in
writing, software distributed under the License is distributed on an "AS
IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied.
"""
__author__ = "Robert Csapo"
__email__ = "rcsapo@cisco.com"
__version__ = "1.1"
__copyright__ = "Copyright (c) 2020 Cisco and/or its affiliates."
__license__ = "Cisco Sample Code License, Version 1.1"
__app__ = "cisco-dnac-network-devices-cfg-backup-s3"
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__app__ + " - help section\n\n"
"Using Environment (os.environ) overrides arguments below",
formatter_class=RawTextHelpFormatter,
)
parser.add_argument(
"--dnac",
nargs=3,
metavar=("DNAC_HOST", "DNAC_USER", "DNAC_PASS"),
help="Cisco DNA Center Hostname\nCisco DNA Center Username\n"
"Cisco DNA Center Password\nCisco DNA Center SSL Verify",
)
parser.add_argument(
"--aws",
nargs=3,
metavar=("S3BUCKET", "AWS_ACCESS_KEY", "AWS_SECRET_KEY"),
help="AWS S3 Bucket S3BUCKET Name\nAWS S3 AWS_ACCESS_KEY\n"
"AWS S3 AWS_SECRET_KEY",
)
parser.add_argument(
"--gcp",
nargs=3,
metavar=("S3BUCKET", "AWS_ACCESS_KEY", "AWS_SECRET_KEY"),
help="Google Cloud Storage S3BUCKET Bucket Name\n"
"Google Cloud Storage ACCESS_KEY\nGoogle Cloud Storage SECRET_KEY",
)
parser.add_argument(
"--do",
nargs=4,
metavar=("S3BUCKET", "AWS_ACCESS_KEY", "AWS_SECRET_KEY", "ENDPOINT_URL"),
help="DigitalOcean Spaces S3BUCKET Bucket Name\n"
"DigitalOcean Spaces ACCESS_KEY\nDigitalOcean Spaces SECRET_KEY\n"
"DigitalOcean Spaces Bucket ENDPOINT_URL",
)
parser.add_argument(
"--minio",
nargs=4,
metavar=("S3BUCKET", "AWS_ACCESS_KEY", "AWS_SECRET_KEY", "ENDPOINT_URL"),
help="MinIO Inc. S3BUCKET Bucket Name\nMinIO Inc. ACCESS_KEY\n"
"MinIO Inc. SECRET_KEY\nMinIO Inc. Server ENDPOINT_URL",
)
parser.add_argument(
"--insecure",
default=True,
action="store_false",
help="Disables SSL/TLS verification",
)
parser.add_argument(
"--api", default="2.1", help="Cisco DNA Center Platform Version"
)
parser.add_argument(
"--version", action="version", version=__app__ + " v" + __version__
)
args = parser.parse_args()
""" Logging events with timestamp """
logging.basicConfig(
format="%(asctime)s %(levelname)-8s %(message)s",
level=logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S",
)
logging.info("Starting Application")
""" Store settings in class """
dnacApi = dnac.dnacApiClass(args)
""" Execute first collection of configs """
try:
if args.api == "2.1":
""" Supported version """
dnac.collect_cfg(dnacApi)
else:
""" Unsupported version """
dnac.ciscoDnacCollectCfgs(dnacApi)
except KeyboardInterrupt:
print("")
logging.error("Keyboard Interrupt - Closing application")
sys.exit()
""" Set schedule for recurring collections """
timeOnDay = "03:00"
logging.info("Next collection scheduled at %s" % (timeOnDay))
schedule.every().day.at(timeOnDay).do(dnac.ciscoDnacCollectCfgs, dnacApi)
""" Start recurring collections loop """
while True:
try:
schedule.run_pending()
time.sleep(1)
except KeyboardInterrupt:
print("")
logging.error("Keyboard Interrupt - Closing application")
sys.exit()