Esempio n. 1
0
 def get(self, request, enterprise_id, *args, **kwargs):
     regions = region_services.get_regions_by_enterprise_id(enterprise_id)
     remind = False
     if regions and len(regions) > 1:
         ent_cfg_svc = EnterpriseConfigService(enterprise_id)
         data = ent_cfg_svc.get_config_by_key("APPSTORE_IMAGE_HUB")
         if data and data.enable:
             image_config_dict = eval(data.value)
             hub_url = image_config_dict.get("hub_url", None)
             if not hub_url:
                 remind = True
         else:
             remind = True
     result = general_message(200, "success", "检测成功", bean={"remind": remind})
     return Response(result, status=result.get("code", 200))
Esempio n. 2
0
 def get_slug_hub_info(self, store=None, app_id=None, enterprise_id=None):
     image_config = {
         "ftp_host": None,
         "ftp_port": None,
         "namespace": None,
         "ftp_username": None,
         "ftp_password": None
     }
     data = None
     if store:
         store_client = get_market_client(store.access_key, store.url)
         data = store_client.get_app_hub_info(app_id=app_id,
                                              market_domain=store.domain,
                                              _return_http_data_only=True)
         image_config["ftp_host"] = data.hub_url
         image_config["ftp_username"] = data.hub_user
         image_config["ftp_password"] = data.hub_password
         image_config["namespace"] = data.namespace
     if not data:
         data = EnterpriseConfigService(enterprise_id).get_config_by_key(
             "APPSTORE_IMAGE_HUB")
         if data:
             image_config_dict = eval(data.value)
             namespace = (image_config_dict.get("namespace")
                          if image_config_dict.get("namespace") else
                          data.enterprise_id)
             image_config["ftp_host"] = image_config_dict.get(
                 "hub_url", None)
             image_config["ftp_username"] = image_config_dict.get(
                 "hub_user", None)
             image_config["ftp_password"] = image_config_dict.get(
                 "hub_password", None)
             image_config["namespace"] = namespace
     return image_config
Esempio n. 3
0
 def get(self, request, enterprise_id, *args, **kwargs):
     enter = enterprise_repo.get_enterprise_by_enterprise_id(enterprise_id=enterprise_id)
     ent = enter.to_dict()
     if ent:
         ent.update(EnterpriseConfigService(enterprise_id).initialization_or_get_config)
     result = general_message(200, "success", "查询成功", bean=ent)
     return Response(result, status=result["code"])
Esempio n. 4
0
 def put(self, request, *args, **kwargs):
     data = request.data.get("oauth_services")
     enable = data.get("enable")
     EnterpriseConfigService(
         request.user.enterprise_id).update_config_enable_status(
             key="OAUTH_SERVICES", enable=enable)
     rst = {"data": {"bean": {"oauth_services": data}}}
     return Response(rst, status=status.HTTP_200_OK)
Esempio n. 5
0
 def is_no_multiple_region_hub(self, enterprise_id):
     data = EnterpriseConfigService(enterprise_id).get_config_by_key(
         "APPSTORE_IMAGE_HUB")
     if data and data.enable:
         image_config_dict = eval(data.value)
         if image_config_dict["hub_url"]:
             return False
     return True
