Beispiel #1
0
def connect_to_host(user, passw, list_commands, list_devices):
    proc = os.getpid()
    logger = AkarLogging(log_level, f'process:{proc}').get_color_logger()
    dict_netmiko = dict()
    dict_netmiko["ip"] = list_devices["ip"]
    dict_netmiko["device_type"] = list_devices["device_type"]
    dict_netmiko["timeout"] = list_devices["timeout"]
    dict_netmiko["username"] = user
    dict_netmiko["global_delay_factor"] = list_devices["global_delay_factor"]
    dict_netmiko["password"] = passw
    hostname = "noname"
    logger.debug(f'{dict_netmiko}')
    ret = dict()
    return_message = ""
    try:
        id_ssh = nm.ConnectHandler(**dict_netmiko)
        id_ssh.read_channel()
        find_hostname = id_ssh.find_prompt()
        hostname = re.match("([^#]*)#", find_hostname).group(1).strip()

        return_message += "!**************** {} : {} ****************\n".format(
            hostname, list_devices["ip"])
        logger.info("Connected to hostname: {} with Ip : {} ... OK".format(
            hostname, dict_netmiko["ip"]))
        for cmd in list_commands:
            return_message += "====>>>> {:30s} <<<<====\n".format(cmd)
            cmd_return = id_ssh.send_command(cmd)
            return_message += "{}\n".format(cmd_return)
    except Exception as error:
        return_message += "!#host_error:{}:{}\n".format(
            list_devices["ip"], hostname)
        return_message += "{}\n".format(error)
        if re.search("timed-out", str(error)):
            return return_message
        else:
            return_message += "!#host_error:{}:{}\n".format(
                list_devices["ip"], hostname)
            return_message += "{}\n".format(error)
    ret[hostname] = return_message
    return ret
Beispiel #2
0
class RunAnsibleRoles():
    def __init__(self, nor_hosts: nornir.core.Nornir) -> None:
        # bpdb.set_trace()
        self._logger = AkarLogging(logging.INFO,
                                   "RunAnsibleRoles").get_color_logger()
        self._logger.info(f'Srvs Hosts: {nor_hosts.inventory.hosts}')
        ans_host_template = Path(Path(__file__).parent, ANS_HOSTS_TEMPLATE)
        gtmp = GenTemplates(template_file=ans_host_template,
                            template_data=nor_hosts)
        self.ans_hosts = gtmp.generate_template()
        self._logger.info(f'hosts: {self.ans_hosts}')
        self.ansible_roles = dict()
        for h in nor_hosts.inventory.hosts:
            if 'ansible_roles' in nor_hosts.inventory.hosts[h].data[
                    'lab_config']:
                for role in nor_hosts.inventory.hosts[h].data['lab_config'][
                        'ansible_roles']:
                    if role not in self.ansible_roles:
                        self.ansible_roles[role] = list()
                    self.ansible_roles[role].append(h)
        ans_roles_template = Path(Path(__file__).parent, ANS_ROLES_TEMPLATE)
        gtmp = GenTemplates(template_file=ans_roles_template,
                            template_data=self.ansible_roles)
        self.ans_playbook = gtmp.generate_template()
        self.ans_playbook_file = str(Path(Path.cwd(), "gen_playbook.yml"))
        with open(self.ans_playbook_file, mode='w') as f:
            f.write(self.ans_playbook)
#        self._logger.info(f'playbook: {self.ans_playbook}')

#playbook = ANS_PLAYBOOK
        envvars = {
            'ANSIBLE_ROLES_PATH': '~/my_ansible_roles:',
        }
        extravars: Dict[str, Any] = {}
        #     "jenkins_role": "True",
        #     "net_os": "ubuntu"
        # }
        kwargs = {
            'playbook': self.ans_playbook_file,
            'inventory': self.ans_hosts,
            #        'private_data_dir': '.',
            'envvars': envvars,
            'extravars': extravars
        }
        self._logger.info(f'Run ansible playbooks')
        result = ansible_runner.run(**kwargs)
        # stdout = result.stdout.read()
        # events = list(result.events)
        # stats = result.stats

        # print(json.dumps(stats, indent=4))

    def get_roles(self):
        pass
