-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e892c22
commit b8f2080
Showing
8 changed files
with
179 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
apiserver/paasng/paasng/accessories/dev_sandbox/management/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# -*- coding: utf-8 -*- | ||
# TencentBlueKing is pleased to support the open source community by making | ||
# 蓝鲸智云 - PaaS 平台 (BlueKing - PaaS System) available. | ||
# Copyright (C) 2017 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 | ||
# | ||
# http://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. | ||
# | ||
# We undertake not to change the open source license (MIT license) applicable | ||
# to the current version of the project delivered to anyone in the future. |
16 changes: 16 additions & 0 deletions
16
apiserver/paasng/paasng/accessories/dev_sandbox/management/commands/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# -*- coding: utf-8 -*- | ||
# TencentBlueKing is pleased to support the open source community by making | ||
# 蓝鲸智云 - PaaS 平台 (BlueKing - PaaS System) available. | ||
# Copyright (C) 2017 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 | ||
# | ||
# http://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. | ||
# | ||
# We undertake not to change the open source license (MIT license) applicable | ||
# to the current version of the project delivered to anyone in the future. |
58 changes: 58 additions & 0 deletions
58
apiserver/paasng/paasng/accessories/dev_sandbox/management/commands/recycle_dev_sandbox.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# -*- coding: utf-8 -*- | ||
# TencentBlueKing is pleased to support the open source community by making | ||
# 蓝鲸智云 - PaaS 平台 (BlueKing - PaaS System) available. | ||
# Copyright (C) 2017 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 | ||
# | ||
# http://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. | ||
# | ||
# We undertake not to change the open source license (MIT license) applicable | ||
# to the current version of the project delivered to anyone in the future. | ||
|
||
"""Recycle dev sandbox. | ||
By default, only expired sandboxes are recycled. Use the --all flag to recycle all sandboxes. | ||
Examples: | ||
# 仅回收过期沙箱 | ||
python manage.py recycle_dev_sandbox | ||
# 全量回收沙箱 | ||
python manage.py recycle_dev_sandbox --all | ||
""" | ||
|
||
from django.core.management.base import BaseCommand | ||
|
||
from paas_wl.bk_app.dev_sandbox.controller import DevSandboxWithCodeEditorController | ||
from paasng.accessories.dev_sandbox.models import DevSandbox | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "回收沙箱" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"--all", | ||
dest="all", | ||
action="store_true", | ||
help="recycle all dev sandboxes", | ||
) | ||
|
||
def handle(self, all, *args, **options): | ||
for dev_sandbox in DevSandbox.objects.all(): | ||
controller = DevSandboxWithCodeEditorController( | ||
app=dev_sandbox.module.application, | ||
module_name=dev_sandbox.module.name, | ||
dev_sandbox_code=dev_sandbox.code, | ||
owner=dev_sandbox.owner, | ||
) | ||
if all or dev_sandbox.is_expired(): | ||
controller.delete() | ||
dev_sandbox.delete() |
60 changes: 60 additions & 0 deletions
60
...paasng/paasng/accessories/dev_sandbox/management/commands/renew_dev_sandbox_expired_at.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# -*- coding: utf-8 -*- | ||
# TencentBlueKing is pleased to support the open source community by making | ||
# 蓝鲸智云 - PaaS 平台 (BlueKing - PaaS System) available. | ||
# Copyright (C) 2017 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 | ||
# | ||
# http://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. | ||
# | ||
# We undertake not to change the open source license (MIT license) applicable | ||
# to the current version of the project delivered to anyone in the future. | ||
|
||
import logging | ||
|
||
import requests | ||
from django.core.management.base import BaseCommand | ||
|
||
from paas_wl.bk_app.dev_sandbox.controller import DevSandboxWithCodeEditorController | ||
from paasng.accessories.dev_sandbox.models import DevSandbox | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "更新沙箱状态" | ||
|
||
def handle(self, *args, **options): | ||
for dev_sandbox in DevSandbox.objects.all(): | ||
controller = DevSandboxWithCodeEditorController( | ||
app=dev_sandbox.module.application, | ||
module_name=dev_sandbox.module.name, | ||
dev_sandbox_code=dev_sandbox.code, | ||
owner=dev_sandbox.owner, | ||
) | ||
try: | ||
detail = controller.get_detail() | ||
except Exception as e: | ||
# 防止沙箱资源被管理员删除等导致的报错 | ||
logger.warning("Failed to get detail of dev sandbox: %s. Error: %s", dev_sandbox.code, e) | ||
continue | ||
|
||
url = detail.urls.code_editor_health_url | ||
# 沙箱相关域名无法确定协议,因此全部遍历 http 和 https | ||
if check_alive(f"http://{url}") or check_alive(f"https://{url}"): | ||
dev_sandbox.renew_expire_at() | ||
|
||
|
||
# 通过 code_editor_health_url 检查沙箱是否存活 | ||
def check_alive(url: str) -> bool: | ||
try: | ||
resp = requests.get(url) | ||
return resp.status_code == 200 and resp.json().get("status") == "alive" | ||
except Exception as e: | ||
logger.warning("Dev sandbox status check failed for URL: %s. Error: %s", url, e) | ||
return False |
18 changes: 18 additions & 0 deletions
18
.../paasng/accessories/dev_sandbox/migrations/0002_rename_expire_at_devsandbox_expired_at.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 4.2.16 on 2024-11-11 07:49 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('dev_sandbox', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.RenameField( | ||
model_name='devsandbox', | ||
old_name='expire_at', | ||
new_name='expired_at', | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters