]> Frank Brehm's Git Trees - pixelpark/puppetmaster-webhooks.git/commitdiff
Adding execution of r10k
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 15 Feb 2017 12:35:01 +0000 (13:35 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 15 Feb 2017 12:35:01 +0000 (13:35 +0100)
lib/webhooks/__init__.py
lib/webhooks/r10k.py

index 19fc98ff09c2b153be91169a2b61bb96c167ea87..cfb1f3fd770ea80466de2325f5c78d9f816b7eb9 100644 (file)
@@ -1,6 +1,6 @@
 #!/bin/env python3
 # -*- coding: utf-8 -*-
 
-__version__ = '0.4.4'
+__version__ = '0.4.5'
 
 # vim: ts=4 et list
index c522fcca5d019db9af8cac61ee97701bc7fa07ca..0f6f0a1d25b1e8d11c9f5bdf0165a18a47d184dd 100644 (file)
@@ -16,6 +16,8 @@ import textwrap
 import datetime
 import locale
 import ssl
+import pipes
+import subprocess
 
 from http.client import HTTPSConnection
 
@@ -224,15 +226,73 @@ class R10kHookApp(BaseHookApp):
         locale.setlocale(locale.LC_ALL, self.locale)
         os.environ['LANG'] = self.locale
 
+        return True
+
     # -------------------------------------------------------------------------
     def run(self):
         """Main routine."""
 
         LOG.info("Starting {} ...".format(self.appname))
 
+        if not self.exec_r10k():
+            return
+
         ssl_context = ssl.SSLContext()
         ssl_context.verify_mode = ssl.CERT_NONE
 
+    # -------------------------------------------------------------------------
+    def exec_r10k(self):
+
+        res = True
+
+        r10k_loglevel = 'info'
+        if self.verbose:
+            if self.verbose > 2:
+                r10k_loglevel = 'debug2'
+            elif self.verbose > 1:
+                r10k_loglevel = 'debug1'
+            else:
+                r10k_loglevel = 'debug'
+
+        cmd = []
+        if self.do_sudo:
+            cmd = ['sudo', '-n']
+
+        cmd += [
+            self.r10k_bin, 'deploy', 'environment',
+            self.ref, '--puppetfile', '--verbose', r10k_loglevel
+        ]
+        cmd_str = ' '.join(map(lambda x: pipes.quote(x), cmd))
+        if self.verbose > 2:
+            LOG.debug("Cmd: {}".format(pp(cmd)))
+        LOG.debug("Executing: {}".format(cmd_str))
+
+        if self.simulate:
+            LOG.info("Simulation mode, don't executing {!r}.".format(self.r10k_bin))
+            return res
+
+        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+        (stdoutdata, stderrdata) = proc.communicate()
+        ret_val = proc.wait()
+
+        LOG.debug("Return value: {}".format(ret_val))
+        if stdoutdata:
+            msg = "Output:\n{}".format(to_str(stdoutdata))
+            LOG.debug(msg)
+            self.print_out(msg)
+        else:
+            LOG.debug("No output.")
+        if stderrdata:
+            msg = "Error messages on '{c}':\n{e}".format(c=cmd_str, e=to_str(stderrdata))
+            if ret_val:
+                LOG.warn(msg)
+                self.error_data.append(msg)
+                res = False
+            else:
+                LOG.debug(msg)
+            self.print_out(msg)
+
+        return res
 
 # =============================================================================