[libvirt-ci PATCH 06/13] lcitool: Introduce methods to load and validate the TOML 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 TOML 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 | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)

diff --git a/guests/lcitool b/guests/lcitool
index 138b5e2..f08bd98 100755
--- a/guests/lcitool
+++ b/guests/lcitool
@@ -28,6 +28,7 @@ import subprocess
 import sys
 import tempfile
 import textwrap
+import toml
 import yaml
 
 # This is necessary to maintain Python 2.7 compatibility
@@ -133,6 +134,21 @@ class Util:
 
 class Config:
 
+    def __init__(self):
+        path = self._get_config_file("config.toml")
+
+        try:
+            self.dict = toml.load(path)
+            self._validate()
+
+        except Exception as ex:
+            raise Exception(
+                "Missing or invalid config.toml file: {}".format(ex)
+            )
+
+        # fill in default options
+        self._fill_default_options(self.dict)
+
     @staticmethod
     def _get_config_file(name):
         try:
@@ -154,6 +170,42 @@ class Config:
 
         return os.path.join(config_dir, name)
 
+    @staticmethod
+    def _fill_default_options(cfg):
+        flavor = cfg.get("install").get("flavor", "test")
+        cfg["install"]["flavor"] = flavor
+
+        if flavor == "gitlab":
+            url = cfg.get("gitlab").get("gitlab_url", "https://gitlab.com";)
+            cfg["gitlab"]["gitlab_url"] = url
+
+    @staticmethod
+    def _validate_section(cfg, section, *args):
+
+        if cfg.get(section, None) is None:
+            raise KeyError("Section '[{}]' not found".format(section))
+
+        for key in args:
+            if cfg.get(section).get(key, None) is None:
+                raise KeyError("Missing mandatory key '{}.{}'".format(section,
+                                                                      key))
+
+    def _validate(self):
+
+        # verify the [install] section and its mandatory options
+        self._validate_section(self.dict, "install", "root_password")
+
+        # we need to check flavor here, because later validations depend on it
+        flavor = self.dict.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":
+            self._validate_section(self.dict, "gitlab", "gitlab_runner_secret")
+
     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