From 839da697292a060c7f7c85b5802ccd32714cb30a Mon Sep 17 00:00:00 2001
From: Maarten de Waard <maarten@greenhost.nl>
Date: Wed, 7 Jul 2021 14:26:52 +0200
Subject: [PATCH] remove settings.yml from the mix

---
 ansible/group_vars/all/oas.yml                |  1 +
 ansible/group_vars/all/settings.yml.example   | 11 ----
 .../roles/compatibility-checks/tasks/main.yml |  9 ---
 openappstack/__main__.py                      |  2 +-
 openappstack/cluster.py                       | 65 +++----------------
 test/pytest/test_certs.py                     |  4 +-
 6 files changed, 14 insertions(+), 78 deletions(-)
 delete mode 100644 ansible/group_vars/all/settings.yml.example

diff --git a/ansible/group_vars/all/oas.yml b/ansible/group_vars/all/oas.yml
index cfd1e39d1..a63ad6d25 100644
--- a/ansible/group_vars/all/oas.yml
+++ b/ansible/group_vars/all/oas.yml
@@ -1,6 +1,7 @@
 ---
 # Directory to store generated configuration and cluster state.
 data_directory: "/var/lib/OpenAppStack"
+ip_address: "{{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}"
 
 # Use python3 on cluster nodes for ansible
 ansible_python_interpreter: "/usr/bin/env python3"
diff --git a/ansible/group_vars/all/settings.yml.example b/ansible/group_vars/all/settings.yml.example
deleted file mode 100644
index 931b7f033..000000000
--- a/ansible/group_vars/all/settings.yml.example
+++ /dev/null
@@ -1,11 +0,0 @@
-# External ip address of the cluster.
-ip_address: "203.0.113.6"
-# Main domain name of the cluster services.
-domain: "example.com"
-
-# It is possible to add a docker mirror that serves images from docker.io.
-docker_mirror:
-  enabled: false
-  # endpoint: URL of docker mirror
-  # username: username for auth
-  # password: password for auth
diff --git a/ansible/roles/compatibility-checks/tasks/main.yml b/ansible/roles/compatibility-checks/tasks/main.yml
index 8132c4df0..1ca666a49 100644
--- a/ansible/roles/compatibility-checks/tasks/main.yml
+++ b/ansible/roles/compatibility-checks/tasks/main.yml
@@ -11,12 +11,3 @@
     that: "cluster_dir is defined"
     msg: >
       "Please define the variable `cluster_dir`."
-
-- name: Check if all variables from settings.yml.example are set in your settings.yml file
-  assert:
-    that: "{{ item }} is defined"
-    msg: >
-      "Please define the variable `{{ item }}`."
-  with_items:
-    - ip_address
-    - domain
diff --git a/openappstack/__main__.py b/openappstack/__main__.py
index 56ac73998..2f926528f 100755
--- a/openappstack/__main__.py
+++ b/openappstack/__main__.py
@@ -354,7 +354,7 @@ def create(clus, args):  # pylint: disable=too-many-branches
     if args.docker_mirror_password:
         clus.docker_mirror_password = args.docker_mirror_password
 
-    # Write inventory.yml and settings.yml files
+    # Write inventory.yml
     clus.write_cluster_files()
 
     if args.create_domain_records:
diff --git a/openappstack/cluster.py b/openappstack/cluster.py
index a226c3b6d..a78bbaa5b 100644
--- a/openappstack/cluster.py
+++ b/openappstack/cluster.py
@@ -56,7 +56,7 @@ class Cluster:
         self.domain = None
         # Set this to False if the data needs to be (re)loaded from file
         self.data_loaded = False
-        # Load data from inventory.yml and settings.yml
+        # Load data from inventory.yml
         if load_data:
             self.load_data()
         # Can be used to use a custom disk image.
@@ -68,28 +68,23 @@ class Cluster:
 
     def load_data(self):
         """
-        Loads cluster data from inventory.yml and settings.yml files
+        Loads cluster data from inventory.yml and files
 
         Set self.data_loaded to False if this function should re-read data
         from file.
         """
         if not self.data_loaded:
-            with open(self.settings_file, 'r') as stream:
-                settings = yaml.safe_load(stream)
-                self.ip_address = settings['ip_address']
-                self.domain = settings['domain']
-
-            log.debug("""Read data from settings.yml:
-                ip address: %s
-                domain: %s""", self.ip_address, self.domain)
-
             with open(self.inventory_file, 'r') as stream:
                 inventory = yaml.safe_load(stream)
                 # Work with the master node from the inventory
                 self.hostname = inventory['all']['children']['master']['hosts']
