def action_attach_device(tasker, parameters): """Worker to attach device to configlet. Implement script logic to move a device from one container to another one. Parameters ---------- tasker : dict Information from JSON file describing action to run. parameters : dict CVP Connection information. """ # Create an HTTP session to the CVP server logging.debug('connect to CVP server') client = connect_to_cvp(parameters) logging.info('*************') logging.info('Start working with %s', tasker['container']) container = CvpContainer(name=tasker['container'], cvp_server=client) # logging.info('getting info for container %s', tasker['container']) # info = container.get_info() logging.info('check is devices are already part of container') for device in tasker['devices']: if container.is_device_attached(hostname=device): logging.critical( 'device is already part of that container -- skipping') else: logging.info( 'device is not part of that container -- moving forward') container.attach_device_bulk(hostname_list=tasker['devices'], deploy=tasker['apply'])
def action_delete(configlet_def, parameters): """Manage actions to DELTE a configlet. Create CVP connection and instantiate a CvpConfiglet object Then call appropriate method to start object deletion Parameters option should at least contain following elements: - username - password - cvp (server IP or DNS hostname) Parameters ---------- configlet_def : dict Data from JSON to describe configlet parameters : dict Object with all information to create connection """ client = connect_to_cvp(parameters) logging.info('---') logging.info('* start working with %s', configlet_def['configlet']) my_configlet = CvpConfiglet(cvp_server=client, configlet_file=configlet_def['configlet']) logging.info('* start to delete existing configlet: %s', configlet_def['configlet']) my_configlet.delete_configlet()
def action_add(configlet_def, parameters): """Manage actions to ADD a configlet. Create CVP connection and instantiate a CvpConfiglet object Then call appropriate method to start object creation If apply option is set to true, then, generated tasks are applied by CVP. Otherwise, user has to do it manually Parameters option should at least contain following elements: - username - password - cvp (server IP or DNS hostname) Parameters ---------- configlet_def : dict Data from JSON to describe configlet parameters : dict Object with all information to create connection """ # Create an HTTP session to the CVP server client = connect_to_cvp(parameters) logging.info('---') logging.info('* start working with %s', configlet_def['configlet']) my_configlet = CvpConfiglet(cvp_server=client, configlet_file=configlet_def['configlet']) logging.info(' -> start to create new configlet: %s', configlet_def['configlet']) if my_configlet.on_cvp(): logging.warning(' -> %s is already configured on CVP server, fallback to UPDATE method', # noqa E501 my_configlet.name()) action_update(configlet_def=configlet_def, parameters=parameters) else: # a list of device must be available to create configlet # if list is missing, then we have to break process if 'devices' not in configlet_def: logging.error(' -> ! configlet has no devices configured, cannot create configlet on server') return False # Now create configlet on CVP server my_configlet.deploy_configlet(device_hostnames=configlet_def['devices']) # Check if configlet must be deployed to devices if 'apply' in configlet_def: action_apply(configlet_def=configlet_def, my_configlet=my_configlet) else: logging.warning(' -> deploy option has not been set for the configlet') logging.warning(' --> doing nothing') return True
def action_add_devices(configlet_def, parameters): """Manage actions to add devices to an existing configlet. Create CVP connection to check if configlet exists. if it exists, then call CvpConfiglet.add_device() to add all devices listed in the json task. If apply option is set to true, then, generated tasks are applied by CVP. Otherwise, user has to do it manually Parameters option should at least contain following elements: - username - password - cvp (server IP or DNS hostname) Parameters ---------- configlet_def : dict Data from JSON to describe configlet parameters : dict Object with all information to create connection """ # Create an HTTP session to the CVP server client = connect_to_cvp(parameters) logging.info('---') logging.info('* start working with %s', configlet_def['configlet']) my_configlet = CvpConfiglet(cvp_server=client, configlet_name=configlet_def['configlet']) if my_configlet.on_cvp(): logging.info(' -> %s configlet has been found on CVP', configlet_def['configlet']) my_configlet.add_device(device_hostnames=configlet_def['devices']) # Check if configlet must be deployed to devices if 'apply' in configlet_def: action_apply(configlet_def=configlet_def, my_configlet=my_configlet) else: logging.warning(' -> deploy option has not been set for the configlet') logging.warning(' --> doing nothing') return True
def action_container_create(tasker, parameters): """Worker to create a container. Implement script logic to create a container on CVP. Parameters ---------- tasker : dict Information from JSON file describing action to run. parameters : dict CVP Connection information. """ # Create an HTTP session to the CVP server logging.debug('connect to CVP server') client = connect_to_cvp(parameters) logging.info('*************') logging.info('Start working with %s', tasker['container']) container = CvpContainer(name=tasker['container'], cvp_server=client) logging.info('create container on CVP server') container.create(parent_name=tasker['parent'])
datefmt='%Y-%m-%d %H:%M:%S') # activate SSL enforcement or not # (should be disabled for self-signed certs) if cvprac_abstraction.CERT_VALIDATION is False: requests.packages.urllib3.disable_warnings(InsecureRequestWarning) # Check and create backup folder logging.info('* check if backup folder exists.') path = os.getcwd() if os.path.isdir(CVP['CONFIGLET_BACKUP']) is False: logging.warning(' -> local folder does not exist. create it !') os.mkdir(CVP['CONFIGLET_BACKUP']) os.chdir(CVP['CONFIGLET_BACKUP']) logging.info('* connecting to cloudvision server %s', CVP['HOST']) cvp_server = connect_to_cvp(parameters=OPTIONS) logging.info('* extracting configlet from cloudvision server') configlets_list = cvp_server.api.get_configlets() pp = pprint.PrettyPrinter(indent=2) for configlet in configlets_list['data']: logging.info(' -> exporting configlet %s', configlet['name']) # Get a file object with write permission. file_object = open( "configlet-" + configlet['name'].replace(' ', '-') + ".json", 'w') # Save dict data into the JSON file. json.dump(configlet, file_object, indent=4, sort_keys=True) logging.info('* backup complete. Configlets have been saved in %s', CVP['CONFIGLET_BACKUP'])
def action_update(configlet_def, parameters): """Manage actions to UPDATE and existing configlet. Create CVP connection and instantiate a CvpConfiglet object Then call appropriate method to start object update And finally run tasks Parameters option should at least contain following elements: - username - password - cvp (server IP or DNS hostname) Parameters ---------- configlet_def : dict Data from JSON to describe configlet parameters : dict Object with all information to create connection """ # Create an HTTP session to the CVP server client = connect_to_cvp(parameters) logging.info('---') logging.info('* start working with %s', configlet_def['configlet']) my_configlet = CvpConfiglet(cvp_server=client, configlet_file=configlet_def['configlet']) if my_configlet.on_cvp(): logging.info(' -> start to deploy new version of %s configlet', configlet_def['configlet']) my_configlet.update_configlet() else: logging.warning(' -> %s is not configured on CVP server, fallback to ADD method', # noqa E501 my_configlet.name()) action_add(configlet_def=configlet_def, parameters=parameters) # Now check if configlet has to be applied on devices. # If not, it means a task should be run manually # or a Change Control should be defined as a next item. if 'apply' in configlet_def: if configlet_def['apply']: logging.info(' -> configlet is gonna be deployed') if 'devices' not in configlet_def: logging.info(' -> deploy target is set to all attached devices') my_configlet.deploy_bulk() else: logging.info(' -> configlet will be deployed on some devices') cvp_inventory = CvpInventory(client) for device in configlet_def['devices']: dev_inventory = cvp_inventory.get_device_dict(device) if dev_inventory is not None: logging.info(' -> Deploy %s on %s', my_configlet.name, device) my_configlet.deploy(dev_inventory) else: logging.warning(' -> deploy option has not been set for the configlet') logging.warning(' --> doing nothing') else: logging.warning(' -> deploy option has not been set for the configlet') logging.warning(' --> doing nothing')
def action_create_change_control(parameters, data): """Create a Change-Control. Create a change-control on CVP based on a JSON definition. Current version supports following entries in JSON: - name: change-control name configured on CVP - type: change-control (Must be set with this vaue to engage CC) - country: Country required by CVP for CC - timezone: Timezone required by CVP to run changes **Expected inputs data** JSON file:: [ { "name": "Python_CC", "type": "change-control", "country": "France", "timezone": "Europe/Paris" } ] Todo ---- Manage way to retrieve Template ID / As feature is not part of CVPRAC, ``snapid`` shall be part of the job definition. If not, then we configure it to ``None`` Parameters ---------- configlet_def : dict Data from JSON to describe configlet parameters : dict Object with all information to create connection """ client = connect_to_cvp(parameters) logging.info('---') logging.info('* start change-control creation') change_control = CvpChangeControl(cvp_server=client, name=data['name'].replace(' ', '_')) if 'schedule_at' not in data: data['schedule_at'] = (datetime.now() + timedelta(seconds=180)).strftime("%Y-%m-%d %H:%M") # noqa E501 # Check if mandatory values are set. # Otherwise load default if 'timezone' not in data: logging.debug(' -> timezone not set in json, using default one: %s', CVP['TZ']) data['timezone'] = CVP['TZ'] if 'country' not in data: logging.debug(' -> country not set in json, using default one: %s', CVP['COUNTRY']) data['country'] = CVP['COUNTRY'] if 'snapid' not in data: logging.debug(' -> snapshot ID not configured') data['snapid'] = 'None' if 'apply' in data and data['apply'] is True: logging.info(' -> scheduling change control to be executed at %s', data['schedule_at']) result = change_control.create(tz=data['timezone'], country=data['country'], schedule=True, schedule_at=data['schedule_at'], snap_template=data['snapid'], change_type='Custom', stop_on_error="true") # noqa E501 else: logging.warning(' -> change control must be executed manually') result = change_control.create(tz=data['timezone'], country=data['country'], schedule=True, snap_template=data['snapid'], change_type='Custom', stop_on_error="true") # noqa E501 if result is not None: logging.info(' -> change-control creation is %s (id %s)', result['data'], result['ccId']) # noqa E501
logging.basicConfig( format='%(asctime)s %(levelname)-8s %(message)s', level=LOGLEVEL, datefmt='%Y-%m-%d %H:%M:%S') # activate SSL enforcement or not # (should be disabled for self-signed certs) if cvprac_abstraction.CERT_VALIDATION is False: requests.packages.urllib3.disable_warnings(InsecureRequestWarning) logging.debug('CVP Client inputs:') logging.debug(' > Username is %s', OPTIONS.username) logging.debug(' > Server is %s', OPTIONS.cvp) logging.debug(' > Port is %s', CVP['PORT']) logging.debug(' > Proto is %s', CVP['PROTO']) print '\n--------------------\n' logging.info('connecting to cvp server') cvp_client = connect_to_cvp(OPTIONS) taskManager = CvpTaskManager(cvp_server=cvp_client) if OPTIONS.run_all is True: logging.info('* script will execute all pending tasks') taskManager.deploy_all(task_timeout=OPTIONS.task_timeout) elif OPTIONS.list is True: logging.info('* listing all pending tasks') for task in taskManager.get_tasks(): logging.info(' > Task %s - %s', str(task['workOrderId']), str(task['description'])) print '\n--------------------\n'