Пример #1
0
    def find_playbook_info(self, device_id, playbook_list):

        device_details = self._device_json.get(device_id)
        if not device_details:
            msg = "Device details for the device %s not found" \
                  % device_id
            raise JobException(msg, self._execution_id)
        device_vendor = device_details.get('device_vendor')
        device_family = device_details.get('device_family')

        if not device_vendor or not device_family:
            raise JobException(
                "device_vendor or device_family not found for %s" % device_id,
                self._execution_id)

        for playbook_info in playbook_list:
            pb_vendor_name = playbook_info.get_vendor()
            if not pb_vendor_name:
                # device_vendor agnostic
                return playbook_info
            if pb_vendor_name.lower() == device_vendor.lower():
                pb_device_family = playbook_info.get_device_family()
                if pb_device_family:
                    if device_family.lower() == pb_device_family.lower():
                        return playbook_info
                else:
                    # device_family agnostic
                    return playbook_info
        msg = "Playbook info not found in the job template for " \
              "%s and %s" % (device_vendor, device_family)
        raise JobException(msg, self._execution_id)
Пример #2
0
    def run_playbook_process(self, playbook_info):
        p = None
        try:
            playbook_exec_path = os.path.dirname(__file__) \
                                 + "/playbook_helper.py"
            p = subprocess32.Popen(["python", playbook_exec_path, "-i",
                                   json.dumps(playbook_info)],
                                   close_fds=True, cwd='/')
            p.wait(timeout=self._playbook_timeout)
        except subprocess32.TimeoutExpired as e:
            if p is not None:
                os.kill(p.pid, 9)
            msg = MsgBundle.getMessage(
                      MsgBundle.RUN_PLAYBOOK_PROCESS_TIMEOUT,
                      playbook_uri=playbook_info['uri'],
                      exc_msg=repr(e))
            raise JobException(msg, self._execution_id)
        except Exception as e:
            msg = MsgBundle.getMessage(MsgBundle.
                                       RUN_PLAYBOOK_PROCESS_ERROR,
                                       playbook_uri=playbook_info['uri'],
                                       exc_msg=repr(e))
            raise JobException(msg, self._execution_id)

        if p.returncode != 0:
            msg = MsgBundle.getMessage(MsgBundle.
                                       PLAYBOOK_EXIT_WITH_ERROR,
                                       playbook_uri=playbook_info['uri'])
            raise JobException(msg, self._execution_id)
Пример #3
0
    def create_job_summary_message(self):
        try:
            job_summary_message = "Job summary: \n"

            if len(self._failed_device_jobs) > 0:
                job_summary_message += "Job failed for devices: "
                for failed_device in self._failed_device_jobs:
                    msg = failed_device + ','
                    job_summary_message += msg
                job_summary_message += "\n"
            else:
                job_summary_message += "Job is successful \n"

            job_summary_message += "Detailed job results: \n"
            result_summary = ""
            for entry in self._job_result:
                result_summary += \
                     "%s:%s \n" % (entry, self._job_result[entry].message)
            job_summary_message += result_summary

            return job_summary_message
        except Exception as e:
            msg = "Error while generating the job summary " \
                  "message : %s" % repr(e)
            raise JobException(msg, self._execution_id)
    def create_job_summary_message(self):
        try:
            job_summary_message = "Job summary: \n"

            if self.job_result_status is None:
                job_summary_message += "Error in getting the job completion " \
                                       "status after job execution. \n"
            elif self.job_result_status == JobStatus.FAILURE:
                if len(self.failed_device_jobs) > 0:
                    job_summary_message += "Job failed with for devices: "
                    for failed_device in self.failed_device_jobs:
                        msg = failed_device + ','
                        job_summary_message += msg
                else:
                    job_summary_message += "Job failed. "
                job_summary_message += "\n"
            elif self.job_result_status == JobStatus.SUCCESS:
                job_summary_message += "Job execution completed " \
                                       "successfully. \n"
            if len(self.job_result) > 0:
                job_summary_message += "Detailed job results: \n"
            result_summary = ""
            for entry in self.job_result:
                result_summary += \
                        "%s:%s \n" % (entry, self.job_result[entry])
            job_summary_message += result_summary

            if self.job_result_message is not None:
                job_summary_message += self.job_result_message

            return job_summary_message
        except Exception as e:
            msg = "Error while generating the job summary " \
                  "message : %s" % repr(e)
            raise JobException(msg, self._execution_id)
 def send_job_log(self,
                  job_template_fqname,
                  job_execution_id,
                  message,
                  status,
                  result=None,
                  timestamp=None):
     try:
         job_template_fqname = self.get_fq_name_log_str(job_template_fqname)
         if timestamp is None:
             timestamp = int(round(time.time() * 1000))
         job_log_entry = JobLogEntry(name=job_template_fqname,
                                     execution_id=job_execution_id,
                                     timestamp=timestamp,
                                     message=message,
                                     status=status,
                                     result=result)
         job_log = JobLog(log_entry=job_log_entry)
         job_log.send(sandesh=self.config_logger._sandesh)
         self.config_logger.debug("Created job log for job template: %s, "
                                  " execution id: %s,  status: %s, result: "
                                  "%s, message: %s" %
                                  (job_template_fqname, job_execution_id,
                                   status, result, message))
     except Exception as e:
         msg = "Error while creating the job log for job template " \
               "%s and execution id %s : %s" % (job_template_fqname,
                                                job_execution_id,
                                                repr(e))
         raise JobException(msg, job_execution_id)
Пример #6
0
    def get_playbook_info(self, device_id=None):
        try:
            # get the playbook uri from the job template
            device_family = None
            if device_id:
                device_family = self._job_utils.get_device_family(device_id)
                playbooks = self._job_template.get_job_template_playbooks()
                play_info = self.find_playbook_info(device_family,
                                                    playbooks.playbook_info)
            else:
                playbooks = self._job_template.get_job_template_playbooks()
                play_info = playbooks.playbook_info[0]

            # create the cmd line param for the playbook
            extra_vars = dict()
            extra_vars.update({'input': self._job_input})
            extra_vars.update({'params': self._job_params})
            extra_vars.update(
                {'job_template_id': self._job_template.get_uuid()})
            extra_vars.update({'device_id': device_id})
            extra_vars.update({'device_family': device_family})
            extra_vars.update({'vendor': play_info.vendor})
            playbook_input = {'playbook_input': extra_vars}

            playbook_info = dict()
            playbook_info.update({'uri': play_info.playbook_url})
            playbook_info.update({'extra_vars': playbook_input})

            return playbook_info
        except Exception as e:
            msg = "Error while getting the playbook information from the " \
                  "job template %s : %s" % (self._job_template.get_uuid(),
                                            repr(e))
            raise JobException(msg, self._execution_id)
Пример #7
0
 def find_playbook_info(self, device_family, playbook_list):
     for playbook_info in playbook_list:
         if playbook_info.device_family == device_family:
             return playbook_info
     msg = "Playbook info not found in the job template for %s" % \
           device_family
     raise JobException(msg, self._execution_id)
Пример #8
0
    def update_job_status(self, output, device_id=None):
        try:
            output = json.loads(output)
            # fetch the status and message from output
            message = output.get('message')
            status = output.get('status')

            # update the cummulative job status
            if status == "Success":
                job_status = JobStatus.SUCCESS
            elif status == "Failure":
                job_status = JobStatus.FAILURE
            else:
                job_status = JobStatus.FAILURE
                err_msg = " No completion status received from the playbook." \
                          " Marking the job as failed."
                if message is not None:
                    message += err_msg
                else:
                    message = err_msg

            if self._job_result_status is None or \
                    self._job_result_status == JobStatus.SUCCESS:
                self._job_result_status = job_status

            # update the result
            if device_id is None:
                device_id = self.DUMMY_DEVICE_ID
            self._job_result.update(
                {device_id: self.DeviceJobResult(message, output)})
        except Exception as e:
            msg = "Error while updating the job status after playbook " \
                  "execution : %s" % repr(e)
            raise JobException(msg, self._execution_id)
