Skip to content

Commit

Permalink
feature: PaaS 部署适配(#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuoZhuoCrayon committed Sep 29, 2021
1 parent 5e62e21 commit 80160c4
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ celerybeat-schedule
env/
venv/
ENV/
!config/env/
!env/


# Spyder project settings
Expand Down
10 changes: 1 addition & 9 deletions config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -32,14 +32,6 @@ 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 取值差异
Expand Down
5 changes: 3 additions & 2 deletions config/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@
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
from pipeline.celery.settings import * # noqa
from pipeline.eri.celery import queues
from celery import Celery

from config import env
import env

# pipeline 配置
from pipeline.celery.settings import *


ENVIRONMENT = env.ENVIRONMENT

CELERY_QUEUES.extend(queues.CELERY_QUEUES) # 向 broker 队列中添加 bamboo-engine 专用队列

app = Celery("proj")
Expand Down
2 changes: 1 addition & 1 deletion config/env/env_v3.py → env/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
See the License for the specific language governing permissions and limitations under the License.
"""

from .env_v2 import * # noqa
from .paas_version_diff import * # noqa
14 changes: 0 additions & 14 deletions config/env/__init__.py → env/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from typing import Dict

from apps.utils.enum import EnhanceEnum
from apps.utils.env import get_type_env


class BkPaaSVersion(EnhanceEnum):
Expand All @@ -23,16 +22,3 @@ class BkPaaSVersion(EnhanceEnum):
@classmethod
def _get_member__alias_map(cls) -> Dict[Enum, str]:
return {cls.V2: "V2", cls.V3: "V3具备容器及二进制配置差异"}


BKPAAS_MAJOR_VERSION = get_type_env(key="BKPAAS_MAJOR_VERSION", default=2, _type=int)
BKAPP_IS_V3_CONTAINER = get_type_env(key="BKAPP_IS_V3_CONTAINER", default=False, _type=bool)


if BKPAAS_MAJOR_VERSION == BkPaaSVersion.V3.value:
if BKAPP_IS_V3_CONTAINER:
from .env_v3_container import * # noqa
else:
from .env_v3 import * # noqa
else:
from .env_v2 import * # noqa
41 changes: 41 additions & 0 deletions env/paas_version_diff/__init__.py
Original file line number Diff line number Diff line change
@@ -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
17 changes: 11 additions & 6 deletions config/env/env_v2.py → env/paas_version_diff/paas_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,27 @@
"""


import os
from apps.utils.env import get_type_env

from blueapps.conf.default_settings import APP_CODE

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_HOST = os.getenv("BK_PAAS_HOST", "")
BK_PAAS_INNER_HOST = os.getenv("BK_PAAS_INNER_HOST") or BK_PAAS_HOST
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 = os.getenv("BK_SAAS_HOST", f"{BK_PAAS_HOST}/o/{APP_CODE}")
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)

Expand Down
15 changes: 15 additions & 0 deletions env/paas_version_diff/paas_v3.py
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
from collections import defaultdict
from typing import Dict, List

from blueapps.conf.default_settings import APP_CODE

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")
Expand All @@ -30,7 +30,7 @@
else {}
)

APP_CODE__SAAS_MODULE_HOST_MAP = defaultdict(dict)
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"]
Expand Down

0 comments on commit 80160c4

Please sign in to comment.