success_comment = str() fail_string = f'{{"wo_status": "FAIL", "wo_comment": "{fail_comment}"}}' success_string = f'{{"wo_status": "ENDED", "wo_comment": "{success_comment}"}}' #Create Orchestration object to update GUI dynamically Orchestration = Orchestration(context['UBIQUBEID']) async_update_list = (context['PROCESSINSTANCEID'], context['TASKID'], context['EXECNUMBER']) #Create order object IpamOrderObject = Order(ipam_device_id) IpamOrderObject.command_synchronize(300) #Retrieve info about tenants Orchestration.update_asynchronous_task_details(*async_update_list, 'Retrieving information about customer... ') objects_list = IpamOrderObject.command_objects_instances(ms_ipam_tenant) #Check if customer exists in IPAM system does_tenant_exists = False counter = 0 while not does_tenant_exists and counter < len(objects_list): tenant_object = IpamOrderObject.command_objects_instances_by_id(ms_ipam_tenant, objects_list[counter])[ms_ipam_tenant][objects_list[counter]] if tenant_object['object_id'] == customer_name: does_tenant_exists = True customer_id = tenant_object['id'] counter += 1 #If the customer does not exist in IPAM system, finish the task as fail if not does_tenant_exists: fail_comment = f'Customer {customer_name} has not been found'
#Identify ASN for the site and save to context Orchestration.update_asynchronous_task_details(*async_update_list, 'Retrieve AS number for the site... ') site_object = IpamOrderObject.command_objects_instances_by_id(ms_ipam_site, site)[ms_ipam_site][site] context['site_asn'] = site_object['asn'] Orchestration.update_asynchronous_task_details(*async_update_list, 'Retrieve AS number for the site... OK') time.sleep(3) #Identify IPv4 subnets for the site and save to context Orchestration.update_asynchronous_task_details(*async_update_list, 'Find out a IPv4 block for the site... ') context['site_prefixes_list'] = list() objects_list = IpamOrderObject.command_objects_instances(ms_ipam_prefix) for object in objects_list: prefix_object = IpamOrderObject.command_objects_instances_by_id(ms_ipam_prefix, object)[ms_ipam_prefix][object] try: if prefix_object['tenant'] == customer_name and prefix_object['site'] == site: context['site_prefixes_list'].append(prefix_object) except: pass Orchestration.update_asynchronous_task_details(*async_update_list, 'Find out a IPv4 block for the site... OK') time.sleep(3)
) CeOrderObject.command_synchronize(300) #Remove import connected routes to BGP Orchestration.update_asynchronous_task_details( *async_update_list, 'Configuring CE device... Remove import internal prefix to BGP... ') ms_dict = {ms_router_import_to_bgp: {'10': {'object_id': '10'}}} CeOrderObject.command_execute('DELETE', ms_dict) Orchestration.update_asynchronous_task_details( *async_update_list, 'Configuring CE device... Remove import internal prefix to BGP... OK') CeOrderObject.command_synchronize(300) CeOrderObject.command_synchronize(300) objects_list = CeOrderObject.command_objects_instances( ms_router_bgp_neighbor) for ip_address in context['site_address_list']: if ip_address['object_id'].split('/')[0].replace('.', '_') in objects_list: #Remove BGP neighbor on CE Orchestration.update_asynchronous_task_details( *async_update_list, 'Configuring CE device... Remove BGP peer for {}... '.format( connection['neighbour']['device'])) ms_dict = { ms_router_bgp_neighbor: { ip_address['object_id'].split('/')[0].replace('.', '_'): { 'object_id': ip_address['object_id'].split('/')[0] }
response = json.loads(order.content) return response['k8_pods_list'][pod_name]['namespace'] if __name__ == "__main__": # remove metrics pods info_message('Removing service pods...') for device in device_id_list: d_name = Device(device_id=device).name info_message(f'Removing service pods: {d_name}...') order = Order(device) # syncronize first k8_pods_list = {'k8_pods_list': {'': {}}} order.command_execute('IMPORT', k8_pods_list) order.command_objects_instances('k8_pods_list') pods_list = json.loads(order.content) for pod in pods_list: if get_pod_ns(pod) == context['service_namespace']: info_message(f'Removing service pods: {d_name} {pod}...') order.command_execute('DELETE', {'k8_pods_list': pod}) # remove all applications from user_namespace info_message(f'Removing user apps: {target_device_name}...') order = Order(target_device_id) # syncronize first k8_pods_list = {'k8_pods_list': {'': {}}} order.command_execute('IMPORT', k8_pods_list) order.command_objects_instances('k8_pods_list') ms_instances = json.loads(order.content) pods_list = json.loads(order.content)
fail_string = f'{{"wo_status": "FAIL", "wo_comment": "{fail_comment}"}}' success_string = f'{{"wo_status": "ENDED", "wo_comment": "{success_comment}"}}' #Create Orchestration object to update GUI dynamically Orchestration = Orchestration(context['UBIQUBEID']) async_update_list = (context['PROCESSINSTANCEID'], context['TASKID'], context['EXECNUMBER']) #Create IPAM order object IpamOrderObject = Order(ipam_device_id) IpamOrderObject.command_synchronize(300) Orchestration.update_asynchronous_task_details( *async_update_list, 'Retrieve information about CE device on the site... ') objects_list = IpamOrderObject.command_objects_instances(ms_ipam_device) #Retrieve information about CE device what is located on the site counter = 0 ce_device_id = None ce_device_name = None #Find CE device name first while ce_device_name is None or counter < len(objects_list): device_object = IpamOrderObject.command_objects_instances_by_id( ms_ipam_device, objects_list[counter])[ms_ipam_device][objects_list[counter]] if device_object['role'] == 'CE' and device_object['site'] == site: ce_device_name = device_object['object_id'] counter += 1
async_update_list = (context['PROCESSINSTANCEID'], context['TASKID'], context['EXECNUMBER']) #Create VLAN list if IPAM is used if context['use_ipam'] == 'yes': Orchestration.update_asynchronous_task_details( *async_update_list, 'IPAM integration is used. Retrive VLAN info from IPAM... ') context['ipam_device'] = re.match('^\D+?(\d+?)$', context['ipam_device']).group(1) #Retrive information about VLANs IpamOrderObject = Order(context['ipam_device']) IpamOrderObject.command_synchronize(300) objects_list = IpamOrderObject.command_objects_instances(ms_ipam_vlan) vlan_string = str() counter = 0 context['vlan_list'] = dict() for vlan_name in objects_list: vlan_object = IpamOrderObject.command_objects_instances_by_id( ms_ipam_vlan, vlan_name)[ms_ipam_vlan][vlan_name] if 'site' in vlan_object: if vlan_object['site'] == context['site']: context['vlan_list'][counter] = { 'id': vlan_object['vid'], 'name': vlan_object['object_id'], "__index__": counter } vlan_string += '{} '.format(vlan_object['object_id']) counter += 1
if __name__ == "__main__": ######################################################################################################## # MATH ALGORITHM # ######################################################################################################## info_message('Processing metrics...') cost_summary = {} for device in device_id_list: d_name = Device(device_id=device).name info_message(f'Processing metrics from: {d_name}...') cost_summary[device] = float() order = Order(device) order.command_objects_instances('logs') for ms_item in json.loads(order.content): if check_pod_name(ms_item): order.command_objects_instances_by_id('logs', ms_item) cost = json.loads(order.content)['logs'][ms_item]['min'] cost_summary[device] += float(cost) target_device_id = min(cost_summary, key=cost_summary.get) target_device_name = Device(device_id=target_device_id).name info_message(f'Best offer from: {target_device_name}.', 5) context['target_device_id'] = target_device_id context['target_device_name'] = target_device_name
is_site_prefix_announced_to_all_peers = list() for interface, connection in ce_connections.items(): sed_command = 'sed -i \'s@{original_string}@{rewrite_string}@\' {ms_file}'.format(original_string = '1.1.1.1', rewrite_string = connection['neighbour']['ip_address'].split('/')[0], ms_file = ms_advertised_prefix_file_path) os.system(sed_command) with open(ms_advertised_prefix_file_path) as temp_ms_file: temp_ms_file_content = temp_ms_file.read() CeOrderObject.command_synchronize(300) time.sleep(10) objects_list = CeOrderObject.command_objects_instances(ms_router_advert_by_bgp) is_site_prefix_announced = False counter = 0 while is_site_prefix_announced is False and counter < len(objects_list): prefix_object = CeOrderObject.command_objects_instances_by_id(ms_router_advert_by_bgp, objects_list[counter])[ms_router_advert_by_bgp][objects_list[counter]] if prefix_object['object_id'] == ce_internal_prefix: is_site_prefix_announced = True is_site_prefix_announced_to_all_peers.append(is_site_prefix_announced) counter += 1 sed_command = 'sed -i \'s@{original_string}@{rewrite_string}@\' {ms_file}'.format(original_string = connection['neighbour']['ip_address'].split('/')[0], rewrite_string = '1.1.1.1', ms_file = ms_advertised_prefix_file_path)
import json from msa_sdk.variables import Variables from msa_sdk.msa_api import MSA_API from msa_sdk.order import Order dev_var = Variables() context = Variables.task_call() device_id_list = context["device_id_list"] for device in device_id_list.values(): order = Order(device) order.command_objects_instances('k8s_pods') ms_instances = json.loads(order.content.decode()) for instance in ms_instances: Order(device).command_execute('DELETE', {'k8s_pods': instance}) ret = MSA_API.process_content('ENDED', f'workflow deleted, pods cleared', context, True) print(ret)
def rand_int(): return ''.join(random.choice('0123456789') for i in range(8)) cost_summary = {} ######################################################################################################## # MATH ALGORITHM # ######################################################################################################## for device in device_id_list.values(): cost_summary[device] = float() order = Order(device) order.command_objects_instances('k8s_log') for ms_item in json.loads(order.content.decode()): order.command_objects_instances_by_id('k8s_log', ms_item) cost = json.loads(order.content.decode())['k8s_log'][ms_item]['min'] cost_summary[device] += float(cost) target_device_id = min(cost_summary, key=cost_summary.get) target_device_name = Device(device_id=target_device_id).name context['target_device_id'] = target_device_id context['target_device_name'] = target_device_name ######################################################################################################## # DEPLOY USER APP # ######################################################################################################## '''
context['EXECNUMBER']) Orchestration.update_asynchronous_task_details( *async_update_list, 'Import current configuration... ') #Create Station device order object StationOrderObject = Order(context['station_device_id']) response = StationOrderObject.command_synchronize(300) Orchestration.update_asynchronous_task_details( *async_update_list, 'Import current configuration... OK') time.sleep(3) #Check VLAN IDs Orchestration.update_asynchronous_task_details(*async_update_list, 'Verifying VLANs... ') objects_list = StationOrderObject.command_objects_instances(ms_vlan) do_vlans_exist = list() for number, vlan in context['vlan_list'].items(): Orchestration.update_asynchronous_task_details( *async_update_list, 'Verifying VLANs... ID {}... '.format(vlan['id'])) for vlan_id in objects_list: vlan_object = StationOrderObject.command_objects_instances_by_id( ms_vlan, vlan_id)[ms_vlan][vlan_id] if vlan['id'] == vlan_object['object_id']: Orchestration.update_asynchronous_task_details( *async_update_list, 'Verifying VLANs... ID {}... OK'.format(vlan['id'])) time.sleep(3) do_vlans_exist.append(True) break else: