Beispiel #1
0
def exec_command(**kwargs):
    """main function for executing getconfig commands to southbound drivers"""
    log.debug(f'called w/ {kwargs}')
    lib = kwargs.get("library", False)
    command = kwargs.get("command", False)
    webhook = kwargs.get("webhook", False)
    post_checks = kwargs.get("post_checks", False)

    result = False

    if type(command) == str:
        commandlst = [command]
    else:
        commandlst = command

    if not post_checks:
        try:
            result = {}
            if lib == "netmiko":
                netmik = netmko(**kwargs)
                sesh = netmik.connect()
                result = netmik.sendcommand(sesh, commandlst)
                netmik.logout(sesh)
            elif lib == "napalm":
                napl = naplm(**kwargs)
                sesh = napl.connect()
                result = napl.sendcommand(sesh, commandlst)
                napl.logout(sesh)
            elif lib == "ncclient":
                ncc = ncclien(**kwargs)
                sesh = ncc.connect()
                result = ncc.getconfig(sesh)
                ncc.logout(sesh)
            elif lib == "restconf":
                rc = restconf(**kwargs)
                sesh = rc.connect()
                result = rc.sendcommand(sesh)
                rc.logout(sesh)
            else:
                raise NotImplementedError(f"unknown 'library' parameter {lib}")
        except Exception as e:
            write_meta_error(f"{e}")

    else:
        try:
            result = {}
            if lib == "netmiko":
                netmik = netmko(**kwargs)
                sesh = netmik.connect()
                if commandlst:
                    result = netmik.sendcommand(sesh, commandlst)
                if post_checks:
                    for postcheck in post_checks:
                        command = postcheck["get_config_args"]["command"]
                        post_check_result = netmik.sendcommand(sesh, [command])
                        for matchstr in postcheck["match_str"]:
                            if postcheck[
                                    "match_type"] == "include" and matchstr not in str(
                                        post_check_result):
                                write_meta_error(
                                    f"PostCheck Failed: {matchstr} not found in {post_check_result}"
                                )
                            if postcheck[
                                    "match_type"] == "exclude" and matchstr in str(
                                        post_check_result):
                                write_meta_error(
                                    f"PostCheck Failed: {matchstr} found in {post_check_result}"
                                )
                netmik.logout(sesh)
            elif lib == "napalm":
                napl = naplm(**kwargs)
                sesh = napl.connect()
                if commandlst:
                    result = napl.sendcommand(sesh, commandlst)
                if post_checks:
                    for postcheck in post_checks:
                        command = postcheck["get_config_args"]["command"]
                        post_check_result = napl.sendcommand(sesh, [command])
                        for matchstr in postcheck["match_str"]:
                            if postcheck[
                                    "match_type"] == "include" and matchstr not in str(
                                        post_check_result):
                                write_meta_error(
                                    f"PostCheck Failed: {matchstr} not found in {post_check_result}"
                                )
                            if postcheck[
                                    "match_type"] == "exclude" and matchstr in str(
                                        post_check_result):
                                write_meta_error(
                                    f"PostCheck Failed: {matchstr} found in {post_check_result}"
                                )
                napl.logout(sesh)
            elif lib == "ncclient":
                ncc = ncclien(**kwargs)
                sesh = ncc.connect()
                result = ncc.getconfig(sesh)
                ncc.logout(sesh)
            elif lib == "restconf":
                rc = restconf(**kwargs)
                sesh = rc.connect()
                result = rc.sendcommand(sesh)
                rc.logout(sesh)
        except Exception as e:
            write_meta_error(f"{e}")

    try:
        if webhook:
            current_jobdata = render_netpalm_payload(job_result=result)
            exec_webhook_func(jobdata=current_jobdata, webhook_payload=webhook)
    except Exception as e:
        write_meta_error(f"{e}")

    return result
