[libvirt-ci PATCH 06/12] lcitool: Introduce methods to load and validate the YAML config

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This patch introduce a set of class Config helper methods in order to
parse and validate the new global YAML config.
Currently, only 'install' and 'gitlab' sections are recognized with
the flavor setting defaulting to "test" (backwards compatibility) and
gitlab runner registration url defaulting to "https://gitlab.com";; the
rest of the options are currently mandatory.

Signed-off-by: Erik Skultety <eskultet@xxxxxxxxxx>
---
 guests/lcitool | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)

diff --git a/guests/lcitool b/guests/lcitool
index 2dca8b5..07d0b3c 100755
--- a/guests/lcitool
+++ b/guests/lcitool
@@ -133,6 +133,29 @@ class Util:
 
 class Config:
 
+    def __init__(self):
+
+        # Load the template config containing the defaults first, this must
+        # always succeed.
+        # NOTE: we should load this from /usr/share once we start packaging
+        # lcitool
+        base = Util.get_base()
+        with open(os.path.join(base, "config.yaml"), "r") as fp:
+            self.values = yaml.load(fp, Loader=yaml.Loader)
+
+        try:
+            with open(self._get_config_file("config.yaml"), "r") as fp:
+                user_config = yaml.load(fp, Loader=yaml.Loader)
+        except Exception as e:
+            print("Missing or invalid config.yaml file: {}".format(e),
+                  file=sys.stderr)
+            sys.exit(1)
+
+        # Validate the user provided config and use it to override the default
+        # settings
+        self._validate(user_config)
+        self._update(user_config)
+
     @staticmethod
     def _get_config_file(name):
         try:
@@ -154,6 +177,40 @@ class Config:
 
         return os.path.join(config_dir, name)
 
+    @staticmethod
+    def _validate_section(config, section, keys):
+
+        if config.get(section, None) is None:
+            raise KeyError("Section '{}' not found".format(section))
+
+        for key in keys:
+            if config.get(section).get(key, None) is None:
+                raise KeyError("Missing mandatory key '{}.{}'".format(section,
+                                                                      key))
+
+    @staticmethod
+    def _validate(config):
+
+        # verify the [install] section and its mandatory options
+        Config._validate_section(config, "install", ["root_password"])
+
+        # we need to check flavor here, because later validations depend on it
+        flavor = config.get("install").get("flavor", "test")
+        if flavor not in ["test", "jenkins", "gitlab"]:
+            raise ValueError(
+                "Invalid value '{}' for 'install.flavor'".format(flavor)
+            )
+
+        # verify the optional [gitlab] section and its mandatory options
+        if flavor == "gitlab":
+            Config._validate_section(config, "gitlab", ["runner_secret"])
+
+    def _update(self, values):
+        self.values["install"].update(values["install"])
+
+        if values.get("gitlab", None):
+            self.values["gitlab"].update(values["gitlab"])
+
     def get_flavor(self):
         flavor_file = self._get_config_file("flavor")
 
-- 
2.25.3





[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux