From 019bf3aaddfcaf4ac69bb0c178394a4d5d46855d Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Wed, 15 Feb 2017 13:35:01 +0100 Subject: [PATCH] Adding execution of r10k --- lib/webhooks/__init__.py | 2 +- lib/webhooks/r10k.py | 60 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/lib/webhooks/__init__.py b/lib/webhooks/__init__.py index 19fc98f..cfb1f3f 100644 --- a/lib/webhooks/__init__.py +++ b/lib/webhooks/__init__.py @@ -1,6 +1,6 @@ #!/bin/env python3 # -*- coding: utf-8 -*- -__version__ = '0.4.4' +__version__ = '0.4.5' # vim: ts=4 et list diff --git a/lib/webhooks/r10k.py b/lib/webhooks/r10k.py index c522fcc..0f6f0a1 100644 --- a/lib/webhooks/r10k.py +++ b/lib/webhooks/r10k.py @@ -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 # ============================================================================= -- 2.39.5