Beispiel #3
0
def get_routers_from_openstack():
    logger.info("Get list servers with ip addresses ... ")
    ops_server = ops.Servers(name=lab_name, dbg=logging.WARNING)
    # im = ops.Images()
    # logger.info(srv_ips)
    return ops_server.get_srv_nets(wan_name)


def main():
    lab_routers = ""
    lab_routers = get_routers_from_openstack()
    if lab_routers:
        logger.debug(f"{lab_routers}")
        run_applescript(lab_routers)


if __name__ == "__main__":
    # print(f'Modules path: {sys.path}')
    wan_name = "wan0"
    lab_name = "labv10"
    logger = AkarLogging(logging.INFO, "run_iterm").get_color_logger()
    # cmd_args = cmdArgsParser()
    if len(sys.argv) > 2:
        lab_name = sys.argv[1]
        wan_name = sys.argv[2]
    elif len(sys.argv) == 2:
        lab_name = sys.argv[1]
    logger.info(f"lab: {lab_name} wan: {wan_name}")
    # print(f'lab: {lab_name} wan: {wan_name}')
    main()
Beispiel #4
0
            srv_lab[rt_key]["links"][ln_key]["ipv4"] = ip

    logger.info("Generate stack template")
    logger.debug(f"rt_lab: {json.dumps(rt_lab, indent=2)}")
    if cmd_args.generate:
        generate_stack_template(heat_template_file, out_stack, rt_lab, rt_nets, cmd_args.netid, vm_conf, srv_lab)
    # print(gr_links)
    logger.debug(f"srv_lab: {json.dumps(srv_lab, indent=2)}")
    logger.debug(f"gr_links: {json.dumps(gr_links, indent=2)}")
    logger.debug(f"rt_lab: {json.dumps(rt_lab, indent=2)}")
    if cmd_args.draw:
        draw_net_ipv4_sorted(gr_links)


if __name__ == "__main__":
    logger = AkarLogging(logging.DEBUG, "gen_heat").get_color_logger()
    cmd_args = cmdArgsParser()
    src_dir = os.path.dirname(os.path.realpath(__file__))
    logger.info(f"Source current dir: {__file__}")
    logger.info(f"Source real dir: {os.path.dirname(os.path.realpath(__file__))}")
    gr_links = dict()
    heat_template_file = "template_stack.j2"
    if not os.path.exists("net-stack"):
        os.makedirs("net-stack")
    name_config_yaml = cmd_args.lab
    out_stack = f"{str(cmd_args.output)}/st_{os.path.basename(name_config_yaml)}"
    out_draw = f"{str(cmd_args.output)}/st_{os.path.splitext(os.path.basename(name_config_yaml))[0]}"
    logger.info(f"lab config file: {name_config_yaml}")
    logger.info(f"output heat file: {out_stack}")
    main()
Beispiel #5
0
                                      flag_l2_int=True,
                                      dbg=dbg)
        lst_devices.create_csv_l2_int(out_dir=dir_l2)
        lst_devices.create_csv_l2_int_all(out_dir=dir_reports)
    if ag.vlan:
        logger.info('Get information about VLANs')
        lst_devices = cfg.ListDevices(dir_cfg,
                                      flag_l3_int=False,
                                      flag_vlans=True,
                                      flag_l2_int=False,
                                      dbg=dbg)
        lst_devices.create_csv_vlans(dir_vlans)
        lst_devices.create_csv_vlans_all(dir_reports)
    # elif ag.ospf:
    #     if ag.fospf:
    #         nr.ospf_filter = ag.fospf
    #     nr.ospf_info()


if __name__ == '__main__':
    dbg = logging.INFO
    logger = AkarLogging(dbg, "netinfo").get_color_logger()
    logger.info("Begin netinfo ")
    dbg = logging.INFO
    dir_reports = 'output'
    dir_l2 = f'{dir_reports}/l2-int'
    dir_l3 = f'{dir_reports}/l3-int'
    dir_vlans = f'{dir_reports}/vlans'
    dir_cfg = "./configs/*.txt"
    main()
