def run(self, exec_id): self.init(exec_id=exec_id) try: self.valid_run() self.valid_is_executing() if self.todo.status.content in [ReleaseStatus.Undo, ReleaseStatus.Processing]: if self.todo.status.content == ReleaseStatus.Undo: self.__lock_mission() self.update_todo(status=ReleaseStatus.Processing) result = self.core(list(set([x['host'] for x in Progress.objects.filter(mission=self.mission).values('host')]))) elif self.todo.status.content == ReleaseStatus.Suspend: self.rerun(countdown=DEFAULT_COUNT_DOWN) result = 'mission suspended' else: self.__unlock_mission() if self.todo.status.content == ReleaseStatus.Done: result = 'all done' elif self.todo.status.content == ReleaseStatus.Failed: result = 'exec failed' else: raise ReleaseError('unknown status %s' % self.todo.status.content) return get_result(0, result) except Exception, e: self.__unlock_mission() return get_result(1, str(e))
def run(self): mission = Mission.objects.get(mark=self.mission) if mission: mission.status = 0 mission.save() else: get_result(1, 'error mission id %s ' % self.mission) return super(MissionJobRedoViewAdmin, self).run()
def get_version_result(self, m_result, *args): if m_result[RETCODE] == 0: t_result = m_result[STDOUT][self.host] if isinstance(t_result, dict) and RETCODE in t_result: if t_result[STDOUT] == self.version: result = get_result(0, '%s' % self.version) else: result = get_result( 1, 'wrong version,current version is %s' % t_result[STDOUT]) else: result = get_result(1, t_result) else: result = m_result return result
def post(self, request): host = self.request.data.get('host', None) procs = self.request.data.get('procs', None) if host is None or procs is None: response = HttpResponseBadRequest( json.dumps(get_result(1, 'no correct data in the body'))) else: try: Proc_info().apply_async(args=(host, procs)) response = HttpResponse(json.dumps(get_result(0, 'add'))) except Exception as e: response = HttpResponseServerError( json.dumps( get_result(1, 'add async task failed:{0}'.format( str(e))))) return response
def rebuild(self): self.__valid_executing() env = self.mission.env status = Status.objects.get(content=ReleaseStatus.Undo) cmdb_list = self.__get_list(self.mission.item, env) logger.debug('cmdb_list: %s' % cmdb_list) # fixme: host location change cause rebuild bug for idc in cmdb_list: existed_list = list( set([ x.host for x in Progress.objects.filter( mission=self.mission, location=idc['location']).all() ])) for ip in [x for x in existed_list if x not in idc['list']]: Progress.objects.filter(host=ip, mission=self.mission).delete() step_list = Type_step.objects.filter(type=self.mission.type) for step in step_list: for ip in [x for x in idc['list'] if x not in existed_list]: Progress.objects.filter(~Q(location=idc['location']), host=ip, mission=self.mission).delete() Progress.objects.create(host=ip, step=step.step, dep=self.mission.dep, item=self.mission.item, item_type=self.mission.item_type, type=self.mission.type, env=env, location=idc['location'], step_order=step.order, mission=self.mission, status=status) return get_result(0, 'done!')
def run(self): if self.app_result['count'] == 1: res = self.app_result['results'][0] try: port = int(res.get("port", 80)) except Exception, e: logger.error(str(e)) port = 80 warmup_urls = res['warmup_urls'] if not warmup_urls: all_status = True warm_url = '' else: warm_url = '' all_status = True for m in warmup_urls: logger.debug(m) t_result = self.check( self.host, port, 'http://%s:%s%s' % (self.item, port, m["warmup_url"]), m["expected_codes"], m["expected_text"]) # if t_result: # pass # else: if not t_result: warm_url = m all_status = False break if all_status: return get_result(0, 'done') else: raise ReleaseError('warm this url %s failed' % warm_url)
def progress_host_task(self, todo_host, order, countdown=None): for i in range(0, len(todo_host)): if todo_host[i]: countdown = (i % 5) * 30 if self.mission.type.content.__contains__("expand"): countdown = i * 60 ProgressHostRunTask().apply_async(args=(get_result(0, ''), self.mission, todo_host[i], self.gray_status), kwargs=dict(exec_id=self.exec_id), countdown=countdown) self.update_todo(order=order) self.rerun(countdown=countdown) return 'add to the celery'
def mission_run_view(req): project_name = req.GET.get('project') version = req.GET.get('version') try: call_id = int(req.GET.get('call_id')) except: call_id = None if Project.objects.filter(name=project_name).__len__() == 0: response = HttpResponseBadRequest( json.dumps( get_result(1, 'project not existed {0}'.format(project_name)))) else: project = Project.objects.filter(name=project_name)[0] if Version_history.objects.filter(project=project, version=version).__len__() == 0: response = HttpResponseBadRequest( json.dumps( get_result( 1, 'not ready to publish: not existed {0}:{1} maybe package-job do not complete ' .format(project_name, version)))) else: mission_status = [ x.status.name for x in Mission.objects.filter(project=project, version=version) ] if 'processing' in mission_status or 'in_queue' in mission_status: response = HttpResponseBadRequest( json.dumps( get_result(1, 'this job is already done or processing'))) else: mission = Mission.objects.create( project=project, version=version, status=Status.objects.get(name='undo')) mission.status = Status.objects.get(name='in_queue') mission.save() MissionTask().apply_async(args=(mission.id, )) ## 调用后台任务 response = HttpResponse( json.dumps(get_result(0, 'add to queue'))) return response
def reset(self): mission = Mission.objects.get(mark=self.mission_id) self.__valid_executing(mission) undo_status = Status.objects.get(content=ReleaseStatus.Undo) Mission_control.objects.filter(mission=mission).update( status=undo_status, order=1, lock_status=False) Progress.objects.filter(mission=mission).update(status=undo_status, detail='no result') mission.percent = 0 mission.host_failed = 0 mission.save() return get_result(0, 'done!')
def run(self, rs, mission, host, gray_status, exec_id=0): try: self.init(rs, mission) self.logger.debug('%s %s' % (self.mission, host)) for id_order in range( 1, Progress.objects.filter(mission=self.mission, host=host).count() + 1): progress = Progress.objects.get(mission=self.mission, host=host, step_order=id_order) if self.__phrase_gary_status( gray_status) or not progress.step.gray_status: cmd_result = CmdRunTask().run(self.mission.mark, host, id_order, exec_id=exec_id) self.static_mission_stats( self.__phrase_gary_status(gray_status)) progress = Progress.objects.get(mission=self.mission, host=host, step_order=id_order) if progress.status.content != ReleaseStatus.Done: raise ReleaseError( 'run %s on %s failed.result is:%s' % (progress.step.alias, host, str(cmd_result))) return get_result(0, 'all done!') except Exception, e: Progress.objects.filter( mission=self.mission, host=host, step=get_step(ReleaseStep.Complete)).update(status=get_status( ReleaseStatus.Failed), detail=str(e)) self.static_mission_stats(self.__phrase_gary_status(gray_status)) return get_result(1, str(e))
def run(self, mission): self.init(mission) api = CmdbApi() version_add_url = '/api/cmdb/applications/applicationhistory' item_check_url = '/api/cmdb/applications/applicationgroup?application__name=%s&environment__name=%s' % ( self.mission.item, self.mission.env) for m in api.get(item_check_url)['results']: to_add_url = '/' + '/'.join(m['url'].split('/')[-5:]) api.update(to_add_url, {'version': self.mission.version}) api.create( version_add_url, dict(application_group=m['display_name'], version=self.mission.version, task_id=str(self.mission.mark))) return get_result(0, 'done')
def run(self): if self.app: self.shell('rm -rf /tmp/git/%s' % self.app) self.shell( 'git clone [email protected]:devops/%s.git /tmp/git/%s' % (self.git_path, self.app)) self.shell('cd /tmp/git/%s && echo %s>YMTVersion.txt' % (self.app, self.version)) self.shell('cd /tmp/git/%s && zip -r %s.zip ./*' % (self.app, self.app)) self.shell('cd /tmp/git/%s && md5sum %s.zip >%s.md5' % (self.app, self.app, self.app)) self.upload_ftp(self.app, self.version) return get_result(0, 'package %s %s ok' % (self.app, self.version)) else: raise NameError('no app')
def run(self, mission): self.init(mission) api = JiraApi() query_path = '/api/issues?task_id=%s' % self.mission.mark query_result = api.get(query_path) if self.mission.env == ReleaseEnv.Staging: status = dict(status=u"STAGE发布成功") else: status = dict(status=u"生产发布成功") if query_result['count'] == 1: jira_path = query_result['results'][0]['url'] t_result = api.update(jira_path, status) if t_result == 200: return get_result(0, 'done') else: raise ReleaseError('fail to patch %s' % jira_path) else: raise ReleaseError('jira does not contain this mission %s' % self.mission.mark)
def cancel(self): from ops.celery import app while 1: reserved_act = app.control.inspect().reserved() all_acts = [] for m in reserved_act['celery@Ops_Dev']: n = tuple(eval(m['args'])) mission, a, b = n n_act = {'mission': mission, 'func': m['name'], 'id': m['id']} all_acts.append(n_act) revoke_list = [ x['id'] for x in all_acts if x['mission'] == self.mission_id ] if len(revoke_list) == 0: break else: for n in revoke_list: app.control.revoke(n) return get_result(0, 'done')
def create(self, item_list): self.valid_exist() status = Status.objects.get(content=ReleaseStatus.Undo) step_detail = Type_step.objects.filter(type=self.mission.type) for m in step_detail: step = m.step for n in item_list: for i in n['list']: Progress.objects.create(host=i, step=step, dep=self.mission.dep, item=self.mission.item, item_type=self.mission.item_type, type=self.mission.type, env=self.mission.env, step_order=m.order, mission=self.mission, location=n['location'], status=status) return get_result(0, 'done!')
def progress_host_task(self, todo_host, order, countdown=None): host_list = [] for i in range(0, len(todo_host)): if todo_host[i]: if i == 0: host_list.append(ProgressHostRunTask().s( get_result(0, ''), self.mission, todo_host[i], self.gray_status, exec_id=self.exec_id)) else: host_list.append(ProgressHostRunTask().s( self.mission, todo_host[i], self.gray_status, exec_id=self.exec_id)) chain(host_list).delay() self.update_todo(order=order) self.rerun(countdown=countdown) return 'add to the celery'
def run(self, mission_id, host, order, exec_id=0): self.init(mission_id, host, order, exec_id=exec_id) self.__start() result = None try: if self.progress.step.content in self.salt_cmd_map: cls_cmd = self.salt_cmd_map.get(self.progress.step.content) result = cls_cmd(self.host, self.progress.location, self.mission.item_type, self.mission.dep, self.mission.item, self.mission.version).run() elif self.progress.step.content == ReleaseApiStep.LBUp \ or self.progress.step.content == ReleaseApiStep.LBDown \ or self.progress.step.content == ReleaseApiStep.LBAdd: cmd_query = CmdbApi() if self.progress.step.content == ReleaseApiStep.LBUp: result = CmdLB(self.host, self.progress.location, self.mission.item, cmd_query).lb_up() elif self.progress.step.content == ReleaseApiStep.LBDown: result = CmdLB(self.host, self.progress.location, self.mission.item, cmd_query).lb_down() elif self.progress.step.content == ReleaseApiStep.LBAdd: result = CmdLB(self.host, self.progress.location, self.mission.item, cmd_query).lb_add() elif self.progress.step.content == ReleaseApiStep.HBCheck: result = CmdHbCheck(self.host, self.mission.item).run() elif self.progress.step.content == ReleaseApiStep.Warm: cmd_query = CmdbApi() result = CmdWarm(self.host, self.mission.item, cmd_query).run() # if self.mission.item_type == "iis" : # cmd_query = CmdbApi() # # 1分钟 重试 3次 # result = CmdWarm(self.host, self.mission.item, cmd_query).run() # else: # result = get_result(0, '非IIS不做检查') else: raise ReleaseError('%s this act to do ' % self.progress.step.content) except Exception, e: result = get_result(1, str(e))
def complete(self): Mission.objects.filter(mark=self.mission_id).update(status=True) return get_result(0, 'done!')
def format_response(result): logger.info('result: %s' % result) if result: return HttpResponse(json.dumps(result)) else: return HttpResponse(json.dumps(get_result(1, 'error request')))
def run(self): if self.app and self.version: return PackageAdmin(self.app, self.version).run() else: return get_result(1, 'app and version not provide')
def versioin_console_view(req): if req.method == 'GET': project_name = req.GET.get('project') if Project.objects.filter(name=project_name).__len__() == 0: response = HttpResponseBadRequest( json.dumps( get_result( 1, 'project not existed {0}'.format(project_name)))) else: project = Project.objects.filter(name=project_name)[0] result = [{ 'project': project, "version": x.version, "status": x.status.name, } for x in Version_history.objects.filter(project=project)] response = HttpResponse(json.dumps(result)) elif req.method == 'POST': content = json.loads(req.body) project_name = content.get('project') version = content.get('version') file_name = content.get('file_name') file_md5 = content.get('file_md5') commit_id = content.get('commit_id') download_url = content.get('download_url') if Project.objects.filter( name=project_name).__len__() == 0 and Project.objects.filter( build_name=project_name).__len__() == 0: response = HttpResponseBadRequest( json.dumps( get_result( 1, 'project not existed {0}'.format(project_name)))) else: if Project.objects.filter(name=project_name).__len__() != 0: project = Project.objects.filter(name=project_name)[0] else: project = Project.objects.filter(build_name=project_name)[0] if Version_history.objects.filter(project=project, version=version).__len__() == 0: Version_history.objects.create( project=project, version=version, file_name=file_name, file_md5=file_md5, commit_id=commit_id, download_url=download_url, status=Status.objects.get(name='standby')) response = HttpResponse( json.dumps(get_result(0, 'add to version history'))) elif Version_history.objects.filter( project=project, version=version).__len__() == 1: Version_history.objects.filter( project=project, version=version).update( file_name=file_name, file_md5=file_md5, commit_id=commit_id, status=Status.objects.get(name='standby')) response = HttpResponse( json.dumps(get_result(0, 'add to version history'))) else: response = HttpResponseBadRequest( json.dumps( get_result(1, 'wrong version list please contact admin'))) else: response = HttpResponseBadRequest( json.dumps(get_result(1, 'method not allowed'))) return response
def __create_mission_after(self): self.__create_mission_progress( map(lambda x: dict(location=x['location'], list=x['ipaddresses']), self.item_result['results'])) self.__create_mission_control() return get_result(0, str(self.mission_id))
def format_response(result): if result: return HttpResponse(json.dumps(result)) else: return HttpResponse(json.dumps(get_result(1, 'error request')))