def generate_and_deploy_channel_config_sync(user, id_channel): if not is_valid_int_greater_zero_param(id_channel): raise exceptions.InvalidIdInterfaceException() channel = PortChannel.get_by_pk(id_channel) interfaces = channel.list_interfaces() #group interfaces by equipment equipment_interfaces = dict() for interface in interfaces: if interface.equipamento.id not in equipment_interfaces: equipment_interfaces[interface.equipamento.id] = [] equipment_interfaces[interface.equipamento.id].append(interface) files_to_deploy = {} for equipment_id in equipment_interfaces.keys(): grouped_interfaces = equipment_interfaces[equipment_id] file_to_deploy = _generate_config_file(grouped_interfaces) files_to_deploy[equipment_id] = file_to_deploy #TODO Deploy config file #make separate threads for equipment_id in files_to_deploy.keys(): lockvar = LOCK_INTERFACE_DEPLOY_CONFIG % (equipment_id) equipamento = Equipamento.get_by_pk(equipment_id) status_deploy = deploy_config_in_equipment_synchronous(files_to_deploy[equipment_id], equipamento, lockvar) return status_deploy
def deploy_sync_copy_script_to_multiple_equipments(request): """ Deploy configuration on equipment(s) Default source: TFTP SERVER Default destination: apply config (running-config) Default protocol: tftp Receives script """ try: script = request.DATA["script_data"] id_equips = request.DATA["id_equips"] #Check equipment permissions for id_equip in id_equips: #TODO pass output_data = dict() script_file = facade.create_file_from_script(script, USER_SCRIPTS_REL_PATH) for id_equip in id_equips: equipment_id = int(id_equip) lockvar = LOCK_EQUIPMENT_DEPLOY_CONFIG_USERSCRIPT % (equipment_id) output_data[equipment_id] = dict() try: output_data[equipment_id]["output"] = facade.deploy_config_in_equipment_synchronous(script_file, equipment_id, lockvar) output_data[equipment_id]["status"] = "OK" except Exception, e: log.error("Error applying script file to equipment_id %s: %s" %(equipment_id, e)) output_data[equipment_id]["output"] = str(e) output_data[equipment_id]["status"] = "ERROR" return Response(output_data)
def deploy_networkIPv6_configuration(user, networkipv6, equipment_list): '''Loads template for creating Network IPv6 equipment configuration, creates file and apply config. Args: NetworkIPv6 object Equipamento objects list Returns: List with status of equipments output ''' data = dict() #lock network id to prevent multiple requests to same id with distributedlock(LOCK_NETWORK_IPV6 % networkipv6.id): with distributedlock(LOCK_VLAN % networkipv6.vlan.id): if networkipv6.active == 1: data["output"] = "Network already active. Nothing to do." return data #load dict with all equipment attributes dict_ips = get_dict_v6_to_use_in_configuration_deploy(user, networkipv6, equipment_list) status_deploy = dict() #TODO implement threads for equipment in equipment_list: #generate config file file_to_deploy = _generate_config_file(dict_ips, equipment, TEMPLATE_NETWORKv6_ACTIVATE) #deploy config file in equipments lockvar = LOCK_EQUIPMENT_DEPLOY_CONFIG_NETWORK_SCRIPT % (equipment.id) status_deploy[equipment.id] = deploy_config_in_equipment_synchronous(file_to_deploy, equipment, lockvar) networkipv6.activate(user) transaction.commit() if networkipv6.vlan.ativada == 0: networkipv6.vlan.activate(user) return status_deploy
def post(self, *args, **kwargs): try: log.info('RACK deploy.') rack_id = kwargs.get('rack_id') rack = facade.get_by_pk(rack_id) try: PATH_TO_ADD_CONFIG = get_variable('path_to_add_config') REL_PATH_TO_ADD_CONFIG = get_variable('rel_path_to_add_config') except ObjectDoesNotExist: raise var_exceptions.VariableDoesNotExistException("Erro buscando a variável PATH_TO_ADD_CONFIG ou " "REL_PATH_TO_ADD_CONFIG.") path_config = PATH_TO_ADD_CONFIG + '*' + rack.nome + '*' arquivos = glob.glob(path_config) # Get all files and search for equipments of the rack for var in arquivos: filename_equipments = var.split('/')[-1] rel_filename = REL_PATH_TO_ADD_CONFIG + filename_equipments log.debug("rel_filename: %s" % rel_filename) # Check if file is config relative to this rack if rack.nome in filename_equipments: # Apply config only in spines. Leaves already have all necessary config in startup if "ADD" in filename_equipments: # Check if equipment in under maintenance. If so, does not aplly on it equipment_name = filename_equipments.split('-ADD-')[0] log.debug("equipment_name: %s" % equipment_name) try: equip = Equipamento.get_by_name(equipment_name) if not equip.maintenance: lockvar = LOCK_EQUIPMENT_DEPLOY_CONFIG_USERSCRIPT % (equip.id) output = deploy_facade.deploy_config_in_equipment_synchronous(rel_filename, equip, lockvar) log.debug("equipment output: %s" % (output)) except Exception as e: log.exception(e) raise exceptions.RackAplError(e) # SEPARAR AQUI! datas = dict() success_map = dict() success_map['rack_conf'] = True datas['sucesso'] = success_map return Response(datas, status=status.HTTP_201_CREATED) except exceptions.RackNumberNotFoundError as e: log.exception(e) raise exceptions.NetworkAPIException(e) except var_exceptions.VariableDoesNotExistException as e: log.error(e) raise api_exceptions.NetworkAPIException( 'Erro buscando a variável PATH_TO_ADD_CONFIG ou REL_PATH_TO_ADD_CONFIG. Erro: %s' % e) except Exception as e: log.exception(e) raise api_exceptions.NetworkAPIException(e)
def delete_channel(user, equip_id, interface_list, channel): file_to_deploy = generate_delete_file(user, equip_id, interface_list, channel) #TODO Deploy config file try: lockvar = LOCK_EQUIPMENT % (equip_id) status_deploy = deploy_config_in_equipment_synchronous(file_to_deploy, int(equip_id), lockvar) log.info("Status: %s" % status_deploy) except exceptions.InterfaceException, exception: log.error(exception) raise exceptions.InterfaceException(exception)
def generate_and_deploy_interface_config_sync(user, id_interface): if not is_valid_int_greater_zero_param(id_interface): raise exceptions.InvalidIdInterfaceException() interface = Interface.get_by_pk(id_interface) interfaces = [interface] file_to_deploy = _generate_config_file(interfaces) #TODO Deploy config file lockvar = LOCK_INTERFACE_DEPLOY_CONFIG % (interface.equipamento.id) status_deploy = deploy_config_in_equipment_synchronous(file_to_deploy, interface.equipamento, lockvar) return status_deploy
def remove_deploy_networkIPv6_configuration(user, networkipv6, equipment_list): """Loads template for removing Network IPv6 equipment configuration, creates file and apply config. Args: NetworkIPv6 object Equipamento objects list Returns: List with status of equipments output """ data = dict() # lock network id to prevent multiple requests to same id with distributedlock(LOCK_NETWORK_IPV6 % networkipv6.id): with distributedlock(LOCK_VLAN % networkipv6.vlan.id): if networkipv6.active == 0: data['output'] = 'Network already not active. Nothing to do.' return data # load dict with all equipment attributes dict_ips = get_dict_v6_to_use_in_configuration_deploy( user, networkipv6, equipment_list) status_deploy = dict() # TODO implement threads for equipment in equipment_list: # generate config file file_to_deploy = _generate_config_file( dict_ips, equipment, TEMPLATE_NETWORKv6_DEACTIVATE) # deploy config file in equipments lockvar = LOCK_EQUIPMENT_DEPLOY_CONFIG_NETWORK_SCRIPT % ( equipment.id) status_deploy[equipment.id] = deploy_config_in_equipment_synchronous( file_to_deploy, equipment, lockvar) networkipv6.deactivate(user) transaction.commit() if networkipv6.vlan.ativada == 1: # if there are no other networks active in vlan, remove int # vlan if not _has_active_network_in_vlan(networkipv6.vlan): # remove int vlan for equipment in equipment_list: if equipment.maintenance is not True: status_deploy[ equipment.id] += _remove_svi(equipment, networkipv6.vlan.num_vlan) networkipv6.vlan.remove(user) return status_deploy
def deploy_networkIPv4_configuration(user, networkipv4, equipment_list): """Loads template for creating Network IPv4 equipment configuration, creates file and apply config. Args: equipment_list: NetworkIPv4 object equipment_list: Equipamento objects list Returns: List with status of equipments output """ log.info("deploy_networkIPv4_configuration") data = dict() # lock network id to prevent multiple requests to same id with distributedlock(LOCK_NETWORK_IPV4 % networkipv4.id): with distributedlock(LOCK_VLAN % networkipv4.vlan.id): if networkipv4.active == 1: data['output'] = 'Network already active. Nothing to do.' return data # load dict with all equipment attributes dict_ips = get_dict_v4_to_use_in_configuration_deploy( user, networkipv4, equipment_list) status_deploy = dict() # TODO implement threads for equipment in equipment_list: # generate config file file_to_deploy = _generate_config_file( dict_ips, equipment, TEMPLATE_NETWORKv4_ACTIVATE) # deploy config file in equipments lockvar = LOCK_EQUIPMENT_DEPLOY_CONFIG_NETWORK_SCRIPT % ( equipment.id) status_deploy[equipment.id] = \ deploy_config_in_equipment_synchronous( file_to_deploy, equipment, lockvar) networkipv4.activate(user) transaction.commit() if networkipv4.vlan.ativada == 0: networkipv4.vlan.activate(user) return status_deploy
def deploy_sync_copy_script_to_equipment(request, equipment_id): """ Deploy configuration on equipment(s) Default source: TFTP SERVER Default destination: apply config (running-config) Default protocol: tftp Receives script """ try: script = request.DATA["script_data"] script_file = facade.create_file_from_script(script, USER_SCRIPTS_REL_PATH) equipment_id = int(equipment_id) lockvar = LOCK_EQUIPMENT_DEPLOY_CONFIG_USERSCRIPT % (equipment_id) data = dict() data["output"] = facade.deploy_config_in_equipment_synchronous(script_file, equipment_id, lockvar) data["status"] = "OK" return Response(data) except KeyError, key: log.error(key) raise exceptions.InvalidKeyException(key)