Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dbm-services): 优化资源池导入的flow,执行window机器 #7130 #7142

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dbm-ui/backend/db_services/dbresource/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
RESOURCE_UPDATE_PARAMS,
SPEC_DATA,
)
from backend.db_services.ipchooser.constants import BkOsTypeCode
from backend.db_services.ipchooser.serializers.base import QueryHostsBaseSer
from backend.ticket.builders.common.field import DBTimezoneField
from backend.ticket.constants import TicketStatus
Expand All @@ -35,6 +36,7 @@ class HostInfoSerializer(serializers.Serializer):
ip = serializers.CharField()
host_id = serializers.IntegerField()
bk_cloud_id = serializers.IntegerField()
os_type = serializers.CharField(required=False, default=BkOsTypeCode.LINUX)

for_bizs = serializers.ListSerializer(help_text=_("专属业务的ID列表"), child=serializers.IntegerField())
resource_types = serializers.ListField(
Expand Down
71 changes: 43 additions & 28 deletions dbm-ui/backend/db_services/dbresource/views/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import itertools
import time
from collections import defaultdict
from typing import Dict, List

from django.utils.translation import ugettext_lazy as _
Expand Down Expand Up @@ -51,7 +52,7 @@
SpecCountResourceResponseSerializer,
SpecCountResourceSerializer,
)
from backend.db_services.ipchooser.constants import BkOsType, ModeType
from backend.db_services.ipchooser.constants import BK_OS_CODE__TYPE, BkOsType, ModeType
from backend.db_services.ipchooser.handlers.host_handler import HostHandler
from backend.db_services.ipchooser.handlers.topo_handler import TopoHandler
from backend.db_services.ipchooser.query.resource import ResourceQueryHelper
Expand Down Expand Up @@ -187,34 +188,48 @@ def query_dba_hosts(self, request):
@action(detail=False, methods=["POST"], url_path="import", serializer_class=ResourceImportSerializer)
def resource_import(self, request):
validated_data = self.params_validate(self.get_serializer_class())
root_id = generate_root_id()

# 补充必要的单据参数
validated_data.update(
ticket_type=TicketType.RESOURCE_IMPORT,
created_by=request.user.username,
uid=None,
# 额外补充资源池导入的参数,用于记录操作日志
bill_id=None,
bill_type=None,
task_id=root_id,
operator=request.user.username,
)

# 资源导入记录
import_record = {"task_id": root_id, "operator": request.user.username, "hosts": validated_data["hosts"]}
DBResourceApi.import_operation_create(params=import_record)

# 执行资源导入的后台flow
BaseController(root_id=root_id, ticket_data=validated_data).import_resource_init_step()
host_ids = [host["host_id"] for host in validated_data.pop("hosts")]

# 查询主机信息,并按照集群类型聚合
host_infos = ResourceQueryHelper.search_cc_hosts(role_host_ids=host_ids)
os_hosts = defaultdict(list)
for host in host_infos:
host.update(ip=host["bk_host_innerip"], host_id=host["bk_host_id"])
os_hosts[host["bk_os_type"]].append(host)

# 按照集群类型分别导入
for os_type, hosts in os_hosts.items():
root_id = generate_root_id()

# 补充必要的单据参数
validated_data.update(
ticket_type=TicketType.RESOURCE_IMPORT,
created_by=request.user.username,
uid=None,
hosts=hosts,
# 额外补充资源池导入的参数,用于记录操作日志
bill_id=None,
bill_type=None,
task_id=root_id,
operator=request.user.username,
os_type=BK_OS_CODE__TYPE[os_type],
)

# 缓存当前任务,并删除过期导入任务
now = int(time.time())
cache_key = RESOURCE_IMPORT_TASK_FIELD.format(user=request.user.username)
RedisConn.zadd(cache_key, {root_id: now})
expired_tasks = RedisConn.zrangebyscore(cache_key, "-inf", now - RESOURCE_IMPORT_EXPIRE_TIME)
if expired_tasks:
RedisConn.zrem(cache_key, *expired_tasks)
# 资源导入记录
import_record = {"task_id": root_id, "operator": request.user.username, "hosts": hosts}
DBResourceApi.import_operation_create(params=import_record)

