def _load_template_file(equipment_id, template_type): log.info("_load_template_file") try: INTERFACE_CONFIG_TEMPLATE_PATH = get_variable( 'interface_config_template_path') except ObjectDoesNotExist: raise var_exceptions.VariableDoesNotExistException( 'Erro buscando a variável INTERFACE_CONFIG' '<TOAPPLY,TEMPLATE,FILES>_PATH.') try: equipment_template = (EquipamentoRoteiro.search( None, equipment_id, template_type)).uniqueResult() except: log.error('Template type %s not found. Equip: %s' % (template_type, equipment_id)) raise exceptions.InterfaceTemplateException() filename_in = INTERFACE_CONFIG_TEMPLATE_PATH + \ equipment_template.roteiro.roteiro # Read contents from file try: file_handle = open(filename_in, 'r') template_file = Template(file_handle.read()) file_handle.close() except IOError, e: log.error('Error opening template file for read: %s. Equip: %s' % (filename_in, equipment_id)) raise e
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)