| 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 |
# This script is an example on provisioning registered devices in CloudVision that is based on |
| 6 |
# Arista Test Drive (ATD) and similar to what the ansible playbooks do in |
| 7 |
# https://github.com/arista-netdevops-community/atd-avd. |
| 8 |
# It does the following: |
| 9 |
# - creates and uploads configlets, |
| 10 |
# - creates the container hierarchy in Network Provisiong |
| 11 |
# - moves the devices to their target containers |
| 12 |
# - assigns the configlets to the devices |
| 13 |
# - creates a change control from the genereated tasks |
| 14 |
# - approves and executes the change control |
| 15 |
|
| 16 |
import uuid |
| 17 |
import time |
| 18 |
import ssl |
| 19 |
from datetime import datetime |
| 20 |
from cvprac.cvp_client import CvpClient |
| 21 |
ssl._create_default_https_context = ssl._create_unverified_context |
| 22 |
|
| 23 |
# Create connection to CloudVision |
| 24 |
clnt = CvpClient() |
| 25 |
clnt.connect(['cvp1'],'username', 'password') |
| 26 |
|
| 27 |
# Create container topology |
| 28 |
container_name = "DC1_LEAFS" |
| 29 |
container_topology = [{"containerName": "ATD_FABRIC", "parentContainerName": 'Tenant'}, |
| 30 |
{"containerName": "ATD_LEAFS", "parentContainerName": 'ATD_FABRIC'}, |
| 31 |
{"containerName": "pod1", "parentContainerName": 'ATD_LEAFS'}, |
| 32 |
{"containerName": "pod2", "parentContainerName": 'ATD_LEAFS'}, |
| 33 |
{"containerName": "ATD_SERVERS", "parentContainerName": 'ATD_FABRIC'}, |
| 34 |
{"containerName": "ATD_SPINES", "parentContainerName": 'ATD_FABRIC'}, |
| 35 |
{"containerName": "ATD_TENANT_NETWORKS", "parentContainerName": 'ATD_FABRIC'}] |
| 36 |
for container in container_topology: |
| 37 |
try: |
| 38 |
container_name = container['containerName'] |
| 39 |
# Get parent container information |
| 40 |
parent = clnt.api.get_container_by_name(container['parentContainerName']) |
| 41 |
print(f'Creating container {container_name}\n') |
| 42 |
clnt.api.add_container(container_name,parent["name"],parent["key"]) |
| 43 |
except Exception as e: |
| 44 |
if "Data already exists in Database" in str(e): |
| 45 |
print ("Container already exists, continuing...") |
| 46 |
|
| 47 |
# Create device mappers |
| 48 |
devices = [{'deviceName': "leaf1", |
| 49 |
'configlets': ["BaseIPv4_Leaf1", "AVD_leaf1"], |
| 50 |
"parentContainerName": "pod1"}, |
| 51 |
{'deviceName': "leaf2", |
| 52 |
'configlets': ["BaseIPv4_Leaf2", "AVD_leaf2"], |
| 53 |
"parentContainerName": "pod1"}, |
| 54 |
{'deviceName': "leaf3", |
| 55 |
'configlets': ["BaseIPv4_Leaf3", "AVD_leaf3"], |
| 56 |
"parentContainerName": "pod2"}, |
| 57 |
{'deviceName': "leaf4", |
| 58 |
'configlets': ["BaseIPv4_Leaf4", "AVD_leaf4"], |
| 59 |
"parentContainerName": "pod2"}, |
| 60 |
{'deviceName': "spine1", |
| 61 |
'configlets': ["BaseIPv4_Spine1", "AVD_spine1"], |
| 62 |
"parentContainerName": "ATD_SPINES"}, |
| 63 |
{'deviceName': "spine2", |
| 64 |
'configlets': ["BaseIPv4_Spine2", "AVD_spine2"], |
| 65 |
"parentContainerName": "ATD_SPINES"}] |
| 66 |
|
| 67 |
task_list = [] |
| 68 |
for device in devices: |
| 69 |
# Load the AVD configlets from file |
| 70 |
with open("./configlets/AVD_" + device['deviceName'] + ".cfg", "r") as file: |
| 71 |
configlet_file = file.read() |
| 72 |
avd_configlet_name = device['configlets'][1] |
| 73 |
base_configlet_name = device['configlets'][0] # preloaded configlet in an ATD environment |
| 74 |
container_name = device['parentContainerName'] |
| 75 |
base_configlet = clnt.api.get_configlet_by_name(base_configlet_name) |
| 76 |
configlets = [base_configlet] |
| 77 |
# Update the AVD configlets if they exist, otherwise upload them from the configlets folder |
| 78 |
print (f"Creating configlet {avd_configlet_name} for {device['deviceName']}\n") |
| 79 |
try: |
| 80 |
configlet = clnt.api.get_configlet_by_name(avd_configlet_name) |
| 81 |
clnt.api.update_configlet(configlet_file, configlet['key'], avd_configlet_name) |
| 82 |
configlets.append(configlet) |
| 83 |
except: |
| 84 |
clnt.api.add_configlet(avd_configlet_name, configlet_file) |
| 85 |
configlet = clnt.api.get_configlet_by_name(avd_configlet_name) |
| 86 |
configlets.append(configlet) |
| 87 |
# Get device data |
| 88 |
device_data = clnt.api.get_device_by_name(device['deviceName'] + ".atd.lab") |
| 89 |
# Get the parent container data for the device |
| 90 |
container = clnt.api.get_container_by_name(container_name) |
| 91 |
device_name = device['deviceName'] |
| 92 |
print(f"Moving device {device_name} to container {container_name}\n") |
| 93 |
# The move action will create the task first, however if the devices are already in the target |
| 94 |
# container, for instance if the script was run multiple times than the move action will |
| 95 |
# not generate a task anymore, therefore it's better to create the task list from the |
| 96 |
# Update Config action which will reuse the Move Device action's task if one exists, |
| 97 |
# otherwise will create a new one. |
| 98 |
move = clnt.api.move_device_to_container("python", device_data, container) |
| 99 |
apply_configlets = clnt.api.apply_configlets_to_device("", device_data, configlets) |
| 100 |
task_list = task_list + apply_configlets['data']['taskIds'] |
| 101 |
|
| 102 |
print(f"Generated task IDs are: {task_list}\n") |
| 103 |
|
| 104 |
# Generate unique ID for the change control |
| 105 |
cc_id = str(uuid.uuid4()) |
| 106 |
cc_name = f"Change_{datetime.now().strftime('%Y%m%d_%H%M%S')}" |
| 107 |
|
| 108 |
print("Creating Change control with the list of tasks") |
| 109 |
clnt.api.change_control_create_for_tasks(cc_id, cc_name, task_list, series=False) |
| 110 |
|
| 111 |
print("Approving Change Control") |
| 112 |
# adding a few seconds sleep to avoid small time diff between the local system and CVP |
| 113 |
time.sleep(2) |
| 114 |
approve_note = "Approving CC via cvprac" |
| 115 |
clnt.api.change_control_approve(cc_id, notes=approve_note) |
| 116 |
|
| 117 |
# Start the change control |
| 118 |
print("Executing Change Control...") |
| 119 |
start_note = "Start the CC via cvprac" |
| 120 |
clnt.api.change_control_start(cc_id, notes=start_note) |