# 执行资源导入的后台flow
validated_data.update(hosts=list(hosts), os_type=BK_OS_CODE__TYPE[os_type])
BaseController(root_id=root_id, ticket_data=validated_data).import_resource_init_step()

# 缓存当前任务,并删除过期导入任务
now = int(time.time())
cache_key = RESOURCE_IMPORT_TASK_FIELD.format(user=request.user.username)
RedisConn.zadd(cache_key, {root_id: now})
expired_tasks = RedisConn.zrangebyscore(cache_key, "-inf", now - RESOURCE_IMPORT_EXPIRE_TIME)
if expired_tasks:
RedisConn.zrem(cache_key, *expired_tasks)

return Response()

Expand Down
3 changes: 3 additions & 0 deletions dbm-ui/backend/flow/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
# window操作系统超级账号,标准运维调用
WINDOW_ADMIN_USER_FOR_CHECK = "Administrator"

# linux操作系统超级账号,标准运维调用
LINUX_ADMIN_USER_FOR_CHECK = "root"

# tendisplus默认kvstorecount
DEFAULT_TENDISPLUS_KVSTORECOUNT = 10

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
from backend.components.dbresource.client import DBResourceApi
from backend.configuration.constants import SystemSettingsEnum
from backend.configuration.models import SystemSettings
from backend.db_services.ipchooser.constants import BkOsType
from backend.flow.consts import LINUX_ADMIN_USER_FOR_CHECK, WINDOW_ADMIN_USER_FOR_CHECK
from backend.flow.engine.bamboo.scene.common.builder import Builder
from backend.flow.plugins.components.collections.common.external_service import ExternalServiceComponent
from backend.flow.plugins.components.collections.common.sa_idle_check import CheckMachineIdleComponent
Expand All @@ -39,12 +41,23 @@ def machine_init_flow(self):
p = Builder(root_id=self.root_id, data=self.data)
ip_list = self.data["hosts"]
bk_biz_id = self.data["bk_biz_id"]

if self.data.get("os_type", BkOsType.LINUX.value) == BkOsType.WINDOWS.value:
# 如果是window类型机器,用administrator账号
account_name = WINDOW_ADMIN_USER_FOR_CHECK
else:
account_name = LINUX_ADMIN_USER_FOR_CHECK

# 先执行空闲检查
if env.SA_CHECK_TEMPLATE_ID:
p.add_act(
act_name=_("执行sa空闲检查"),
act_component_code=CheckMachineIdleComponent.code,
kwargs=asdict(InitCheckForResourceKwargs(ips=[host["ip"] for host in ip_list], bk_biz_id=bk_biz_id)),
kwargs=asdict(
InitCheckForResourceKwargs(
ips=[host["ip"] for host in ip_list], bk_biz_id=bk_biz_id, account_name=account_name
)
),
)

# 在执行sa初始化
Expand All @@ -53,7 +66,7 @@ def machine_init_flow(self):
p.add_act(
act_name=_("执行sa初始化"),
act_component_code=SaInitComponent.code,
kwargs={"ips": [host["ip"] for host in ip_list], "bk_biz_id": bk_biz_id},
kwargs={"ips": [host["ip"] for host in ip_list], "bk_biz_id": bk_biz_id, "account_name": account_name},
)

# 调用资源导入接口
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def _execute(self, data, parent_data) -> bool:
"${biz_cc_id}": bk_biz_id,
"${job_ip_list}": "\n".join(iplist),
"${bk_biz_id}": bk_biz_id,
"${job_account}": kwargs.get("account_name", "root"),
},
}
rpdata = BkSopsApi.create_task(param)
Expand Down
1 change: 1 addition & 0 deletions dbm-ui/backend/flow/utils/mysql/mysql_act_dataclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ class InitCheckForResourceKwargs:

ips: list
bk_biz_id: int = env.DBA_APP_BK_BIZ_ID
account_name: str = "root"


@dataclass()
Expand Down
Loading