Skip to content

Commit

Permalink
feature: 节点管理「新建任务」插件优化
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuoZhuoCrayon committed Aug 10, 2023
1 parent 0c06510 commit 101c849
Show file tree
Hide file tree
Showing 6 changed files with 608 additions and 688 deletions.
71 changes: 67 additions & 4 deletions pipeline_plugins/components/collections/sites/open/nodeman/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import ujson as json
from django.utils.translation import ugettext_lazy as _
from pipeline.core.flow.activity import Service, StaticIntervalGenerator
from pipeline.core.flow.io import IntItemSchema, StringItemSchema
from pipeline.core.flow.io import ArrayItemSchema, IntItemSchema, ObjectItemSchema, StringItemSchema

from api.collections.nodeman import BKNodeManClient
from gcloud.conf import settings
Expand Down Expand Up @@ -140,11 +140,11 @@ def get_ip_list(self, ip_str):
def get_host_id_list(self, ip_str, executor, bk_cloud_id, bk_biz_id):
# 如果开启了ipv6的逻辑,则执行
if settings.ENABLE_IPV6:
ipv6_list, ipv4_list, _, _ = extract_ip_from_ip_str(ip_str)
ipv6_list, ipv4_list, *_ = extract_ip_from_ip_str(ip_str)
ip_list = ipv4_list + ipv6_list
bk_host_id_dict_ipv6 = get_host_id_by_inner_ipv6(executor, self.logger, bk_cloud_id, bk_biz_id, ip_list)

return list(bk_host_id_dict_ipv6.values())
bk_host_id_dict = get_host_id_by_inner_ip(executor, self.logger, bk_cloud_id, bk_biz_id, ip_list)
return list(set(bk_host_id_dict_ipv6.values()) | set(bk_host_id_dict.values()))

ip_list = get_ip_by_regex(ip_str)
bk_host_id_dict = get_host_id_by_inner_ip(executor, self.logger, bk_cloud_id, bk_biz_id, ip_list)
Expand Down Expand Up @@ -282,3 +282,66 @@ def schedule(self, data, parent_data, callback_data=None):
data.set_outputs("ex_data", error_log)
self.finish_schedule()
return False


