From 10f416463baa3050911c2509f796ec03fc4759e8 Mon Sep 17 00:00:00 2001 From: Alessandro Pilotti Date: Wed, 30 Apr 2014 05:54:09 +0300 Subject: [PATCH] Adds Windows licensing plugin Adds support for performing an automated activation. --- cloudbaseinit/plugins/factory.py | 1 + cloudbaseinit/plugins/windows/licensing.py | 67 ++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 cloudbaseinit/plugins/windows/licensing.py diff --git a/cloudbaseinit/plugins/factory.py b/cloudbaseinit/plugins/factory.py index db650cc1..495507be 100644 --- a/cloudbaseinit/plugins/factory.py +++ b/cloudbaseinit/plugins/factory.py @@ -25,6 +25,7 @@ opts = [ 'cloudbaseinit.plugins.windows.sethostname.SetHostNamePlugin', 'cloudbaseinit.plugins.windows.createuser.CreateUserPlugin', 'cloudbaseinit.plugins.windows.networkconfig.NetworkConfigPlugin', + 'cloudbaseinit.plugins.windows.licensing.WindowsLicensingPlugin', 'cloudbaseinit.plugins.windows.sshpublickeys.' 'SetUserSSHPublicKeysPlugin', 'cloudbaseinit.plugins.windows.extendvolumes.ExtendVolumesPlugin', diff --git a/cloudbaseinit/plugins/windows/licensing.py b/cloudbaseinit/plugins/windows/licensing.py new file mode 100644 index 00000000..8bd3e5d8 --- /dev/null +++ b/cloudbaseinit/plugins/windows/licensing.py @@ -0,0 +1,67 @@ +# Copyright 2014 Cloudbase Solutions Srl +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# 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 os + +from oslo.config import cfg + +from cloudbaseinit.openstack.common import log as logging +from cloudbaseinit.osutils import factory as osutils_factory +from cloudbaseinit.plugins import base + +opts = [ + cfg.BoolOpt('activate_windows', default=False, + help='Activates Windows automatically'), +] + +CONF = cfg.CONF +CONF.register_opts(opts) + +LOG = logging.getLogger(__name__) + + +class WindowsLicensingPlugin(base.BasePlugin): + def _run_slmgr(self, osutils, args): + if osutils.check_sysnative_dir_exists(): + cscript_dir = osutils.get_sysnative_dir() + else: + cscript_dir = osutils.get_system32_dir() + + # Not SYSNATIVE, as it is already executed by a x64 process + slmgr_dir = osutils.get_system32_dir() + + cscript_path = os.path.join(cscript_dir, "cscript.exe") + slmgr_path = os.path.join(slmgr_dir, "slmgr.vbs") + + (out, err, exit_code) = osutils.execute_process( + [cscript_path, slmgr_path] + args, False) + + if exit_code: + raise Exception('slmgr.vbs failed with error code %(exit_code)s.\n' + 'Output: %(out)s\nError: %(err)s' % + {'exit_code': exit_code, 'out': out, 'err': err}) + return out + + def execute(self, service, shared_data): + if CONF.activate_windows: + osutils = osutils_factory.get_os_utils() + + license_info = self._run_slmgr(osutils, ['/dlv']) + LOG.info('Microsoft Windows license info:\n%s' % license_info) + + LOG.info("Activating Windows") + activation_result = self._run_slmgr(osutils, ['/ato']) + LOG.debug("Activation result:\n%s" % activation_result) + + return (base.PLUGIN_EXECUTION_DONE, False)