From 003d254895c2d5b7355e93ff7480c48b63c07030 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Mon, 27 Nov 2017 14:30:59 +0100 Subject: [PATCH] Fixing setting of simulation mode, fixing the situation, if no environment directory exists --- lib/webhooks/__init__.py | 2 +- lib/webhooks/base_app.py | 22 +++++++++++++++++++++- lib/webhooks/r10k.py | 18 +++++++++++++++++- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/lib/webhooks/__init__.py b/lib/webhooks/__init__.py index 4a06591..abbf885 100644 --- a/lib/webhooks/__init__.py +++ b/lib/webhooks/__init__.py @@ -1,6 +1,6 @@ #!/bin/env python3 # -*- coding: utf-8 -*- -__version__ = '0.6.3' +__version__ = '0.6.4' # vim: ts=4 et list diff --git a/lib/webhooks/base_app.py b/lib/webhooks/base_app.py index 92d9581..2c6978d 100644 --- a/lib/webhooks/base_app.py +++ b/lib/webhooks/base_app.py @@ -46,6 +46,10 @@ class BaseHookApp(object): cgi_bin_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) base_dir = os.path.dirname(cgi_bin_dir) + puppetlabs_cfg_dir = os.sep + os.path.join('etc', 'puppetlabs') + puppet_envs_dir = os.path.join(puppetlabs_cfg_dir, 'code', 'environments') + fileserver_dir = os.path.join(puppetlabs_cfg_dir, 'code', 'fileserver') + special_chars_re = re.compile(r'[^a-z0-9_\-]', re.IGNORECASE) dev_re = re.compile(r'^dev') @@ -182,6 +186,15 @@ class BaseHookApp(object): """The logfile for STDERR of this application.""" return os.path.join(self.log_directory, self.appname + '.error.log') + # ----------------------------------------------------------- + @property + def env_dir(self): + """The directory containing the current r10k environment.""" + cur_env = getattr(self, 'ref', None) + if cur_env is None: + return None + return os.path.join(self.puppet_envs_dir, cur_env) + # ------------------------------------------------------------------------- def __str__(self): """ @@ -217,6 +230,10 @@ class BaseHookApp(object): res['log_directory'] = self.log_directory res['error_logfile'] = self.error_logfile res['logfile'] = self.logfile + res['puppetlabs_cfg_dir'] = self.puppetlabs_cfg_dir + res['puppet_envs_dir'] = self.puppet_envs_dir + res['env_dir'] = self.env_dir + res['fileserver_dir'] = self.fileserver_dir return res @@ -276,6 +293,7 @@ class BaseHookApp(object): os.environ['REQUEST_METHOD'] = 'GET' if self.cmdline_args.simulate: + sys.stderr.write("\nSimulation mode - nothing is really done.\n\n") self.simulate = True # ------------------------------------------------------------------------- @@ -406,7 +424,7 @@ class BaseHookApp(object): v=config['verbose'], f=yaml_file, e=e) LOG.warn(msg) - if 'simulate' in config: + if 'simulate' in config and not self.simulate: self.simulate = config['simulate'] if 'do_sudo' in config: @@ -620,6 +638,8 @@ class BaseHookApp(object): LOG.info("Executing webhook {a!r} for Git SSH URL {u!r}, branch {b!r}.".format( a=self.appname, u=self.git_ssh_url, b=self.ref)) + LOG.debug("Environment directory should be {!r}.".format(self.env_dir)) + return True # ------------------------------------------------------------------------- diff --git a/lib/webhooks/r10k.py b/lib/webhooks/r10k.py index eec299a..5b69980 100644 --- a/lib/webhooks/r10k.py +++ b/lib/webhooks/r10k.py @@ -16,6 +16,7 @@ import locale import pipes import subprocess import urllib.parse +import time # Third party modules @@ -170,6 +171,19 @@ class R10kHookApp(BaseHookApp): LOG.warn("Executing {!r} was not successful.".format(self.r10k_bin)) return + return self.del_env_cache() + + # ------------------------------------------------------------------------- + def del_env_cache(self): + + if self.env_dir is None: + raise RuntimeError("No environment defined.") + + LOG.debug("Searching for environment directory {!r} ...".format(self.env_dir)) + if not os.path.isdir(self.env_dir): + LOG.info("Environment directory {!r} does not exists.".format(self.env_dir)) + return + key_file = os.path.join( self.puppetmaster_ssl_dir, 'private_keys', self.puppetmaster_host + '.pem') cert_file = os.path.join( @@ -259,9 +273,11 @@ class R10kHookApp(BaseHookApp): LOG.info("Executing: {}".format(cmd_str)) if self.simulate: - LOG.info("Simulation mode, don't executing {!r}.".format(self.r10k_bin)) + LOG.warn("Simulation mode, don't executing {!r}.".format(self.r10k_bin)) + time.sleep(3) return res + LOG.debug("Really executing ...") proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdoutdata, stderrdata) = proc.communicate() ret_val = proc.wait() -- 2.39.5