Пример #9
0
    def run_playbook(self, playbook_info):
        try:
            # create job log to capture the start of the playbook
            device_id = \
                playbook_info['extra_vars']['playbook_input'].get(
                    'device_id', "")
            msg = MsgBundle.getMessage(MsgBundle.START_EXECUTE_PLAYBOOK_MSG,
                                       playbook_uri=playbook_info['uri'],
                                       device_id=device_id,
                                       input_params=json.dumps(
                                                    playbook_info['extra_vars']
                                                    ['playbook_input']
                                                    ['input']),
                                       extra_params=json.dumps(
                                                    playbook_info['extra_vars']
                                                    ['playbook_input']
                                                    ['params']))
            self._logger.debug(msg)
            self._job_log_utils.send_job_log(self._job_template.fq_name,
                                             self._execution_id,
                                             msg, JobStatus.IN_PROGRESS.value)

            if not os.path.exists(playbook_info['uri']):
                msg = MsgBundle.getMessage(MsgBundle.PLAYBOOK_NOT_FOUND,
                                           playbook_uri=playbook_info['uri'])
                raise JobException(msg,
                                   self._execution_id)

            # Run playbook in a separate process. This is needed since
            # ansible cannot be used in a greenlet based patched environment
            self.run_playbook_process(playbook_info)

            # create job log to capture completion of the playbook execution
            msg = MsgBundle.getMessage(MsgBundle.PB_EXEC_COMPLETE_WITH_INFO,
                                       playbook_uri=playbook_info['uri'],
                                       device_id=device_id)
            self._logger.debug(msg)
            self._job_log_utils.send_job_log(self._job_template.fq_name,
                                             self._execution_id,
                                             msg, JobStatus.IN_PROGRESS.value)
        except JobException:
            raise
        except Exception as e:
            msg = MsgBundle.getMessage(MsgBundle.RUN_PLAYBOOK_ERROR,
                                       playbook_uri=playbook_info['uri'],
                                       exc_msg=repr(e))
            raise JobException(msg, self._execution_id)
Пример #10
0
    def run_playbook(self, playbook_info):
        try:
            # create job log to capture the start of the playbook
            device_id = \
                playbook_info['extra_vars']['playbook_input']['device_id']
            if device_id is None:
                device_id = ""
            msg = "Starting to execute the playbook %s for device " \
                  "%s with input %s and params %s " % \
                  (playbook_info['uri'], device_id,
                   json.dumps(playbook_info['extra_vars']['playbook_input']
                              ['input']),
                   json.dumps(playbook_info['extra_vars']['playbook_input']
                              ['params']))
            self._logger.debug(msg)
            self._job_log_utils.send_job_log(self._job_template.fq_name,
                                             self._execution_id, msg,
                                             JobStatus.IN_PROGRESS.value)

            if not os.path.exists(playbook_info['uri']):
                raise JobException(
                    "Playbook %s does not "
                    "exist" % playbook_info['uri'], self._execution_id)

            # Run playbook in a separate process. This is needed since
            # ansible cannot be used in a greenlet based patched environment
            self.run_playbook_process(playbook_info)

            # create job log to capture completion of the playbook execution
            msg = "Completed to execute the playbook %s for" \
                  " device %s" % (playbook_info['uri'],
                                  playbook_info['extra_vars']['playbook_input']
                                  ['device_id'])
            self._logger.debug(msg)
            self._job_log_utils.send_job_log(self._job_template.fq_name,
                                             self._execution_id, msg,
                                             JobStatus.IN_PROGRESS.value)
            return
        except JobException:
            raise
        except Exception as e:
            msg = "Error while executing the playbook %s : %s" % \
                  (playbook_info['uri'], repr(e))
            raise JobException(msg, self._execution_id)
