faf1e5
From bbe1338c356cb5bbc1196b7f4ba620f95d2b5fd1 Mon Sep 17 00:00:00 2001
faf1e5
From: Eduardo Otubo <otubo@redhat.com>
faf1e5
Date: Tue, 5 May 2020 08:08:18 +0200
faf1e5
Subject: [PATCH 3/5] exoscale: fix sysconfig cloud_config_modules overrides
faf1e5
faf1e5
RH-Author: Eduardo Otubo <otubo@redhat.com>
faf1e5
Message-id: <20200504085238.25884-4-otubo@redhat.com>
faf1e5
Patchwork-id: 96246
faf1e5
O-Subject: [RHEL-7.8.z cloud-init PATCH 3/5] exoscale: fix sysconfig cloud_config_modules overrides
faf1e5
Bugzilla: 1827207
faf1e5
RH-Acked-by: Cathy Avery <cavery@redhat.com>
faf1e5
RH-Acked-by: Mohammed Gamal <mgamal@redhat.com>
faf1e5
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
faf1e5
faf1e5
commit d1b022217a652c7a84d5430c9e571987864d3982
faf1e5
Author: Chad Smith <chad.smith@canonical.com>
faf1e5
Date:   Wed Aug 28 00:58:16 2019 +0000
faf1e5
faf1e5
    exoscale: fix sysconfig cloud_config_modules overrides
faf1e5
faf1e5
    Make sure Exoscale supplements or overrides existing system config
faf1e5
    setting cloud_config_modules instead of replacing it with a one item
faf1e5
    list set-passords
faf1e5
faf1e5
    LP: #1841454
faf1e5
faf1e5
Signed-off-by: Eduardo Otubo <otubo@redhat.com>
faf1e5
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
faf1e5
---
faf1e5
 cloudinit/sources/DataSourceExoscale.py          | 26 ++++++++++++++++--------
faf1e5
 tests/unittests/test_datasource/test_exoscale.py | 24 ++++++++++++++--------
faf1e5
 2 files changed, 33 insertions(+), 17 deletions(-)
faf1e5
faf1e5
diff --git a/cloudinit/sources/DataSourceExoscale.py b/cloudinit/sources/DataSourceExoscale.py
faf1e5
index 52e7f6f..fdfb4ed 100644
faf1e5
--- a/cloudinit/sources/DataSourceExoscale.py
faf1e5
+++ b/cloudinit/sources/DataSourceExoscale.py
faf1e5
@@ -6,6 +6,7 @@
faf1e5
 from cloudinit import ec2_utils as ec2
faf1e5
 from cloudinit import log as logging
faf1e5
 from cloudinit import sources
faf1e5
+from cloudinit import helpers
faf1e5
 from cloudinit import url_helper
faf1e5
 from cloudinit import util
faf1e5
 
faf1e5
@@ -20,13 +21,6 @@ URL_RETRIES = 6
faf1e5
 
faf1e5
 EXOSCALE_DMI_NAME = "Exoscale"
faf1e5
 
faf1e5
-BUILTIN_DS_CONFIG = {
faf1e5
-    # We run the set password config module on every boot in order to enable
faf1e5
-    # resetting the instance's password via the exoscale console (and a
faf1e5
-    # subsequent instance reboot).
faf1e5
-    'cloud_config_modules': [["set-passwords", "always"]]
faf1e5
-}
faf1e5
-
faf1e5
 
faf1e5
 class DataSourceExoscale(sources.DataSource):
faf1e5
 
faf1e5
@@ -42,8 +36,22 @@ class DataSourceExoscale(sources.DataSource):
faf1e5
             self.ds_cfg.get('password_server_port', PASSWORD_SERVER_PORT))
faf1e5
         self.url_timeout = self.ds_cfg.get('timeout', URL_TIMEOUT)
faf1e5
         self.url_retries = self.ds_cfg.get('retries', URL_RETRIES)
faf1e5
-
faf1e5
-        self.extra_config = BUILTIN_DS_CONFIG
faf1e5
+        self.extra_config = {}
faf1e5
+
faf1e5
+    def activate(self, cfg, is_new_instance):
faf1e5
+        """Adjust set-passwords module to run 'always' during each boot"""
faf1e5
+        # We run the set password config module on every boot in order to
faf1e5
+        # enable resetting the instance's password via the exoscale console
faf1e5
+        # (and a subsequent instance reboot).
faf1e5
+        # Exoscale password server only provides set-passwords user-data if
faf1e5
+        # a user has triggered a password reset. So calling that password
faf1e5
+        # service generally results in no additional cloud-config.
faf1e5
+        # TODO(Create util functions for overriding merged sys_cfg module freq)
faf1e5
+        mod = 'set_passwords'
faf1e5
+        sem_path = self.paths.get_ipath_cur('sem')
faf1e5
+        sem_helper = helpers.FileSemaphores(sem_path)
faf1e5
+        if sem_helper.clear('config_' + mod, None):
faf1e5
+            LOG.debug('Overriding module set-passwords with frequency always')
faf1e5
 
