Blob Blame History Raw
From bbe1338c356cb5bbc1196b7f4ba620f95d2b5fd1 Mon Sep 17 00:00:00 2001
From: Eduardo Otubo <otubo@redhat.com>
Date: Tue, 5 May 2020 08:08:18 +0200
Subject: [PATCH 3/5] exoscale: fix sysconfig cloud_config_modules overrides

RH-Author: Eduardo Otubo <otubo@redhat.com>
Message-id: <20200504085238.25884-4-otubo@redhat.com>
Patchwork-id: 96246
O-Subject: [RHEL-7.8.z cloud-init PATCH 3/5] exoscale: fix sysconfig cloud_config_modules overrides
Bugzilla: 1827207
RH-Acked-by: Cathy Avery <cavery@redhat.com>
RH-Acked-by: Mohammed Gamal <mgamal@redhat.com>
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>

commit d1b022217a652c7a84d5430c9e571987864d3982
Author: Chad Smith <chad.smith@canonical.com>
Date:   Wed Aug 28 00:58:16 2019 +0000

    exoscale: fix sysconfig cloud_config_modules overrides

    Make sure Exoscale supplements or overrides existing system config
    setting cloud_config_modules instead of replacing it with a one item
    list set-passords

    LP: #1841454

Signed-off-by: Eduardo Otubo <otubo@redhat.com>
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
---
 cloudinit/sources/DataSourceExoscale.py          | 26 ++++++++++++++++--------
 tests/unittests/test_datasource/test_exoscale.py | 24 ++++++++++++++--------
 2 files changed, 33 insertions(+), 17 deletions(-)

diff --git a/cloudinit/sources/DataSourceExoscale.py b/cloudinit/sources/DataSourceExoscale.py
index 52e7f6f..fdfb4ed 100644
--- a/cloudinit/sources/DataSourceExoscale.py
+++ b/cloudinit/sources/DataSourceExoscale.py
@@ -6,6 +6,7 @@
 from cloudinit import ec2_utils as ec2
 from cloudinit import log as logging
 from cloudinit import sources
+from cloudinit import helpers
 from cloudinit import url_helper
 from cloudinit import util
 
@@ -20,13 +21,6 @@ URL_RETRIES = 6
 
 EXOSCALE_DMI_NAME = "Exoscale"
 
-BUILTIN_DS_CONFIG = {
-    # We run the set password config module on every boot in order to enable
-    # resetting the instance's password via the exoscale console (and a
-    # subsequent instance reboot).
-    'cloud_config_modules': [["set-passwords", "always"]]
-}
-
 
 class DataSourceExoscale(sources.DataSource):
 
@@ -42,8 +36,22 @@ class DataSourceExoscale(sources.DataSource):
             self.ds_cfg.get('password_server_port', PASSWORD_SERVER_PORT))
         self.url_timeout = self.ds_cfg.get('timeout', URL_TIMEOUT)
         self.url_retries = self.ds_cfg.get('retries', URL_RETRIES)
-
-        self.extra_config = BUILTIN_DS_CONFIG
+        self.extra_config = {}
+
+    def activate(self, cfg, is_new_instance):
+        """Adjust set-passwords module to run 'always' during each boot"""
+        # We run the set password config module on every boot in order to
+        # enable resetting the instance's password via the exoscale console
+        # (and a subsequent instance reboot).
+        # Exoscale password server only provides set-passwords user-data if
+        # a user has triggered a password reset. So calling that password
+        # service generally results in no additional cloud-config.
+        # TODO(Create util functions for overriding merged sys_cfg module freq)
+        mod = 'set_passwords'
+        sem_path = self.paths.get_ipath_cur('sem')
+        sem_helper = helpers.FileSemaphores(sem_path)
+        if sem_helper.clear('config_' + mod, None):
+            LOG.debug('Overriding module set-passwords with frequency always')
 
     def wait_for_metadata_service(self):
         """Wait for the metadata service to be reachable."""
diff --git a/tests/unittests/test_datasource/test_exoscale.py b/tests/unittests/test_datasource/test_exoscale.py
index 350c330..f006119 100644
--- a/tests/unittests/test_datasource/test_exoscale.py
+++ b/tests/unittests/test_datasource/test_exoscale.py
@@ -11,8 +11,10 @@ from cloudinit.sources.DataSourceExoscale import (
     PASSWORD_SERVER_PORT,
     read_metadata)
 from cloudinit.tests.helpers import HttprettyTestCase, mock
+from cloudinit import util
 
 import httpretty
+import os
 import requests
 
 
@@ -63,6 +65,18 @@ class TestDatasourceExoscale(HttprettyTestCase):
         password = get_password()
         self.assertEqual(expected_password, password)
 
+    def test_activate_removes_set_passwords_semaphore(self):
+        """Allow set_passwords to run every boot by removing the semaphore."""
+        path = helpers.Paths({'cloud_dir': self.tmp})
+        sem_dir = self.tmp_path('instance/sem', dir=self.tmp)
+        util.ensure_dir(sem_dir)
+        sem_file = os.path.join(sem_dir, 'config_set_passwords')
+        with open(sem_file, 'w') as stream:
+            stream.write('')
+        ds = DataSourceExoscale({}, None, path)
+        ds.activate(None, None)
+        self.assertFalse(os.path.exists(sem_file))
+
     def test_get_data(self):
         """The datasource conforms to expected behavior when supplied
         full test data."""
@@ -95,8 +109,6 @@ class TestDatasourceExoscale(HttprettyTestCase):
         self.assertEqual(ds.get_config_obj(),
                          {'ssh_pwauth': True,
                           'password': expected_password,
-                          'cloud_config_modules': [
-                              ["set-passwords", "always"]],
                           'chpasswd': {
                               'expire': False,
                           }})
@@ -130,9 +142,7 @@ class TestDatasourceExoscale(HttprettyTestCase):
         self.assertEqual(ds.userdata_raw.decode("utf-8"), "#cloud-config")
         self.assertEqual(ds.metadata, {"instance-id": expected_id,
                                        "local-hostname": expected_hostname})
-        self.assertEqual(ds.get_config_obj(),
-                         {'cloud_config_modules': [
-                             ["set-passwords", "always"]]})
+        self.assertEqual(ds.get_config_obj(), {})
 
     def test_get_data_no_password(self):
         """The datasource conforms to expected behavior when no password is
@@ -163,9 +173,7 @@ class TestDatasourceExoscale(HttprettyTestCase):
         self.assertEqual(ds.userdata_raw.decode("utf-8"), "#cloud-config")
         self.assertEqual(ds.metadata, {"instance-id": expected_id,
                                        "local-hostname": expected_hostname})
-        self.assertEqual(ds.get_config_obj(),
-                         {'cloud_config_modules': [
-                             ["set-passwords", "always"]]})
+        self.assertEqual(ds.get_config_obj(), {})
 
     @mock.patch('cloudinit.sources.DataSourceExoscale.get_password')
     def test_read_metadata_when_password_server_unreachable(self, m_password):
-- 
1.8.3.1