蓝鲸智云实现虚拟机交付(四)-配置平台自定义(CMDB)

简介

前面的内容我们实现了创建虚拟机、跳板机納管等内容,就剩下最后一步注册到cmdb就可以完整实现整个交付过程。

但是蓝鲸标准运维默认没有cmdb注册原子,因此我们需要自定义补充。

思路

注册cmdb的过程如下:
1.安装cmdb agent,此步骤我们已经集成到模板的开机启动中;
2.将主机添加到cmdb的主机空闲资源池;
3.将主机空闲资源池中的服务器转移到关联业务下的空闲模块;

以上部署的实现可以通过蓝鲸配置平台API(add_host_to_resource)实现,需要主机注册原子提供两个参数:
1.主机ip,此ip是贯穿整个交付过程;
2.业务id,从当前页面通过js直接获取即可;
其他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"
                    }
                ]
            }
        },
        {
            tag_code: "cc_bk_biz_id",
            type: "select",
            attrs: {
                name: gettext("业务id"),
                placeholder: gettext("业务id"),    
                hookable: true,
                items: [
                    {text: $.context.biz_cc_id, value: $.context.biz_cc_id},
                ],
                default: $.context.biz_cc_id,
                validation: [
                    {
                        type: "required"
                    }
                ]
            }
        },
    ]   
})();

注意:
因为我们需要把主机添加到关联业务下,而不是主机空闲资源池,因此我们需要知道当前业务id,此id可以通过js 获取当前页面所有的业务即$.context.biz_cc_id。

前端展示如下:
在这里插入图片描述
2.主机注册原子后端开发

# -*- coding: utf-8 -*-
'''
#cmdb 自定义原子
1.主机注册到资源池
2.根据cc_bk_biz_id 主机注册后可直接转移到指定业务的空闲模块下
'''
from pipeline.conf import settings
from pipeline.core.flow.activity import Service
from pipeline.component_framework.component import Component
from blueking.component.shortcuts import 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")
        client = get_client_by_user(executor)
        client.set_bk_api_ver('v2')

        cc_register_ip = data.get_one_of_inputs("cc_register_ip")
        cc_bk_biz_id = data.get_one_of_inputs("cc_bk_biz_id")

        kwargs = { 
            "bk_app_code": "bk-sops-atoms",
            "bk_app_secret": "5cab4837-8d90-f2ef98ddecf3",
            "bk_username": executor,
            "bk_supplier_account": "0",
            "bk_biz_id": cc_bk_biz_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'

至此,我们的虚拟机交付的整个过程也接近尾声可,下一步直接通过蓝鲸自带的发送通知原子,给相关人员发送邮件或微信通知即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值