def task_fail_start(self, exception, conn): results = to_str(exception) self.curr_instance = datetime.now() res = TaskResult(self.task_id, self.curr_instance, self.FAILED, results) u = User.by_user_name(self.user_name) if u is None: u = User.by_user_name(u'admin') email = u.email_address notification = Notification(to_unicode(self.task_id), self.name, self.curr_instance, results, self.user_name, email) conn.merge(res) conn.add(notification) if not self.repeating and self.entity_id != None: UIUpdateManager().set_updated_tasks(self.task_id, self.user_name, self.entity_id) return None
def evaluate(self, environ, credentials): if not request.identity: self.unmet() userid = request.identity["repoze.who.userid"] u = User.by_user_name(userid) r = DBHelper().find_by_name(Role, self.role) if not u.has_role(r): self.unmet()
def process_ha_events(self, conn): try: tc = TaskCreator() auth = AuthorizationService() auth.user = User.by_user_name(u'admin') grps = conn.query(ServerGroup).all() for grp in grps: running_ha_evts = conn.query(HAEvent).filter(HAEvent.status == HAEvent.STARTED).filter(HAEvent.sp_id == grp.id).all() if running_ha_evts: continue ha_events = conn.query(HAEvent).filter(HAEvent.status == HAEvent.IDLE).filter(HAEvent.sp_id == grp.id).order_by(HAEvent.event_id.asc()).all() if len(ha_events) > 0: tc.ha_action(auth, grp.id, grp.name) finally: conn.commit()
def do_work(self, runtime_context, args=None, kw=None): self.task_manager = runtime_context.pop() self.curr_instance = datetime.now() args = self.params (recover, resume) = (False, False) visible = False if kw: resume = kw.get('resume', False) recover = kw.get('recover', False) kw = self.kw_params self.quiet = self.get_context_param('quiet') == True if not self.quiet and resume == False and recover == False: self.task_started() # cancelled = False # results = None try: #1487 try: #1359 task_result_running_instance = True if not args: args = [] if not kw: kw = {} auth = AuthorizationService() auth.email_address = '' user = User.by_user_name(self.user_name) auth.user = user if user is None: #564 u = User.by_user_name(u'admin') auth.email_address = u.email_address logger.info('User: '******' does not exist in CMS.') result = LDAPManager().validate_user(self.user_name) if result['success'] == True: #523 group_names = result['group'] groups = Group.by_group_names(group_names) if not groups: #465 msg = 'Groups: ' + str(group_names) + ' does not exist in CMS.' logger.info(msg) raise Exception(msg) #561--598 else: auth.user_name = self.user_name auth.groups = groups if result.get('email_address'): auth.email_address = result['email_address'] #598 else: logger.info(result['msg']) raise Exception('Error in LDAP chcek: ' + result['msg']) #598 else: auth.user = user auth.user_name = user.user_name auth.email_address = user.email_address #598 TaskUtil.set_task_context(self.task_id) if recover != True and resume != True: raw_result = self.exec_task(auth, self.context, *args, **kw) #884 else: #884 runn = self.get_running_instance() if runn: self.curr_instance = runn.timestamp #785 else: #785 task_result = self.get_task_result_instance() if isinstance(task_result.results, str): task_result.results += 'can not resume task. No running instance' #779 else: #779 if not task_result.results: task_result.results = 'can not resume task. No running instance' task_result_running_instance = False if task_result_running_instance: #883 if recover == True: raw_result = self.recover_task(auth, self.context, *args, **kw) #884 else: if resume == True: raw_result = self.resume_task(auth, self.context, *args, **kw) if task_result_running_instance: #1354 cancelled = False results = raw_result if isinstance(raw_result, dict): #1152 if raw_result.get('status') == constants.TASK_CANCELED: #1031 e = raw_result.get('msg') + '\n' + raw_result.get('results') transaction.abort() cancelled = True if not self.quiet: self.task_fail(e, auth, cancelled=True) #1094 else: if raw_result.get('status') == Task.SUCCEEDED: results = raw_result.get('results') visible = raw_result.get('visible', False) if type(results)==dict: #1148 if results.get('success')==True: results = 'Task Completed Successfully.' else: #1355 if raw_result is not None and self.processors is not None: #1250 #isinstance(raw_result, dict) results = [raw_result] for p in self.processors: #1246 if issubclass(p, Processor): p().process_output(results) else: results = raw_result if results is None: #1340 desc_tuple = self.get_short_desc() if desc_tuple is None: results = 'Task Completed Successfully.' else: short_desc,short_desc_params = desc_tuple desc = short_desc % short_desc_params results = desc + ' Completed Successfully.' transaction.commit() except Exception as e: logger.exception(to_str(e)) transaction.abort() if not self.quiet: self.task_fail(e, auth) else: if not self.quiet and cancelled == False: self.task_success(results, visible) finally: DBSession.remove() return None