Esempio n. 6
0
    def start_migrate(self, user, current_team, current_region, migrate_team, migrate_region, backup_id, migrate_type, event_id,
                      restore_id):
        backup_record = backup_record_repo.get_record_by_backup_id(current_team.tenant_id, backup_id)
        if not backup_record:
            raise ErrBackupRecordNotFound

        s3_info = EnterpriseConfigService(user.enterprise_id).get_cloud_obj_storage_info()
        if backup_record.mode == "full-online" and not s3_info:
            raise ErrObjectStorageInfoNotFound

        if migrate_type == "recover":
            is_all_services_closed = self.__check_group_service_status(current_region, current_team, backup_record.group_id)
            if not is_all_services_closed:
                raise ErrNeedAllServiceCloesed

        restore_mode = self.__get_restore_type(current_team, current_region, migrate_team, migrate_region)

        # 数据迁移到其他地方先处理数据中心数据拷贝
        new_group, new_backup_record = self.__copy_backup_record(restore_mode, backup_record, current_team, current_region,
                                                                 migrate_team, migrate_region, migrate_type)
        if not new_backup_record:
            new_backup_record = backup_record

        data = {
            "event_id": make_uuid(),
            "backup_id": new_backup_record.backup_id,
            "restore_mode": restore_mode,
            "tenant_id": migrate_team.tenant_id,
            "s3_config": s3_info,
        }
        body = region_api.star_apps_migrate_task(migrate_region, migrate_team.tenant_name, new_backup_record.backup_id, data)

        if event_id:
            migrate_record = migrate_repo.get_by_event_id(event_id)
            data = region_api.get_apps_migrate_status(migrate_record.migrate_region, migrate_record.migrate_team,
                                                      migrate_record.backup_id, restore_id)
            bean = data["bean"]
            migrate_record.status = bean["status"]
            migrate_record.save()
        else:
            # 创建迁移记录
            params = {
                "group_id": new_group.ID,
                "group_uuid": new_backup_record.group_uuid,
                "event_id": make_uuid(),
                "version": backup_record.version,
                "backup_id": new_backup_record.backup_id,
                "migrate_team": migrate_team.tenant_name,
                "migrate_region": migrate_region,
                "status": "starting",
                "user": user.nick_name,
                "restore_id": body["bean"]["restore_id"],
                "original_group_id": backup_record.group_id,
                "original_group_uuid": backup_record.group_uuid,
                "migrate_type": migrate_type
            }
            migrate_record = migrate_repo.create_migrate_record(**params)
        return migrate_record
Esempio n. 7
0
    def backup_group_apps(self,
                          tenant,
                          user,
                          region,
                          group_id,
                          mode,
                          note,
                          force=False):
        s3_config = EnterpriseConfigService(
            tenant.enterprise_id).get_cloud_obj_storage_info()
        if mode == "full-online" and not s3_config:
            raise ErrObjectStorageInfoNotFound
        services = group_service.get_group_services(group_id)
        event_id = make_uuid()
        group_uuid = self.get_backup_group_uuid(group_id)
        total_memory, metadata = self.get_group_app_metadata(group_id, tenant)
        version = current_time_str("%Y%m%d%H%M%S")

        data = {
            "event_id": event_id,
            "group_id": group_uuid,
            "metadata": json.dumps(metadata),
            "service_ids": [s.service_id for s in services],
            "mode": mode,
            "version": version,
            "s3_config": s3_config,
            "force": force,
        }
        # 向数据中心发起备份任务
        try:
            body = region_api.backup_group_apps(region, tenant.tenant_name,
                                                data)
            bean = body["bean"]
            record_data = {
                "group_id": group_id,
                "event_id": event_id,
                "group_uuid": group_uuid,
                "version": version,
                "team_id": tenant.tenant_id,
                "region": region,
                "status": bean["status"],
                "note": note,
                "mode": mode,
                "backup_id": bean.get("backup_id", ""),
                "source_dir": bean.get("source_dir", ""),
                "source_type": bean.get("source_type", ""),
                "backup_size": bean.get("backup_size", 0),
                "user": user.nick_name,
                "total_memory": total_memory,
            }
            return backup_record_repo.create_backup_records(**record_data)
        except region_api.CallApiError as e:
            logger.exception(e)
            if e.status == 401:
                raise ServiceHandleException(msg="backup failed",
                                             msg_show="有状态组件必须停止方可进行备份")
 def get(self, req, *args, **kwargs):
     key = req.GET.get("key", None)
     ent = enterprise_services.get_enterprise_by_id(
         self.enterprise.enterprise_id)
     if ent is None:
         return Response({"msg": "企业不存在"}, status=status.HTTP_404_NOT_FOUND)
     ent_config = EnterpriseConfigService(
         self.enterprise.enterprise_id).initialization_or_get_config
     if key is None:
         serializer = EnterpriseConfigSeralizer(data=ent_config)
     elif key in list(ent_config.keys()):
         serializer = EnterpriseConfigSeralizer(data={key: ent_config[key]})
     else:
         raise ServiceHandleException(
             status_code=404,
             msg="no found config key {}".format(key),
             msg_show="企业没有 {} 配置".format(key))
     serializer.is_valid(raise_exception=True)
     return Response(serializer.data, status=status.HTTP_200_OK)