Beispiel #2
0
def exec_config(**kwargs):
    """main function for executing setconfig commands to southbound drivers"""
    lib = kwargs.get("library", False)
    config = kwargs.get("config", False)
    j2conf = kwargs.get("j2config", False)
    webhook = kwargs.get("webhook", False)
    pre_checks = kwargs.get("pre_checks", False)
    post_checks = kwargs.get("post_checks", False)
    enable_mode = kwargs.get("enable_mode", False)

    result = False
    pre_check_ok = True

    try:
        write_mandatory_meta()

        if j2conf:
            j2confargs = j2conf.get("args")
            res = render_j2template(j2conf["template"],
                                    template_type="config",
                                    kwargs=j2confargs)
            config = res["data"]["task_result"]["template_render_result"]

        if not pre_checks and not post_checks:

            if lib == "netmiko":
                netmik = netmko(**kwargs)
                sesh = netmik.connect()
                result = netmik.config(sesh, config, enable_mode)
                netmik.logout(sesh)
            elif lib == "napalm":
                napl = naplm(**kwargs)
                sesh = napl.connect()
                result = napl.config(sesh, config)
                napl.logout(sesh)
            elif lib == "ncclient":
                # if we rendered j2config, add it to the kwargs['args'] dict
                if j2conf and config:
                    if not kwargs.get('args', False):
                        kwargs['args'] = {}
                    kwargs['args']['config'] = config
                ncc = ncclien(**kwargs)
                sesh = ncc.connect()
                result = ncc.editconfig(sesh)
                ncc.logout(sesh)
            elif lib == "restconf":
                rcc = restconf(**kwargs)
                sesh = rcc.connect()
                result = rcc.config(sesh)
                rcc.logout(sesh)

        else:
            if lib == "netmiko":
                netmik = netmko(**kwargs)
                sesh = netmik.connect()
                if pre_checks:
                    for precheck in pre_checks:
                        command = precheck["get_config_args"]["command"]
                        pre_check_result = netmik.sendcommand(sesh, [command])
                        for matchstr in precheck["match_str"]:
                            if precheck[
                                    "match_type"] == "include" and matchstr not in str(
                                        pre_check_result):
                                raise NetpalmCheckError(
                                    f"PreCheck Failed: {matchstr} not found in {pre_check_result}"
                                )
                            if precheck[
                                    "match_type"] == "exclude" and matchstr in str(
                                        pre_check_result):
                                raise NetpalmCheckError(
                                    f"PreCheck Failed: {matchstr} found in {pre_check_result}"
                                )

                if pre_check_ok:
                    result = netmik.config(sesh, config, enable_mode)
                    if post_checks:
                        for postcheck in post_checks:
                            command = postcheck["get_config_args"]["command"]
                            post_check_result = netmik.sendcommand(
                                sesh, [command])
                            for matchstr in postcheck["match_str"]:
                                if postcheck[
                                        "match_type"] == "include" and matchstr not in str(
                                            post_check_result):
                                    raise NetpalmCheckError(
                                        f"PostCheck Failed: {matchstr} not found in {post_check_result}"
                                    )
                                if postcheck[
                                        "match_type"] == "exclude" and matchstr in str(
                                            post_check_result):
                                    raise NetpalmCheckError(
                                        f"PostCheck Failed: {matchstr} found in {post_check_result}"
                                    )
                netmik.logout(sesh)

            elif lib == "napalm":
                napl = naplm(**kwargs)
                sesh = napl.connect()
                if pre_checks:
                    for precheck in pre_checks:
                        command = precheck["get_config_args"]["command"]
                        pre_check_result = napl.sendcommand(sesh, [command])
                        for matchstr in precheck["match_str"]:
                            if precheck[
                                    "match_type"] == "include" and matchstr not in str(
                                        pre_check_result):
                                raise NetpalmCheckError(
                                    f"PreCheck Failed: {matchstr} not found in {pre_check_result}"
                                )
                            if precheck[
                                    "match_type"] == "exclude" and matchstr in str(
                                        pre_check_result):
                                raise NetpalmCheckError(
                                    f"PreCheck Failed: {matchstr} found in {pre_check_result}"
                                )

                if pre_check_ok:
                    result = napl.config(sesh, config)
                    if post_checks:
                        for postcheck in post_checks:
                            command = postcheck["get_config_args"]["command"]
                            post_check_result = napl.sendcommand(
                                sesh, [command])
                            for matchstr in postcheck["match_str"]:
                                if postcheck[
                                        "match_type"] == "include" and matchstr not in str(
                                            post_check_result):
                                    raise NetpalmCheckError(
                                        f"PostCheck Failed: {matchstr} not found in {post_check_result}"
                                    )
                                if postcheck[
                                        "match_type"] == "exclude" and matchstr in str(
                                            post_check_result):
                                    raise NetpalmCheckError(
                                        f"PostCheck Failed: {matchstr} found in {post_check_result}"
                                    )
                napl.logout(sesh)

            elif lib == "ncclient":
                ncc = ncclien(**kwargs)
                sesh = ncc.connect()
                result = ncc.editconfig(sesh)
                ncc.logout(sesh)
            elif lib == "restconf":
                rcc = restconf(**kwargs)
                sesh = rcc.connect()
                result = rcc.config(sesh)
                rcc.logout(sesh)

        if webhook:
            current_jobdata = render_netpalm_payload(job_result=result)
            exec_webhook_func(jobdata=current_jobdata, webhook_payload=webhook)

    except Exception as e:
        write_meta_error(e)

    return result