Пример #11
0
    def get_playbook_info(self, device_id=None):
        try:
            # get the playbook uri from the job template
            device_family = None
            if device_id:
                playbooks = self._job_template.get_job_template_playbooks()
                play_info = self.find_playbook_info(device_id,
                                                    playbooks.playbook_info)
            else:
                playbooks = self._job_template.get_job_template_playbooks()
                play_info = playbooks.playbook_info[0]

            # create the cmd line param for the playbook
            extra_vars = dict()
            extra_vars.update({'input': self._job_input})
            extra_vars.update({'params': self._job_params})
            extra_vars.update(
                {'job_template_id': self._job_template.get_uuid()})
            extra_vars.update({'device_id': device_id})
            extra_vars.update({'device_family': device_family})
            extra_vars.update({'auth_token': self._auth_token})
            extra_vars.update({'job_execution_id': self._execution_id})
            extra_vars.update({'vendor': play_info.vendor})
            if self._device_json is not None:
                device_data = self._device_json.get(device_id)
                if device_data is not None:
                    extra_vars.update({
                        'device_management_ip':
                        device_data.get('device_management_ip')
                    })
                    extra_vars.update({
                        'device_username':
                        device_data.get('device_username')
                    })
                    extra_vars.update({
                        'device_password':
                        device_data.get('device_password')
                    })
                    extra_vars.update(
                        {'device_fqname': device_data.get('device_fqname')})
                    self._logger.debug("Passing the following device "
                                       "ip to playbook %s " %
                                       device_data.get('device_management_ip'))
            playbook_input = {'playbook_input': extra_vars}

            playbook_info = dict()
            playbook_info.update({'uri': play_info.playbook_uri})
            playbook_info.update({'extra_vars': playbook_input})

            return playbook_info
        except Exception as e:
            msg = "Error while getting the playbook information from the " \
                  "job template %s : %s" % (self._job_template.get_uuid(),
                                            repr(e))
            raise JobException(msg, self._execution_id)
Пример #12
0
 def read_job_template(self):
     try:
         job_template = self._vnc_api.job_template_read(
             id=self._job_template_id)
         self._logger.debug("Read job template %s from "
                            "database" % self._job_template_id)
     except Exception as e:
         msg = MsgBundle.getMessage(MsgBundle.READ_JOB_TEMPLATE_ERROR,
                                    job_template_id=self._job_template_id)
         raise JobException(msg, self._job_execution_id)
     return job_template
Пример #13
0
 def read_job_template(self):
     try:
         job_template = self._vnc_api.job_template_read(
             id=self._job_template_id)
         self._logger.debug("Read job template %s from "
                            "database" % self._job_template_id)
     except Exception as e:
         msg = "Error while reading the job template %s from " \
               "database" % self._job_template_id
         raise JobException(msg, self._job_execution_id)
     return job_template
Пример #14
0
    def get_playbook_info(self, device_id=None):
        try:
            # get the playbook uri from the job template
            if device_id:
                playbooks = self._job_template.get_job_template_playbooks()
                play_info = self.find_playbook_info(device_id,
                                                    playbooks.playbook_info)
            else:
                playbooks = self._job_template.get_job_template_playbooks()
                play_info = playbooks.playbook_info[0]

            # create the cmd line param for the playbook
            extra_vars = {
                'input': self._job_input,
                'params': self._job_params,
                'job_template_id': self._job_template.get_uuid(),
                'job_template_fqname': self._job_template.fq_name,
                'device_id': device_id,
                'auth_token': self._auth_token,
                'job_execution_id': self._execution_id,
                'vendor': play_info.vendor,
                'args': self._sandesh_args
            }
            if self._device_json is not None:
                device_data = self._device_json.get(device_id)
                if device_data is not None:
                    extra_vars['device_management_ip'] = \
                        device_data.get('device_management_ip')
                    extra_vars['device_username'] = \
                        device_data.get('device_username')
                    extra_vars['device_password'] = \
                        device_data.get('device_password')
                    extra_vars['device_fqname'] = \
                        device_data.get('device_fqname')
                    extra_vars['device_family'] = \
                        device_data.get('device_family')
                    self._logger.debug("Passing the following device "
                                       "ip to playbook %s " %
                                       device_data.get('device_management_ip'))
            playbook_input = {'playbook_input': extra_vars}

            playbook_info = dict()
            playbook_info['uri'] = play_info.playbook_uri
            playbook_info['extra_vars'] = playbook_input

            return playbook_info
        except JobException:
            raise
        except Exception as e:
            msg = "Error while getting the playbook information from the " \
                  "job template %s : %s" % (self._job_template.get_uuid(),
                                            repr(e))
            raise JobException(msg, self._execution_id)