faf1e5
     def wait_for_metadata_service(self):
faf1e5
         """Wait for the metadata service to be reachable."""
faf1e5
diff --git a/tests/unittests/test_datasource/test_exoscale.py b/tests/unittests/test_datasource/test_exoscale.py
faf1e5
index 350c330..f006119 100644
faf1e5
--- a/tests/unittests/test_datasource/test_exoscale.py
faf1e5
+++ b/tests/unittests/test_datasource/test_exoscale.py
faf1e5
@@ -11,8 +11,10 @@ from cloudinit.sources.DataSourceExoscale import (
faf1e5
     PASSWORD_SERVER_PORT,
faf1e5
     read_metadata)
faf1e5
 from cloudinit.tests.helpers import HttprettyTestCase, mock
faf1e5
+from cloudinit import util
faf1e5
 
faf1e5
 import httpretty
faf1e5
+import os
faf1e5
 import requests
faf1e5
 
faf1e5
 
faf1e5
@@ -63,6 +65,18 @@ class TestDatasourceExoscale(HttprettyTestCase):
faf1e5
         password = get_password()
faf1e5
         self.assertEqual(expected_password, password)
faf1e5
 
faf1e5
+    def test_activate_removes_set_passwords_semaphore(self):
faf1e5
+        """Allow set_passwords to run every boot by removing the semaphore."""
faf1e5
+        path = helpers.Paths({'cloud_dir': self.tmp})
faf1e5
+        sem_dir = self.tmp_path('instance/sem', dir=self.tmp)
faf1e5
+        util.ensure_dir(sem_dir)
faf1e5
+        sem_file = os.path.join(sem_dir, 'config_set_passwords')
faf1e5
+        with open(sem_file, 'w') as stream:
faf1e5
+            stream.write('')
faf1e5
+        ds = DataSourceExoscale({}, None, path)
faf1e5
+        ds.activate(None, None)
faf1e5
+        self.assertFalse(os.path.exists(sem_file))
faf1e5
+
faf1e5
     def test_get_data(self):
faf1e5
         """The datasource conforms to expected behavior when supplied
faf1e5
         full test data."""
faf1e5
@@ -95,8 +109,6 @@ class TestDatasourceExoscale(HttprettyTestCase):
faf1e5
         self.assertEqual(ds.get_config_obj(),
faf1e5
                          {'ssh_pwauth': True,
faf1e5
                           'password': expected_password,
faf1e5
-                          'cloud_config_modules': [
faf1e5
-                              ["set-passwords", "always"]],
faf1e5
                           'chpasswd': {
faf1e5
                               'expire': False,
faf1e5
                           }})
faf1e5
@@ -130,9 +142,7 @@ class TestDatasourceExoscale(HttprettyTestCase):
faf1e5
         self.assertEqual(ds.userdata_raw.decode("utf-8"), "#cloud-config")
faf1e5
         self.assertEqual(ds.metadata, {"instance-id": expected_id,
faf1e5
                                        "local-hostname": expected_hostname})
faf1e5
-        self.assertEqual(ds.get_config_obj(),
faf1e5
-                         {'cloud_config_modules': [
faf1e5
-                             ["set-passwords", "always"]]})
faf1e5
+        self.assertEqual(ds.get_config_obj(), {})
faf1e5
 
faf1e5
     def test_get_data_no_password(self):
faf1e5
         """The datasource conforms to expected behavior when no password is
faf1e5
@@ -163,9 +173,7 @@ class TestDatasourceExoscale(HttprettyTestCase):
faf1e5
         self.assertEqual(ds.userdata_raw.decode("utf-8"), "#cloud-config")
faf1e5
         self.assertEqual(ds.metadata, {"instance-id": expected_id,
faf1e5
                                        "local-hostname": expected_hostname})
faf1e5
-        self.assertEqual(ds.get_config_obj(),
faf1e5
-                         {'cloud_config_modules': [
faf1e5
-                             ["set-passwords", "always"]]})
faf1e5
+        self.assertEqual(ds.get_config_obj(), {})
faf1e5
 
faf1e5
     @mock.patch('cloudinit.sources.DataSourceExoscale.get_password')
faf1e5
     def test_read_metadata_when_password_server_unreachable(self, m_password):
faf1e5
-- 
faf1e5
1.8.3.1
faf1e5