From 153787b29779c6a1bb262d0beee428c57438bf7f Mon Sep 17 00:00:00 2001 From: crayon <873217631@qq.com> Date: Tue, 30 Nov 2021 17:41:44 +0800 Subject: [PATCH] =?UTF-8?q?feature:=20PaaS=E5=AE=B9=E5=99=A8=E9=83=A8?= =?UTF-8?q?=E7=BD=B2=E9=80=82=E9=85=8D=20(closed=20#19)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + app_desc.yaml | 65 +++++++++++++++++++++ apps/api/domains.py | 2 +- apps/exceptions.py | 2 +- apps/iam/handlers/permission.py | 15 +++-- apps/utils/cache.py | 30 +++++++++- apps/utils/enum.py | 66 ++++++++++++++++++++++ apps/utils/env.py | 43 ++++++++++++++ apps/utils/string.py | 31 ++++++++++ blueking/component/conf.py | 2 +- config/__init__.py | 13 ++--- config/default.py | 21 ++++--- env/__init__.py | 12 ++++ env/constants.py | 24 ++++++++ env/paas_version_diff/__init__.py | 41 ++++++++++++++ env/paas_version_diff/paas_v2.py | 41 ++++++++++++++ env/paas_version_diff/paas_v3.py | 15 +++++ env/paas_version_diff/paas_v3_container.py | 61 ++++++++++++++++++++ requirements.txt | 4 +- 19 files changed, 460 insertions(+), 30 deletions(-) create mode 100644 app_desc.yaml create mode 100644 apps/utils/enum.py create mode 100644 apps/utils/env.py create mode 100644 apps/utils/string.py create mode 100644 env/__init__.py create mode 100644 env/constants.py create mode 100644 env/paas_version_diff/__init__.py create mode 100644 env/paas_version_diff/paas_v2.py create mode 100644 env/paas_version_diff/paas_v3.py create mode 100644 env/paas_version_diff/paas_v3_container.py diff --git a/.gitignore b/.gitignore index 83d1b32..0c70b81 100644 --- a/.gitignore +++ b/.gitignore @@ -96,6 +96,8 @@ celerybeat-schedule env/ venv/ ENV/ +!env/ + # Spyder project settings .spyderproject diff --git a/app_desc.yaml b/app_desc.yaml new file mode 100644 index 0000000..5c8e8fd --- /dev/null +++ b/app_desc.yaml @@ -0,0 +1,65 @@ +spec_version: 2 +app_version: "{{APP_VERSION}}" +app: + region: default + bk_app_code: "bk_gsekit" + bk_app_name: 进程配置管理 + market: + category: 运维工具 + introduction: 进程配置管理 + display_options: + width: 1300 + height: 720 + is_win_maximize: True + open_mode: "new_tab" +modules: + default: + is_default: True + source_dir: src + language: Python + services: + - name: mysql + - name: redis + - name: rabbitmq + env_variables: + - key: BKAPP_IS_V3_CONTAINER + value: "True" + description: 是否部署在V3PaaS容器 + - key: PIP_VERSION + value: "20.2.3" + description: 固化pip版本 + + svc_discovery: + bk_saas: + - bk_app_code: "bk_iam" + - bk_app_code: "bk_gsekit" + + processes: + web: + command: gunicorn --timeout 600 --max-requests 200 --max-requests-jitter 20 wsgi -w 8 -b :$PORT --access-logfile - --error-logfile - --access-logformat '[%(h)s] %({request_id}i)s %(u)s %(t)s "%(r)s" %(s)s %(D)s %(b)s "%(f)s" "%(a)s"' + plan: 4C2G5R + replicas: 1 + dworker: + command: celery -A blueapps.core.celery worker -Q default -n default@%h -c 5 -l info --maxtasksperchild=50 + plan: 4C2G5R + replicas: 1 + cworker: + command: celery -A blueapps.core.celery worker -Q pipeline_additional_task,pipeline_additional_task_priority -n common_worker@%h -l info -c 5 --maxtasksperchild=50 + plan: 4C2G5R + replicas: 1 + ereworker: + command: celery -A blueapps.core.celery worker -Q er_execute -n er_execute_worker@%h -l info -P threads -c 64 --maxtasksperchild=50 + plan: 4C2G5R + replicas: 1 + ersworker: + command: celery -A blueapps.core.celery worker -Q er_schedule -n er_schedule_worker@%h -l info -P threads -c 64 --maxtasksperchild=50 + plan: 4C2G5R + replicas: 1 + beat: + command: celery -A blueapps.core.celery beat -l info + plan: 4C2G5R + replicas: 1 + pwatch: + command: python manage.py watch_process + plan: 4C2G5R + replicas: 1 diff --git a/apps/api/domains.py b/apps/api/domains.py index 11b86cc..95cad90 100644 --- a/apps/api/domains.py +++ b/apps/api/domains.py @@ -13,7 +13,7 @@ ESB_PREFIX = "/api/c/compapi/v2/" -APIGATEWAY_ROOT_PREFIX_FORMAT = "{}{}{{}}/".format(settings.BK_PAAS_INNER_HOST, ESB_PREFIX) +APIGATEWAY_ROOT_PREFIX_FORMAT = "{}{}{{}}/".format(settings.BK_COMPONENT_API_URL, ESB_PREFIX) CC_APIGATEWAY_ROOT = os.getenv("BKAPP_CC_APIGATEWAY_ROOT") or APIGATEWAY_ROOT_PREFIX_FORMAT.format("cc") JOB_APIGATEWAY_ROOT_V3 = os.getenv("BKAPP_JOB_APIGATEWAY_ROOT") or APIGATEWAY_ROOT_PREFIX_FORMAT.format("jobv3") diff --git a/apps/exceptions.py b/apps/exceptions.py index 619ce97..4552f83 100644 --- a/apps/exceptions.py +++ b/apps/exceptions.py @@ -68,7 +68,7 @@ def __init__(self, context=None, data=None, **kwargs): if kwargs.get("permission"): permission = kwargs.get("permission") client = IAM( - settings.APP_ID, settings.APP_TOKEN, settings.BK_IAM_INNER_HOST, settings.BK_PAAS_INNER_HOST + settings.APP_ID, settings.APP_TOKEN, settings.BK_IAM_INNER_HOST, settings.BK_COMPONENT_API_URL ) for action in permission.get("actions") or []: action["name"] = action.get("name") or action.get("id") diff --git a/apps/iam/handlers/permission.py b/apps/iam/handlers/permission.py index 2ddea94..d6fb56b 100644 --- a/apps/iam/handlers/permission.py +++ b/apps/iam/handlers/permission.py @@ -56,9 +56,10 @@ def __init__(self, username: str = "", request=None): @classmethod def get_iam_client(cls): if settings.BK_IAM_SKIP: - return DummyIAM(settings.APP_ID, settings.APP_TOKEN, - settings.BK_IAM_INNER_HOST, settings.BK_PAAS_INNER_HOST) - return IAM(settings.APP_ID, settings.APP_TOKEN, settings.BK_IAM_INNER_HOST, settings.BK_PAAS_INNER_HOST) + return DummyIAM( + settings.APP_ID, settings.APP_TOKEN, settings.BK_IAM_INNER_HOST, settings.BK_COMPONENT_API_URL + ) + return IAM(settings.APP_ID, settings.APP_TOKEN, settings.BK_IAM_INNER_HOST, settings.BK_COMPONENT_API_URL) def make_request(self, action: Union[ActionMeta, str], resources: List[Resource] = None) -> Request: """ @@ -125,7 +126,9 @@ def _make_application( related_resources.append( RelatedResourceType( - system_id=related_resource.system_id, type=related_resource.id, instances=instances, + system_id=related_resource.system_id, + type=related_resource.id, + instances=instances, ) ) @@ -207,7 +210,9 @@ def is_allowed( if not result and raise_exception: apply_data, apply_url = self.get_apply_data([action], resources) raise PermissionDeniedError( - action_name=action.name, apply_url=apply_url, permission=apply_data, + action_name=action.name, + apply_url=apply_url, + permission=apply_data, ) return result diff --git a/apps/utils/cache.py b/apps/utils/cache.py index 9a6b341..2b8b85c 100644 --- a/apps/utils/cache.py +++ b/apps/utils/cache.py @@ -8,7 +8,31 @@ an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ -from dogpile.cache import make_region -cache_dict = {} -region = make_region().configure("dogpile.cache.memory", arguments={"cache_dict": cache_dict}) +from functools import wraps +from typing import Callable, Optional + + +def class_member_cache(name: Optional[str] = None): + """ + 类成员缓存 + :param name: 缓存名称,为空则使用 class_func.__name__ + :return: + """ + + def class_member_cache_inner(class_func: Callable) -> Callable: + @wraps(class_func) + def wrapper(self, *args, **kwargs): + + cache_field = f"_{name or class_func.__name__}" + + cache_member = getattr(self, cache_field, None) + if cache_member: + return cache_member + cache_member = class_func(self, *args, **kwargs) + setattr(self, cache_field, cache_member) + return cache_member + + return wrapper + + return class_member_cache_inner diff --git a/apps/utils/enum.py b/apps/utils/enum.py new file mode 100644 index 0000000..c04d686 --- /dev/null +++ b/apps/utils/enum.py @@ -0,0 +1,66 @@ +# -*- coding: utf-8 -*- +""" +Tencent is pleased to support the open source community by making 蓝鲸 (Blueking) available. +Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. +Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. +You may obtain a copy of the License at https://opensource.org/licenses/MIT +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and limitations under the License. +""" +import abc +from enum import Enum +from typing import Any, Dict, List, Tuple + +from ..utils.cache import class_member_cache + + +class EnhanceEnum(Enum): + """增强枚举类,提供常用的枚举值列举方法""" + + @classmethod + @abc.abstractmethod + def _get_member__alias_map(cls) -> Dict[Enum, str]: + """ + 获取枚举成员与释义的映射关系 + :return: + """ + raise NotImplementedError + + @classmethod + @class_member_cache() + def list_member_values(cls) -> List[Any]: + """ + 获取所有的枚举成员值 + :return: + """ + member_values = [] + for member in cls._member_names_: + member_values.append(cls._value2member_map_[member].value) + return member_values + + @classmethod + @class_member_cache() + def get_member_value__alias_map(cls) -> Dict[Any, str]: + """ + 获取枚举成员值与释义的映射关系,缓存计算结果 + :return: + """ + member_value__alias_map = {} + member__alias_map = cls._get_member__alias_map() + + for member, alias in member__alias_map.items(): + if type(member) is not cls: + raise ValueError(f"except member type -> {cls}, but got -> {type(member)}") + member_value__alias_map[member.value] = alias + + return member_value__alias_map + + @classmethod + @class_member_cache() + def list_choices(cls) -> List[Tuple[Any, Any]]: + """ + 获取可选项列表,一般用于序列化器、model的choices选项 + :return: + """ + return list(cls.get_member_value__alias_map().items()) diff --git a/apps/utils/env.py b/apps/utils/env.py new file mode 100644 index 0000000..477f0f3 --- /dev/null +++ b/apps/utils/env.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- +""" +Tencent is pleased to support the open source community by making 蓝鲸 (Blueking) available. +Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. +Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. +You may obtain a copy of the License at https://opensource.org/licenses/MIT +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and limitations under the License. +""" + +import os +from typing import Any + +from apps.utils.string import str2bool + + +def get_type_env(key: str, default: Any = None, _type: type = str, exempt_empty_str: bool = False) -> Any: + """ + 获取环境变量并转为目标类型 + :param key: 变量名 + :param default: 默认值,若获取不到环境变量会默认使用该值 + :param _type: 环境变量需要转换的类型,不会转 default + :param exempt_empty_str: 是否豁免空串 + :return: + """ + value = os.getenv(key) or default + if value == default: + return value + + # 豁免空串 + if isinstance(value, str) and not value and exempt_empty_str: + return value + + if _type == bool: + return str2bool(value) + + try: + value = _type(value) + except TypeError: + raise TypeError(f"can not convert env value -> {value} to type -> {_type}") + + return value diff --git a/apps/utils/string.py b/apps/utils/string.py new file mode 100644 index 0000000..35e2c6c --- /dev/null +++ b/apps/utils/string.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +""" +Tencent is pleased to support the open source community by making 蓝鲸 (Blueking) available. +Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. +Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. +You may obtain a copy of the License at https://opensource.org/licenses/MIT +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and limitations under the License. +""" + +from typing import Optional + + +def str2bool(string: Optional[str], strict: bool = True) -> bool: + """ + 字符串转布尔值 + 对于bool(str) 仅在len(str) == 0 or str is None 的情况下为False,为了适配bool("False") 等环境变量取值情况,定义该函数 + 参考:https://stackoverflow.com/questions/21732123/convert-true-false-value-read-from-file-to-boolean + :param string: + :param strict: 严格校验,非 False / True / false / true 时抛出异常,用于环境变量的转换 + :return: + """ + if string in ["False", "false"]: + return False + if string in ["True", "true"]: + return True + + if strict: + raise ValueError(f"{string} can not convert to bool") + return bool(string) diff --git a/blueking/component/conf.py b/blueking/component/conf.py index c30fc0b..9f0362f 100644 --- a/blueking/component/conf.py +++ b/blueking/component/conf.py @@ -8,7 +8,7 @@ APP_CODE = settings.APP_ID SECRET_KEY = settings.APP_TOKEN - COMPONENT_SYSTEM_HOST = getattr(settings, "BK_PAAS_INNER_HOST", settings.BK_PAAS_HOST) + COMPONENT_SYSTEM_HOST = settings.BK_COMPONENT_API_URL DEFAULT_BK_API_VER = getattr(settings, "DEFAULT_BK_API_VER", "v2") except Exception: APP_CODE = "" diff --git a/config/__init__.py b/config/__init__.py index 70a8bb4..1f3e15c 100644 --- a/config/__init__.py +++ b/config/__init__.py @@ -13,7 +13,7 @@ __author__ = "蓝鲸智云" __copyright__ = "Copyright © 2012-2019 Tencent BlueKing. All Rights Reserved." -__all__ = ["celery_app", "ENVIRONMENT", "RUN_VER", "BK_URL", "BASE_DIR"] +__all__ = ["celery_app", "RUN_VER", "BK_URL", "BASE_DIR"] # This will make sure the app is always imported when # Django starts so that shared_task will use this app. @@ -32,16 +32,11 @@ def get_env_or_raise(key): return value -# V3判断环境的环境变量为BKPAAS_ENVIRONMENT -if "BKPAAS_ENVIRONMENT" in os.environ: - ENVIRONMENT = os.getenv("BKPAAS_ENVIRONMENT", "dev") -# V2判断环境的环境变量为BK_ENV -else: - PAAS_V2_ENVIRONMENT = os.environ.get("BK_ENV", "development") - ENVIRONMENT = {"development": "dev", "testing": "stag", "production": "prod"}.get(PAAS_V2_ENVIRONMENT) - # SaaS运行版本,如非必要请勿修改 RUN_VER = os.environ.get("BKPAAS_ENGINE_REGION", "open") +# 兼容 V3 取值差异 +if RUN_VER == "default": + RUN_VER = "open" APP_ID = APP_CODE = os.environ.get("APP_ID", "bk_gsekit") APP_TOKEN = SECRET_KEY = os.environ.get("APP_TOKEN", "28b7b410-c7b7-4537-9a65-8ce55738170e") diff --git a/config/default.py b/config/default.py index 9d2d090..4bc523e 100644 --- a/config/default.py +++ b/config/default.py @@ -8,7 +8,6 @@ an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ -import importlib from blueapps.conf.default_settings import * # noqa from blueapps.conf.log import get_logging_config_dict @@ -16,10 +15,14 @@ from pipeline.eri.celery import queues from celery import Celery +import env + # pipeline 配置 from pipeline.celery.settings import * +ENVIRONMENT = env.ENVIRONMENT + CELERY_QUEUES.extend(queues.CELERY_QUEUES) # 向 broker 队列中添加 bamboo-engine 专用队列 app = Celery("proj") @@ -187,8 +190,10 @@ # =============================================================================== BK_PAAS_HOST = os.environ.get("BK_PAAS_HOST", "") BK_PAAS_INNER_HOST = os.environ.get("BK_PAAS_INNER_HOST", BK_PAAS_HOST) +BK_COMPONENT_API_URL = env.BK_COMPONENT_API_URL + BK_CC_HOST = os.environ.get("BK_CC_HOST", BK_PAAS_HOST.replace("paas", "cmdb")) -BK_SAAS_HOST = os.environ.get("BK_SAAS_HOST", f"{BK_PAAS_HOST}/o/{APP_CODE}/") +BK_SAAS_HOST = env.BK_SAAS_HOST BK_ADMIN_USERNAME = os.getenv("BKAPP_ADMIN_USERNAME", "admin") @@ -211,19 +216,17 @@ # ============================================================================== # IAM # ============================================================================== -BK_IAM_SYSTEM_ID = "bk_gsekit" +BK_IAM_SKIP = False BK_IAM_SYSTEM_NAME = "GSEKIT" BK_IAM_MIGRATION_APP_NAME = "iam" -BK_IAM_SKIP = False - -BK_IAM_INNER_HOST = os.getenv("BK_IAM_V3_INNER_HOST", "http://bkiam.service.consul") -# BK_IAM_RESOURCE_API_HOST = os.getenv("BKAPP_IAM_RESOURCE_API_HOST", f"{BK_PAAS_HOST}/o/{APP_CODE}/") -BK_IAM_RESOURCE_API_HOST = os.getenv("BKAPP_IAM_RESOURCE_API_HOST", f"{BK_PAAS_INNER_HOST}/o/{APP_CODE}/") +BK_IAM_SYSTEM_ID = os.getenv("BKAPP_IAM_SYSTEM_ID", "bk_gsekit") +BK_IAM_RESOURCE_API_HOST = env.BK_IAM_RESOURCE_API_HOST # 权限中心 SaaS host BK_IAM_APP_CODE = os.getenv("BK_IAM_V3_APP_CODE", "bk_iam") -BK_IAM_SAAS_HOST = os.environ.get("BK_IAM_V3_SAAS_HOST", f"{BK_PAAS_HOST}/o/{BK_IAM_APP_CODE}/") +BK_IAM_INNER_HOST = os.getenv("BK_IAM_V3_INNER_HOST", "http://bkiam.service.consul") +BK_IAM_SAAS_HOST = env.BK_IAM_SAAS_HOST # TAM TAM_AEGIS_KEY = os.getenv("BKAPP_TAM_AEGIS_KEY") diff --git a/env/__init__.py b/env/__init__.py new file mode 100644 index 0000000..a0edcc7 --- /dev/null +++ b/env/__init__.py @@ -0,0 +1,12 @@ +# -*- coding: utf-8 -*- +""" +Tencent is pleased to support the open source community by making 蓝鲸 (Blueking) available. +Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. +Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. +You may obtain a copy of the License at https://opensource.org/licenses/MIT +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and limitations under the License. +""" + +from .paas_version_diff import * # noqa diff --git a/env/constants.py b/env/constants.py new file mode 100644 index 0000000..4d193e7 --- /dev/null +++ b/env/constants.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +""" +Tencent is pleased to support the open source community by making 蓝鲸 (Blueking) available. +Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. +Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. +You may obtain a copy of the License at https://opensource.org/licenses/MIT +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and limitations under the License. +""" + +from enum import Enum +from typing import Dict + +from apps.utils.enum import EnhanceEnum + + +class BkPaaSVersion(EnhanceEnum): + V2 = 2 + V3 = 3 + + @classmethod + def _get_member__alias_map(cls) -> Dict[Enum, str]: + return {cls.V2: "V2", cls.V3: "V3具备容器及二进制配置差异"} diff --git a/env/paas_version_diff/__init__.py b/env/paas_version_diff/__init__.py new file mode 100644 index 0000000..8eb9aaa --- /dev/null +++ b/env/paas_version_diff/__init__.py @@ -0,0 +1,41 @@ +# -*- coding: utf-8 -*- +""" +Tencent is pleased to support the open source community by making 蓝鲸 (Blueking) available. +Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. +Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. +You may obtain a copy of the License at https://opensource.org/licenses/MIT +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and limitations under the License. +""" +from apps.utils.env import get_type_env + +from .. import constants + +# 差异化赋值后的统一命名 settings +__all__ = [ + # PaaS 部署环境,标准化为 stag / dev + "ENVIRONMENT", + # esb 访问地址 + "BK_COMPONENT_API_URL", + # 权限中心 SaaS 地址 + "BK_IAM_SAAS_HOST", + # 提供给权限中心的资源回调地址 + "BK_IAM_RESOURCE_API_HOST", + # SaaS 访问地址 + "BK_SAAS_HOST", +] + +# PaaS 版本 +BKPAAS_MAJOR_VERSION = get_type_env(key="BKPAAS_MAJOR_VERSION", default=constants.BkPaaSVersion.V2.value, _type=int) +# 是否为 PaaS V3 容器化部署版本 +BKAPP_IS_V3_CONTAINER = get_type_env(key="BKAPP_IS_V3_CONTAINER", default=False, _type=bool) + + +if BKPAAS_MAJOR_VERSION == constants.BkPaaSVersion.V3.value: + if BKAPP_IS_V3_CONTAINER: + from .paas_v3_container import * # noqa + else: + from .paas_v3 import * # noqa +else: + from .paas_v2 import * # noqa diff --git a/env/paas_version_diff/paas_v2.py b/env/paas_version_diff/paas_v2.py new file mode 100644 index 0000000..595ccd4 --- /dev/null +++ b/env/paas_version_diff/paas_v2.py @@ -0,0 +1,41 @@ +# -*- coding: utf-8 -*- +""" +Tencent is pleased to support the open source community by making 蓝鲸 (Blueking) available. +Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. +Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. +You may obtain a copy of the License at https://opensource.org/licenses/MIT +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and limitations under the License. +""" + + +from apps.utils.env import get_type_env + + +APP_CODE = get_type_env(key="APP_ID", default="", _type=str) + +BK_PAAS_HOST = get_type_env(key="BK_PAAS_HOST", default="", _type=str) + +BK_PAAS_INNER_HOST = get_type_env(key="BK_PAAS_INNER_HOST", default=BK_PAAS_HOST, _type=str) + +PAAS_V2_ENVIRONMENT = get_type_env(key="BK_ENV", default="development", _type=str) + +# define envs + +# PaaS 部署环境 +ENVIRONMENT = {"development": "dev", "testing": "stag", "production": "prod"}.get(PAAS_V2_ENVIRONMENT) + +# esb 访问地址 +BK_COMPONENT_API_URL = BK_PAAS_HOST or BK_PAAS_INNER_HOST or "" + +# SaaS访问地址 +BK_SAAS_HOST = get_type_env(key="BK_SAAS_HOST", default=f"{BK_PAAS_HOST}/o/{APP_CODE}", _type=str) + +BK_IAM_APP_CODE = get_type_env(key="BK_IAM_V3_APP_CODE", default="bk_iam", _type=str) + +BK_IAM_SAAS_HOST = get_type_env(key="BK_IAM_V3_SAAS_HOST", default=f"{BK_PAAS_HOST}/o/{BK_IAM_APP_CODE}/", _type=str) + +BK_IAM_RESOURCE_API_HOST = get_type_env( + key="BKAPP_IAM_RESOURCE_API_HOST", default=f"{BK_PAAS_INNER_HOST}/o/{APP_CODE}/", _type=str +) diff --git a/env/paas_version_diff/paas_v3.py b/env/paas_version_diff/paas_v3.py new file mode 100644 index 0000000..cbd23d6 --- /dev/null +++ b/env/paas_version_diff/paas_v3.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- +""" +Tencent is pleased to support the open source community by making 蓝鲸 (Blueking) available. +Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. +Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. +You may obtain a copy of the License at https://opensource.org/licenses/MIT +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and limitations under the License. +""" + +from .paas_v2 import * # noqa + +# PaaS 二进制版本 v2 v3 主要的差异配置 +ENVIRONMENT = get_type_env(key="BKPAAS_ENVIRONMENT", default="dev", _type=str) diff --git a/env/paas_version_diff/paas_v3_container.py b/env/paas_version_diff/paas_v3_container.py new file mode 100644 index 0000000..fdfc058 --- /dev/null +++ b/env/paas_version_diff/paas_v3_container.py @@ -0,0 +1,61 @@ +# -*- coding: utf-8 -*- +""" +Tencent is pleased to support the open source community by making 蓝鲸 (Blueking) available. +Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. +Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. +You may obtain a copy of the License at https://opensource.org/licenses/MIT +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and limitations under the License. +""" + +import base64 +import json +import os +from collections import defaultdict +from typing import Dict, List + +from apps.utils.env import get_type_env + +DEFAULT_MODULE_NAME = "default" + +APP_CODE = get_type_env(key="BKPAAS_APP_ID", default="", _type=str) + +ENVIRONMENT = os.getenv("BKPAAS_ENVIRONMENT", "dev") + +BKPAAS_SERVICE_ADDRESSES_BKSAAS = os.getenv("BKPAAS_SERVICE_ADDRESSES_BKSAAS") +BKPAAS_SERVICE_ADDRESSES_BKSAAS_LIST: List[Dict[str, Dict[str, str]]] = ( + json.loads(base64.b64decode(BKPAAS_SERVICE_ADDRESSES_BKSAAS).decode("utf-8")) + if BKPAAS_SERVICE_ADDRESSES_BKSAAS + else {} +) + +APP_CODE__SAAS_MODULE_HOST_MAP: Dict[str, Dict[str, str]] = defaultdict(lambda: defaultdict(str)) + +for item in BKPAAS_SERVICE_ADDRESSES_BKSAAS_LIST: + module_info = item["key"] + bk_app_code = module_info.get("bk_app_code") + module_name = module_info.get("module_name") + + if not bk_app_code: + continue + if not module_name or module_name == "None": + module_name = DEFAULT_MODULE_NAME + + APP_CODE__SAAS_MODULE_HOST_MAP[bk_app_code][module_name] = item["value"].get(ENVIRONMENT) + +# define envs + +# esb 访问地址 +BK_COMPONENT_API_URL = get_type_env(key="BK_COMPONENT_API_URL", _type=str) + +# SaaS访问地址 +BK_SAAS_HOST = APP_CODE__SAAS_MODULE_HOST_MAP[APP_CODE][DEFAULT_MODULE_NAME] + +BK_IAM_APP_CODE = get_type_env(key="BK_IAM_V3_APP_CODE", default="bk_iam", _type=str) + +BK_IAM_SAAS_HOST = get_type_env( + key="BK_IAM_V3_SAAS_HOST", default=APP_CODE__SAAS_MODULE_HOST_MAP[BK_IAM_APP_CODE][DEFAULT_MODULE_NAME], _type=str +) + +BK_IAM_RESOURCE_API_HOST = get_type_env(key="BKAPP_IAM_RESOURCE_API_HOST", default=BK_SAAS_HOST, _type=str) diff --git a/requirements.txt b/requirements.txt index 2d5c6a2..21ee45f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,7 +2,9 @@ # 需要额外的python包,可直接在文件后面添加 # 请确保指定的包和版本号,可通过pip安装 -blueapps-open==4.0.1 +# sed -i 's|blueapps-open|blueapps|g' requirements.txt +# 确保内外部版本统一,打包脚本的替换规则为:blueapps-open -> blueapps,不替换版本号 +blueapps-open==4.0.2rc0 python-json-logger==0.1.7 requests==2.22.0 MarkupSafe==1.1.1