Пример #15
0
    def get_device_family(self, device_id):
        try:
            pr = self._vnc_api.physical_router_read(id=device_id)
            self._logger.debug(
                "Read device family as %s for device "
                "%s" % (pr.get_physical_router_device_family(), device_id))
        except Exception as e:
            msg = "Error while reading the device family from DB for " \
                  "device %s " % device_id
            raise JobException(msg, self._job_execution_id)

        return pr.get_physical_router_device_family()
Пример #16
0
 def run_playbook_process(self, playbook_info):
     p = None
     try:
         playbook_exec_path = os.path.dirname(__file__) \
                              + "/playbook_helper.py"
         p = subprocess.Popen([
             "python", playbook_exec_path, "-i",
             json.dumps(playbook_info)
         ],
                              close_fds=True,
                              cwd='/')
         p.wait()
         if p.returncode != 0:
             msg = "Playbook exited with error."
             raise JobException(msg, self._execution_id)
     except JobException:
         raise
     except Exception as e:
         msg = "Exception in creating a playbook process " \
               "for %s : %s" % (playbook_info['uri'], repr(e))
         raise JobException(msg, self._execution_id)
     return
 def test_sandesh_timeout(self):
     mocked_sandesh_utils = flexmock()
     flexmock(SandeshUtils, __new__=mocked_sandesh_utils)
     mocked_sandesh_utils.should_receive('__init__')
     mocked_sandesh_utils.should_receive('wait_for_connection_establish')\
                         .and_raise(JobException())
     args = {"collectors": ['127.0.0.1:8086']}
     exc_msg = self.assertRaises(
         JobException,
         JobLogUtils,
         "rdm_exc_id",
         json.dumps(args))
     self.assertEqual(str(exc_msg), "JobException in execution" +
                      " (None): " + MsgBundle.getMessage(
                      MsgBundle.SANDESH_INITIALIZATION_TIMEOUT_ERROR))
 def initialize_sandesh_logger(self, config_args):
     # parse the logger args
     args = self.parse_logger_args(config_args)
     args.random_collectors = args.collectors
     if args.collectors:
         args.random_collectors = random.sample(args.collectors,
                                                len(args.collectors))
         self.args = args
     # initialize logger
     logger = JobLogger(args=args,
                        sandesh_instance_id=self.sandesh_instance_id)
     try:
         sandesh_util = SandeshUtils()
         sandesh_util.wait_for_connection_establish(logger)
     except JobException:
         raise JobException("Sandesh initialization timeout after 15s")
     logger.info("Sandesh is initialized. Config logger instance created.")
     return logger