-
-            log.debug(
-                'Read data from inventory.yml:\n\thostname: %s', self.hostname)
+                self.domain = self.hostname
+                self.ip_address = \
+                        inventory['hosts'][self.hostname]['ansible_host']
+            log.debug("""Read data from inventory:
+                ip address: %s
+                domain: %s
+                hostname: %s""", self.ip_address, self.domain, self.hostname)
         else:
             log.debug('Not loading cluster data from file. Set '
                       'Cluster.data_loaded to False if you want a reload.')
@@ -157,41 +152,9 @@ class Cluster:
         self.hostname = hostname
 
     def write_cluster_files(self):
-        """Creates an inventory.yml and settings.yml file for the cluster"""
+        """Creates an inventory.yml and dotenv file for the cluster"""
         self.make_cluster_directories()
         ansible.create_inventory(self)
-
-        # Create settings
-        with open(os.path.join(ansible.ANSIBLE_PATH, 'group_vars',
-                               'all', 'settings.yml.example'),
-                  'r') as stream:
-            settings = yaml.safe_load(stream)
-
-        settings['ip_address'] = self.ip_address
-        settings['domain'] = self.domain
-        settings['cluster_dir'] = self.cluster_dir
-        if self.docker_mirror_endpoint \
-                and self.docker_mirror_server \
-                and self.docker_mirror_username \
-                and self.docker_mirror_password:
-            settings['docker_mirror']['enabled'] = True
-            settings['docker_mirror']['endpoint'] = self.docker_mirror_endpoint
-            settings['docker_mirror']['username'] = self.docker_mirror_username
-            settings['docker_mirror']['password'] = self.docker_mirror_password
-            settings['docker_mirror']['server'] = self.docker_mirror_server
-
-        file_contents = yaml.safe_dump(settings, default_flow_style=False)
-        log.debug(file_contents)
-
-        # Create CLUSTER_DIR/group_vars/all/ if non-existent
-        vars_dir = os.path.dirname(self.settings_file)
-        if not os.path.exists(vars_dir):
-            os.makedirs(vars_dir)
-
-        with open(self.settings_file, 'w') as stream:
-            stream.write(file_contents)
-            log.info("Created %s", self.settings_file)
-
         dotenv_file = """CLUSTER_NAME={name}
 CLUSTER_DIR={cluster_dir}
 IP_ADDRESS={ip_address}
@@ -225,12 +188,6 @@ KUBECONFIG={secret_dir}/kube_config_cluster.yml
         """Path to the ansible inventory.yml for this cluster"""
         return os.path.join(self.cluster_dir, 'inventory.yml')
 
-    @property
-    def settings_file(self):
-        """Path to the ansible settings.yml for this cluster"""
-        return os.path.join(self.cluster_dir, 'group_vars', 'all',
-                            'settings.yml')
-
     @property
     def dotenv_file(self):
         """Path to the .cluster.env file with relevant environment variables"""
@@ -328,7 +285,6 @@ KUBECONFIG={secret_dir}/kube_config_cluster.yml
 
     Configuration:
       - Inventory file: {inventory_file}
-      - Settings file: {settings_file}
 
     Kubectl:
 
@@ -341,5 +297,4 @@ KUBECONFIG={secret_dir}/kube_config_cluster.yml
                 hostname=self.hostname,
                 domain=self.domain,
                 inventory_file=self.inventory_file,
-                settings_file=self.settings_file,
                 secret_dir=self.secret_dir))
diff --git a/test/pytest/test_certs.py b/test/pytest/test_certs.py
index 2b40c7678..a1ee39c6b 100755
--- a/test/pytest/test_certs.py
+++ b/test/pytest/test_certs.py
@@ -121,8 +121,8 @@ def test_cert_validation(host, app): # pylint: disable=too-many-statements
             print("Using domain %s from FQDN environment variable." % domain)
         else:
             ansible_vars = host.ansible.get_variables()
-            domain = ansible_vars["domain"]
-            print("Using domain %s from ansible settings.yml." % domain)
+            domain = ansible_vars["inventory_hostname"]
+            print("Using domain %s from ansible inventory." % domain)
 
         add_custom_cert_authorities(certifi.where(),
                                     ['pytest/le-staging-bundle.pem'])
-- 
GitLab