class NodeManNewBaseService(NodeManBaseService):
def inputs_format(self):
return [
self.InputItem(
name=_("业务 ID"),
key="bk_biz_id",
type="int",
schema=IntItemSchema(description=_("当前操作所属的 CMDB 业务 ID")),
),
self.InputItem(
name=_("节点类型"),
key="nodeman_node_type",
type="string",
schema=StringItemSchema(description=_("AGENT(表示直连区域安装 Agent)、 PROXY(表示安装 Proxy)")),
),
self.InputItem(
name=_("操作详情"),
key="nodeman_op_info",
type="object",
schema=ObjectItemSchema(
description=_("操作内容信息"),
property_schemas={
"nodeman_ap_id": StringItemSchema(description=_("接入点 ID")),
"nodeman_op_type": StringItemSchema(
description=_(
"任务操作类型,可以是 INSTALL(安装)、 REINSTALL(重装)、" " UNINSTALL (卸载)、 REMOVE (移除)或 UPGRADE (升级)"
)
),
"nodeman_hosts": ArrayItemSchema(
description=_("需要被操作的主机信息(安装与重装时需要)"),
item_schema=ObjectItemSchema(
description=_("主机相关信息"),
property_schemas={
"nodeman_bk_cloud_id": StringItemSchema(description=_("管控区域ID")),
"nodeman_ap_id": StringItemSchema(description=_("接入点")),
"inner_ip": StringItemSchema(description=_("内网 IP")),
"login_ip": StringItemSchema(description=_("主机登录 IP,可以为空,适配复杂网络时填写")),
"data_ip": StringItemSchema(description=_("主机数据 IP,可以为空,适配复杂网络时填写")),
"outer_ip": StringItemSchema(description=_("外网 IP, 可以为空")),
"os_type": StringItemSchema(description=_("操作系统类型,可以是 LINUX, WINDOWS, 或 AIX")),
"port": StringItemSchema(description=_("端口号")),
"account": StringItemSchema(description=_("登录帐号")),
"auth_type": StringItemSchema(description=_("认证方式,可以是 PASSWORD 或 KEY")),
"auth_key": StringItemSchema(description=_("认证密钥,根据认证方式,是登录密码或者登陆密钥")),
},
),
),
"nodeman_other_hosts": ArrayItemSchema(
description=_("需要被操作的主机信息(升级,卸载,移除时需要)"),
item_schema=ObjectItemSchema(
description=_("主机相关信息"),
property_schemas={
"nodeman_bk_cloud_id": StringItemSchema(description=_("管控区域ID")),
"nodeman_ip_str": StringItemSchema(description=_("IP")),
},
),
),
},
),
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@

from django.utils.translation import ugettext_lazy as _
from pipeline.component_framework.component import Component
from pipeline.core.flow.io import ArrayItemSchema, IntItemSchema, ObjectItemSchema, StringItemSchema

from api.collections.nodeman import BKNodeManClient
from gcloud.conf import settings
from gcloud.utils.crypto import decrypt_auth_key, encrypt_auth_key
from pipeline_plugins.components.collections.sites.open.nodeman.base import (
NodeManBaseService,
NodeManNewBaseService,
get_host_id_by_inner_ip,
get_host_id_by_inner_ipv6,
get_nodeman_rsa_public_key,
Expand All @@ -45,23 +44,14 @@
HOST_EXTRA_PARAMS_IPV6 = ["inner_ipv6", "outer_ipv6"]


class NodemanCreateTaskService(NodeManBaseService):
class NodemanCreateTaskService(NodeManNewBaseService):
def execute(self, data, parent_data):
executor = parent_data.inputs.executor
client = BKNodeManClient(username=executor)
bk_biz_id = data.inputs.bk_biz_id

node_type = data.inputs.nodeman_node_type

nodeman_ticket = data.get_one_of_inputs("nodeman_ticket", {})
nodeman_tjj_ticket = nodeman_ticket.get("nodeman_tjj_ticket", "")
if nodeman_tjj_ticket:
try:
nodeman_tjj_ticket = decrypt_auth_key(nodeman_tjj_ticket, settings.RSA_PRIV_KEY)
except Exception:
# password is not encrypted
pass

nodeman_op_info = data.inputs.nodeman_op_info
op_type = nodeman_op_info.get("nodeman_op_type", "")
nodeman_hosts = nodeman_op_info.get("nodeman_hosts", [])
Expand Down Expand Up @@ -190,8 +180,6 @@ def execute(self, data, parent_data):

kwargs = {"job_type": job_name, "hosts": all_hosts, "action": "job_install"}

if nodeman_tjj_ticket:
kwargs.update({"tcoa_ticket": nodeman_tjj_ticket})
else:
data.set_outputs("ex_data", _("无效的操作请求:{}".format(job_name)))
return False
Expand All @@ -201,64 +189,6 @@ def execute(self, data, parent_data):

return self.get_job_result(result, data, action, kwargs)

def inputs_format(self):
return [
self.InputItem(
name=_("业务 ID"), key="bk_biz_id", type="int", schema=IntItemSchema(description=_("当前操作所属的 CMDB 业务 ID")),
),
self.InputItem(
name=_("节点类型"),
key="nodeman_node_type",
type="string",
schema=StringItemSchema(description=_("AGENT(表示直连区域安装 Agent)、 PROXY(表示安装 Proxy)")),
),
self.InputItem(
name=_("操作详情"),
key="nodeman_op_info",
type="object",
schema=ObjectItemSchema(
description=_("操作内容信息"),
property_schemas={
"nodeman_ap_id": StringItemSchema(description=_("接入点 ID")),
"nodeman_op_type": StringItemSchema(
description=_(
"任务操作类型,可以是 INSTALL(安装)、 REINSTALL(重装)、" " UNINSTALL (卸载)、 REMOVE (移除)或 UPGRADE (升级)"
)
),
"nodeman_hosts": ArrayItemSchema(
description=_("需要被操作的主机信息(安装与重装时需要)"),
item_schema=ObjectItemSchema(
description=_("主机相关信息"),
property_schemas={
"nodeman_bk_cloud_id": StringItemSchema(description=_("管控区域ID")),
"nodeman_ap_id": StringItemSchema(description=_("接入点")),
"inner_ip": StringItemSchema(description=_("内网 IP")),
"login_ip": StringItemSchema(description=_("主机登录 IP,可以为空,适配复杂网络时填写")),
"data_ip": StringItemSchema(description=_("主机数据 IP,可以为空,适配复杂网络时填写")),
"outer_ip": StringItemSchema(description=_("外网 IP, 可以为空")),
"os_type": StringItemSchema(description=_("操作系统类型,可以是 LINUX, WINDOWS, 或 AIX")),
"port": StringItemSchema(description=_("端口号")),
"account": StringItemSchema(description=_("登录帐号")),
"auth_type": StringItemSchema(description=_("认证方式,可以是 PASSWORD 或 KEY")),
"auth_key": StringItemSchema(description=_("认证密钥,根据认证方式,是登录密码或者登陆密钥")),
},
),
),
"nodeman_other_hosts": ArrayItemSchema(
description=_("需要被操作的主机信息(升级,卸载,移除时需要)"),
item_schema=ObjectItemSchema(
description=_("主机相关信息"),
property_schemas={
"nodeman_bk_cloud_id": StringItemSchema(description=_("管控区域ID")),
"nodeman_ip_str": StringItemSchema(description=_("IP")),
},
),
),
},
),
),
]


class NodemanCreateTaskComponent(Component):
name = _("新建任务")
Expand Down
Loading

0 comments on commit 101c849

Please sign in to comment.