Beispiel #6
0
if __name__ == "__main__":
    mp.freeze_support()
    start_time = time.time()
    listComm = list()
    device_type = "cisco_ios_ssh"
    username_ssh = "root"
    list_dict_hosts_ssh = []
    list_comm = []
    logger = AkarLogging(log_level, "Main Process").get_color_logger()

    arg = check_argument_parser()
    timeout_ssh = int(arg.ssh_timeout)
    num_proc_ssh = int(arg.number_proc)
    if num_proc_ssh == 0:
        num_proc_ssh = int(mp.cpu_count()) * 2
    logger.info("Maximum of processes ssh allow : {}".format(num_proc_ssh))
    pass_ssh = "cisco"
    hosts = list()
    if arg.host_ip:
        hosts = arg.host_ip.split(",")
    if arg.lab:
        hosts = get_routers_from_openstack(arg.lab, arg.netid)
    #        exit(0)
    if hosts:
        for hh in hosts:
            data = dict()
            data["ip"] = hh.strip()
            data["host"] = hh.strip()
            data["timeout"] = timeout_ssh
            data["device_type"] = device_type
            data["global_delay_factor"] = 2
Beispiel #7
0
import argparse
import logging
import akarlibs.yamltoheat as ym
from akarlibs.akarlogging import AkarLogging

description = "openstack: Create heat file from yaml "
epilog = "Alexey Karpov "


def cmdArgsParser():
    logger.info("Parsing arguments ...")
    parser = argparse.ArgumentParser(description=description, epilog=epilog)
    parser.add_argument("-l", "--lab", help="File lab in format YAML", action="store", dest="lab", default="", required=True)
    parser.add_argument("-n", "--netid", help="Openstack stack MGMT network name", action="store", dest="netid", default="wan0")
    parser.add_argument("-z", "--zone", help="Openstack stack Availability zone", action="store", dest="zone", default="nova:osc")
    parser.add_argument("-o", "--output", help="Output dir for stack files", action="store", dest="output", default="net-stack")
    parser.add_argument("-d", "--draw", help="Draw map", dest="draw", action="store_true")
    parser.add_argument("-g", "--generate", help="Generate heat stack file", dest="generate", action="store_true")
    return parser.parse_args()


if __name__ == "__main__":
    logger = AkarLogging(logging.INFO, "gen_heat").get_color_logger()
    logger.info("================ Run gen-heatv3 =================")
    cmd_args = cmdArgsParser()
    name_config_yaml = cmd_args.lab
    logger.info(f'File of lab: {name_config_yaml}')
    ht = ym.DevicesInfo(name_config_yaml, dbg=logging.INFO)
    ym.YamlToHeat(ht, dbg=logging.INFO)
    # print(str(ht))
Beispiel #8
0
if __name__ == "__main__":
    logger = AkarLogging(logging.INFO, "gen_heat").get_color_logger()

    cmd_args = cmdArgsParser()
    stack_name = cmd_args.name
    curdir = str(os.getcwd()).split('/')[-1]
    if os.path.exists(f'{os.getcwd()}/oslabs.yml'):
        os.remove(f'{os.getcwd()}/oslabs.yml')
    with open(f'{os.getcwd()}/oslabs.yml', mode="w") as fn:
        fn.write(f'---\nwan: wan0\nlab: {stack_name}')
    res = re.match(r'^(.*)v\d+$', curdir)
    if not cmd_args.skip:
        gen_heat_os(f'{res.group(1)}.yaml')
    stack_template = f'st_{res.group(1)}.yaml'
    # print(f'Stack template: {stack_template} Stack Name: {stack_name}')
    cmd_run = f'ansible-playbook ans_lab_main.yml  -e stack_template={stack_template} -e stack_name={stack_name}'
    if cmd_args.skip:
        cmd_run += f' -e stack_skip=true'
    if cmd_args.list:
        cmd_run += f' -l {cmd_args.list}'
    if cmd_args.dell:
        cmd_run_del = f'{cmd_run} -t init_del'
        run_ansible(cmd_run_del)
        time.sleep(2)
    if cmd_args.tags:
        cmd_run += f' -t {cmd_args.tags}'
    else:
        cmd_run += f' --skip-tags init_del'
    logger.info(f'{cmd_run}')
    run_ansible(cmd_run)