From 00dc97a640f44b47e337caebb4ed1d057d2bb4ca Mon Sep 17 00:00:00 2001 From: crayon <873217631@qq.com> Date: Tue, 14 Nov 2023 22:14:32 +0800 Subject: [PATCH] =?UTF-8?q?feature:=20=E6=8F=92=E4=BB=B6=E5=8C=85=E5=AF=BC?= =?UTF-8?q?=E5=85=A5=E5=91=BD=E4=BB=A4=E6=94=AF=E6=8C=81=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=E6=A0=87=E7=AD=BE=20(closed=20#1922)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commands/init_official_plugins.py | 44 +++++++++++++++ .../tests/plugin/test_manage_commands.py | 53 +++++++++++++++++++ scripts/workflows/bk_ci.sh | 6 ++- scripts/workflows/local_settings.py | 0 scripts/workflows/migrate_checker.sh | 23 ++++++++ 5 files changed, 124 insertions(+), 2 deletions(-) mode change 100644 => 100755 scripts/workflows/local_settings.py create mode 100755 scripts/workflows/migrate_checker.sh diff --git a/apps/backend/management/commands/init_official_plugins.py b/apps/backend/management/commands/init_official_plugins.py index ca5c261ff..366ba61de 100644 --- a/apps/backend/management/commands/init_official_plugins.py +++ b/apps/backend/management/commands/init_official_plugins.py @@ -19,14 +19,24 @@ from django.core.management.base import BaseCommand from django.db.transaction import atomic +from apps.backend.management.commands import utils from apps.backend.plugin import tools +from apps.backend.subscription.handler import SubscriptionHandler from apps.core.files.storage import get_storage +from apps.core.tag.constants import TargetType +from apps.core.tag.handlers import TagHandler from apps.node_man import constants, models from apps.utils import files from common.log import logger +log_and_print = utils.get_log_and_print("init_official_plugins") + class Command(BaseCommand): + def add_arguments(self, parser): + parser.add_argument("-t", "--tag", help="为目标版本所指定的标签", type=str) + parser.add_argument("-r", "--run-policy", help="触发部署策略", action="store_true", default=False) + def handle(self, *args, **options): """ 初始化内置官方插件 @@ -38,6 +48,10 @@ def handle(self, *args, **options): file_count = 0 # 成功导入插件包的技术 package_count = 0 + tag = options.get("tag") + run_policy = options.get("run_policy") or False + + log_and_print(f"options: tag -> {tag}, run_policy -> {run_policy}") storage = get_storage() @@ -94,6 +108,36 @@ def handle(self, *args, **options): file_count += 1 package_count += len(package_list) + if tag and package_list: + one_of_pkg = package_list[0] + log_and_print( + f"tag to be created: name -> {tag}, plugin_name -> {one_of_pkg.plugin_desc.name}, " + f"version -> {one_of_pkg.version}" + ) + TagHandler.publish_tag_version( + name=tag, + target_type=TargetType.PLUGIN.value, + target_id=one_of_pkg.plugin_desc.id, + target_version=one_of_pkg.version, + ) + + if tag and run_policy: + to_be_run_policies = models.Subscription.objects.filter( + plugin_name=one_of_pkg.plugin_desc.name, + category=models.Subscription.CategoryType.POLICY, + pid=models.Subscription.ROOT, + enable=True, + ) + for policy in to_be_run_policies: + log_and_print(f"policy to be run: name -> {policy.name}, id -> {policy.id}") + try: + run_result = SubscriptionHandler(policy.id).run() + log_and_print(f"policy run: name -> {policy.name}, id -> {policy.id}, result -> {run_result}") + except Exception as e: + log_and_print( + f"policy run failed but skipped: name -> {policy.name}, id -> {policy.id}, error -> {e}" + ) + logger.info( "all package under path->[%s] is import success, file_count->[%s] package_count->[%s]" % (settings.BK_OFFICIAL_PLUGINS_INIT_PATH, file_count, package_count) diff --git a/apps/backend/tests/plugin/test_manage_commands.py b/apps/backend/tests/plugin/test_manage_commands.py index 9a6d19a9b..f11310c06 100644 --- a/apps/backend/tests/plugin/test_manage_commands.py +++ b/apps/backend/tests/plugin/test_manage_commands.py @@ -14,6 +14,8 @@ from django.core.management import call_command from apps.backend.tests.plugin import utils +from apps.core.tag.targets import PluginTargetHelper +from apps.mock_data import backend_mkd from apps.mock_data import utils as mock_data_utils from apps.node_man import models @@ -32,6 +34,57 @@ def test_import_command(self): self.assertTrue(models.PluginConfigTemplate.objects.all().exists()) +class ImportCommandWithTagTestCase(ImportCommandTestCase): + def test_import_command(self): + """测试导入命令""" + call_command("init_official_plugins", tag="stable") + self.assertTrue(models.Packages.objects.all().exists()) + self.assertTrue(models.UploadPackage.objects.all().exists()) + self.assertTrue(models.PluginConfigTemplate.objects.all().exists()) + + plugin_desc = models.GsePluginDesc.objects.get(name=utils.PLUGIN_NAME) + tag = PluginTargetHelper.get_tag(plugin_desc.id, utils.PACKAGE_VERSION) + self.assertEqual(tag.name, "stable") + + +class ImportCommandWithRunPolicyTestCase(ImportCommandTestCase): + def test_import_command(self): + """测试导入命令""" + + scope = backend_mkd.subscription.unit.SUBSCRIPTION_DATA["scope"] + sub = models.Subscription.objects.create( + bk_biz_id=scope["bk_biz_id"], + object_type=scope["object_type"], + node_type=scope["node_type"], + nodes=scope["nodes"], + target_hosts=backend_mkd.subscription.unit.SUBSCRIPTION_DATA.get("target_hosts"), + from_system="blueking", + creator="admin", + enable=True, + name="test_policy", + pid=models.Subscription.ROOT, + plugin_name=utils.PLUGIN_NAME, + category=models.Subscription.CategoryType.POLICY, + ) + + SubscriptionHandler = mock.MagicMock() + SubscriptionHandler.run = mock.MagicMock(return_value={"task_id": 1, "subscription_id": sub.id}) + with mock.patch( + "apps.backend.management.commands.init_official_plugins.SubscriptionHandler", + return_value=SubscriptionHandler, + ): + call_command("init_official_plugins", tag="stable", run_policy=True) + + SubscriptionHandler.run.assert_called_once() + self.assertTrue(models.Packages.objects.all().exists()) + self.assertTrue(models.UploadPackage.objects.all().exists()) + self.assertTrue(models.PluginConfigTemplate.objects.all().exists()) + + plugin_desc = models.GsePluginDesc.objects.get(name=utils.PLUGIN_NAME) + tag = PluginTargetHelper.get_tag(plugin_desc.id, utils.PACKAGE_VERSION) + self.assertEqual(tag.name, "stable") + + class ImportCommandBkRepoTestCase(ImportCommandTestCase): OVERWRITE_OBJ__KV_MAP = mock_data_utils.OVERWRITE_OBJ__KV_MAP diff --git a/scripts/workflows/bk_ci.sh b/scripts/workflows/bk_ci.sh index 0c6cb39c0..d44b34cee 100755 --- a/scripts/workflows/bk_ci.sh +++ b/scripts/workflows/bk_ci.sh @@ -6,9 +6,11 @@ cat << EOF SCRIPT_DIR -> "$SCRIPT_DIR" EOF -${SCRIPT_DIR}/prepare_services.sh +${SCRIPT_DIR}/prepare_services.sh || exit 1 -${SCRIPT_DIR}/install.sh +${SCRIPT_DIR}/install.sh || exit 1 + +${SCRIPT_DIR}/migrate_checker.sh || exit 1 ${SCRIPT_DIR}/code_quality.sh diff --git a/scripts/workflows/local_settings.py b/scripts/workflows/local_settings.py old mode 100644 new mode 100755 diff --git a/scripts/workflows/migrate_checker.sh b/scripts/workflows/migrate_checker.sh new file mode 100755 index 000000000..363dc1101 --- /dev/null +++ b/scripts/workflows/migrate_checker.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +python manage.py makemigrations --check --dry-run + + +if [[ $? -ne 0 ]]; +then + echo "Migrations 检测未通过!" + exit 1 +else + echo "单元测试已通过" +fi + + +python manage.py migrate + +if [[ $? -ne 0 ]]; +then + echo "Migrate 检测未通过!" + exit 1 +else + echo "单元测试已通过" +fi