def post(self): ''' 创建发版任务失败后,重试 下载与解压 :return: ''' argument = self.body_arguments publish_plan_id = argument.pop('publish_plan_id', None) if publish_plan_id is None: raise HTTPError(status_code=400, reason="Missing argument:publish_plan_id") with session_scope() as ss: plan = ss.query(PublishPlan).filter_by(id=publish_plan_id, status=3).one_or_none() if plan is None: raise HTTPError(status_code=400, reason="查找的publish_plan_id: {} 不存在,或者已创建成功 ".format(publish_plan_id)) # 状态重置为创建中 plan.status = 1 ss.flush() inventory_version = plan.inventory_version jenkins_url_list = [app.jenkins_url for app in plan.application_list] task_id = uuid() down_load_and_archive_package.apply_async( kwargs={'jenkins_url_list': jenkins_url_list, 'inventory_version': inventory_version, 'publish_plan_id': publish_plan_id}, task_id=task_id) audit_log(self, description='重试发版计划', resource_type=1, resource_id=publish_plan_id) self.render_json_response(code=200, msg="OK", publish_plan_id=publish_plan_id, task_id=task_id)
def delete(self): """ delete user by id""" arguments = self.body_arguments if not arguments: raise HTTPError(status_code=400, reason="json arguments is invalid") _id = arguments.pop("id", None) if not _id: raise HTTPError(status_code=400, reason="ID is required") self.session.query(User).get(_id).delete() self.render_json_response(code=200, msg="OK")
def delete(self): """ delete publish plan by id""" arguments = self.body_arguments if not arguments: raise HTTPError(status_code=400, reason="json arguments is invalid") _id = arguments.pop("id", None) if not _id: raise HTTPError(status_code=400, reason="ID is required") with session_scope() as ss: ss.query(PublishPlan).filter_by(id=_id).update({'is_delete': 1}) audit_log(self, description='删除发版计划', resource_type=1, resource_id=_id) self.render_json_response(code=200, msg='ok', res={'id': _id})
def put(self): """update user""" arguments = self.body_arguments if not arguments: raise HTTPError(status_code=400, reason="json arguments is invalid") _id = arguments.pop("id", None) if not _id: raise HTTPError(status_code=400, reason="ID is required") with session_scope() as ss: ss.query(User).get(_id).update(arguments) res = ss.query(User).get(_id).to_dict() self.render_json_response(code=200, msg="OK", id=_id, res=res)
def put(self): res = {} rightful_keys = ( 'id', 'step', 'title', 'note', 'action', 'status', ) body_kwargs = take_out_unrightful_arguments(rightful_keys, self.body_arguments) id_ = body_kwargs.pop('id', None) if id_ is None: raise HTTPError(status_code=400, reason='Missing argument "id"') with session_scope() as ss: q = ss.query(PublishPattern).filter_by(id=id_) q.update(body_kwargs) db_res = q.one_or_none() if db_res is not None: res = db_res.to_dict() audit_log(self, description='更新发版步骤', resource_type=4, resource_id=id_) self.render_json_response(code=200, res=res)
def put(self): res = {} rightful_keys = ( 'id', 'publish_pattern_id', 'publish_host_id', 'status', ) body_kwargs = take_out_unrightful_arguments(rightful_keys, self.body_arguments) _id = body_kwargs.pop('id', None) pattern_id = body_kwargs.pop('publish_pattern_id', None) host_id = body_kwargs.pop('publish_host_id', None) if (_id is None) and (pattern_id is None or host_id is None): raise HTTPError( status_code=400, reason= '"id" or ("publish_pattern_id" and "publish_host_id") is required' ) with session_scope() as ss: if _id is not None: q = ss.query(PublishPatternHost).filter_by( id=_id).with_for_update() else: q = ss.query(PublishPatternHost).filter_by( publish_pattern_id=pattern_id, publish_host_id=host_id) pattern_host = q.one_or_none() if pattern_host is None: raise HTTPError(status_code=400, reason='Id not found: %d' % _id) q.update(body_kwargs) ss.flush() new_status = body_kwargs.pop('status', None) if new_status is not None: for t in pattern_host.publish_pattern_tasks: t.status = new_status ss.flush() upgrade_pattern_status(ss, pattern_host.publish_pattern_id) res = pattern_host.to_dict() self.render_json_response(code=200, res=res)
def put(self, *args, **kwargs): ''' 修改应用包地址 :param args: :param kwargs: :return: ''' argument = self.body_arguments publish_plan_id = argument.pop('publish_plan_id', None) application_argus = argument.pop('applications', None) if publish_plan_id is None: raise HTTPError(status_code=400, reason="Missing argument:publish_plan_id") # new_inventory_version = str(datetime.now().strftime('%m%d%H%M')) jenkins_url_list = [] with session_scope() as ss: # update publish plan publish_plan = ss.query(PublishPlan).filter_by(id=publish_plan_id).one_or_none() if publish_plan is None: raise HTTPError(status_code=400, reason="查找的publish_plan_id: {} 不存在 ".format(publish_plan_id)) # publish_plan.inventory_version = new_inventory_version inventory_version = publish_plan.inventory_version # 状态重置为创建中 publish_plan.status = 1 ss.flush() # update publish application for app_dict in application_argus: publish_application_id = app_dict.pop('publish_application_id') jenkins_url_list.append(app_dict['jenkins_url']) q = ss.query(PublishApplication).filter_by(id=publish_application_id) q.update(app_dict) task_id = uuid() down_load_and_archive_package.apply_async( kwargs={'jenkins_url_list': jenkins_url_list, 'inventory_version': inventory_version, 'publish_plan_id': publish_plan_id}, task_id=task_id) audit_log(self, description='修改应用包地址', resource_type=1, resource_id=publish_plan_id) self.render_json_response(code=200, msg="OK", publish_plan_id=publish_plan_id, task_id=task_id)
def delete(self): _id = self.body_arguments.pop('id', None) if _id is None: raise HTTPError(status_code=400, reason='Missing argument "id"') with session_scope() as ss: ss.query(PublishProject).filter_by(id=_id).update({'is_delete': 1}) audit_log(self, description='删除发版项目', resource_type=5, resource_id=_id) self.render_json_response(code=200, id=_id, res={'id': _id})
def page_arguments_to_int(page): ''' use only in parser page arguments :param page: :return: ''' try: page = int(page) if page < 1: raise HTTPError( status_code=400, reason= "ValueError ,page &page_size arguments must be number and >0") except ValueError: raise HTTPError( status_code=400, reason= "ValueError ,page &page_size arguments must be number and > 0") return page
def put(self): """update publish plan""" res = {} arguments = self.body_arguments if not arguments: raise HTTPError(status_code=400, reason="json arguments is invalid") _id = arguments.pop("id", None) if not _id: raise HTTPError(status_code=400, reason="ID is required") with session_scope() as ss: q = ss.query(PublishPlan).filter_by(id=_id) q.update(arguments) db_res = q.one_or_none() if db_res: res = db_res.to_dict() audit_log(self, description='更新发版计划', resource_type=1, resource_id=_id) self.render_json_response(code=200, msg="OK", id=_id, res=res)
def session_scope(session): try: yield session session.commit() except exc.IntegrityError as e: session.rollback() raise HTTPError(status_code=400, reason=str(e)) except orm.exc.NoResultFound as e: session.rollback() raise HTTPError(status_code=404, reason=str(e)) except exc.InvalidRequestError as e: error_info = e.args[0] value = Filed.search(error_info) if value: value = value.group(2) reason = "InvalidRequestError, arguments %s is not allowed" % value else: reason = "InvalidRequestError, please check your request arguments" session.rollback() raise HTTPError(status_code=400, reason=reason) except Exception as e: session.rollback() raise HTTPError(status_code=400, reason=str(e))
def post(self): try: project_name = self.body_arguments.pop('name') except KeyError: raise HTTPError(status_code=400, reason="Missing argument 'name'") with session_scope() as ss: publish_project = PublishProject() publish_project.name = project_name publish_project.create_user_id = self.user['id'] ss.add(publish_project) ss.flush() res = publish_project.to_dict() project_id = res['id'] audit_log(self, description='创建发版项目', resource_type=5, resource_id=project_id) self.render_json_response(code=200, res=res, id=project_id)
def post(self): """ add user object argument should be list :return: """ arguments = self.body_arguments if not arguments: raise HTTPError(status_code=400, reason="json arguments is invalid") arguments.pop('id', None) with session_scope() as ss: user_instance = User(arguments) ss.add(user_instance) ss.flush() _id = user_instance.id res = ss.query(User).get(_id).to_dict() self.render_json_response(code=200, msg="OK", id=_id, res=res)
def post(self): """ add publish plan object argument should be list :return: """ # task_id = uuid() user_id = self.user['id'] arguments = self.body_arguments if not arguments: raise HTTPError(status_code=400, reason="json arguments is invalid") arguments.pop('id', None) application_list = arguments.pop('application_list') ''' application_list argument example: [ { "application_name": "canyin", "application_type":1, "application_deploy_path":'xx/xx', "jenkins_url":'xxxx', "host_list":[ { 'host_name': 'xxx', 'host_ip': 'xxx', "host_flag": 'master', 'rollback_version':'' } ] } ] ''' # application_name_list = [url['application_name'] for url in application_list] jenkins_url_list = [url['jenkins_url'] for url in application_list] try: publish_pattern_list = arguments.pop('publish_pattern') """ publish_pattern argument example: [ { 'step': 1, 'title': 'title', 'note': 'note', 'action': 1, 'publish_host': [ { 'application_name': 'application_name', 'host_name': 'host_name' } ] } ] """ except KeyError: raise HTTPError(status_code=400, reason="publish_pattern argument is required") # 先创建 publish_plan inventory_version = str(datetime.now().strftime('%m%d%H%M')) with session_scope() as ss: publish_plan = PublishPlan() publish_plan.type = arguments.pop('type') publish_plan.title = arguments.pop('title') publish_plan.description = arguments.pop('description') publish_plan.create_user_id = user_id publish_plan.status = 1 # 正在创建发版 publish_plan.inventory_version = inventory_version publish_plan.publish_project_id = arguments.pop('publish_project_id') ss.add(publish_plan) ss.flush() publish_plan_id = publish_plan.id # 创建publish application and publish host publish_application_dict = {} for application in application_list: application_name = application['application_name'] if application_name: application_type_num = application_type_map[application['application_type']] publish_application = PublishApplication() publish_application.application_name = application_name publish_application.application_type = application_type_num publish_application.jenkins_url = application['jenkins_url'] publish_application.publish_plan_id = publish_plan_id publish_application.deploy_real_path = application['application_deploy_path'] # publish_application.target_version = target_version 后面update ss.add(publish_application) ss.flush() # 创建 publish host host_dict = {} for host_args in application['host_list']: host_args.update({ 'publish_application_id': publish_application.id, }) host_args['host_flag'] = publish_host_flag_map[host_args['host_flag']] publish_host = PublishHost(**host_args) ss.add(publish_host) ss.flush() host_dict.update({ host_args['host_name']: publish_host.id }) publish_application_dict.update({ application_name: { "id": publish_application.id, "type": application_type_num, "hosts": host_dict } }) # insert publish_pattern and publish_pattern_host for pattern_dict in publish_pattern_list: publish_host_list = pattern_dict.pop('publish_host') # insert publish_pattern pattern = PublishPattern(**pattern_dict) pattern.publish_plan_id = publish_plan_id ss.add(pattern) ss.flush() pattern_id = pattern.id for publish_host_kwargs in publish_host_list: pattern_application_name = publish_host_kwargs.pop('application_name') host_name = publish_host_kwargs['host_name'] publish_host_id = publish_application_dict[pattern_application_name]['hosts'][host_name] # 创建 PublishPatternHost pattern_host = PublishPatternHost() pattern_host.publish_host_id = publish_host_id pattern_host.publish_pattern_id = pattern_id ss.add(pattern_host) ss.flush() # insert publish_pattern_task pattern_host_id = pattern_host.id try: tasks = get_action_tasks( action=pattern_dict['action'], application_type=publish_application_dict[pattern_application_name]["type"], application_name=pattern_application_name ) except PublishError as e: raise HTTPError(status_code=400, reason=str(e)) ss.add_all( [PublishPatternTask(publish_pattern_host_id=pattern_host_id, task_name=task) for task in tasks]) ss.flush() task_id = uuid() down_load_and_archive_package.apply_async( kwargs={'jenkins_url_list': jenkins_url_list, 'inventory_version': inventory_version, 'publish_plan_id': publish_plan_id}, task_id=task_id) audit_log(self, description='创建发版计划', resource_type=1, resource_id=publish_plan_id) self.render_json_response(code=200, msg="OK", publish_plan_id=publish_plan_id, task_id=task_id)
def post(self): """ 执行pattern request params: { "pattern_id": pattern_id } """ pattern_id = self.body_arguments.pop("pattern_id", None) if pattern_id is None: raise HTTPError(status_code=400, reason="Missing argument 'pattern_id'") ready_host_tasks = {} upgrade_plan_finish = False with session_scope() as ss: try: # check pattern status pattern = ss.query(PublishPattern).filter_by(id=pattern_id).with_for_update().one() if pattern.status == 1: raise HTTPError(status_code=400, reason="请求步骤正在执行中") except NoResultFound: raise HTTPError(status_code=400, reason="Find pattern id not found: %s" % pattern_id) # 检查是否需要最后更新plan状态 max_step = ss.query(func.max( PublishPattern.step)).filter_by(publish_plan_id=pattern.publish_plan_id).one()[0] if max_step == pattern.step: upgrade_plan_finish = True pattern_hosts = ss.query(PublishPatternHost).filter( PublishPatternHost.publish_pattern_id == pattern_id).all() if not pattern_hosts: raise HTTPError(status_code=400, reason="No host on the pattern_id: %s" % pattern_id) pattern_hosts = [t.to_dict() for t in pattern_hosts] for pattern_host in pattern_hosts: tasks = [] publish_host = pattern_host['publish_host'] for task in pattern_host['publish_pattern_tasks']: task_name = task['task_name'] task_kwargs = { "publish_host_id_list": [publish_host['id']], "task_name": task_name, "pattern_id": pattern_id, } # 主机有执行中的任务就跳出,主机的任务不再重复执行 if task['status'] == 1: break # 只执行状态是 0:待执行 3:失败 的任务 if task['status'] not in (0, 3): continue tasks.append(task_kwargs) if tasks: host_tasks = ready_host_tasks.setdefault(publish_host['host_name'], []) host_tasks.extend(tasks) # 链式执行任务 run_chain_tasks( session=ss, ready_host_tasks=ready_host_tasks.values(), upgrade_plan_finish=upgrade_plan_finish, plan_id=pattern.publish_plan_id) audit_log(handler=self, description="执行发版步骤", resource_type=4, resource_id=pattern_id) self.render_json_response(code=200, res=ready_host_tasks)