Пример #19
0
 def send_job_execution_uve(self,
                            timestamp=None,
                            percentage_completed=None):
     try:
         if timestamp is None:
             timestamp = strftime("%Y-%m-%d %H:%M:%S", gmtime())
         job_exe_data = JobExecution(
             name=self._job_template_id,
             execution_id=self._job_execution_id,
             job_start_ts=timestamp,
             percentage_completed=percentage_completed)
         job_uve = UveJobExecution(data=job_exe_data)
         job_uve.send(sandesh=self._logger._sandesh)
     except Exception as e:
         msg = "Error while sending the job execution UVE for job " \
               "template %s and execution id %s : %s" % \
               (self._job_template_id, self._job_execution_id, repr(e))
         raise JobException(msg, self._job_execution_id)
 def initialize_sandesh_logger(self, config_args):
     # parse the logger args
     args = self.parse_logger_args(config_args)
     args.random_collectors = args.collectors
     if args.collectors:
         args.random_collectors = random.sample(args.collectors,
                                                len(args.collectors))
         self.args = args
     # initialize logger
     logger = JobLogger(args=args,
                        sandesh_instance_id=self.sandesh_instance_id)
     try:
         sandesh_util = SandeshUtils(logger)
         sandesh_util.wait_for_connection_establish()
     except JobException:
         msg = MsgBundle.getMessage(
             MsgBundle.SANDESH_INITIALIZATION_TIMEOUT_ERROR)
         raise JobException(msg)
     logger.info("Sandesh is initialized. Config logger instance created.")
     return logger
Пример #21
0
    def find_playbook_info(self, device_family, device_vendor, playbook_list):

        for playbook_info in playbook_list:
            pb_vendor_name = playbook_info.get_vendor()
            if not pb_vendor_name:
                # device_vendor agnostic
                return playbook_info
            if pb_vendor_name.lower() == device_vendor.lower():
                pb_device_family = playbook_info.get_device_family()
                if pb_device_family:
                    if device_family.lower() == pb_device_family.lower():
                        return playbook_info
                else:
                    # device_family agnostic
                    return playbook_info
        msg = MsgBundle.getMessage(MsgBundle.
                                   PLAYBOOK_INFO_DEVICE_MISMATCH,
                                   device_vendor=device_vendor,
                                   device_family=device_family)
        raise JobException(msg, self._execution_id)
 def send_job_execution_uve(self,
                            job_template_fqname,
                            job_execution_id,
                            timestamp=None,
                            percentage_completed=None):
     try:
         job_template_fqname = self.get_fq_name_log_str(job_template_fqname)
         if timestamp is None:
             timestamp = int(round(time.time() * 1000))
         job_exe_data = JobExecution(
             name=job_template_fqname,
             execution_id=job_execution_id,
             job_start_ts=timestamp,
             percentage_completed=percentage_completed)
         job_uve = UveJobExecution(data=job_exe_data)
         job_uve.send(sandesh=self.config_logger._sandesh)
     except Exception as e:
         msg = "Error while sending the job execution UVE for job " \
               "template %s and execution id %s : %s" % \
               (job_template_fqname, job_execution_id, repr(e))
         raise JobException(msg, job_execution_id)
    def send_prouter_object_log(self,
                                prouter_fqname,
                                job_execution_id,
                                job_input,
                                job_template_fqname,
                                onboarding_state,
                                os_version=None,
                                serial_num=None,
                                timestamp=None):
        try:
            job_template_fqname = self.get_fq_name_log_str(job_template_fqname)
            prouter_fqname = self.get_fq_name_log_str(prouter_fqname)
            if timestamp is None:
                timestamp = int(round(time.time() * 1000))
            # create the prouter object log
            prouter_log_entry = PRouterOnboardingLogEntry(
                name=prouter_fqname,
                job_execution_id=job_execution_id,
                os_version=os_version,
                serial_num=serial_num,
                onboarding_state=onboarding_state,
                timestamp=timestamp,
                job_template_fqname=job_template_fqname,
                job_input=job_input)

            prouter_log = PRouterOnboardingLog(log_entry=prouter_log_entry)
            prouter_log.send(sandesh=self.config_logger._sandesh)
            self.config_logger.debug(
                "Created prouter object log for router: %s, "
                " execution id: %s,  job_template: %s, os_version: "
                "%s, serial_num: %s, onboarding_state %s" %
                (prouter_fqname, job_execution_id, job_template_fqname,
                 os_version, serial_num, onboarding_state))
        except Exception as e:
            msg = MsgBundle.getMessage(MsgBundle.SEND_PROUTER_OBJECT_LOG_ERROR,
                                       prouter_fqname=prouter_fqname,
                                       job_execution_id=job_execution_id,
                                       exc_msg=repr(e))

            raise JobException(msg, job_execution_id)
 def send_job_execution_uve(self,
                            job_template_fqname,
                            job_execution_id,
                            timestamp=None,
                            percentage_completed=None):
     try:
         job_template_fqname = self.get_fq_name_log_str(job_template_fqname)
         if timestamp is None:
             timestamp = int(round(time.time() * 1000))
         job_exe_data = JobExecution(
             name=job_template_fqname,
             execution_id=job_execution_id,
             job_start_ts=timestamp,
             percentage_completed=percentage_completed)
         job_uve = UveJobExecution(data=job_exe_data)
         job_uve.send(sandesh=self.config_logger._sandesh)
     except Exception as e:
         msg = MsgBundle.getMessage(MsgBundle.SEND_JOB_EXC_UVE_ERROR,
                                    job_template_fqname=job_template_fqname,
                                    job_execution_id=job_execution_id,
                                    exc_msg=repr(e))
         raise JobException(msg, job_execution_id)
