gushiren

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

目前所支持的windows镜像都是未激活状态,未激活状态下很多功能无法使用。

以后将要实现的功能是,windows虚机启动后,网络正常后能与KMS服务器通信,自动激活key

 

 

目前想到两种办法:

1、bat批处理脚本,开启自动运行,利用slmgr激活

2、cloudbase-init的LocalScriptsPlugin功能,将写好的脚本放在 cloudbaseinit.plugins.common.localscripts目录下

3、cloudbaseinit.plugins.windows.licensing.WindowsLicensingPlugin插件,该插件在代码里能实现set kms主机,及slmgr直接注入key

from oslo_log import log as oslo_logging
from cloudbaseinit import conf as cloudbaseinit_conf
from cloudbaseinit import constant
from cloudbaseinit.osutils import factory as osutils_factory
from cloudbaseinit.plugins.common import base
from cloudbaseinit.utils.windows import licensing
 
CONF = cloudbaseinit_conf.CONF
LOG = oslo_logging.getLogger(__name__)
 
 
class WindowsLicensingPlugin(base.BasePlugin):
 
    def _set_product_key(self, service, manager):
        if not CONF.set_kms_product_key and not CONF.set_avma_product_key:
            return
 
        description, license_family, is_current = manager.get_kms_product()
        if is_current:
            LOG.info('Product "%s" is already the current one, no need to set '
                     'a product key', description)
        else:
            use_avma = service.get_use_avma_licensing()
            if use_avma is None:
                use_avma = CONF.set_avma_product_key
            LOG.debug("Use AVMA: %s", use_avma)
 
            product_key = None
            if use_avma:
                product_key = manager.get_volume_activation_product_key(
                    license_family, constant.VOL_ACT_AVMA)
                if not product_key:
                    LOG.error("AVMA product key not found for this OS")
 
            if not product_key and CONF.set_kms_product_key:
                product_key = manager.get_volume_activation_product_key(
                    license_family, constant.VOL_ACT_KMS)
                if not product_key:
                    LOG.error("KMS product key not found for this OS")
 
            if product_key:
                LOG.info("Setting product key: %s", product_key)
                manager.set_product_key(product_key)
 
    def _set_kms_host(self, service, manager):
        kms_host = service.get_kms_host() or CONF.kms_host
        if kms_host:
            LOG.info("Setting KMS host: %s", kms_host)
            manager.set_kms_host(*kms_host.split(':'))
 
    def _activate_windows(self, service, manager):
        if CONF.activate_windows:
            # note(alexpilotti): KMS clients activate themselves
            # so this could be skipped if a KMS host is set
            LOG.info("Activating Windows")
            activation_result = manager.activate_windows()
            LOG.debug("Activation result:\n%s" % activation_result)
 
    def _log_licensing_info(self, manager):
        if CONF.log_licensing_info:
            license_info = manager.get_licensing_info()
            LOG.info('Microsoft Windows license info:\n%s' % license_info)
 
    def execute(self, service, shared_data):
        osutils = osutils_factory.get_os_utils()
 
        if osutils.is_nano_server():
            LOG.info("Licensing info and activation are not available on "
                     "Nano Server")
        else:
            manager = licensing.get_licensing_manager()
 
            eval_end_date = manager.is_eval()
            if eval_end_date:
                LOG.info("Evaluation license, skipping activation. "
                         "Evaluation end date: %s", eval_end_date)
            else:
                self._set_product_key(service, manager)
                self._set_kms_host(service, manager)
                self._activate_windows(service, manager)
                manager.refresh_status()
 
            self._log_licensing_info(manager)
 
        return base.PLUGIN_EXECUTION_DONE, False
cloudbase-init-master.cloudbaseinit.plugins.windows.licensing.py

cloud-init配置文件,需要开启cloudbaseinit.plugins.windows.licensing.WindowsLicensingPlugin插件,activate_windows=true,check_latest_version=false

参考:http://cloudbase-init.readthedocs.io/en/latest/plugins.html#winrm-listener-main

 

posted on 2018-08-21 17:15  gushiren  阅读(571)  评论(0编辑  收藏  举报