This is the 23rd day of my participation in the August More Text Challenge.More challenges in August

Introduction to the

In the two articles “Blue Whale Implements vsphere VIRTUAL Machine Delivery – Virtual Machine Management (vsphere)” and “Blue Whale Implements Virtual Machine Delivery – JUMP Machine Management (JUMP)”, we have achieved virtual machine creation and JUMP machine management, and the last step is to register with CMDB to complete the whole delivery process.

But blue Whale standard operations does not have CMDB registered atoms by default, so we need a custom supplement.

Train of thought

The procedure for registering a CMDB is as follows:

  1. Install CMDB Agent. This step has been integrated into the startup of the template.

  2. Add hosts to the idle host resource pool in the CMDB.

  3. Move servers from the idle resource pool on hosts to idle modules associated with services.

The above deployment can be implemented through the Blue Whale configuration Platform API (add_host_to_resource), which requires the host registration atom to provide two parameters:

  1. Host IP, which is used throughout the delivery process;
  2. Service ID. After the host is registered according to cc_bk_biz_ID, the host can be directly transferred to the idle module of the specified service.

For other API parameters, see add_host_to_resource interface documentation of blue Whale Configuration Platform.

The process is as follows:

Host registration (CMDB) development

1. Host registration atomic front-end development

vim cc_register.js
(function(){
    $.atoms.cc_register = [
        {
            tag_code: "cc_register_ip".type: "input",
            attrs: {
                name: gettext("Host IP"),
                placeholder: gettext("Please enter the host IP address"),
                hookable: true,
                validation: [
                    {
                        type: "required"}}},]})();Copy the code

The completion of development is shown as follows:

2. Host registration atomic back end development

vim cmdb.py
# -*- coding: utf-8 -*-
CMDB custom atom 1. Register the host to the resource pool 2. After registering the host according to cc_bk_biz_ID, the host can be directly transferred to the idle module of the specified service.


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 "Configuration Platform Customization (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 'query result'), key='data'.type='list'),
            self.OutputItem(name=(U 'Return message'), key='message'.type='str'),
            self.OutputItem(name=(U 'Exception information'), key='ex_data'.type='str')]class CCRegisterComponent(Component) :
        name = U 'Host Registration'
        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

Copy the code

Note: We need to add the host to the associated business, not the host free resource pool, so we need to know the current business ID, which can be used to get all the business on the current page through biz_cc_id.

3. The final effect

conclusion

So far, the entire process of virtual machine shelf has been completed. In retrospect, the three atoms of blue Whale are mainly realized: virtual machine management (VSPHERE), JUMP management (JUMP) and configuration platform customization (CMDB). Each chapter can be automated individually or as a process through standard o&M. The diagram below: