Beispiel #1
0
import os, os.path
from datetime import datetime
import netmiko
import argparse
from MSPO_utils import netmiko_connect, get_MSPO_env

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Restore ASA device configuration "
    )
    parser.add_argument('customer', help="Customer name")
    parser.add_argument('device', help="Device name")
    parser.add_argument('cfgfile', help="Name of the configuration file to be restored")
    args = parser.parse_args()
# Check if configuration file exist
    cfg = get_MSPO_env()
    CUSTOMER_CFG_DIR = cfg.get('customer_configs') + "/" + args.customer
    if os.path.isdir(CUSTOMER_CFG_DIR) == False:
        print("Customer folder not found: ", CUSTOMER_CFG_DIR)
        exit(1)
    DEVICE_DIR = CUSTOMER_CFG_DIR + "/" + args.device
    if os.path.isdir(DEVICE_DIR) == False:
        print("Device folder not found: ", DEVICE_DIR)
        exit(1)
    CFG_FILE = DEVICE_DIR + "/" + args.cfgfile
    if os.path.isfile(CFG_FILE) == False:
        print("Configuration File not found: ", CFG_FILE)
        exit(1)
# Connect to the ASA
    ssh_conn = netmiko_connect(args.customer, args.device)
# Send the configuration, asa will close the communication channel when applying the configuration
def renderjinja2(customer, device, cfg_entity):

    cfg = get_MSPO_env()
    # We first need to merge global, all_device, device specific yaml in to a single one
    # set golbal config parameter file path
    CFG_GLOBAL = cfg.get('vars') + "/global.yml"
    if not os.path.isfile(CFG_GLOBAL):
        print("global.yml does'not not exist:", CFG_GLOBAL)
        exit(1)
# set customer all parameter file path
    CFG_CUSTOMER = cfg.get('customer_vars') + "/" + customer + "/all.yml"
    # set device model specific parameter file path
    # for this we need to know the device model from teh device specific connect.yml files
    CONNECT_FILE = cfg.get(
        'customer_vars'
    ) + "/" + customer + "/sites/" + device + "/" + "connect.yml"
    if not os.path.isfile(CONNECT_FILE):
        print("connect.yml does'not not exist:", CONNECT_FILE)
        exit(1)
    connect_data = yaml.safe_load(open(CONNECT_FILE))
    model = connect_data.get('device_model')
    CFG_ALL = cfg.get(
        'customer_vars') + "/" + customer + "/devices/" + model + ".yml"
    if not os.path.isfile(CFG_ALL):
        print("all.yml does'not not exist:", CFG_ALL)
        exit(1)
# set device specific configuration file path, existence of this file is not mandatory as some template only use parameters from above file
    CFG_DEVICE = cfg.get(
        'customer_vars'
    ) + "/" + customer + "/sites/" + device + "/" + cfg_entity + ".yml"
    # now that we haev the path to the files, merge them into a single one
    STAGING_DIR = cfg.get('staging') + "/" + customer
    if os.path.isdir(STAGING_DIR) == False:
        os.mkdir(STAGING_DIR)
    CFG_MERGED_FILE = STAGING_DIR + "/" + cfg_entity + ".yml"
    fout = open(CFG_MERGED_FILE, 'w')
    fin = open(CFG_GLOBAL, 'r')
    fout.writelines(fin.readlines())
    fin.close()
    fin = open(CFG_CUSTOMER, 'r')
    fout.writelines(fin.readlines()[1:])
    fin.close()
    fin = open(CFG_ALL, 'r')
    fout.writelines(fin.readlines()[1:])
    fin.close()
    if os.path.isfile(CFG_DEVICE):
        fin = open(CFG_DEVICE, 'r')
        fout.writelines(fin.readlines()[1:])
        fin.close()
    fout.close()
    config = yaml.safe_load(open(CFG_MERGED_FILE))
    # load jinja2 template
    TEMPLATES = cfg.get('templates') + "/" + model
    env = Environment(loader=FileSystemLoader(TEMPLATES),
                      trim_blocks=True,
                      lstrip_blocks=True)
    TEMPLATE_FILE = cfg_entity + ".j2"
    template = env.get_template(TEMPLATE_FILE)
    # Check if configuration directory exist for the device
    CUSTOMER_CFG_DIR = cfg.get('customer_configs') + "/" + customer
    if os.path.isdir(CUSTOMER_CFG_DIR) == False:
        os.mkdir(CUSTOMER_CFG_DIR)
    DEVICE_DIR = CUSTOMER_CFG_DIR + "/" + device
    if os.path.isdir(DEVICE_DIR) == False:
        os.mkdir(DEVICE_DIR)
    CFG_FILE = DEVICE_DIR + "/" + cfg_entity + ".txt"
    # render cfg_entity
    f = open(CFG_FILE, "w")
    f.write(template.render(config))
    f.close