|
| 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 typing import List |
| 13 | + |
| 14 | +from django.conf import settings |
| 15 | + |
| 16 | +from apps.core.concurrent.retry import RetryHandler |
| 17 | +from apps.node_man import constants, models |
| 18 | +from apps.utils.batch_request import request_multi_thread |
| 19 | +from common.api import NodeApi |
| 20 | +from common.api.exception import DataAPIException |
| 21 | + |
| 22 | +from ..base import CommonData |
| 23 | +from ..subsubscription import SubSubscriptionBaseService |
| 24 | +from .base import AgentBaseService |
| 25 | + |
| 26 | + |
| 27 | +class StopPluginsService(SubSubscriptionBaseService, AgentBaseService): |
| 28 | + @staticmethod |
| 29 | + @RetryHandler(interval=1, retry_times=1, exception_types=[DataAPIException]) |
| 30 | + def call_create_subscription_api(params): |
| 31 | + return NodeApi.create_subscription(params) |
| 32 | + |
| 33 | + @classmethod |
| 34 | + def create_subscriptions(cls, common_data: CommonData) -> List[int]: |
| 35 | + |
| 36 | + running_processes = models.ProcessStatus.objects.filter( |
| 37 | + status=constants.ProcStateType.RUNNING, |
| 38 | + bk_host_id__in=common_data.bk_host_ids, |
| 39 | + proc_type=constants.ProcType.PLUGIN, |
| 40 | + is_latest=True, |
| 41 | + ) |
| 42 | + |
| 43 | + host_ids_processes = {} |
| 44 | + for process in running_processes: |
| 45 | + if process.bk_host_id not in host_ids_processes: |
| 46 | + host_ids_processes[process.bk_host_id] = [] |
| 47 | + host_ids_processes[process.bk_host_id].append(process) |
| 48 | + |
| 49 | + params_list = [] |
| 50 | + for host_id, processes in host_ids_processes.items(): |
| 51 | + for process in processes: |
| 52 | + params_list.append( |
| 53 | + { |
| 54 | + "params": { |
| 55 | + "run_immediately": True, |
| 56 | + "category": models.Subscription.CategoryType.ONCE, |
| 57 | + "bk_username": settings.SYSTEM_USE_API_ACCOUNT, |
| 58 | + "is_main": True, |
| 59 | + "plugin_name": process.name, |
| 60 | + "scope": { |
| 61 | + "node_type": models.Subscription.NodeType.INSTANCE, |
| 62 | + "object_type": models.Subscription.ObjectType.HOST, |
| 63 | + "nodes": [{"bk_host_id": host_id}], |
| 64 | + }, |
| 65 | + "steps": [ |
| 66 | + { |
| 67 | + "id": process.name, |
| 68 | + "type": "PLUGIN", |
| 69 | + "config": { |
| 70 | + "job_type": constants.JobType.MAIN_STOP_PLUGIN, |
| 71 | + "plugin_name": process.name, |
| 72 | + "plugin_version": process.version or "latest", |
| 73 | + "config_templates": [ |
| 74 | + { |
| 75 | + "name": "{}.conf".format(process.name), |
| 76 | + "version": process.version or "latest", |
| 77 | + "is_main": True, |
| 78 | + } |
| 79 | + ], |
| 80 | + }, |
| 81 | + "params": {"context": {}}, |
| 82 | + } |
| 83 | + ], |
| 84 | + } |
| 85 | + } |
| 86 | + ) |
| 87 | + subscription_ids = request_multi_thread( |
| 88 | + cls.call_create_subscription_api, params_list, get_data=lambda x: [x["subscription_id"]] |
| 89 | + ) |
| 90 | + return subscription_ids |
0 commit comments