Пример #25
0
 def send_job_log(self, message, status, result=None, timestamp=None):
     try:
         if timestamp is None:
             timestamp = strftime("%Y-%m-%d %H:%M:%S", gmtime())
         job_log_entry = JobLogEntry(name=self._job_template_id,
                                     execution_id=self._job_execution_id,
                                     timestamp=timestamp,
                                     message=message,
                                     status=status,
                                     result=result)
         job_log = JobLog(log_entry=job_log_entry)
         job_log.send(sandesh=self._logger._sandesh)
         self._logger.debug("Created job log for job template: %s, "
                            "execution id: %s,  status: %s, result: %s,"
                            " message: %s" %
                            (self._job_template_id, self._job_execution_id,
                             status, result, message))
     except Exception as e:
         msg = "Error while creating the job log for job template " \
               "%s and execution id %s : %s" % (self._job_template_id,
                                                self._job_execution_id,
                                                repr(e))
         raise JobException(msg, self._job_execution_id)
Пример #26
0
    def run_playbook(self, playbook_info):
        try:
            # create job log to capture the start of the playbook
            device_id = playbook_info['extra_vars']['playbook_input']
            ['device_id']
            if device_id is None:
                device_id = ""
            msg = "Starting to execute the playbook %s for device " \
                  "%s with input %s and params %s " % \
                  (playbook_info['uri'], device_id,
                   json.dumps(playbook_info['extra_vars']['playbook_input']
                              ['input']),
                   json.dumps(playbook_info['extra_vars']['playbook_input']
                              ['params']))
            self._logger.debug(msg)

            if not os.path.exists(playbook_info['uri']):
                raise JobException(
                    "Playbook %s does not "
                    "exist" % playbook_info['uri'], self._execution_id)

            loader = DataLoader()
            inventory = InventoryManager(loader=loader, sources=['localhost'])
            variable_manager = VariableManager(loader=loader,
                                               inventory=inventory)

            Options = namedtuple('Options', [
                'listtags', 'listtasks', 'listhosts', 'syntax', 'connection',
                'module_path', 'forks', 'remote_user', 'private_key_file',
                'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args',
                'scp_extra_args', 'become', 'become_method', 'become_user',
                'verbosity', 'check', 'diff'
            ])
            options = Options(listtags=False,
                              listtasks=False,
                              listhosts=False,
                              syntax=False,
                              connection='ssh',
                              module_path=None,
                              forks=100,
                              remote_user='******',
                              private_key_file=None,
                              ssh_common_args=None,
                              ssh_extra_args=None,
                              sftp_extra_args=None,
                              scp_extra_args=None,
                              become=None,
                              become_method=None,
                              become_user=None,
                              verbosity=None,
                              check=False,
                              diff=False)

            variable_manager.extra_vars = playbook_info['extra_vars']

            passwords = dict(vault_pass='******')

            pbex = PlaybookExecutor(playbooks=[playbook_info['uri']],
                                    inventory=inventory,
                                    variable_manager=variable_manager,
                                    loader=loader,
                                    options=options,
                                    passwords=passwords)

            pbex.run()
            self._logger.debug("Completed executing the playbook %s. "
                               "Collecting output." % playbook_info['uri'])

            output = self.get_plugin_output(pbex)
            self._logger.debug("Output for playbook %s : "
                               "%s" %
                               (playbook_info['uri'], json.dumps(output)))

            # create job log to capture completion of the playbook execution
            msg = "Completed to execute the playbook %s for" \
                  " device %s" % (playbook_info['uri'],
                                  playbook_info['extra_vars']['playbook_input']
                                  ['device_id'])
            self._logger.debug(msg)

            return output
        except Exception as e:
            msg = "Error while executing the playbook %s : %s" % \
                  (playbook_info['uri'], repr(e))
            raise JobException(msg, self._execution_id)
