Ejemplo n.º 1
0
 def post(self, request, app_id, *args, **kwargs):
     sos = ServiceGroupOperationsSerializer(data=request.data)
     sos.is_valid(raise_exception=True)
     app = group_service.get_app_by_id(self.team, self.region_name, app_id)
     if not app:
         raise ErrAppNotFound
     service_ids = sos.data.get("service_ids", None)
     if not service_ids or len(service_ids) == 0:
         service_ids = app_service.get_group_services_by_id(app_id)
     # TODO: Check the amount of resources used
     action = sos.data.get("action")
     if action == "stop":
         self.has_perms([300006, 400008])
     if action == "start":
         self.has_perms([300005, 400006])
     if action == "upgrade":
         self.has_perms([300007, 400009])
     if action == "deploy":
         self.has_perms([300008, 400010])
     app_manage_service.batch_operations(self.team, self.region_name,
                                         request.user, action, service_ids,
                                         None)
     result = {"msg": "操作成功"}
     rst_serializer = SuccessSerializer(data=result)
     rst_serializer.is_valid()
     return Response(rst_serializer.data, status=status.HTTP_200_OK)
Ejemplo n.º 2
0
    def post(self, request, *args, **kwargs):
        """
        ---
        parameters:
            - name: tenantName
              description: 租户名
              required: true
              type: string
              paramType: path
            - name: action
              description: 操作名称 stop| start|upgrade|deploy
              required: true
              type: string
              paramType: form
            - name: group_id
              description: 组id
              required: true
              type: string
              paramType: path

        """
        action = request.data.get("action", None)
        group_id = int(kwargs.get("group_id", None))
        services = group_service_relation_repo.list_service_groups(group_id)
        if not services:
            result = general_message(400, "not service", "当前组内无组件,无法操作")
            return Response(result)
        service_ids = [service.service_id for service in services]
        if action not in ("stop", "start", "upgrade", "deploy"):
            return Response(general_message(400, "param error", "操作类型错误"), status=400)
        # 去除掉第三方组件
        for service_id in service_ids:
            service_obj = service_repo.get_service_by_service_id(service_id)
            if service_obj and service_obj.service_source == "third_party":
                service_ids.remove(service_id)

        if action == "stop":
            self.has_perms([300006, 400008])
        if action == "start":
            self.has_perms([300005, 400006])
        if action == "upgrade":
            self.has_perms([300007, 400009])
        if action == "deploy":
            self.has_perms([300008, 400010])
            # 批量操作
        app_manage_service.batch_operations(self.tenant, self.region_name, self.user, action, service_ids, self.oauth_instance)
        result = general_message(200, "success", "操作成功")
        return Response(result, status=result["code"])
Ejemplo n.º 3
0
    def deploy(self, tenant, region_name, user, record: AppUpgradeRecord):
        if not record.can_deploy():
            raise ErrAppUpgradeRecordCanNotDeploy

        # failed events
        component_records = component_upgrade_record_repo.list_by_app_record_id(record.ID)
        component_records = [record for record in component_records]
        failed_component_records = {
            record.event_id: record
            for record in component_records if record.status in
            [UpgradeStatus.PARTIAL_UPGRADED.value, UpgradeStatus.PARTIAL_ROLLBACK.value, UpgradeStatus.DEPLOY_FAILED.value]
        }

        component_ids = [record.service_id for record in failed_component_records.values()]

        try:
            events = app_manage_service.batch_operations(tenant, region_name, user, "deploy", component_ids)
            status = UpgradeStatus.UPGRADING.value \
                if record.record_type == UpgradeType.UPGRADE.value else UpgradeStatus.ROLLING.value
            upgrade_repo.change_app_record_status(record, status)
        except ServiceHandleException as e:
            upgrade_repo.change_app_record_status(record, UpgradeStatus.DEPLOY_FAILED.value)
            raise ErrAppUpgradeDeployFailed(e.msg)
        except Exception as e:
            upgrade_repo.change_app_record_status(record, UpgradeStatus.DEPLOY_FAILED.value)
            raise e
        self._update_component_records(record, failed_component_records.values(), events)
Ejemplo n.º 4
0
 def group_services_operation(self, tenant, action, service_ids):
     return app_manage_service.batch_operations(tenant, None, action,
                                                service_ids)
Ejemplo n.º 5
0
    def post(self, request, *args, **kwargs):
        """
        ---
        parameters:
            - name: tenantName
              description: 租户名
              required: true
              type: string
              paramType: path
            - name: action
              description: 操作名称 stop| start|upgrade|deploy
              required: true
              type: string
              paramType: form
            - name: group_id
              description: 组id
              required: true
              type: string
              paramType: path

        """
        try:
            action = request.data.get("action", None)

            group_id = int(kwargs.get("group_id", None))
            services = group_service_relation_repo.get_services_obj_by_group(
                group_id)
            if not services:
                result = general_message(400, "not service", "当前组内无应用,无法操作")
                return Response(result)
            service_ids = [service.service_id for service in services]
            if action not in ("stop", "start", "upgrade", "deploy"):
                return Response(general_message(400, "param error", "操作类型错误"),
                                status=400)
            # 去除掉三方服务
            for service_id in service_ids:
                service_obj = service_repo.get_service_by_service_id(
                    service_id)
                if service_obj:
                    if service_obj.service_source == "third_party":
                        service_ids.remove(service_id)

            # 校验权限
            identitys = team_services.get_user_perm_identitys_in_permtenant(
                user_id=self.user.user_id, tenant_name=self.tenant_name)
            perm_tuple = team_services.get_user_perm_in_tenant(
                user_id=self.user.user_id, tenant_name=self.tenant_name)

            if action == "stop":
                if "stop_service" not in perm_tuple and "owner" not in identitys and "admin" not in identitys and "developer" not in identitys:
                    return Response(general_message(400, "Permission denied",
                                                    "没有关闭应用权限"),
                                    status=400)
            if action == "start":
                if "start_service" not in perm_tuple and "owner" not in identitys and "admin" not in identitys and "developer" not in identitys:
                    return Response(general_message(400, "Permission denied",
                                                    "没有启动应用权限"),
                                    status=400)
            if action == "upgrade":
                if "restart_service" not in perm_tuple and "owner" not in identitys and "admin" not in identitys and "developer" not in identitys:
                    return Response(general_message(400, "Permission denied",
                                                    "没有更新应用权限"),
                                    status=400)
            if action == "deploy":
                if "deploy_service" not in perm_tuple and "owner" not in identitys and "admin" not in identitys and "developer" not in identitys:
                    return Response(general_message(400, "Permission denied",
                                                    "没有重新构建权限"),
                                    status=400)
                # 批量操作
            code, msg = app_manage_service.batch_operations(
                self.tenant, self.user, action, service_ids)
            if code != 200:
                result = general_message(code, "batch manage error", msg)
            else:
                result = general_message(200, "success", "操作成功")

        except Exception as e:
            logger.exception(e)
            result = error_message(e.message)
        return Response(result, status=result["code"])