]> Frank Brehm's Git Trees - pixelpark/puppetmaster-webhooks.git/commitdiff
Fixing setting of simulation mode, fixing the situation, if no environment directory...
authorFrank Brehm <frank.brehm@pixelpark.com>
Mon, 27 Nov 2017 13:30:59 +0000 (14:30 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Mon, 27 Nov 2017 13:30:59 +0000 (14:30 +0100)
lib/webhooks/__init__.py
lib/webhooks/base_app.py
lib/webhooks/r10k.py

index 4a065910c2ede021e1db713316f6dee761e0f24c..abbf8850f389b0db257a0d1ff40a7ee48a4b82cb 100644 (file)
@@ -1,6 +1,6 @@
 #!/bin/env python3
 # -*- coding: utf-8 -*-
 
-__version__ = '0.6.3'
+__version__ = '0.6.4'
 
 # vim: ts=4 et list
index 92d9581efde87ec0ffe06ecee72dcdb02a03298d..2c6978dba6a66f35f0baeae63c407a6dc1ddd36c 100644 (file)
@@ -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
 
     # -------------------------------------------------------------------------
index eec299a08e1e67a98a7eee5bc4df060d96183f78..5b699808a964b7eeaa03a35e4a1d0a59207f6ca9 100644 (file)
@@ -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()