Esempio n. 9
0
 def delete(self, request, enterprise_id, *args, **kwargs):
     key = request.GET.get("key")
     if not key:
         result = general_message(404, "no found config key", "重置失败")
         return Response(result, status=result.get("code", 200))
     value = request.data.get(key)
     if not value:
         result = general_message(404, "no found config value", "重置失败")
         return Response(result, status=result.get("code", 200))
     ent_config_servier = EnterpriseConfigService(enterprise_id)
     key = key.upper()
     if key in ent_config_servier.cfg_keys:
         data = ent_config_servier.delete_config(key)
         try:
             result = general_message(200, "success", "重置成功", bean=data)
         except Exception as e:
             logger.debug(e)
             raise ServiceHandleException(msg="update enterprise config failed", msg_show="重置失败")
     else:
         result = general_message(404, "can not delete key value", "该配置不可重置")
     return Response(result, status=result.get("code", 200))
Esempio n. 10
0
    def get(self, request, *args, **kwargs):
        """
        查询备份信息
        ---
        parameters:
            - name: tenantName
              description: 团队名称
              required: true
              type: string
              paramType: path
            - name: page
              description: 页码
              required: false
              type: string
              paramType: query
            - name: page_size
              description: 每页数量
              required: false
              type: string
              paramType: query
            - name: group_id
              description: 组ID
              required: true
              type: string
              paramType: query
        """
        group_id = request.GET.get("group_id", None)
        if not group_id:
            return Response(general_message(400, "group id is not found",
                                            "请指定需要查询的组"),
                            status=400)
        page = int(request.GET.get("page", 1))
        page_size = int(request.GET.get("page_size", 10))

        backups = groupapp_backup_service.get_group_back_up_info(
            self.tenant, self.region_name, group_id)
        paginator = JuncheePaginator(backups, int(page_size))
        backup_records = paginator.page(int(page))
        obj_storage = EnterpriseConfigService(
            self.user.enterprise_id).get_cloud_obj_storage_info()
        bean = {"is_configed": obj_storage is not None}
        result = general_message(
            200,
            "success",
            "查询成功",
            bean=bean,
            list=[backup.to_dict() for backup in backup_records],
            total=paginator.count)
        return Response(result, status=result["code"])
    def put(self, request, enterprise_id, *args, **kwargs):
        enable = bool_argument(parse_item(request, "enable", required=True))
        hub_url = parse_item(request, "hub_url", required=True)
        namespace = parse_item(request, "namespace")
        hub_user = parse_item(request, "hub_user")
        hub_password = parse_item(request, "hub_password")

        ent_cfg_svc = EnterpriseConfigService(enterprise_id)
        ent_cfg_svc.update_config_enable_status(key="APPSTORE_IMAGE_HUB", enable=enable)
        ent_cfg_svc.update_config_value(
            key="APPSTORE_IMAGE_HUB",
            value={
                "hub_url": hub_url,
                "namespace": namespace,
                "hub_user": hub_user,
                "hub_password": hub_password,
            })
        return Response(status=status.HTTP_200_OK)
    def put(self, request, enterprise_id, *args, **kwargs):
        enable = bool_argument(parse_item(request, "enable", required=True))
        provider = parse_item(request, "provider", required=True)
        endpoint = parse_item(request, "endpoint", required=True)
        bucket_name = parse_item(request, "bucket_name", required=True)
        access_key = parse_item(request, "access_key", required=True)
        secret_key = parse_item(request, "secret_key", required=True)

        if provider not in ("alioss", "s3"):
            raise AbortRequest("provider {} not in (\"alioss\", \"s3\")".format(provider))

        ent_cfg_svc = EnterpriseConfigService(enterprise_id)
        ent_cfg_svc.update_config_enable_status(key="OBJECT_STORAGE", enable=enable)
        ent_cfg_svc.update_config_value(
            key="OBJECT_STORAGE",
            value={
                "provider": provider,
                "endpoint": endpoint,
                "bucket_name": bucket_name,
                "access_key": access_key,
                "secret_key": secret_key,
            })
        return Response(status=status.HTTP_200_OK)