Exemple #1
0
def dryrun(**kwargs):
    lib = kwargs.get("library", False)
    config = kwargs.get("config", False)
    j2conf = kwargs.get("j2config", False)
    webhook = kwargs.get("webhook", False)
    enable_mode = kwargs.get("enable_mode", False)
    result = False

    if j2conf:
        j2confargs = j2conf.get("args")
        try:
            res = render_j2template(j2conf["template"], template_type="config", kwargs=j2confargs)
            config = res["data"]["task_result"]["template_render_result"]
        except Exception as e:
            config = False
            write_meta_error(f"{e}")

    try:
        result = {}
        if lib == "napalm":
            napl = naplm(**kwargs)
            sesh = napl.connect()
            result = napl.config(session=sesh, command=config, dry_run=True)
            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(session=sesh, dry_run=True)
            ncc.logout(sesh)
        elif lib == "netmiko":
            netmik = netmko(**kwargs)
            sesh = netmik.connect()
            result = netmik.config(sesh, config, enable_mode, dry_run=True)
            netmik.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
Exemple #2
0
 def webhook_exec(self, job_data):
     try:
         module = importlib.import_module(self.webhook_name)
         run_whook = getattr(module, "run_webhook")
         job_data["webhook_args"] = self.webhook_args
         whook_data = job_data
         if self.webhook_j2_name:
             res = render_j2template(self.webhook_j2_name,
                                     template_type="webhook",
                                     kwargs=job_data)
             whook_data = json.loads(
                 res["data"]["task_result"]["template_render_result"])
         res = run_whook(payload=whook_data)
         return res
     except Exception as e:
         return e
Exemple #3
0
 def validate_template(self, template_name):
     try:
         args = self.posted_kwargs.get("args", None)
         # redner the j2 template
         rendered_template = render_j2template(templat=template_name,
                                               template_type="service",
                                               kwargs=args)
         # get the rendered data as json
         data = json.loads(rendered_template["data"]["task_result"]
                           ["template_render_result"])
         # double check the template complies with the base model
         ServiceModelTemplate(__root__=data)
         self.template_json = data
         return True
     except Exception as e:
         write_meta_error(f"validate_template: {e}")
         log.error(f"validate_template: {e}")
Exemple #4
0
 def webhook_exec(self, job_data):
     try:
         log.info(f"webhook_exec: importing {self.webhook_name}")
         module = importlib.import_module(self.webhook_name)
         run_whook = getattr(module, "run_webhook")
         job_data["webhook_args"] = self.webhook_args
         whook_data = job_data
         log.info(f"webhook_exec: webhook data loaded {whook_data}")
         if self.webhook_j2_name:
             log.info(
                 f"webhook_exec: rendering webhook j2 template {self.webhook_j2_name}"
             )
             res = render_j2template(self.webhook_j2_name,
                                     template_type="webhook",
                                     kwargs=job_data)
             whook_data = json.loads(
                 res["data"]["task_result"]["template_render_result"])
         res = run_whook(payload=whook_data)
         return res
     except Exception as e:
         log.error(f"webhook_exec: webhook run failure {e}")
         return e
Exemple #5
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