Skip to content

Commit

Permalink
feature: 插件包导入命令支持指定标签 (closed #1922)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuoZhuoCrayon committed Nov 14, 2023
1 parent 5439253 commit 19ce06d
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 3 deletions.
44 changes: 44 additions & 0 deletions apps/backend/management/commands/init_official_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
初始化内置官方插件
Expand All @@ -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()

Expand Down Expand Up @@ -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)
Expand Down
53 changes: 53 additions & 0 deletions apps/backend/tests/plugin/test_manage_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
6 changes: 4 additions & 2 deletions scripts/workflows/bk_ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Empty file modified scripts/workflows/local_settings.py
100644 → 100755
Empty file.
23 changes: 23 additions & 0 deletions scripts/workflows/migrate_checker.sh
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion scripts/workflows/prepare_services.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exit 1
exit 0
if [ "$YUM_INSTALL_SERVICE" ]; then
yum install mysql-devel -y
yum install redis -y
Expand Down

0 comments on commit 19ce06d

Please sign in to comment.