def _stage_acls(acls, log=None, sanitize_acl=False): """stage the new ACL files for load_acl""" import os, shutil from trigger.acl import parse as acl_parse from trigger.conf import settings acl_contents = [] tftp_paths = [] fails = [] for acl in acls: nonce = os.urandom(8).encode('hex') acl_nonce = '%s.%s' % (acl, nonce) src_file = os.path.join(settings.FIREWALL_DIR, acl) dst_file = os.path.join(settings.TFTPROOT_DIR, acl_nonce) if not os.path.exists(dst_file): try: shutil.copyfile(src_file, dst_file) except Exception as err: fails.append("Unable to stage TFTP File %s" % str(acls)) continue else: os.chmod(dst_file, 0644) with open(src_file, 'r') as src_acl: file_contents = src_acl.read() acl_contents.append(file_contents) tftp_paths.append(acl_nonce) # strip comments if brocade if (sanitize_acl): msg = 'Sanitizing ACL {0} as {1}'.format(src_file, dst_file) log.msg(msg) aclobj = acl_parse(file_contents) aclobj.strip_comments() output = '\n'.join(aclobj.output(replace=True)) + '\n' with open(dst_file, 'w') as dst_acl: dst_acl.write(output) return acl_contents, tftp_paths, fails
def _stage_acls(acls, log=None, sanitize_acl=False): """stage the new ACL files for load_acl""" import os from trigger.acl import parse as acl_parse acl_contents = [] tftp_paths = [] fails = [] for acl in acls: nonce = os.urandom(8).encode('hex') source = FIREWALL_DIR + '/%s' % acl dest = TFTPROOT_DIR + '/%s.%s' % (acl, nonce) try: os.stat(dest) except OSError: try: shutil.copyfile(source, dest) except: fails.append("Unable to stage TFTP File %s" % str(acls)) continue else: os.chmod(dest, 0644) file_contents = file(FIREWALL_DIR + '/' + acl).read() acl_contents.append(file_contents) tftp_paths.append("%s.%s" % (acl, nonce)) #strip comments if brocade if (sanitize_acl): msg = 'Sanitizing ACL {0} as {1}'.format(source, dest) log.msg(msg) with open(source, 'r') as src_acl: acl = acl_parse(src_acl) acl.strip_comments() output = '\n'.join(acl.output(replace=True)) + '\n' with open(dest, 'w') as dst_acl: dst_acl.write(output) return acl_contents, tftp_paths, fails