Debian Codegraph @

docs/labs/lab06-provisioning/vc_task_retrigger.py

raw
1 # Copyright (c) 2021 Arista Networks, Inc.
2 # Use of this source code is governed by the Apache License 2.0
3 # that can be found in the COPYING file.
4
5 # Example on how to re-trigger task creation if a config push task was previously
6 # cancelled and the device is still config out of sync
7 import argparse
8 import ssl
9 import sys
10 from packaging.version import parse
11 from getpass import getpass
12 from cvprac.cvp_client import CvpClient
13 import requests.packages.urllib3
14 requests.packages.urllib3.disable_warnings()
15
16
17 if ((sys.version_info.major == 3) or
18 (sys.version_info.major == 2 and sys.version_info.minor == 7 and
19 sys.version_info.micro >= 5)):
20 ssl._create_default_https_context = ssl._create_unverified_context
21
22
23 def main():
24
25 compliance = {"0001": "Config is out of sync",
26 "0003": "Config & image out of sync",
27 "0004": "Config, Image and Device time are in sync",
28 "0005": "Device is not reachable",
29 "0008": "Config, Image and Extensions are out of sync",
30 "0009": "Config and Extensions are out of sync",
31 "0012": "Config, Image, Extension and Device time are out of sync",
32 "0013": "Config, Image and Device time are out of sync",
33 "0014": "Config, Extensions and Device time are out of sync",
34 "0016": "Config and Device time are out of sync"
35 }
36 # Create connection to CloudVision
37 clnt = CvpClient()
38
39 parser = argparse.ArgumentParser(
40 description='Script to recreate a task, if a previous config push was cancelled')
41 parser.add_argument('-u', '--username', default='username')
42 parser.add_argument('-p', '--password', default=None)
43 parser.add_argument('-c', '--cvpserver', action='append')
44 parser.add_argument('-f', '--filter', action='append', default=None)
45 args = parser.parse_args()
46
47 if args.password is None:
48 args.password = getpass()
49
50 for cvpserver in args.cvpserver:
51 print("Connecting to %s" % cvpserver)
52 try:
53 clnt.connect(nodes=[cvpserver], username=args.username, password=args.password)
54 except Exception as e:
55 print("Unable to connect to CVP: %s" % str(e))
56
57 # Get the current CVP version
58 cvp_release = clnt.api.get_cvp_info()['version']
59 if parse(cvp_release) < parse('2020.3.0'):
60 # For older CVP, we manually trigger a compliance check
61 try:
62 clnt.api.check_compliance('root', 'container')
63 except:
64 # Bad practice, but the check compliance applied to a container can't actually work
65 # since the complianceIndication key doesn't exist on the container level
66 pass
67 else:
68 # with continuous compliance checks, triggering the check is no longer required
69 pass
70
71 device_filters = []
72 if args.filter is not None:
73 for entry in args.filter:
74 device_filters.extend(entry.split(','))
75
76 # Get inventory
77 print("Collecting inventory...")
78 devices = clnt.api.get_inventory()
79 print("%d devices in inventory" % len(devices) )
80
81 for switch in devices:
82 if (switch['status'] == 'Registered' and
83 switch['parentContainerId'] != 'undefined_container'):
84
85 if len(device_filters) > 0:
86 # iterate over device filters, and update task for
87 # any devices not in compliance
88
89 for filter_term in device_filters:
90 print("Checking device: %s" % switch['hostname'])
91 if filter_term in switch['hostname']:
92 # generate configlet list
93 cl = clnt.api.get_configlets_by_device_id(switch['systemMacAddress'])
94 # generate a task if config is out of sync
95 if switch['complianceCode'] in compliance.keys():
96 print(clnt.api.apply_configlets_to_device("", switch, cl))
97 else:
98 print("%s is compliant, nothing to do" % switch['hostname'])
99 else:
100 print("Skipping %s due to filter" % switch['hostname'])
101 else:
102 print("Checking device: %s" % switch['hostname'])
103 cl = clnt.api.get_configlets_by_device_id(switch['systemMacAddress'])
104 # generate a task if config is out of sync
105 if switch['complianceCode'] in compliance.keys():
106 print(clnt.api.apply_configlets_to_device("", switch, cl))
107
108 else:
109 print("Skipping %s, device is unregistered for provisioning" % switch['hostname'])
110
111 return 0
112
113
114 if __name__ == "__main__":
115 main()