def getAclGit(acl_file_name, environment, network, user): '''Retrieves the contents of the file acl. :param acl_file_name: acl name :param environment: Environment :param network: v4 or v6 :param user: user :raise GITCommandError: Failed to execute command ''' try: acl = check_name_file(acl_file_name) path = path_acl(environment["nome_ambiente_logico"], environment["nome_divisao"], environment["acl_path"]) mkdir_divison_dc(environment["nome_divisao"], user, environment["acl_path"]) chdir(PATH_TYPES.ACL, network, path) Git.synchronization() content = File.read(acl) return content except (GITCommandError, FileError, Exception), e: logger.error("Erro quando o usuário %s tentou sincronizar no Git" % (user.get_username())) logger.error(e) raise GITCommandError(e)
def get_template_edit(template_name, network, user): '''Retrieves the contents of the file template. :param template_name: template name :param network: IPv4 or IPv6 :param user: user :raise GITCommandError: Failed to execute command ''' try: ip_version = IP_VERSION.IPv4[0] if network == IP_VERSION.IPv4[ 1] else IP_VERSION.IPv6[0] path = "%s%s/%s%s" % (PATH_ACL, ip_version, PATH_ACL_TEMPLATES, template_name) chdir(PATH_TYPES.TEMPLATE, ip_version, path) Git.synchronization() content = File.read(template_name) return content except (GITCommandError, FileError, Exception), e: logger.error("Erro quando o usuário %s tentou sincronizar no Git" % (user.get_username())) logger.error(e) raise GITCommandError(e)
def checkAclGit(acl_file_name, environment, network, user): '''Validates if the file is created acl. :param acl_file_name: acl name :param environment: Environment :param network: v4 or v6 :param user: user :raise GITCommandError: Failed to execute command :return: True case created ''' try: acl = check_name_file(acl_file_name) path = path_acl(environment["nome_ambiente_logico"], environment["nome_divisao"], environment["acl_path"]) mkdir_divison_dc(environment["nome_divisao"], user, environment["acl_path"]) chdir(PATH_TYPES.ACL, network, path) Git.synchronization() return os.path.exists(acl) except (GITCommandError, Exception), e: logger.error("Erro quando o usuário %s tentou sincronizar no Git" % (user.get_username())) logger.error(e) raise GITCommandError(e)
def mkdir_divison_dc(divison_dc, user, acl_path=None): '''Creates the directory division dc in git :param divison_dc: division dc to be created :param user: user :raise GITCommandError: Failed to execute ''' try: divison_dc = str(divison_dc).upper() os.chdir(PATH_ACL) if divison_dc == DIVISON_DC.BORDA: divison_dc = "Borda" directory = divison_dc if acl_path: directory = acl_path Git.synchronization() # Set path - Ipv4 - Ipv6 list_path = [] list_path.append("%s%s/" % (PATH_ACL, 'v4')) list_path.append("%s%s/" % (PATH_ACL, 'v6')) for path in list_path: os.chdir(path) folders = directory.split("/") for folder in folders: if folder: if not os.path.exists(folder): os.mkdir(folder) logger.info("%s criou no Git o diretório: %s/%s" % (user.get_username(), path, folder)) path = "%s/%s" % (path, folder) os.chdir(path) except Exception, e: logger.error( "Erro quando o usuário %s tentou criar o diretório: %s no Git" % (user.get_username(), path + folder)) logger.error(e) raise GITCommandError(e)
def createAclGit(acl_name, environment, network, user): '''Create the file acl. :param acl_name: acl name :param environment: Environment :param network: v4 or v6 :param user: user :raise GITCommandError: Failed to execute command ''' try: acl = check_name_file(acl_name) path = path_acl(environment["nome_ambiente_logico"], environment["nome_divisao"], environment["acl_path"]) mkdir_divison_dc(environment["nome_divisao"], user, environment["acl_path"]) chdir(PATH_TYPES.ACL, network, path) Git.synchronization() File.create(acl) Git.add(acl) Git.commit( acl, "Criação do Arquivo %s pelo usuário: %s" % (acl, user.get_username())) Git.push() logger.info("%s criou no GIT o arquivo: %s" % (user.get_username(), (path + acl))) except (GITCommandError, FileError, Exception), e: logger.error( "Erro quando o usuário %s tentou criar o arquivo: %s no Git" % (user.get_username(), (path + acl))) logger.error(e) raise GITCommandError(e)
def create_template(template_name, network, content, user): '''Create the file template. :param template_name: template name :param network: IPv4 or IPv6 :param content: content :param user: user :raise GITCommandError: Failed to execute command ''' try: ip_version = IP_VERSION.IPv4[0] if network == IP_VERSION.IPv4[ 1] else IP_VERSION.IPv6[0] path = "%s%s/%s%s" % (PATH_ACL, ip_version, PATH_ACL_TEMPLATES, template_name) chdir(PATH_TYPES.TEMPLATE, ip_version, path) Git.synchronization() File.create(template_name) Git.add(template_name) Git.commit( template_name, "Criação do Arquivo %s pelo usuário: %s" % (template_name, user.get_username())) Git.push() logger.info("%s criou no GIT o arquivo: %s" % (user.get_username(), (path + template_name))) alter_template(template_name, network, content, user) except (GITCommandError, FileError, Exception), e: logger.error( "Erro quando o usuário %s tentou criar o arquivo: %s no Git" % (user.get_username(), (path + template_name))) logger.error(e) raise GITCommandError(e)
def deleteAclGit(acl_name, environment, network, user): '''Delete acl file :param acl_name: acl name :param environment: Environment :param network: v4 or v6 :param user: user :raise GITCommandError: Failed to execute command ''' try: acl = check_name_file(acl_name) path = path_acl(environment["nome_ambiente_logico"], environment["nome_divisao"], environment["acl_path"]) os.chdir(PATH_ACL) Git.synchronization() path_to_acl = "%s/%s/%s" % (network, path, acl) Git.remove(path_to_acl) Git.commit( path_to_acl, "Exclusão do Arquivo %s pelo usuário:%s" % (acl, user.get_username())) Git.push() logger.info("%s excluiu no GIT o arquivo: %s" % (user.get_username(), (path + acl))) except (GITCommandError, FileError, Exception), e: logger.error( "Erro quando o usuário %s tentou excluiu o arquivo: %s no Git" % (user.get_username(), (path + acl))) logger.error(e) raise GITCommandError(e)
def alterAclGit(acl_name, acl_content, environment, comment, network, user): '''Change the contents of the file acl. :param acl_name: acl name :param acl_content: acl content :param environment: Environment :param comment: comments of user :param network: v4 or v6 :param user: user :raise GITCommandError: Failed to execute command ''' try: acl = check_name_file(acl_name) path = path_acl(environment["nome_ambiente_logico"], environment["nome_divisao"], environment["acl_path"]) chdir(PATH_TYPES.ACL, network, path) Git.synchronization() File.write(acl, acl_content) Git.commit(acl, "%s comentou: %s" % (user.get_username(), comment)) Git.push() logger.info( "%s alterou no GIT o arquivo: %s Comentário do Usuário: %s" % (user.get_username(), (path + acl), comment)) except (GITCommandError, FileError, Exception), e: logger.error( "Erro quando o usuário %s tentou atualizar o arquivo: %s no Git" % (user.get_username(), (path + acl))) logger.error(e) raise GITCommandError(e)
def get_templates(user, return_as_dict=False): """ Get acl templates for list :param user: Instance of current user :param return_as_dict: If the method will return a dictionary or a list :return: list or dict of templates. :: list: [{'name': < template_name >, 'network': < template_network >},...] dict: { "ipv4": [{'name': < template_name >, 'network': < template_network >},...], "ipv6": [{'name': < template_name >, 'network': < template_network >},...] } :raise GITCommandError: Failed to execute command """ try: os.chdir(PATH_ACL) Git.synchronization() aux = dict() aux['ipv4'] = list() aux['ipv6'] = list() templates = list() path_v4 = "%s%s/%s" % (PATH_ACL, IP_VERSION.IPv4[0], PATH_ACL_TEMPLATES) path_v6 = "%s%s/%s" % (PATH_ACL, IP_VERSION.IPv6[0], PATH_ACL_TEMPLATES) if os.path.isdir(path_v4): templates += [{ 'name': f, 'network': IP_VERSION.IPv4[1] } for f in listdir(path_v4) if isfile(join(path_v4, f))] if return_as_dict: aux['ipv4'] = templates templates = [] if os.path.isdir(path_v6): templates += [{ 'name': f, 'network': IP_VERSION.IPv6[1] } for f in listdir(path_v6) if isfile(join(path_v6, f))] if return_as_dict: aux['ipv6'] = templates templates = [] templates = aux if return_as_dict else templates return templates except (GITCommandError, FileError, Exception), e: logger.error("Erro quando o usuário %s tentou sincronizar no Git" % (user.get_username())) logger.error(e) raise GITCommandError(e)
def scriptAclGit(acl_name, vlan, environment, network, user, template_name): '''Generates the acl based on a template :param acl_name: acl name :param vlan: Vvlan :param environment: Environment :param network: v4 or v6 :param user: user :param temple_name: Template Name :raise GITCommandError: Failed to execute command ''' try: acl = check_name_file(acl_name) acl_name = check_name_file(acl_name, extention=False) if template_name: path_env = environment['acl_path'] if environment[ 'acl_path'] else environment['nome_divisao'] chdir(PATH_TYPES.ACL, network, path_env) Git.synchronization() arquivo = open("./%s" % acl, "w") chdir(PATH_TYPES.TEMPLATE, network) file_template = open(template_name, "r") content_template = file_template.read() nova_acl = replace_template(acl_name, vlan, content_template, network) chdir(PATH_TYPES.ACL, network, path_env) arquivo.write("%s" % nova_acl) arquivo.close() file_template.close() Git.commit( acl, "%s gerou Script para a acl %s" % (user.get_username(), acl)) Git.push() logger.info("%s alterou no GIT o arquivo: %s" % (user.get_username(), acl)) else: if ((environment["nome_divisao"] == "BE") and (environment["nome_ambiente_logico"] == "PRODUCAO") and (environment["nome_grupo_l3"] == "CORE/DENSIDADE")): path_env = environment['acl_path'] if environment[ 'acl_path'] else DIVISON_DC.BE chdir(PATH_TYPES.ACL, network, path_env) Git.synchronization() arquivo = open("./%s" % acl, "w") chdir(PATH_TYPES.TEMPLATE, network) file_template = open( PREFIX_TEMPLATES + TEMPLATES.BE + EXTENTION_FILE, "r") content_template = file_template.read() nova_acl = replace_template(acl_name, vlan, content_template, network) chdir(PATH_TYPES.ACL, network, path_env) arquivo.write("%s" % nova_acl) arquivo.close() file_template.close() Git.commit( acl, "%s gerou Script para a acl %s" % (user.get_username(), acl)) Git.push() logger.info("%s alterou no GIT o arquivo: %s" % (user.get_username(), acl)) if ((environment["nome_divisao"] == DIVISON_DC.FE) and (environment["nome_ambiente_logico"] == ENVIRONMENT_LOGICAL.HOMOLOGACAO) and (environment["nome_grupo_l3"] == "CORE/DENSIDADE")): path_env = environment['acl_path'] if environment[ 'acl_path'] else DIVISON_DC.DEV_QA_FE chdir(PATH_TYPES.ACL, network, path_env) Git.synchronization() arquivo = open("./%s" % acl, "w") chdir(PATH_TYPES.TEMPLATE, network) file_template = open( PREFIX_TEMPLATES + TEMPLATES.FE_DEV_QA + EXTENTION_FILE, "r") content_template = file_template.read() nova_acl = replace_template(acl_name, vlan, content_template, network) chdir(PATH_TYPES.ACL, network, path_env) arquivo.write("%s" % nova_acl) arquivo.close() file_template.close() Git.commit( acl, "%s gerou Script para a acl %s" % (user.get_username(), acl)) Git.push() logger.info("%s alterou no GIT o arquivo: %s" % (user.get_username(), acl)) if ((environment["nome_divisao"] == DIVISON_DC.FE) and (environment["nome_ambiente_logico"] == ENVIRONMENT_LOGICAL.PORTAL) and (environment["nome_grupo_l3"] == "CORE/DENSIDADE")): path_env = environment['acl_path'] if environment[ 'acl_path'] else DIVISON_DC.FE chdir(PATH_TYPES.ACL, network, path_env) Git.synchronization() arquivo = open("./%s" % acl, "w") chdir(PATH_TYPES.TEMPLATE, network) if "staging" in acl.lower(): file_template = open( PREFIX_TEMPLATES + TEMPLATES.FE_STAGING + EXTENTION_FILE, "r") else: file_template = open( PREFIX_TEMPLATES + TEMPLATES.FE_PORTAL + EXTENTION_FILE, "r") content_template = file_template.read() nova_acl = replace_template(acl_name, vlan, content_template, network) chdir(PATH_TYPES.ACL, network, path_env) arquivo.write("%s" % nova_acl) arquivo.close() file_template.close() Git.commit( acl, "%s gerou Script para a acl %s" % (user.get_username(), acl)) Git.push() logger.info("%s alterou no GIT o arquivo: %s" % (user.get_username(), acl)) if ((environment["nome_divisao"] == DIVISON_DC.FE) and (environment["nome_ambiente_logico"] == ENVIRONMENT_LOGICAL.APLICATIVOS) and (environment["nome_grupo_l3"] == "CORE/DENSIDADE")): path_env = environment['acl_path'] if environment[ 'acl_path'] else DIVISON_DC.FE chdir(PATH_TYPES.ACL, network, path_env) Git.synchronization() arquivo = open("./%s" % acl, "w") chdir(PATH_TYPES.TEMPLATE, network) file_template = open( PREFIX_TEMPLATES + TEMPLATES.FE_APLICATIVOS + EXTENTION_FILE, "r") content_template = file_template.read() nova_acl = replace_template(acl_name, vlan, content_template, network) chdir(PATH_TYPES.ACL, network, path_env) arquivo.write("%s" % nova_acl) arquivo.close() file_template.close() Git.commit( acl, "%s gerou Script para a acl %s" % (user.get_username(), acl)) Git.push() logger.info("%s alterou no GIT o arquivo: %s" % (user.get_username(), acl)) if ((environment["nome_divisao"] == DIVISON_DC.BE) and (environment["nome_ambiente_logico"] == ENVIRONMENT_LOGICAL.HOMOLOGACAO) and (environment["nome_grupo_l3"] == "CORE/DENSIDADE")): path_env = environment['acl_path'] if environment[ 'acl_path'] else DIVISON_DC.DEV_QA chdir(PATH_TYPES.ACL, network, path_env) Git.synchronization() arquivo = open("./%s" % acl, "w") chdir(PATH_TYPES.TEMPLATE, network) file_template = open( PREFIX_TEMPLATES + TEMPLATES.BEHO + EXTENTION_FILE, "r") content_template = file_template.read() nova_acl = replace_template(acl_name, vlan, content_template, network) chdir(PATH_TYPES.ACL, network, path_env) arquivo.write("%s" % nova_acl) arquivo.close() file_template.close() Git.commit( acl, "%s gerou Script para a acl %s" % (user.get_username(), acl)) Git.push() logger.info("%s alterou no GIT o arquivo: %s" % (user.get_username(), acl)) except (GITCommandError, FileError, Exception), e: logger.error( "Erro quando o usuário %s tentou gerar o arquivo: %s no Git" % (user.get_username(), acl)) logger.error(e) raise GITCommandError(e)
chdir(PATH_TYPES.TEMPLATE, ip_version, path) Git.synchronization() File.read(template_name) return True except FileError, e: return False except (GITCommandError, Exception), e: logger.error("Erro quando o usuário %s tentou sincronizar no Git" % (user.get_username())) logger.error(e) raise GITCommandError(e) def delete_template(template_name, network, user): '''Delete template file :param template_name: template name :param network: IPv4 or IPv6 :param user: user :raise GITCommandError: Failed to execute command ''' try: ip_version = IP_VERSION.IPv4[0] if network == IP_VERSION.IPv4[ 1] else IP_VERSION.IPv6[0]