Пример #27
0
    def get_playbook_info(self, device_id=None):
        try:
            # create the cmd line param for the playbook
            extra_vars = {
                'input': self._job_input,
                'params': self._job_params,
                'job_template_id': self._job_template.get_uuid(),
                'job_template_fqname': self._job_template.fq_name,
                'auth_token': self._auth_token,
                'job_execution_id': self._execution_id,
                'args': self._sandesh_args
            }
            playbooks = self._job_template.get_job_template_playbooks()

            if device_id:
                if not self._device_json:
                    msg = MsgBundle.getMessage(MsgBundle.DEVICE_JSON_NOT_FOUND)
                    raise JobException(msg,
                                       self._execution_id)

                device_data = self._device_json.get(device_id)
                if not device_data:
                    msg = MsgBundle.getMessage(MsgBundle.NO_DEVICE_DATA_FOUND,
                                               device_id=device_id)
                    raise JobException(msg, self._execution_id)

                device_family = device_data.get('device_family')
                device_vendor = device_data.get('device_vendor')

                if not device_vendor or not device_family:
                    msg = MsgBundle.getMessage(MsgBundle.
                                               DEVICE_VENDOR_FAMILY_MISSING,
                                               device_id=device_id)
                    raise JobException(msg, self._execution_id)

                # check for credentials,required param; else playbooks
                # will fail
                device_username = device_data.get('device_username')
                device_password = device_data.get('device_password')

                if not device_username or not device_password:
                    msg = MsgBundle.getMessage(MsgBundle.
                                               NO_CREDENTIALS_FOUND,
                                               device_id=device_id)
                    raise JobException(msg, self._execution_id)

                # update extra-vars to reflect device-related params
                device_fqname = device_data.get('device_fqname')
                device_management_ip = device_data.get('device_management_ip')
                extra_vars.update({
                    'device_id': device_id,
                    'device_fqname': device_fqname,
                    'device_management_ip':
                        device_management_ip,
                    'vendor': device_vendor,
                    'device_family': device_family,
                    'device_username': device_username,
                    'device_password': device_password
                })

                self._logger.debug("Passing the following device "
                                   "ip to playbook %s " % device_management_ip)

                # get the playbook uri from the job template
                play_info = self.find_playbook_info(device_family,
                                                    device_vendor,
                                                    playbooks.playbook_info)
            else:
                # get the playbook uri from the job template
                play_info = playbooks.playbook_info[0]

            playbook_input = {'playbook_input': extra_vars}

            playbook_info = dict()
            playbook_info['uri'] = play_info.playbook_uri
            playbook_info['extra_vars'] = playbook_input

            return playbook_info
        except JobException:
            raise
        except Exception as e:
            msg = MsgBundle.getMessage(
                      MsgBundle.GET_PLAYBOOK_INFO_ERROR,
                      job_template_id=self._job_template.get_uuid(),
                      exc_msg=repr(e))
            raise JobException(msg, self._execution_id)