Skip to content

Commit

Permalink
licensing: properly set KMS host and product key
Browse files Browse the repository at this point in the history
If a trial license key was set, the KMS default product key and host was
not properly configured, as the execution of the licensing plugin ended
abruptly.

The fix is to set the kms and product key without checking the evaluation
date corresponding for the trial key.

Change-Id: I45e9364661208c454ddf2be0ff925d149fe0a6b0
  • Loading branch information
ader1990 committed Jun 2, 2020
1 parent d53e765 commit 1582ebe
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 43 deletions.
40 changes: 19 additions & 21 deletions cloudbaseinit/plugins/windows/licensing.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,37 +64,35 @@ def _set_kms_host(self, service, manager):
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)
# 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)
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()
return base.PLUGIN_EXECUTION_DONE, False

manager = licensing.get_licensing_manager()

# set kms / avma product keys and kms hosts if any
self._set_product_key(service, manager)
self._set_kms_host(service, manager)

if CONF.activate_windows:
self._activate_windows(service, manager)

if CONF.log_licensing_info:
manager.refresh_status()
self._log_licensing_info(manager)

return base.PLUGIN_EXECUTION_DONE, False
36 changes: 14 additions & 22 deletions cloudbaseinit/tests/plugins/windows/test_licensing.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ def test_activate_windows(self):
expected_logs = [
"Activating Windows",
"Activation result:\n%s" % activate_result]
with testutils.ConfPatcher('activate_windows', True):
with self.snatcher:
self._licensing._activate_windows(mock_service, mock_manager)
with self.snatcher:
self._licensing._activate_windows(mock_service, mock_manager)
self.assertEqual(self.snatcher.output, expected_logs)
mock_manager.activate_windows.assert_called_once_with()

Expand All @@ -110,37 +109,33 @@ def _test_execute(self, mock_get_licensing_manager,
mock_set_product_key,
mock_set_kms_host,
mock_activate_windows,
nano=False, is_eval=True):
nano=False):
mock_service = mock.Mock()
mock_manager = mock.Mock()
mock_get_licensing_manager.return_value = mock_manager
mock_osutils = mock.MagicMock()
mock_osutils.is_nano_server.return_value = nano
mock_get_os_utils.return_value = mock_osutils
mock_manager.is_eval.return_value = is_eval
mock_manager.get_licensing_info.return_value = "fake"
expected_logs = []
with self.snatcher:
response = self._licensing.execute(service=mock_service,
shared_data=None)
with testutils.ConfPatcher('activate_windows', True):
with self.snatcher:
response = self._licensing.execute(service=mock_service,
shared_data=None)

mock_get_os_utils.assert_called_once_with()
if nano:
expected_logs = ["Licensing info and activation are "
"not available on Nano Server"]
self.assertEqual(self.snatcher.output, expected_logs)
return # no activation available
return
else:
if not is_eval:
mock_set_product_key.assert_called_once_with(mock_service,
mock_manager)
mock_set_kms_host.assert_called_once_with(mock_service,
mock_set_product_key.assert_called_once_with(mock_service,
mock_manager)
mock_set_kms_host.assert_called_once_with(mock_service,
mock_manager)
mock_activate_windows.assert_called_once_with(mock_service,
mock_manager)
mock_activate_windows.assert_called_once_with(mock_service,
mock_manager)
else:
expected_logs.append("Evaluation license, skipping activation"
". Evaluation end date: %s" % is_eval)
expected_logs.append('Microsoft Windows license info:\nfake')
mock_manager.get_licensing_info.assert_called_once_with()

Expand All @@ -150,8 +145,5 @@ def _test_execute(self, mock_get_licensing_manager,
def test_execute_nano(self):
self._test_execute(nano=True)

def test_execute_is_evaluated(self):
self._test_execute()

def test_execute(self):
self._test_execute(is_eval=False)
self._test_execute()

0 comments on commit 1582ebe

Please sign in to comment.