def create_job_summary_message(self): job_summary_message = MsgBundle.getMessage( MsgBundle.JOB_SUMMARY_MESSAGE_HDR) if self.job_result_status is None: job_summary_message += MsgBundle.getMessage( MsgBundle.JOB_RESULT_STATUS_NONE) elif self.job_result_status == JobStatus.FAILURE: if len(self.failed_device_jobs) > 0: job_summary_message += MsgBundle.getMessage( MsgBundle.JOB_MULTI_DEVICE_FAILED_MESSAGE_HDR) 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 += MsgBundle.getMessage( MsgBundle.JOB_EXECUTION_COMPLETE) if len(self.job_result) > 0: job_summary_message += MsgBundle.getMessage( MsgBundle.PLAYBOOK_RESULTS_MESSAGE) 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
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)
def test_execute_job_input_schema_ip_not_found(self): # create job template fake_job_template = flexmock( job_template_type='device', job_template_job_runtime='ansible', job_template_multi_device_job=False, job_template_playbooks=TestJobManagerUtils. playbooks_list, name='input_schema_template_ip', fq_name=["default-global-system-config", "input_schema_template_ip"], uuid='random_uuid') mock_vnc = flexmock() flexmock(VncApi).new_instances(mock_vnc) flexmock(mock_vnc).should_receive('job_template_read')\ .with_args(id="random_uuid")\ .and_return(fake_job_template) TestJobManagerUtils.mock_sandesh_check() job_input_json = { "job_template_id": "random_uuid", "job_execution_id": TestJobManagerUtils.execution_id, "auth_token": "6e7d7f87faa54fac96a2a28ec752336a", "args": TestJobManagerUtils.args } log_utils = JobLogUtils( sandesh_instance_id=TestJobManagerUtils.execution_id, config_args=json.dumps(TestJobManagerUtils.args)) jm = JobManager(log_utils.get_config_logger(), mock_vnc, job_input_json, log_utils) fake_schema = TestJobManagerUtils.fake_schema fake_job_template.should_receive('get_job_template_input_schema')\ .and_return(fake_schema) fake_job_template.should_receive( 'get_job_template_multi_device_job')\ .and_return(False) fake_job_template.should_receive('get_uuid').and_return('random_uuid') # mock the job_handler to raise an exception fake_job_template.should_receive('get_job_template_playbooks') sys_exit_msg = self.assertRaises(SystemExit, jm.start_job) self.assertEqual(sys_exit_msg.code, MsgBundle.getMessage(MsgBundle.JOB_EXC_REC_HDR) + MsgBundle.getMessage( MsgBundle.INPUT_SCHEMA_INPUT_NOT_FOUND) + " ")
def test_execute_job_generic_exception(self): # create job template job_template = JobTemplate(job_template_type='device', job_template_job_runtime='ansible', job_template_multi_device_job=False, job_template_playbooks=TestJobManagerUtils. playbooks_list, name='single_device_template_gen_exc') job_template_uuid = self._vnc_lib.job_template_create(job_template) TestJobManagerUtils.mock_sandesh_check() job_input_json, log_utils = TestJobManagerUtils.get_min_details( job_template_uuid) jm = JobManager(log_utils.get_config_logger(), self._vnc_lib, job_input_json, log_utils) # mock the job_handler to raise a general exception mock_job_handler = flexmock() flexmock(JobHandler).new_instances(mock_job_handler) e = Exception("Mock " "Generic Exception") flexmock(mock_job_handler).should_receive('handle_job')\ .and_raise(e) exc_msg = repr(e) sys_exit_msg = self.assertRaises(SystemExit, jm.start_job) self.assertEqual(sys_exit_msg.code, MsgBundle.getMessage( MsgBundle.EXC_JOB_ERR_HDR) + exc_msg + " ")
def test_execute_job_generic_exception_multi_device(self): # create job template job_template = JobTemplate(job_template_type='device', job_template_job_runtime='ansible', job_template_multi_device_job=True, job_template_playbooks=TestJobManagerUtils. playbooks_list, name='multi_device_template_gen_exc') job_template_uuid = self._vnc_lib.job_template_create(job_template) TestJobManagerUtils.mock_sandesh_check() job_input_json, log_utils = TestJobManagerUtils.get_min_details( job_template_uuid) jm = JobManager(log_utils.get_config_logger(), self._vnc_lib, job_input_json, log_utils) exc_msg = repr(TypeError( "'NoneType' object has no attribute '__getitem__'")) sys_exit_msg = self.assertRaises(SystemExit, jm.start_job) self.assertEqual(sys_exit_msg.code, MsgBundle.getMessage( MsgBundle.EXC_JOB_ERR_HDR) + exc_msg + " ")
def handle_job(self, result_handler, device_id=None): try: msg = "Starting playbook execution for job template %s with " \ "execution id %s" % (self._job_template.get_uuid(), self._execution_id) self._logger.debug(msg) # get the playbook information from the job template playbook_info = self.get_playbook_info(device_id) # run the playbook self.run_playbook(playbook_info) msg = MsgBundle.getMessage( MsgBundle.PLAYBOOK_EXECUTION_COMPLETE, job_template_id=self._job_template.get_uuid(), job_execution_id=self._execution_id) self._logger.debug(msg) result_handler.update_job_status(JobStatus.SUCCESS, msg, device_id) except JobException as e: self._logger.error("%s" % e.msg) self._logger.error("%s" % traceback.format_exc()) result_handler.update_job_status(JobStatus.FAILURE, e.msg, device_id) except Exception as e: self._logger.error("Error while executing job %s " % repr(e)) self._logger.error("%s" % traceback.format_exc()) result_handler.update_job_status(JobStatus.FAILURE, e.message, device_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 = MsgBundle.getMessage(MsgBundle.SEND_JOB_LOG_ERROR, job_template_fqname=job_template_fqname, job_execution_id=job_execution_id, exc_msg=repr(e)) raise JobException(msg, job_execution_id)
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)
def test_execute_job_input_schema(self): # create job template fake_job_template = flexmock( job_template_type='device', job_template_job_runtime='ansible', job_template_multi_device_job=False, job_template_playbooks=TestJobManagerUtils. playbooks_list, name='input_schema_template', fq_name=["default-global-system-config", "input_schema_template"], uuid='random_uuid') TestJobManagerUtils.mock_sandesh_check() job_input_json, log_utils = TestJobManagerUtils.get_min_details( "random_uuid") mock_vnc = flexmock() flexmock(VncApi).new_instances(mock_vnc) flexmock(mock_vnc).should_receive( 'job_template_read').with_args( id="random_uuid").and_return(fake_job_template) jm = JobManager(log_utils.get_config_logger(), mock_vnc, job_input_json, log_utils) e = Exception("'name' is a required property") fake_schema = TestJobManagerUtils.fake_schema fake_job_template.should_receive( 'get_job_template_input_schema').and_return(fake_schema) fake_job_template.should_receive( 'get_job_template_multi_device_job').and_return(False) fake_job_template.should_receive('get_uuid').and_return('random_uuid') # mock the job_handler to raise an exception fake_job_template.should_receive('get_job_template_playbooks') sys_exit_msg = self.assertRaises(SystemExit, jm.start_job) self.assertEqual(sys_exit_msg.code, MsgBundle.getMessage(MsgBundle.JOB_EXC_REC_HDR) + MsgBundle.getMessage(MsgBundle.INVALID_SCHEMA, job_template_id="random_uuid", exc_obj=e) + " ")
def close_sandesh_connection(self): try: self.wait_for_msg_send() except JobException as e: msg = MsgBundle.getMessage(MsgBundle.CLOSE_SANDESH_EXCEPTION) self._logger.error(msg) e.msg = msg raise e finally: self.uninit_sandesh()
def test_execute_job_no_credentials(self): # create job template job_template = JobTemplate(job_template_type='device', job_template_job_runtime='ansible', job_template_multi_device_job=True, job_template_playbooks=TestJobManagerUtils. playbooks_list, name='multi_device_no_credentials') job_template_uuid = self._vnc_lib.job_template_create(job_template) device_id = "aad74e24-a00b-4eb3-8412-f8b9412925c3" TestJobManagerUtils.mock_sandesh_check() job_input_json, log_utils = TestJobManagerUtils.get_min_details( job_template_uuid) job_input_json.update({"params": {"device_list": [device_id]}, "device_json": { device_id: {"device_family": "MX", "device_vendor": "Juniper"}}}) jm = JobManager(log_utils.get_config_logger(), self._vnc_lib, job_input_json, log_utils) sys_exit_msg = self.assertRaises(SystemExit, jm.start_job) self.assertEqual( sys_exit_msg.code, MsgBundle.getMessage(MsgBundle.JOB_SUMMARY_MESSAGE_HDR) + MsgBundle.getMessage(MsgBundle. JOB_MULTI_DEVICE_FAILED_MESSAGE_HDR) + device_id + ",\n" + MsgBundle.getMessage(MsgBundle.PLAYBOOK_RESULTS_MESSAGE) + device_id + ":" + MsgBundle.getMessage( MsgBundle. NO_CREDENTIALS_FOUND, device_id=device_id) + " \n")
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
def test_execute_job_no_template_id(self): job_input_json = {"no_template_id": "Missing template_id"} exc_msg = self.assertRaises( Exception, JobManager, None, None, job_input_json, None) self.assertEqual(str(exc_msg), MsgBundle.getMessage( MsgBundle.JOB_TEMPLATE_MISSING))
def test_execute_job_no_execution_id(self): job_input_json = {"job_template_id": "random_template_id"} exc_msg = self.assertRaises( Exception, JobManager, None, None, job_input_json, None) self.assertEqual(str(exc_msg), MsgBundle.getMessage( MsgBundle.JOB_EXECUTION_ID_MISSING))
def test_execute_job_explicit_mismatch(self): # create job template job_template = JobTemplate(job_template_type='device', job_template_job_runtime='ansible', job_template_multi_device_job=True, job_template_playbooks=TestJobManagerUtils. playbooks_list, name='multi_device_explicit_mismatch') job_template_uuid = self._vnc_lib.job_template_create(job_template) TestJobManagerUtils.mock_sandesh_check() device_id = "aad74e24-a00b-4eb3-8412-f8b9412925c3" job_input_json, log_utils = TestJobManagerUtils.get_min_details( job_template_uuid) job_input_json.update({"params": {"device_list": [device_id]}, "device_json": { device_id: {"device_family": "MX", "device_vendor": "Arista", "device_username": "******", "device_password": "******"}}}) jmgr = JobManager(log_utils.get_config_logger(), self._vnc_lib, job_input_json, log_utils) sys_exit_msg = self.assertRaises(SystemExit, jmgr.start_job) self.assertEqual( sys_exit_msg.code, MsgBundle.getMessage(MsgBundle.JOB_SUMMARY_MESSAGE_HDR) + MsgBundle.getMessage(MsgBundle. JOB_MULTI_DEVICE_FAILED_MESSAGE_HDR) + device_id + ",\n" + MsgBundle.getMessage(MsgBundle.PLAYBOOK_RESULTS_MESSAGE) + device_id + ":" + MsgBundle.getMessage(MsgBundle.PLAYBOOK_INFO_DEVICE_MISMATCH, device_vendor="Arista", device_family="MX") + " \n")
def test_execute_job_run_pb_process_gen_exc(self): # create job template job_template = JobTemplate(job_template_type='device', job_template_job_runtime='ansible', job_template_multi_device_job=False, job_template_playbooks=TestJobManagerUtils. playbooks_list, name='run_pb_prc_gen_exc') job_template_uuid = self._vnc_lib.job_template_create(job_template) TestJobManagerUtils.mock_sandesh_check() job_input_json, log_utils = TestJobManagerUtils.get_min_details( job_template_uuid) jm = JobManager(log_utils.get_config_logger(), self._vnc_lib, job_input_json, log_utils) flexmock(os.path).should_receive('exists').and_return(True) # mock the call to raise exception fake_process = flexmock(returncode=None) fake_process.should_receive('wait') # flexmock(subprocess).should_receive('TimeoutExpired') e = Exception('mock gen exception in run_playbook_process') flexmock(subprocess32).should_receive('Popen').and_raise(e) sys_exit_msg = self.assertRaises(SystemExit, jm.start_job) self.assertEqual( sys_exit_msg.code, MsgBundle.getMessage(MsgBundle.JOB_SUMMARY_MESSAGE_HDR) + MsgBundle.getMessage(MsgBundle. JOB_SINGLE_DEVICE_FAILED_MESSAGE_HDR) + MsgBundle.getMessage(MsgBundle.RUN_PLAYBOOK_PROCESS_ERROR, playbook_uri=TestJobManagerUtils.play_info. playbook_uri, exc_msg=repr(e)))
def test_execute_job_run_pb_gen_exc(self): # create job template job_template = JobTemplate(job_template_type='device', job_template_job_runtime='ansible', job_template_multi_device_job=False, job_template_playbooks=TestJobManagerUtils. playbooks_list, name='run_pb_gen_exc') job_template_uuid = self._vnc_lib.job_template_create(job_template) TestJobManagerUtils.mock_sandesh_check() job_input_json, log_utils = TestJobManagerUtils.get_min_details( job_template_uuid) jm = JobManager(log_utils.get_config_logger(), self._vnc_lib, job_input_json, log_utils) e = Exception('some gen exc') # mock the call to raise exception flexmock(json).should_receive('dumps')\ .and_raise(e) sys_exit_msg = self.assertRaises(SystemExit, jm.start_job) self.assertEqual(sys_exit_msg.code, MsgBundle.getMessage(MsgBundle. JOB_SUMMARY_MESSAGE_HDR) + MsgBundle. getMessage(MsgBundle. JOB_SINGLE_DEVICE_FAILED_MESSAGE_HDR) + MsgBundle.getMessage(MsgBundle.RUN_PLAYBOOK_ERROR, playbook_uri=TestJobManagerUtils. play_info. playbook_uri, exc_msg=repr(e)))
def test_execute_job_read_job_template(self): job_template_id = str(uuid.uuid4()) TestJobManagerUtils.mock_sandesh_check() job_input_json, log_utils = TestJobManagerUtils.get_min_details( job_template_id) mock_vnc = flexmock() flexmock(VncApi).new_instances(mock_vnc) flexmock(mock_vnc).should_receive('job_template_read')\ .with_args(id=job_template_id)\ .and_raise(NoIdError('No such job Template id')) jm = JobManager(log_utils.get_config_logger(), mock_vnc, job_input_json, log_utils) sys_exit_msg = self.assertRaises(SystemExit, jm.start_job) self.assertEqual( sys_exit_msg.code, MsgBundle.getMessage(MsgBundle.JOB_EXC_REC_HDR) + MsgBundle.getMessage(MsgBundle.READ_JOB_TEMPLATE_ERROR, job_template_id=job_template_id) + " ")
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(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
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_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)
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)
msg = msg + "\n" + e.message sys.exit(msg) def parse_args(): parser = argparse.ArgumentParser(description='Ansible playbook input ' 'parameters') parser.add_argument('-i', '--playbook_input', nargs=1, help='Playbook input json') return parser.parse_args() if __name__ == "__main__": playbook_input_json = None try: playbook_params = parse_args() playbook_input_json = json.loads(playbook_params.playbook_input[0]) if playbook_input_json is None: sys.exit(MsgBundle.getMessage(MsgBundle.NO_PLAYBOOK_INPUT_DATA)) except Exception as e: print >> sys.stderr, "Failed to start playbook due "\ "to Exception: %s" % traceback.print_stack() sys.exit( MsgBundle.getMessage(MsgBundle.PLAYBOOK_INPUT_PARSING_ERROR, exc_msg=repr(e))) playbook_helper = PlaybookHelper() playbook_helper.execute_playbook(playbook_input_json)
def execute_playbook(self, playbook_info): try: 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'] pbex = PlaybookExecutor(playbooks=[playbook_info['uri']], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=None) ret_val = pbex.run() if ret_val != 0: msg = MsgBundle.getMessage( MsgBundle.PLAYBOOK_RETURN_WITH_ERROR) raise Exception(msg) output = self.get_plugin_output(pbex) if output is None or output.get('status') is None: msg = MsgBundle.getMessage(MsgBundle.PLAYBOOK_OUTPUT_MISSING) raise Exception(msg) if output.get('status').lower() == "failure": msg = MsgBundle.getMessage(MsgBundle.PLAYBOOK_STATUS_FAILED) raise Exception(msg) return output except Exception as e: msg = MsgBundle.getMessage(MsgBundle.PLAYBOOK_EXECUTE_ERROR, exc_msg=repr(e)) if e.message: msg = msg + "\n" + e.message sys.exit(msg)