From: Frank Brehm Date: Fri, 27 Jan 2017 11:01:30 +0000 (+0100) Subject: Finished underlying action X-Git-Tag: 0.8.4~48 X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=158274f4dce0de0087baddd05ba93ef698803d50;p=pixelpark%2Fpuppetmaster-webhooks.git Finished underlying action --- diff --git a/deploy.yaml b/deploy.yaml index 2179b9c..ef6c3a9 100644 --- a/deploy.yaml +++ b/deploy.yaml @@ -1,4 +1,5 @@ --- +# May be overridden by /etc/pixelpark/deploy.yaml verbose: 0 do_sudo: true log_dir: '/var/log/webhooks' diff --git a/lib/webhooks/deploy.py b/lib/webhooks/deploy.py index 81dc5b0..357695a 100644 --- a/lib/webhooks/deploy.py +++ b/lib/webhooks/deploy.py @@ -45,6 +45,7 @@ class WebhookDeployApp(object): base_dir = os.path.dirname(cgi_bin_dir) special_chars_re = re.compile(r'[^a-z0-9_\-]', re.IGNORECASE) + dev_re = re.compile(r'^dev') mail_bodies = { 'special_chars': "Received special characters in module name", @@ -92,6 +93,8 @@ class WebhookDeployApp(object): self.ignore_projects = [] + self.error_data = [] + self.default_parent_dir = '/www/data' self.default_email = DEFAULT_EMAIL self.mail_to_addresses = [] @@ -443,22 +446,24 @@ class WebhookDeployApp(object): try: self.json_data = json.loads(self.data) except Exception as e: - LOG.error("Got a {n} reading input data as JSON: {e}".format( - n=e.__class__.__name__, e=e)) - LOG.error("Input data: {!r}".format(self.data)) - sys.exit(0) - - LOG.debug("Got JSON data:\n{}".format(pp(self.json_data))) - - try: - self.perform() - except Exception as e: - LOG.error("Got a {n} performing the deploy: {e}".format( - n=e.__class__.__name__, e=e)) - LOG.error("Traceback:\n{}".format(traceback.format_exc())) + msg = "Got a {n} reading input data as JSON: {e}".format(n=e.__class__.__name__, e=e) + msg += "\nInput data: {!r}".format(self.data) + LOG.error(msg) + error_data.append(msg) + else: - LOG.info("Finished.") - sys.exit(0) + LOG.debug("Got JSON data:\n{}".format(pp(self.json_data))) + + try: + self.perform() + except Exception as e: + msg = "Got a {n} performing the deploy: {e}".format(n=e.__class__.__name__, e=e) + msg += "\n\nTraceback:\n{}".format(traceback.format_exc()) + error_data.append(msg) + LOG.error(msg) + finally: + LOG.info("Finished.") + sys.exit(0) # ------------------------------------------------------------------------- def perform(self): @@ -474,8 +479,9 @@ class WebhookDeployApp(object): return True if self.special_chars_re.search(self.name): - LOG.error(("Project {!r}: " + self.mail_bodies['special_chars']).format( - self.full_name)) + msg = "Project {!r}: ".format(self.full_name) + self.mail_bodies['special_chars'] + LOG.error(msg) + error_data.append(msg) return True committers = [] @@ -529,7 +535,9 @@ class WebhookDeployApp(object): if self.full_name == full_name: return self.deploy(cfg) - LOG.error("Could not find a definition for project {!r}.".format(self.full_name)) + msg = "Could not find a definition for project {!r}.".format(self.full_name) + error_data.append(msg) + LOG.error(msg) return True @@ -538,14 +546,86 @@ class WebhookDeployApp(object): LOG.info("Deploying Hiera working directory ...") - return True + ns = 'puppet' + pname = 'hiera' + full_name = ns + '/' + pname + parent_dir = '/www/data/puppet-hiera' + if 'parent_dir' in cfg and cfg['parent_dir']: + parent_dir = cfg['parent_dir'] + workdir = pname + if 'workdir' in cfg and cfg['workdir']: + workdir = cfg['workdir'] + + full_path = os.path.join(parent_dir, workdir) + + LOG.info("Deploying working directory {!r} for Hiera ...".format(full_path)) + + if not os.access(parent_dir, os.F_OK): + msg = "Parent directory {!r} for Hiera does not exists.".format(parent_dir) + LOG.error(msg) + error_data.append(msg) + return True + + if not os.path.isdir(parent_dir): + msg = "Path of parent directory {!r} for Hiera is not a directory.".format(parent_dir) + LOG.error(msg) + error_data.append(msg) + return True + + return self.ensure_workingdir(parent_dir, workdir) # ------------------------------------------------------------------------- def deploy_puppet_modules(self): LOG.info("Deploying puppet modules working directory ...") - return True + ns = 'puppet' + pname = cfg['name'] + full_name = ns + '/' + pname + parent_dir = self.default_parent_dir + if 'parent_dir' in cfg and cfg['parent_dir']: + parent_dir = cfg['parent_dir'] + + if not os.access(parent_dir, os.F_OK): + msg = "Parent directory {d!r} of project {p!r} does not exists.".format( + d=parent_dir, p=full_name) + LOG.error(msg) + error_data.append(msg) + return True + + if not os.path.isdir(parent_dir): + msg = ("Path for parent directory {d!r} for project {p!r} " + "is not a directory.").format(d=parent_dir, p=full_name) + LOG.error(msg) + error_data.append(msg) + return True + + env_branch = 'undefined' + if self.dev_re.match(self.ref): + env_branch = 'development' + elif self.ref == 'master': + env_branch = 'test' + full_path_branch = os.path.join(parent_dir, env_branch) + if not os.access(full_path_branch, os.F_OK): + msg = "Branch directory {d!r} of project {p!r} does not exists.".format( + d=full_path_branch, p=full_name) + LOG.error(msg) + error_data.append(msg) + return True + + modules_dir = os.path.join(full_path_branch, 'modules') + if not os.access(modules_dir, os.F_OK): + msg = "Modules directory {d!r} of project {p!r} does not exists.".format( + d=modules_dir, p=full_name) + LOG.error(msg) + error_data.append(msg) + return True + + branc2clone = None + if env_branch =='test': + branc2clone = 'master' + + return self.ensure_workingdir(parent_dir=modules_dir, workdir=pname, branch=branc2clone) # ------------------------------------------------------------------------- def deploy(self, cfg): @@ -568,14 +648,18 @@ class WebhookDeployApp(object): f=full_path, p=full_name)) if not os.access(parent_dir, os.F_OK): - LOG.error("Parent directory {d!r} for project {p!r} does not exists.".format( - d=parent_dir, p=full_name)) + msg = "Parent directory {d!r} of project {p!r} does not exists.".format( + d=parent_dir, p=full_name) + LOG.error(msg) + error_data.append(msg) return True if not os.path.isdir(parent_dir): - LOG.error(( + msg = ( "Path for parent directory {d!r} for project {p!r} " - "is not a directory.").format(d=parent_dir, p=full_name)) + "is not a directory.").format(d=parent_dir, p=full_name) + LOG.error(msg) + error_data.append(msg) return True return self.ensure_workingdir(parent_dir, workdir, branch) @@ -583,37 +667,43 @@ class WebhookDeployApp(object): # ------------------------------------------------------------------------- def ensure_workingdir(self, parent_dir, workdir, branch=None): - os.chdir(parent_dir) - cmd = [] - if self.do_sudo: - cmd = ['sudo', '-n'] - if os.access(workdir, os.F_OK): - os.chdir(workdir) - cmd += ['git', 'pull'] - else: - cmd += ['git', 'clone', self.git_ssh_url, workdir] - if branch: - cmd += ['-b', branch] - if self.verbose > 2: - LOG.debug("Cmd: {}".format(pp(cmd))) - cmd_str = ' '.join(map(lambda x: pipes.quote(x), cmd)) - LOG.debug("Executing: {}".format(cmd_str)) - - git = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - (stdoutdata, stderrdata) = git.communicate() - ret_val = git.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)) - LOG.warn(msg) - self.print_out(msg) + cur_dir = os.getcwd() + + try: + os.chdir(parent_dir) + cmd = [] + if self.do_sudo: + cmd = ['sudo', '-n'] + if os.access(workdir, os.F_OK): + os.chdir(workdir) + cmd += ['git', 'pull'] + else: + cmd += ['git', 'clone', self.git_ssh_url, workdir] + if branch: + cmd += ['-b', branch] + if self.verbose > 2: + LOG.debug("Cmd: {}".format(pp(cmd))) + cmd_str = ' '.join(map(lambda x: pipes.quote(x), cmd)) + LOG.debug("Executing: {}".format(cmd_str)) + + git = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + (stdoutdata, stderrdata) = git.communicate() + ret_val = git.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)) + LOG.warn(msg) + error_data.append(msg) + self.print_out(msg) + finally: + os.chdir(cur_dir) # =============================================================================