summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Dittberner <jandd@cacert.org>2018-04-14 21:26:05 +0200
committerJan Dittberner <jandd@cacert.org>2018-04-14 21:26:05 +0200
commit55f251c9f812d42ba9f0586ca5434478a42fb4c1 (patch)
tree2453e9dc2df178e6692aa252e3ceb8c3e01cc665
parent1d085ce6103c9ad64233c805c3fc0d1f01e7d505 (diff)
downloadcacert-puppet-55f251c9f812d42ba9f0586ca5434478a42fb4c1.tar.gz
cacert-puppet-55f251c9f812d42ba9f0586ca5434478a42fb4c1.tar.xz
cacert-puppet-55f251c9f812d42ba9f0586ca5434478a42fb4c1.zip
Add proper logging to git-pull-hook
-rwxr-xr-xsitemodules/profiles/files/puppet_server/git-pull-hook51
-rw-r--r--sitemodules/profiles/templates/puppet_server/git-pull-hook.ini.epp1
2 files changed, 46 insertions, 6 deletions
diff --git a/sitemodules/profiles/files/puppet_server/git-pull-hook b/sitemodules/profiles/files/puppet_server/git-pull-hook
index 32fd1c4..ed34047 100755
--- a/sitemodules/profiles/files/puppet_server/git-pull-hook
+++ b/sitemodules/profiles/files/puppet_server/git-pull-hook
@@ -10,10 +10,12 @@ Configuration is read from /etc/git-pull-hook.ini, ~/.git-pull-hook.ini and
a git-pull-hook.ini in the working directory in that order.
"""
+import logging
+import logging.config
+import os
from configparser import ConfigParser
from http import HTTPStatus
from http.server import HTTPServer, BaseHTTPRequestHandler
-import os
from subprocess import Popen, PIPE
ENV_FOR_GIT = {
@@ -23,9 +25,11 @@ ENV_FOR_GIT = {
TOKENS = []
GIT_DIRECTORY = ""
+LOGGER = None
+
def read_ini():
- global ENV_FOR_GIT, TOKENS, GIT_DIRECTORY
+ global ENV_FOR_GIT, TOKENS, GIT_DIRECTORY, LOGGER
config = ConfigParser()
config.read(['/etc/git-pull-hook.ini',
os.path.expanduser('~/.git-pull-hook.ini'),
@@ -35,6 +39,30 @@ def read_ini():
config['git-pull-hook']['tokens'].split(',')]
GIT_DIRECTORY = config['git-pull-hook']['git_directory']
+ logging.config.dictConfig({
+ 'version': 1,
+ 'formatters': {
+ 'full': {
+ 'format': '%(asctime)s %(levelname)-8s %(message)s',
+ 'datefmt': '%Y-%m-%d %H:%M:%S',
+ }
+ },
+ 'handlers': {
+ 'file': {
+ 'class': 'logging.FileHandler',
+ 'filename': config.get('git-pull-hook', 'logfile'),
+ 'formatter': 'full',
+ },
+ },
+ 'loggers': {
+ 'git-pull-hook': {
+ 'handlers': ['file'],
+ 'level': 'INFO',
+ }
+ }
+ })
+ LOGGER = logging.getLogger('git-pull-hook')
+
class GitHookRequestHandler(BaseHTTPRequestHandler):
"""
@@ -42,6 +70,11 @@ class GitHookRequestHandler(BaseHTTPRequestHandler):
with a known authentication token in an "Authentication" HTTP header.
"""
+ def __init__(self, request, client_address, server):
+ global LOGGER
+ self.log = LOGGER
+ super().__init__(request, client_address, server)
+
def _send_data(self, message):
self.send_header("Content-Type", "text/plain; charset=utf8")
self.end_headers()
@@ -75,14 +108,20 @@ class GitHookRequestHandler(BaseHTTPRequestHandler):
if self.headers['Authentication'] in [token for token in TOKENS]:
self._handle_pull()
else:
- self.send_response(HTTPStatus.UNAUTHORIZED)
- self._send_data('You have to send a valid token in the "Authentication" header.')
+ self.send_error(HTTPStatus.UNAUTHORIZED,
+ 'You have to send a valid token in the "Authentication" header.')
elif self.path == '/health':
self.send_response(HTTPStatus.OK)
self._send_data("I'm healthy!")
else:
- self.send_error(HTTPStatus.BAD_REQUEST)
- self._send_data("You requested something I do not understand.")
+ self.send_error(HTTPStatus.BAD_REQUEST,
+ "You requested something I do not understand.")
+
+ def log_error(self, format, *args):
+ self.log.error("%s - %s" % (self.address_string(), format), *args)
+
+ def log_message(self, format, *args):
+ self.log.info("%s - %s" % (self.address_string(), format), *args)
def run(server_class=HTTPServer, handler_class=GitHookRequestHandler):
diff --git a/sitemodules/profiles/templates/puppet_server/git-pull-hook.ini.epp b/sitemodules/profiles/templates/puppet_server/git-pull-hook.ini.epp
index 8d292b2..2876c16 100644
--- a/sitemodules/profiles/templates/puppet_server/git-pull-hook.ini.epp
+++ b/sitemodules/profiles/templates/puppet_server/git-pull-hook.ini.epp
@@ -6,3 +6,4 @@
ssh_passphrase=<%= $ssh_passphrase %>
tokens=<%= $tokens.join(',') %>
git_directory=<%= $git_directory %>
+logfile=/var/log/git-pull-hook.log