def deploy(self, tenant, service, user, is_upgrade, committer_name=None): code, msg, event = event_service.create_event(tenant, service, user, self.DEPLOY, committer_name) if code != 200: return code, msg, event body = dict() # 默认更新升级 body["action"] = "deploy" service.build_upgrade = False if is_upgrade: body["action"] = "upgrade" service.build_upgrade = True service.deploy_version = datetime.datetime.now().strftime('%Y%m%d%H%M%S') service.save() event.deploy_version = service.deploy_version event.save() clone_url = service.git_url body["deploy_version"] = service.deploy_version body["operator"] = str(user.nick_name) body["event_id"] = event.event_id body["service_id"] = service.service_id envs = env_var_repo.get_build_envs(tenant.tenant_id, service.service_id) body["envs"] = envs kind = self.__get_service_kind(service) service_source = service_source_repo.get_service_source(service.tenant_id, service.service_id) if kind == "build_from_source_code" or kind == "source": body["repo_url"] = clone_url body["branch"] = service.code_version body["server_type"] = service.server_type if service.service_source == "market": try: # 获取组对象 group_obj = tenant_service_group_repo.get_group_by_service_group_id(service.tenant_service_group_id) if group_obj: # 获取内部市场对象 rain_app = rainbond_app_repo.get_rainbond_app_by_key_and_version(group_obj.group_key, group_obj.group_version) if rain_app: # 解析app_template的json数据 apps_template = json.loads(rain_app.app_template) apps_list = apps_template.get("apps") if service_source and service_source.extend_info: extend_info = json.loads(service_source.extend_info) for app in apps_list: if app["service_share_uuid"] == extend_info["source_service_share_uuid"]: # 如果是slug包,获取内部市场最新的数据保存(如果是最新,就获取最新,不是最新就获取之前的) share_image = app.get("share_image", None) share_slug_path = app.get("share_slug_path", None) new_extend_info = {} if share_image: if app.get("service_image", None): body["image_url"] = share_image body["user"] = app.get("service_image").get("hub_user") body["password"] = app.get("service_image").get("hub_password") new_extend_info = app["service_image"] if share_slug_path: slug_info = app.get("service_slug") slug_info["slug_path"] = share_slug_path new_extend_info = slug_info body["slug_info"] = new_extend_info # 如果是image,获取内部市场最新镜像版本保存(如果是最新,就获取最新,不是最新就获取之前的, 不会报错) service.cmd = app.get("cmd", "") service.version = app["version"] service.is_upgrate = False service.save() new_extend_info["source_deploy_version"] = app.get("deploy_version") new_extend_info["source_service_share_uuid"] = app.get("service_share_uuid") \ if app.get("service_share_uuid", None)\ else app.get("service_key", "") service_source.extend_info = json.dumps(new_extend_info) service_source.save() except Exception as e: logger.exception('===========000============>'.format(e)) body["image_url"] = service.image if service_source: extend_info = json.loads(service_source.extend_info) if service.is_slug(): body["slug_info"] = extend_info else: body["image_url"] = service.image body["kind"] = kind body["service_alias"] = service.service_alias body["service_name"] = service.service_name body["tenant_name"] = tenant.tenant_name body["enterprise_id"] = tenant.enterprise_id body["lang"] = service.language body["cmd"] = service.cmd if service_source: if service_source.user_name or service_source.password: body["user"] = service_source.user_name body["password"] = service_source.password if service_source.extend_info: extend_info = json.loads(service_source.extend_info) if not service.is_slug(): hub_user = extend_info.get("hub_user", None) hub_password = extend_info.get("hub_password", None) if hub_user or hub_password: body["user"] = hub_user body["password"] = hub_password logger.debug('-------------deploy-----body-------------------->{0}'.format(body)) try: region_api.build_service(service.service_region, tenant.tenant_name, service.service_alias, body) except region_api.CallApiError as e: logger.exception(e) if event: event.message = u"应用构建失败".format(e.message) event.final_status = "complete" event.status = "failure" event.save() return 507, "构建异常", event return 200, "操作成功", event
def deploy(self, tenant, service, user, committer_name=None): code, msg, event = event_service.create_event(tenant, service, user, self.DEPLOY, committer_name) if code != 200: return code, msg, event body = {} if not service.deploy_version: body["action"] = "deploy" else: body["action"] = "upgrade" service.deploy_version = datetime.datetime.now().strftime('%Y%m%d%H%M%S') service.save() event.deploy_version = service.deploy_version event.save() clone_url = service.git_url if service.code_from == "github": code_user = clone_url.split("/")[3] code_project_name = clone_url.split("/")[4].split(".")[0] clone_url = "https://" + user.github_token + "@github.com/" + code_user + "/" + code_project_name + ".git" body["deploy_version"] = service.deploy_version body["operator"] = str(user.nick_name) body["event_id"] = event.event_id body["service_id"] = service.service_id envs = env_var_repo.get_build_envs(tenant.tenant_id, service.service_id) body["envs"] = envs kind = self.__get_service_kind(service) if kind == "build_from_source_code" or kind == "source": body["repo_url"] = clone_url body["branch"] = service.code_version body["kind"] = kind body["service_alias"] = service.service_alias body["tenant_name"] = tenant.tenant_name body["enterprise_id"] = tenant.enterprise_id body["lang"] = service.language body["image_url"] = service.image service_source = service_source_repo.get_service_source(service.tenant_id, service.service_id) if service_source: if service_source.user_name or service_source.password: body["user"] = service_source.user_name body["password"] = service_source.password if service_source.extend_info: extend_info = json.loads(service_source.extend_info) if service.is_slug(): body["slug_info"] = extend_info else: hub_user = extend_info.get("hub_user", None) hub_password = extend_info.get("hub_password", None) if hub_user or hub_password: body["user"] = hub_user body["password"] = hub_password try: region_api.build_service(service.service_region, tenant.tenant_name, service.service_alias, body) except region_api.CallApiError as e: logger.exception(e) if event: event.message = u"应用部署失败".format(e.message) event.final_status = "complete" event.status = "failure" event.save() return 507, "部署异常", event return 200, "操作成功", event