diff options
author | Jan Dittberner <jandd@cacert.org> | 2019-07-18 22:48:23 +0200 |
---|---|---|
committer | Jan Dittberner <jandd@cacert.org> | 2019-07-18 22:48:23 +0200 |
commit | fd8303b2cadfe90f6e58b03fc17fc290f17f5066 (patch) | |
tree | d85117eb484abb75914fea7d0b1b5874b71a300a | |
parent | 09c942d512e679a824969e18753822c7853b15ab (diff) | |
download | cacert-puppet-fd8303b2cadfe90f6e58b03fc17fc290f17f5066.tar.gz cacert-puppet-fd8303b2cadfe90f6e58b03fc17fc290f17f5066.tar.xz cacert-puppet-fd8303b2cadfe90f6e58b03fc17fc290f17f5066.zip |
Try to improve robustness of git-pull-hook
- use subprocess.run and handle CalledProcessError
-rwxr-xr-x | sitemodules/profiles/files/puppet_server/git-pull-hook | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/sitemodules/profiles/files/puppet_server/git-pull-hook b/sitemodules/profiles/files/puppet_server/git-pull-hook index 6c92b24..a815313 100755 --- a/sitemodules/profiles/files/puppet_server/git-pull-hook +++ b/sitemodules/profiles/files/puppet_server/git-pull-hook @@ -13,10 +13,10 @@ a git-pull-hook.ini in the working directory in that order. import logging import logging.config import os +import subprocess from configparser import ConfigParser from http import HTTPStatus from http.server import HTTPServer, BaseHTTPRequestHandler -from subprocess import Popen, PIPE ENV_FOR_GIT = {"PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"} @@ -80,35 +80,39 @@ class GitHookRequestHandler(BaseHTTPRequestHandler): def _handle_pull(self): try: - git_proc = Popen( + git_proc = subprocess.run( ["sshpass", "-e", "-P", "passphrase", "git", "pull"], env=ENV_FOR_GIT, cwd=GIT_DIRECTORY, - stdout=PIPE, - stderr=PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + check=True, + text=True, ) - stdout, stderr = git_proc.communicate() - for line in stderr.decode("UTF-8").splitlines(): - self.log_error("git: %s", line) - for line in stdout.decode("UTF-8").splitlines(): + for line in git_proc.stdout.splitlines(): + self.log_message("git: %s", line) + except subprocess.CalledProcessError as e: + self.log_error( + "Could not pull changes for %s: %s", GIT_DIRECTORY, e.returncode + ) + for line in e.stdout.splitlines(): self.log_message("git: %s", line) - except Exception as e: - self.log_error("Could not pull changes for %s: %s", GIT_DIRECTORY, e) self._send_data("Error updating the repository.") try: - r10k_proc = Popen( + r10k_proc = subprocess.run( ["/opt/puppetlabs/puppet/bin/r10k", "puppetfile", "install"], cwd=GIT_DIRECTORY, - stdout=PIPE, - stderr=PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + check=True, + text=True, ) - stdout, stderr = git_proc.communicate() - for line in stderr.decode("UTF-8").splitlines(): - self.log_error("r10k: %s", line) - for line in stdout.decode("UTF-8").splitlines(): + for line in r10k_proc.stdout.splitlines(): + self.log_message("r10k: %s", line) + except subprocess.CalledProcessError as e: + self.log_error("Could not update modules from Puppetfile: %s", e.returncode) + for line in e.stdout.splitlines(): self.log_message("r10k: %s", line) - except Exception as e: - self.log_error("Could not update modules from Puppetfile: %s", e) self._send_data("Error updating modules.") self.send_response(HTTPStatus.OK) |