[libvirt] [jenkins-ci PATCH 1/8] lcitool: Make the script location-independent

Andrea Bolognani abologna at redhat.com
Thu Jul 19 16:32:01 UTC 2018


Up until now, the only way to run lcitool has been from
the same directory it lives. After this patch, the script
is able to figure out its own location and adjust all
paths accordingly.

Suggested-by: Katerina Koukiou <kkoukiou at redhat.com>
Signed-off-by: Andrea Bolognani <abologna at redhat.com>
---
 guests/lcitool | 61 +++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 48 insertions(+), 13 deletions(-)

diff --git a/guests/lcitool b/guests/lcitool
index 22b08dd..5526a27 100755
--- a/guests/lcitool
+++ b/guests/lcitool
@@ -43,6 +43,10 @@ class Error(Exception):
 
 class Util:
 
+    @staticmethod
+    def get_base():
+        return os.path.dirname(os.path.abspath(__file__))
+
     @staticmethod
     def mksalt():
         alphabeth = string.ascii_letters + string.digits
@@ -83,8 +87,8 @@ class Config:
         try:
             config_dir = os.environ["XDG_CONFIG_HOME"]
         except KeyError:
-            config_dir = os.path.join(os.environ["HOME"], ".config/")
-        config_dir = os.path.join(config_dir, "lcitool/")
+            config_dir = os.path.join(os.environ["HOME"], ".config")
+        config_dir = os.path.join(config_dir, "lcitool")
 
         # Create the directory if it doesn't already exist
         if not os.path.exists(config_dir):
@@ -179,13 +183,18 @@ class Config:
 class Inventory:
 
     def __init__(self):
+        base = Util.get_base()
+        ansible_cfg_path = os.path.join(base, "ansible.cfg")
+
         try:
             parser = configparser.SafeConfigParser()
-            parser.read("./ansible.cfg")
+            parser.read(ansible_cfg_path)
             inventory_path = parser.get("defaults", "inventory")
         except Exception:
             raise Error("Can't find inventory location in ansible.cfg")
 
+        inventory_path = os.path.join(base, inventory_path)
+
         self._facts = {}
         try:
             # We can only deal with trivial inventories, but that's
@@ -217,12 +226,19 @@ class Inventory:
                 facts[fact] = some_facts[fact]
 
     def _read_all_facts(self, host):
+        base = Util.get_base()
+
+        sources = [
+            os.path.join(base, "group_vars", "all"),
+            os.path.join(base, "host_vars", host),
+        ]
+
         facts = {}
 
         # We load from group_vars/ first and host_vars/ second, sorting
         # files alphabetically; doing so should result in our view of
         # the facts matching Ansible's
-        for source in ["./group_vars/all/", "./host_vars/{}/".format(host)]:
+        for source in sources:
             for item in sorted(os.listdir(source)):
                 yaml_path = os.path.join(source, item)
                 if not os.path.isfile(yaml_path):
@@ -243,15 +259,20 @@ class Inventory:
 class Projects:
 
     def __init__(self):
+        base = Util.get_base()
+
+        mappings_path = os.path.join(base, "vars", "mappings.yml")
+
         try:
-            with open("./vars/mappings.yml", "r") as infile:
+            with open(mappings_path, "r") as infile:
                 mappings = yaml.load(infile)
                 self._mappings = mappings["mappings"]
         except Exception:
             raise Error("Can't load mappings")
 
+        source = os.path.join(base, "vars", "projects")
+
         self._packages = {}
-        source = "./vars/projects/"
         for item in os.listdir(source):
             yaml_path = os.path.join(source, item)
             if not os.path.isfile(yaml_path):
@@ -338,6 +359,8 @@ class Application:
             print(project)
 
     def _action_install(self, hosts, _projects):
+        base = Util.get_base()
+
         flavor = self._config.get_flavor()
 
         for host in self._inventory.expand_pattern(hosts):
@@ -358,6 +381,8 @@ class Application:
                 facts["install_network"],
             )
 
+            install_config = os.path.join(base, facts["install_config"])
+
             # preseed files must use a well-known name to be picked up by
             # d-i; for kickstart files, we can use whatever name we please
             # but we need to point anaconda in the right direction through
@@ -381,7 +406,7 @@ class Application:
                 "--graphics", "none",
                 "--console", "pty",
                 "--sound", "none",
-                "--initrd-inject", facts["install_config"],
+                "--initrd-inject", install_config,
                 "--extra-args", extra_arg,
                 "--wait", "0",
             ]
@@ -396,28 +421,38 @@ class Application:
                 raise Error("Failed to install '{}'".format(host))
 
     def _action_update(self, hosts, _projects):
+        base = Util.get_base()
+
         flavor = self._config.get_flavor()
         vault_pass_file = self._config.get_vault_password_file()
         root_pass_file = self._config.get_root_password_file()
 
         ansible_hosts = ",".join(self._inventory.expand_pattern(hosts))
 
+        ansible_cfg_path = os.path.join(base, "ansible.cfg")
+        playbook_path = os.path.join(base, "site.yml")
+
         extra_vars = "flavor={} root_password_file={}".format(
             flavor,
             root_pass_file,
         )
 
-        cmd = ["ansible-playbook"]
+        cmd = [
+            "ansible-playbook",
+            "--limit", ansible_hosts,
+            "--extra-vars", extra_vars,
+        ]
 
         # Provide the vault password if available
         if vault_pass_file is not None:
             cmd += ["--vault-password-file", vault_pass_file]
 
-        cmd += [
-            "--limit", ansible_hosts,
-            "--extra-vars", extra_vars,
-            "./site.yml",
-        ]
+        cmd += [playbook_path]
+
+        # We need to point Ansible to the correct configuration file,
+        # and for some reason this has to be done using the environment
+        # rather than through the command line
+        os.environ["ANSIBLE_CONFIG"] = ansible_cfg_path
 
         try:
             subprocess.check_call(cmd)
-- 
2.17.1




More information about the libvir-list mailing list