]> Frank Brehm's Git Trees - pixelpark/puppetmaster-webhooks.git/commitdiff
Adding search for commad 'r10k' in lib/webhooks/r10k.py
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 15 Feb 2017 11:20:08 +0000 (12:20 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 15 Feb 2017 11:20:08 +0000 (12:20 +0100)
lib/webhooks/__init__.py
lib/webhooks/r10k.py

index ff30aa1e3c1690808682bfe22e149c37c2523e6c..349f40b5d6dbdd25be2a9f236980055009d43028 100644 (file)
@@ -1,6 +1,6 @@
 #!/bin/env python3
 # -*- coding: utf-8 -*-
 
-__version__ = '0.4.2'
+__version__ = '0.4.3'
 
 # vim: ts=4 et list
index 2bea9b10f16dea16c8afb3b5e828821e4e29df12..b113fa74e59ebd499afa04ab14b55f83b7659315 100644 (file)
@@ -41,6 +41,7 @@ class R10kHookApp(BaseHookApp):
         """Constructor."""
 
         self.ignore_projects = []
+        self.r10k_bin = None
         self.description = textwrap.dedent('''\
             Receives push events as JSON-Data and synchronizes
             the local repository and deploys it with r10k.
@@ -51,6 +52,8 @@ class R10kHookApp(BaseHookApp):
         super(R10kHookApp, self).__init__(
             appname=appname, verbose=verbose, version=version)
 
+        self.search_r10k_bin()
+
     # -------------------------------------------------------------------------
     def as_dict(self):
         """
@@ -64,6 +67,63 @@ class R10kHookApp(BaseHookApp):
 
         return res
 
+    # -------------------------------------------------------------------------
+    def search_r10k_bin(self):
+
+        path_list = []
+        cmd = 'r10k'
+
+        search_path = os.environ.get('PATH', None)
+        if not search_path:
+            search_path = os.defpath
+
+        search_path_list = [
+            '/opt/pixelpark/bin',
+            '/www/bin',
+        ]
+
+        search_path_list += search_path.split(os.pathsep)
+
+        default_path = [
+            '/usr/local/sbin',
+            '/usr/local/bin',
+            '/usr/sbin',
+            '/usr/bin',
+            '/sbin',
+            '/bin',
+        ]
+        search_path_list += default_path
+
+        for d in search_path_list:
+            if not os.path.exists(d):
+                continue
+            if not os.path.isdir(d):
+                continue
+            d_abs = os.path.realpath(d)
+            if d_abs not in path_list:
+                path_list.append(d_abs)
+
+        if self.verbose > 1:
+            LOG.debug("Searching for command {c!r} in:\n{p}".format(
+                c=cmd, p=pp(path_list)))
+
+        for d in path_list:
+            p = os.path.join(d, cmd)
+            if os.path.exists(p):
+                if self.verbose > 2:
+                    LOG.debug("Found {!r} ...".format(p))
+                if os.access(p, os.X_OK):
+                    self.r10k_bin = p
+                    break
+                else:
+                    LOG.debug("Command {!r} is not executable.".format(p))
+
+        if self.r10k_bin:
+            LOG.debug("Found {c!r} in {p!r}.".format(c=cmd, p=self.r10k_bin))
+        else:
+            LOG.error("Command {!r} not found.".format(cmd))
+            sys.exit(9)
+
     # -------------------------------------------------------------------------
     def evaluate_config(self, config, yaml_file):