|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-节点管理(BlueKing-BK-NODEMAN) available. |
| 4 | +Copyright (C) 2017-2022 THL A29 Limited, a Tencent company. All rights reserved. |
| 5 | +Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at https://opensource.org/licenses/MIT |
| 7 | +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 8 | +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 9 | +specific language governing permissions and limitations under the License. |
| 10 | +""" |
| 11 | + |
| 12 | +from collections import defaultdict |
| 13 | +from typing import List |
| 14 | + |
| 15 | +from django.conf import settings |
| 16 | + |
| 17 | +from apps.core.concurrent.retry import RetryHandler |
| 18 | +from apps.node_man import constants, models |
| 19 | +from apps.utils.batch_request import request_multi_thread |
| 20 | +from common.api import NodeApi |
| 21 | +from common.api.exception import DataAPIException |
| 22 | + |
| 23 | +from ..base import CommonData |
| 24 | +from ..subsubscription import SubSubscriptionBaseService |
| 25 | +from .base import AgentBaseService |
| 26 | + |
| 27 | + |
| 28 | +class StopPluginsService(SubSubscriptionBaseService, AgentBaseService): |
| 29 | + @staticmethod |
| 30 | + @RetryHandler(interval=1, retry_times=1, exception_types=[DataAPIException]) |
| 31 | + def call_create_subscription_api(params): |
| 32 | + return NodeApi.create_subscription(params) |
| 33 | + |
| 34 | + @classmethod |
| 35 | + def create_subscriptions(cls, common_data: CommonData) -> List[int]: |
| 36 | + |
| 37 | + host_ids_group_by_os = defaultdict(list) |
| 38 | + for host in common_data.host_id_obj_map.values(): |
| 39 | + host_ids_group_by_os[host.os_type.lower()].append(host.bk_host_id) |
| 40 | + |
| 41 | + installed_running_plugin_names = models.ProcessStatus.objects.filter( |
| 42 | + status=constants.ProcStateType.RUNNING, |
| 43 | + bk_host_id__in=common_data.bk_host_ids, |
| 44 | + proc_type=constants.ProcType.PLUGIN, |
| 45 | + ).values_list("name", flat=True) |
| 46 | + |
| 47 | + plugin_name__os_type_set = set( |
| 48 | + models.Packages.objects.filter( |
| 49 | + project__in=installed_running_plugin_names, os__in=host_ids_group_by_os.keys() |
| 50 | + ).values_list("project", "os") |
| 51 | + ) |
| 52 | + params_list = [] |
| 53 | + for (plugin_name, os_type) in plugin_name__os_type_set: |
| 54 | + params_list.append( |
| 55 | + { |
| 56 | + "params": { |
| 57 | + "run_immediately": True, |
| 58 | + "category": models.Subscription.CategoryType.ONCE, |
| 59 | + "bk_username": settings.SYSTEM_USE_API_ACCOUNT, |
| 60 | + "scope": { |
| 61 | + "node_type": models.Subscription.NodeType.INSTANCE, |
| 62 | + "object_type": models.Subscription.ObjectType.HOST, |
| 63 | + "nodes": [{"bk_host_id": bk_host_id} for bk_host_id in host_ids_group_by_os[os_type]], |
| 64 | + }, |
| 65 | + "steps": [ |
| 66 | + { |
| 67 | + "id": plugin_name, |
| 68 | + "type": "PLUGIN", |
| 69 | + "config": { |
| 70 | + "job_type": constants.JobType.MAIN_STOP_PLUGIN, |
| 71 | + "plugin_name": plugin_name, |
| 72 | + "plugin_version": "latest", |
| 73 | + "config_templates": [ |
| 74 | + {"name": "{}.conf".format(plugin_name), "version": "latest", "is_main": True} |
| 75 | + ], |
| 76 | + }, |
| 77 | + "params": {"context": {}}, |
| 78 | + } |
| 79 | + ], |
| 80 | + } |
| 81 | + } |
| 82 | + ) |
| 83 | + subscription_ids = request_multi_thread( |
| 84 | + cls.call_create_subscription_api, params_list, get_data=lambda x: [x["subscription_id"]] |
| 85 | + ) |
| 86 | + return subscription_ids |
0 commit comments