其他
蓝鲸实现虚拟机交付-配置平台(CMDB)
前面的内容我们实现了创建虚拟机、跳板机管理等内容,就剩下最后一步注册到cmdb就可以完整实现整个交付过程。
但是蓝鲸标准运维默认没有cmdb注册原子,因此我们需要自定义补充。
思路
注册cmdb的过程如下:
1.安装cmdb agent,此步骤我们已经集成到模板的开机启动中;
2.将主机添加到cmdb的主机空闲资源池;
3.将主机空闲资源池中的服务器转移到关联业务下的空闲模块;
以上部署的实现可以通过蓝鲸配置平台API(add_host_to_resource)实现,需要主机注册原子提供两个参数:
主机ip,此ip贯穿整个交付过程;
业务id,根据cc_bk_biz_id 主机注册后可直接转移到指定业务的空闲模块下;
其他API的参数需要查看蓝鲸配置平台的add_host_to_resource接口文档。
流程如下:
主机注册(CMDB)开发
1.主机注册原子前端开发
vim cc_register.js
(function(){
$.atoms.cc_register = [
{
tag_code: "cc_register_ip",
type: "input",
attrs: {
name: gettext("主机IP"),
placeholder: gettext("请输入主机IP"),
hookable: true,
validation: [
{
type: "required"
}
]
}
},
]
})();
开发完成展示如下:
2.主机注册原子后端开发
vim cmdb.py
# -*- coding: utf-8 -*-
"""
cmdb 自定义原子
1.主机注册到资源池
2.根据cc_bk_biz_id 主机注册后可直接转移到指定业务的空闲模块下
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.
"""
import logging
from pipeline.conf import settings
from pipeline.core.flow.activity import Service
from pipeline.component_framework.component import Component
from gcloud.conf import settings
logger = logging.getLogger('root')
get_client_by_user = settings.ESB_GET_CLIENT_BY_USER
__group_name__ = u"配置平台自定义(CMDB)"
class CCRegisterService(Service):
__need_schedule__ = False
def execute(self, data, parent_data):
executor = parent_data.get_one_of_inputs("executor")
biz_cc_id = parent_data.get_one_of_inputs('biz_cc_id')
client = get_client_by_user(executor)
client.set_bk_api_ver('v2')
cc_register_ip = data.get_one_of_inputs("cc_register_ip")
kwargs = {
"bk_app_code": "bk-sops-atoms",
"bk_app_secret": "5cab4837-4799-43b4-8d90-f2ef98ddecf3",
"bk_username": executor,
"bk_supplier_account": "0",
"bk_biz_id": biz_cc_id,
"host_info": {
"0": {
"bk_host_innerip": cc_register_ip,
"bk_cloud_id": 0,
"import_from": "3"
}
}
}
print kwargs
api_result = client.cc.add_host_to_resource(kwargs)
if api_result['result']:
data.set_outputs('data', api_result['data'])
data.set_outputs('message', api_result['message'])
return True
else:
data.set_outputs('ex_data', api_result['message'])
return False
def outputs_format(self):
return [
self.OutputItem(name=(u'查询结果'), key='data', type='list'),
self.OutputItem(name=(u'返回信息'), key='message', type='str'),
self.OutputItem(name=(u'异常信息'), key='ex_data', type='str')
]
class CCRegisterComponent(Component):
name = u'主机注册'
code = 'cc_register'
bound_service = CCRegisterService
#form = settings.STATIC_URL + 'custom_atoms/cmdb/cc_register.js'
form = '%scityre_atoms/cc_register.js' % settings.STATIC_URL
注意:我们需要把主机添加到关联业务下,而不是主机空闲资源池,因此我们需要知道当前业务id,此id可以通过biz_cc_id获取当前页面所有的业务。
3.最终实现效果
总结
至此我们关于虚拟机上架的全部流程已经全部完成,回顾下主要实现了蓝鲸的三个原子:虚拟机管理(VSPHERE)、跳板机管理(JUMP)、配置平台自定义(CMDB)。每个章节都可以单独进行自动化操作,也可以通过标准运维以流程的方式进行操作。如下图:
蓝鲸实现vsphere虚拟机交付-虚拟机管理(VSPHERE)
vcenter自定义规范定制虚拟机-vsphere client