Browse Source

Automatically upgrade legacy config files.

Devan Goodwin 9 years ago
parent
commit
7063a66354
2 changed files with 92 additions and 10 deletions
  1. 31 10
      utils/src/ooinstall/oo_config.py
  2. 61 0
      utils/test/oo_config_tests.py

+ 31 - 10
utils/src/ooinstall/oo_config.py

@@ -73,7 +73,6 @@ class Host(object):
 
 
 class OOConfig(object):
-    new_config = True
     default_dir = os.path.normpath(
         os.environ.get('XDG_CONFIG_HOME',
                        os.environ['HOME'] + '/.config/') + '/openshift/')
@@ -86,19 +85,22 @@ class OOConfig(object):
             self.config_path = os.path.normpath(self.default_dir +
                                                 self.default_file)
         self.settings = {}
-        self.read_config()
-        self.set_defaults()
+        self._read_config()
+        self._set_defaults()
 
-    def read_config(self, is_new=False):
+    def _read_config(self):
         self.hosts = []
         try:
-            new_settings = None
             if os.path.exists(self.config_path):
                 cfgfile = open(self.config_path, 'r')
-                new_settings = yaml.safe_load(cfgfile.read())
+                self.settings = yaml.safe_load(cfgfile.read())
                 cfgfile.close()
-            if new_settings:
-                self.settings = new_settings
+
+                # Use the presence of a Description as an indicator this is
+                # a legacy config file:
+                if 'Description' in self.settings:
+                    self._upgrade_legacy_config()
+
                 # Parse the hosts into DTO objects:
                 if 'hosts' in self.settings:
                     for host in self.settings['hosts']:
@@ -114,9 +116,28 @@ class OOConfig(object):
                                                                               ferr.strerror))
         except yaml.scanner.ScannerError:
             raise OOConfigFileError('Config file "{}" is not a valid YAML document'.format(self.config_path))
-        self.new_config = is_new
 
-    def set_defaults(self):
+    def _upgrade_legacy_config(self):
+        new_hosts = []
+        if 'validated_facts' in self.settings:
+            for key, value in self.settings['validated_facts'].iteritems():
+                if 'masters' in self.settings and key in self.settings['masters']:
+                    value['master'] = True
+                if 'nodes' in self.settings and key in self.settings['nodes']:
+                    value['node'] = True
+                new_hosts.append(value)
+        self.settings['hosts'] = new_hosts
+
+        remove_settings = ['validated_facts', 'Description', 'Name',
+            'Subscription', 'Vendor', 'Version', 'masters', 'nodes']
+        for s in remove_settings:
+            del self.settings[s]
+
+        # A legacy config implies openshift-enterprise 3.0:
+        self.settings['variant'] = 'openshift-enterprise'
+        self.settings['variant_version'] = '3.0'
+
+    def _set_defaults(self):
 
         if 'ansible_inventory_directory' not in self.settings:
             self.settings['ansible_inventory_directory'] = \

+ 61 - 0
utils/test/oo_config_tests.py

@@ -32,6 +32,26 @@ hosts:
     node: true
 """
 
+# Used to test automatic upgrading of config:
+LEGACY_CONFIG = """
+Description: This is the configuration file for the OpenShift Ansible-Based Installer.
+Name: OpenShift Ansible-Based Installer Configuration
+Subscription: {type: none}
+Vendor: OpenShift Community
+Version: 0.0.1
+ansible_config: /home/dgoodwin/.python-eggs/ooinstall-3.0.0-py2.7.egg-tmp/ooinstall/ansible.cfg
+ansible_inventory_directory: /home/dgoodwin/.config/openshift/.ansible
+ansible_log_path: /tmp/ansible.log
+ansible_plugins_directory: /home/dgoodwin/.python-eggs/ooinstall-3.0.0-py2.7.egg-tmp/ooinstall/ansible_plugins
+masters: [10.0.0.1]
+nodes: [10.0.0.2, 10.0.0.3]
+validated_facts:
+  10.0.0.1: {hostname: master-private.example.com, ip: 10.0.0.1, public_hostname: master.example.com, public_ip: 24.222.0.1}
+  10.0.0.2: {hostname: node1-private.example.com, ip: 10.0.0.2, public_hostname: node1.example.com, public_ip: 24.222.0.2}
+  10.0.0.3: {hostname: node2-private.example.com, ip: 10.0.0.3, public_hostname: node2.example.com, public_ip: 24.222.0.3}
+"""
+
+
 CONFIG_INCOMPLETE_FACTS = """
 hosts:
   - ip: 10.0.0.1
@@ -74,6 +94,47 @@ class OOInstallFixture(unittest.TestCase):
         return path
 
 
+class LegacyOOConfigTests(OOInstallFixture):
+
+    def setUp(self):
+        OOInstallFixture.setUp(self)
+        self.cfg_path = self.write_config(os.path.join(self.work_dir,
+            'ooinstall.conf'), LEGACY_CONFIG)
+        self.cfg = OOConfig(self.cfg_path)
+
+    def test_load_config_memory(self):
+        self.assertEquals('openshift-enterprise', self.cfg.settings['variant'])
+        self.assertEquals('3.0', self.cfg.settings['variant_version'])
+
+        self.assertEquals(3, len(self.cfg.hosts))
+        h1 = self.cfg.get_host('10.0.0.1')
+        self.assertEquals('10.0.0.1', h1.ip)
+        self.assertEquals('24.222.0.1', h1.public_ip)
+        self.assertEquals('master-private.example.com', h1.hostname)
+        self.assertEquals('master.example.com', h1.public_hostname)
+
+        h2 = self.cfg.get_host('10.0.0.2')
+        self.assertEquals('10.0.0.2', h2.ip)
+        self.assertEquals('24.222.0.2', h2.public_ip)
+        self.assertEquals('node1-private.example.com', h2.hostname)
+        self.assertEquals('node1.example.com', h2.public_hostname)
+
+        h3 = self.cfg.get_host('10.0.0.3')
+        self.assertEquals('10.0.0.3', h3.ip)
+        self.assertEquals('24.222.0.3', h3.public_ip)
+        self.assertEquals('node2-private.example.com', h3.hostname)
+        self.assertEquals('node2.example.com', h3.public_hostname)
+
+        self.assertFalse('masters' in self.cfg.settings)
+        self.assertFalse('nodes' in self.cfg.settings)
+        self.assertFalse('Description' in self.cfg.settings)
+        self.assertFalse('Name' in self.cfg.settings)
+        self.assertFalse('Subscription' in self.cfg.settings)
+        self.assertFalse('Vendor' in self.cfg.settings)
+        self.assertFalse('Version' in self.cfg.settings)
+        self.assertFalse('validates_facts' in self.cfg.settings)
+
+
 class OOConfigTests(OOInstallFixture):
 
     def test_load_config(self):