Beispiel #1
0
    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)
Beispiel #2
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 #3
0
                f'[blue]{nt}',
                f'[magenta1]{ipv4_addr} / {ipv6_addr}',
                f'[magenta]{intr.name}',
                f'[cyan]{intr.mac_addr}',
                f'[bright_green]{intr.dev}',
                f'[yellow]{intr.bridge}',
                f'{intr.mtu}',
                f'{intr.model}',
            )
            ii += 1
        console.print(table)
        if cmd_args.xml:
            print(f'{srv.kvminfo.xml}')
        if cmd_args.vnc:
            p_ssh = Popen([
                '/Applications/VNC Viewer.app/Contents/MacOS/vncviewer',
                f'{srv.host}:{str(srv.kvminfo.vm_vnc_port)}'
            ])
        if cmd_args.serial:
            tn = telnetlib.Telnet(srv.host, srv.kvminfo.vm_serial_port)
            tn.interact()
        logger.info(f"{srv.image_name}")
        logger.info(f"{srv.name} : {srv.instance}")


if __name__ == "__main__":
    logger = AkarLogging(logging.WARNING, "OS Info").get_color_logger()
    cmd_args = cmdArgsParser()
    console = Console()
    main()
Beispiel #4
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 #5
0
def check_stack_online(tname) -> None:
    ops_srvs = ops.Servers(name=tname, dbg=logging.INFO)
    ops_srvs.check_hosts_online()


def gen_heat_os(name_config_yaml) -> None:
    logger.info("================ Run gen-heat-openstack =================")
    os.chdir(cur_dir.parent)
    ht = ym.DevicesInfo(name_config_yaml, dbg=logging.INFO)
    ym.YamlToHeat(ht, dbg=logging.INFO)
    os.chdir(cur_dir)


if __name__ == "__main__":
    TEMPLATES_DIR = "/Users/alex/Dropbox/automation/net-labsv2/playbooks/templates"
    logger = AkarLogging(logging.INFO, "Openstack Labs").get_color_logger()
    InventoryPluginRegister.register("LabInventory", MyLabInventory)

    args = cmdArgsParser()
    stack_name = args.name
    stack_wan = args.wan or "wan0"
    cur_dir = Path.cwd()
    cur_name_dir = str(cur_dir.name)
    cur_name_base = (re.match(r'^(.*)v\d+$', cur_name_dir)).group(1)
    stack_template = f'st_{cur_name_base}.yaml'
    # print(f'{cur_name_base} = {stack_template}')
    if not args.skip:
        gen_heat_os(f'{cur_name_base}.yaml')
        create_stack(stack_name, stack_template)
        check_stack_online(stack_name)
    nr = InitNornir(
Beispiel #6
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 #7
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 #8
0
def main():
    lab_host = cmd_args.name
    host_intrf = f"net{int(cmd_args.interface)-1}"
    logger.info(f"host: {lab_host} interface: {host_intrf}")
    ops_server = ops.Servers(name=lab_host,
                             dbg=logging.WARNING,
                             name_underline=False)
    # print(f'srv: {ops_server}')
    if len(ops_server.servers) > 1:
        logging.error("Exist few elements but need one")
        exit(1)
    else:
        srv = ops_server.servers[0]
    logger.info(f"{srv.name} : {srv.instance}")
    logger.info(f"Interface: {srv.kvminfo.interfaces[host_intrf].dev}")
    os_name = f"root@{srv.host}"
    logger.info(f'os_name: {os_name}')
    p_ssh = Popen([
        "ssh", os_name, "tcpdump", "-U", "-i",
        str(srv.kvminfo.interfaces[host_intrf].dev), "-s", "0", "-w", "-"
    ],
                  stdout=PIPE)
    p_wireshark = Popen(["wireshark", "-k", "-i", "-"], stdin=p_ssh.stdout)


if __name__ == "__main__":
    logger = AkarLogging(logging.INFO, "os_inventory").get_color_logger()
    cmd_args = cmdArgsParser()
    main()
Beispiel #9
0
            return_message += "!#host_error:{}:{}\n".format(
                list_devices["ip"], hostname)
            return_message += "{}\n".format(error)
    ret[hostname] = return_message
    return ret


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:
Beispiel #10
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 #11
0
    return parser.parse_args()


def main():
    logger.info("Get list servers with ip addresses ... ")
    file_config = "oslabs.yml"
    lab_routers = ""
    wan = "wan0"
    lab = "labv22"
    if cmd_args.host:
        print("{}")
        exit(0)

    if os.path.exists(file_config):
        with open(file_config) as file:
            param_list = yaml.load(file, Loader=yaml.FullLoader)
        wan = param_list["wan"]
        lab = param_list["lab"]
        # print(param_list)

    ops_server = ops.Servers(name=lab, dbg=logging.ERROR)
    ops_server.create_dynamic_inventory(wan)
    # if cmd_args.list:
    #     ops_server.create_dynamic_inventory(wan)


if __name__ == "__main__":
    logger = AkarLogging(logging.ERROR, "os_inventory").get_color_logger()
    cmd_args = cmdArgsParser()
    main()