蓝鲸智云实现虚拟机交付(三)-跳板机管理(JUMP)
简介蓝鲸智云实现虚拟机交付(二)-虚拟机管理(VSPHERE)原子实现的是从模板克隆新的虚拟机,下面我们需要将其添加到jumpserver中。在此我们要借助于jumpserver的API,我们可以直接查看jumpserver swagger来查看相关API。可参见官网说明:jumpserver开发文档思路jumpserver添加主机的流程:1.创建资产,需要主机的基本信息、管理用户、...
简介
蓝鲸智云实现虚拟机交付(二)-虚拟机管理(VSPHERE)原子实现的是从模板克隆新的虚拟机,下面我们需要将其添加到jumpserver中。
在此我们要借助于jumpserver的API,我们可以直接查看jumpserver swagger来查看相关API。
可参见官网说明:
jumpserver开发文档
思路
jumpserver添加主机的流程:
1.创建资产,需要主机的基本信息、管理用户、节点等信息;
2.推送系统用户,需要将主机推送到指定的系统用户下;
因此我们需要在跳板机管理下建两个原子:
1.创建资产原子
2.推送系统用户原子
流程如下:
跳板机管理(JUMP)开发
1.创建资产原子前端开发
vim jump_asset_create.js
(function(){
$.atoms.jump_asset_create = [
{
tag_code: "jump_asset_hostname",
type: "input",
attrs: {
name: gettext("跳板机主机名"),
placeholder: gettext("主机名"),
hookable: true,
validation: [
{
type: "required"
}
]
}
},
{
tag_code: "jump_asset_host",
type: "input",
attrs: {
name: gettext("IP"),
placeholder: gettext("IP"),
hookable: true,
validation: [
{
type: "required"
}
]
}
},
{
tag_code: "jump_asset_protocol",
type: "select",
attrs: {
name: gettext("协议"),
placeholder: gettext("协议"),
items: [
{text: "ssh", value: "ssh"},
],
hookable: true,
validation: [
{
type: "required"
}
]
}
},
{
tag_code: "jump_asset_port",
type: "select",
attrs: {
name: gettext("端口"),
placeholder: gettext("SSH端口"),
items: [
{text: "1022", value: "1022"},
{text: "22", value: "22"},
],
hookable: true,
validation: [
{
type: "required"
}
]
}
},
{
tag_code: "jump_asset_platform",
type: "select",
attrs: {
name: gettext("系统平台"),
placeholder: gettext("系统平台"),
items: [
{text: "Linux", value: "Linux"},
],
hookable: true,
validation: [
{
type: "required"
}
]
}
},
{
tag_code: "jump_asset_adminuser",
type: "select",
attrs: {
name: gettext("管理用户"),
placeholder: gettext("管理用户"),
items: [
{text: "all-server-root", value: "f2fe91582dcf44f4a711295953b1ffe2"},
],
hookable: true,
validation: [
{
type: "required"
}
]
}
},
{
tag_code: "jump_asset_node",
type: "select",
attrs: {
name: gettext("跳板机节点"),
placeholder: gettext("节点"),
hookable: true,
remote: true,
remote_url: $.context.site_url + 'pipeline/jump_assets_nodes/' + $.context.biz_cc_id + '/',
remote_data_init: function(resp) {
return resp.data;
},
validation: [
{
type: "required"
}
]
}
},
]
})();
前段展示如下:
2.推送系统用户原子前端开发
vim jump_system_user_push.js
(function(){
$.atoms.jump_system_user_push = [
{
tag_code: "jump_asset_id",
type: "input",
attrs: {
name: gettext("资产"),
placeholder: gettext("资产uuid"),
hookable: true,
validation: [
{
type: "required"
}
]
}
},
{
tag_code: "jump_system_user",
type: "select",
attrs: {
name: gettext("系统用户"),
placeholder: gettext("系统用户uuid"),
items: [
{text: "first_line_sa", value: "1e955415-df33-4bab-9228-42199c6714d4"},
{text: "second_line_sa", value: "991e668b-0bf0-4743-8bcb-2442a524f7da"},
],
hookable: true,
validation: [
{
type: "required"
}
]
}
},
]
})();
前端展示如下:
3.两个原子的后端开发
# -*- coding: utf-8 -*-
'''
jumpserver 跳板机管理
'''
from pipeline.conf import settings
from pipeline.core.flow.activity import Service
from pipeline.component_framework.component import Component
import requests
import json
import logging
logger = logging.getLogger("root")
__group_name__ = u"跳板机管理(JUMP)"
class JumpAssetCreateService(Service):
__need_schedule__ = False
def execute(self, data, parent_data):
jump_asset_hostname = data.get_one_of_inputs('jump_asset_hostname')
jump_asset_host = data.get_one_of_inputs('jump_asset_host')
jump_asset_protocol = data.get_one_of_inputs('jump_asset_protocol')
jump_asset_port = data.get_one_of_inputs('jump_asset_port')
jump_asset_platform = data.get_one_of_inputs('jump_asset_platform')
#jump_asset_adminuser和jump_asset_node必须是uuid,否则请求jumpserver不成功
jump_asset_adminuser = data.get_one_of_inputs('jump_asset_adminuser')
jump_asset_node = data.get_one_of_inputs('jump_asset_node')
headers = {
"Authorization": "Token xxxxxxxxxxxxx",
"Content-Type": "application/json"
}
param = {
"hostname": jump_asset_hostname,
"ip": jump_asset_host,
"protocol": jump_asset_protocol,
"port": jump_asset_port,
"platform": jump_asset_platform,
"admin_user": jump_asset_adminuser,
"nodes": [jump_asset_node]
}
response = requests.post('http://jumpserver.test.cn/api/assets/v1/assets/', data=json.dumps(param), headers=headers)
if response.status_code < 300:
asset_id = response.json()["id"]
data.set_outputs('asset_id', asset_id)
return True
else:
data.set_outputs('ex_data', u"资产更新失败(可能资产已经存在),status_code: %s" % str(response.status_code))
return False
def outputs_format(self):
return [
self.OutputItem(name=(u'资产id'), key='asset_id', type='str'),
self.OutputItem(name=(u'异常信息'), key='ex_data', type='str')
]
class JumpAssetCreateComponent(Component):
name = u'创建资产'
code = 'jump_asset_create'
bound_service = JumpAssetCreateService
form = settings.STATIC_URL + 'custom_atoms/jumpserver/jump_asset_create.js'
class JumpSystemUserPushService(Service):
__need_schedule__ = False
def execute(self, data, parent_data):
#jump_asset和jump_system_user必须是uuid,否则请求jumpserver不成功
jump_asset_id = data.get_one_of_inputs('jump_asset_id')
jump_system_user = data.get_one_of_inputs('jump_system_user')
headers = {
"Authorization": "Token xxxxxxxxxxxxxxxxxxx",
"Content-Type": "application/json"
}
url = 'http://jumpserver.test.cn/api/assets/v1/system-user/' + jump_system_user.strip() + '/asset/' + jump_asset_id.strip() + '/push/'
try:
#推送系统用户
response = requests.get(url, headers=headers)
if response.status_code < 300:
task = response.json()["task"]
data.set_outputs('data', task)
return True
else:
data.set_outputs('ex_data', u"推送系统用户失败,status_code: %s" % str(response.status_code))
return False
except Exception as e:
data.set_outputs('ex_data', e)
logger.error(e)
return False
def outputs_format(self):
return [
self.OutputItem(name=(u'查询结果'), key='data', type='str'),
self.OutputItem(name=(u'异常信息'), key='ex_data', type='str')
]
class JumpSystemUserPushComponent(Component):
name = u'推送系统用户'
code = 'jump_system_user_push'
bound_service = JumpSystemUserPushService
form = settings.STATIC_URL + 'custom_atoms/jumpserver/jump_system_user_push.js'
开发过程中需要注意以下:
1.== jumpserver的jump_asset、jump_system_user、jump_asset_adminuser和jump_asset_node必须是uuid,否则请求jumpserver不成功==
- 跳板机节点信息我们需要根据在前段js中异步取jumpserver的节点信息,如下:
vim jump_asset_create.js
#截图其中一部分展示
{
tag_code: "jump_asset_node",
type: "select",
attrs: {
name: gettext("跳板机节点"),
placeholder: gettext("节点"),
hookable: true,
remote: true,
remote_url: $.context.site_url + 'pipeline/jump_assets_nodes/' + $.context.biz_cc_id + '/',
remote_data_init: function(resp) {
return resp.data;
},
validation: [
{
type: "required"
}
]
}
},
其中remote_url链接是异步调用了获取jumpserver 节点api,此部分是我们自定义的,主要涉及以下:
#自定义访问路径
vim bk-sops-atom/pipeline/components/sites/community/urls.py
# -*- coding: utf-8 -*-
"""
Tencent is pleased to support the open source community by making 蓝鲸智云PaaS平台社区版 (BlueKing PaaS Community Edition) available.
Copyright (C) 2017-2019 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
""" # noqa
from django.conf.urls import url
from . import query
urlpatterns = [
url(r'^cc_search_object_attribute/(?P<obj_id>\w+)/(?P<biz_cc_id>\d+)/$', query.cc_search_object_attribute),
url(r'^cc_search_create_object_attribute/(?P<obj_id>\w+)/(?P<biz_cc_id>\d+)/$', query.cc_search_create_object_attribute),
url(r'^cc_search_topo/(?P<obj_id>\w+)/(?P<category>\w+)/(?P<biz_cc_id>\d+)/$', query.cc_search_topo),
url(r'^cc_get_host_by_module_id/(?P<biz_cc_id>\d+)/$', query.cc_get_host_by_module_id),
#新增获取jumpserver节点信息
url(r'^jump_assets_nodes/(?P<biz_cc_id>\d+)/$', query.jump_assets_nodes),
]
#自定义获取jumpserver api的节点函数
vim ./pipeline/components/sites/community/query.py
def jump_assets_nodes(request, biz_cc_id):
"""
@summary: 查询jumpserver节点
@param request:
@param biz_cc_id:
@return:
"""
headers = {
"Authorization": "Token xxxxxxxxxxxx",
"Content-Type": "application/json"
}
response = requests.get('http://jumpserver.test.cn/api/assets/v1/nodes/', headers=headers)
if response.status_code != 200:
message = "[http://jumpserver.test.cn/api/assets/v1/nodes/] status_code: %s" % str(response.status_code)
logger.error(message)
result = {
'result': False,
'data': [],
'message': message
}
return JsonResponse(result)
assets_nodes = []
for item in response.json():
assets_nodes.append({
'value': item['id'],
'text': item['value']
})
return JsonResponse({'result': True, 'data': assets_nodes})
通过以上我们就可以在创建资产原子中选择节点位置了。
4.最终效果
更多推荐
所有评论(0)