def _generate_config_file(interfaces_list): log.info("_generate_config_file") try: INTERFACE_CONFIG_TOAPPLY_REL_PATH = get_variable( 'interface_config_toapply_rel_path') INTERFACE_CONFIG_FILES_PATH = get_variable( 'interface_config_files_path') TEMPLATE_TYPE_INT = get_variable('template_type_int') TEMPLATE_TYPE_CHANNEL = get_variable('template_type_channel') except ObjectDoesNotExist: raise var_exceptions.VariableDoesNotExistException( 'Erro buscando a variável INTERFACE_CONFIG' '<TOAPPLY,TEMPLATE,FILES>_PATH.') # check if all interfaces are configuring same equipment # raises error if not equipment_interfaces = dict() equipment_interfaces[interfaces_list[0].equipamento.nome] = 1 for interface in interfaces_list: if interface.equipamento.nome not in equipment_interfaces: log.error('Error trying to configure multiple interfaces in ' 'different equipments in same call.') raise exceptions.InvalidIdInterfaceException config_to_be_saved = '' equipment_id = interfaces_list[0].equipamento.id request_id = getattr(local, 'request_id', NO_REQUEST_ID) extension = '.py' if interfaces_list[ 0].equipamento.modelo.marca.nome == "HP" else '' filename_out = 'int-d_' + str( interfaces_list[0].id) + '_config_' + str(request_id) + extension log.debug(filename_out) filename_to_save = INTERFACE_CONFIG_FILES_PATH + filename_out rel_file_to_deploy = INTERFACE_CONFIG_TOAPPLY_REL_PATH + filename_out int_template_file = _load_template_file(equipment_id, TEMPLATE_TYPE_INT) channels_configured = {} for interface in interfaces_list: key_dict = _generate_dict(interface) # If Interface is in channel, render the template for channel, # only once for each channel try: if interface.channel is not None: if interface.channel.id is not None and \ interface.channel.id not in channels_configured.keys(): channel_template_file = _load_template_file( equipment_id, TEMPLATE_TYPE_CHANNEL) config_to_be_saved += channel_template_file.render( Context(key_dict)) channels_configured[interface.channel.id] = 1 # Render the template for interface config_to_be_saved += int_template_file.render(Context(key_dict)) except KeyError, exception: log.error('Erro: %s ' % exception) raise exceptions.InvalidKeyException(exception)
def generate_delete_file(user, equip_id, interface_list, channel): try: INTERFACE_CONFIG_TOAPPLY_REL_PATH = get_variable( 'interface_config_toapply_rel_path') INTERFACE_CONFIG_FILES_PATH = get_variable( 'interface_config_files_path') TEMPLATE_REMOVE_CHANNEL = get_variable('template_remove_channel') TEMPLATE_REMOVE_INTERFACE = get_variable('template_remove_interface') except ObjectDoesNotExist: raise var_exceptions.VariableDoesNotExistException( 'Erro buscando a variável INTERFACE_CONFIG_TEMPLATE_PATH,' 'TEMPLATE_REMOVE_CHANNEL ou TEMPLATE_REMOVE_INTERFACE.') key_dict = dict() config_to_be_saved = '' request_id = getattr(local, 'request_id', NO_REQUEST_ID) extension = '.py' if interface_list[ 0].equipamento.modelo.marca.nome == "HP" else '' filename_out = 'equip_' + str(equip_id) + '_channel_' + str( channel.id) + '_remove_' + str(request_id) + extension log.debug(filename_out) filename_to_save = INTERFACE_CONFIG_FILES_PATH + filename_out rel_file_to_deploy = INTERFACE_CONFIG_TOAPPLY_REL_PATH + filename_out key_dict['PORTCHANNEL_NAME'] = channel.nome for i in interface_list: key_dict['INTERFACE_NAME'] = i.interface try: interface_template_file = _load_template_file( int(equip_id), TEMPLATE_REMOVE_INTERFACE) config_to_be_saved += interface_template_file.render( Context(key_dict)) except exceptions.InterfaceTemplateException, e: log.error(e) raise exceptions.InterfaceTemplateException() except KeyError, exception: log.error('Erro: %s ' % exception) raise exceptions.InvalidKeyException(exception)
def _generate_config_file(dict_ips, equipment, template_type): """Load a template and write a file with the rended output Args: 2-dimension dictionary with equipments information for template rendering equipment to render template to template type to load Returns: filename with relative path to settings.TFTPBOOT_FILES_PATH """ config_to_be_saved = '' request_id = getattr(local, 'request_id', NO_REQUEST_ID) filename_out = 'network_equip' + \ str(equipment.id) + '_config_' + str(request_id) filename_to_save = NETWORK_CONFIG_FILES_PATH + filename_out rel_file_to_deploy = NETWORK_CONFIG_TOAPPLY_REL_PATH + filename_out try: network_template_file = _load_template_file(equipment, template_type) key_dict = _generate_template_dict(dict_ips, equipment) config_to_be_saved += network_template_file.render(Context(key_dict)) except KeyError, exception: log.error('Erro: %s ' % exception) raise exceptions_interface.InvalidKeyException(exception)
Context(key_dict)) except exceptions.InterfaceTemplateException, e: log.error(e) raise exceptions.InterfaceTemplateException() except KeyError, exception: log.error('Erro: %s ' % exception) raise exceptions.InvalidKeyException(exception) log.info('facade ' + str(i.interface)) try: channel_template_file = _load_template_file(int(equip_id), TEMPLATE_REMOVE_CHANNEL) config_to_be_saved += channel_template_file.render(Context(key_dict)) except KeyError, exception: log.error('Erro: %s ' % exception) raise exceptions.InvalidKeyException(exception) # Save new file try: file_handle = open(filename_to_save, 'w') file_handle.write(config_to_be_saved) file_handle.close() except IOError, e: log.error('Error writing to config file: %s' % filename_to_save) raise e return rel_file_to_deploy def delete_channel(user, equip_id, interface_list, channel):