summaryrefslogtreecommitdiffstats
path: root/gitautodeploy/httpserver.py
diff options
context:
space:
mode:
authorOliver Poignant <oliver@poignant.se>2016-05-07 19:36:37 +0200
committerOliver Poignant <oliver@poignant.se>2016-05-07 19:36:37 +0200
commit703ff62ad8e386e7d9c0e545f60e46fff93ea8e5 (patch)
treecd5865d5cf6305f75f15a5187e267ca74abbc48f /gitautodeploy/httpserver.py
parente5caf489f037c21e2bc43fd7cdad020942e86589 (diff)
downloadGit-Auto-Deploy-703ff62ad8e386e7d9c0e545f60e46fff93ea8e5.zip
Git-Auto-Deploy-703ff62ad8e386e7d9c0e545f60e46fff93ea8e5.tar.gz
Git-Auto-Deploy-703ff62ad8e386e7d9c0e545f60e46fff93ea8e5.tar.bz2
Support for detailed HTTP responses. Improved error handling.
Diffstat (limited to 'gitautodeploy/httpserver.py')
-rw-r--r--gitautodeploy/httpserver.py101
1 files changed, 73 insertions, 28 deletions
diff --git a/gitautodeploy/httpserver.py b/gitautodeploy/httpserver.py
index 813c4f6..29343c3 100644
--- a/gitautodeploy/httpserver.py
+++ b/gitautodeploy/httpserver.py
@@ -14,6 +14,7 @@ class WebhookRequestHandler(BaseHTTPRequestHandler):
"""Invoked on incoming POST requests"""
from threading import Timer
import logging
+ import json
logger = logging.getLogger()
logger.info('Incoming request from %s:%s' % (self.client_address[0], self.client_address[1]))
@@ -30,41 +31,60 @@ class WebhookRequestHandler(BaseHTTPRequestHandler):
ServiceRequestParser = self.figure_out_service_from_request(request_headers, request_body)
except ValueError, e:
- self.send_response(400)
- self.send_header('Content-type', 'text/plain')
- self.end_headers()
+ self.send_error(400, 'Unprocessable request')
logger.warning('Unable to process incoming request from %s:%s' % (self.client_address[0], self.client_address[1]))
return
- self.send_response(200)
- self.send_header('Content-type', 'text/plain')
- self.end_headers()
-
# Unable to identify the source of the request
if not ServiceRequestParser:
+ self.send_error(400, 'Unrecognized service')
logger.error('Unable to find appropriate handler for request. The source service is not supported.')
return
- logger.info('Using %s to handle the request.' % ServiceRequestParser.__name__)
+ # Send HTTP response before the git pull and/or deploy commands?
+ if not 'detailed-response' in self._config or not self._config['detailed-response']:
+ self.send_response(200, 'OK')
+ self.send_header('Content-type', 'text/plain')
+ self.end_headers()
+
+ try:
- # Could be GitHubParser, GitLabParser or other
- repo_configs, ref, action, repo_urls = ServiceRequestParser(self._config).get_repo_params_from_request(request_headers, request_body)
+ logger.info('Using %s to handle the request.' % ServiceRequestParser.__name__)
- logger.info("Event details - ref: %s; action: %s" % (ref or "master", action))
+ # Could be GitHubParser, GitLabParser or other
+ repo_configs, ref, action, repo_urls = ServiceRequestParser(self._config).get_repo_params_from_request(request_headers, request_body)
- #if success:
- # print "Successfullt handled request using %s" % ServiceHandler.__name__
- #else:
- # print "Unable to handle request using %s" % ServiceHandler.__name__
+ logger.info("Event details - ref: %s; action: %s" % (ref or "master", action))
- if len(repo_configs) == 0:
- logger.warning('Unable to find any of the repository URLs in the config: %s' % ', '.join(repo_urls))
- return
+ #if success:
+ # print "Successfullt handled request using %s" % ServiceHandler.__name__
+ #else:
+ # print "Unable to handle request using %s" % ServiceHandler.__name__
+
+ if len(repo_configs) == 0:
+ logger.warning('Unable to find any of the repository URLs in the config: %s' % ', '.join(repo_urls))
+ return
+
+ # Wait one second before we do git pull (why?)
+ #Timer(1.0, self.process_repositories, (repo_configs,
+ # ref,
+ # action, request_body)).start()
+
+ res = self.process_repositories(repo_configs, ref, action, request_body)
+
+ if 'detailed-response' in self._config and self._config['detailed-response']:
+ self.send_response(200, 'OK')
+ self.send_header('Content-type', 'application/json')
+ self.end_headers()
+ self.wfile.write(json.dumps(res))
+ self.wfile.close()
+
+ except Exception, e:
- # Wait one second before we do git pull (why?)
- Timer(1.0, self.process_repositories, (repo_configs,
- ref,
- action, request_body)).start()
+ if 'detailed-response' in self._config and self._config['detailed-response']:
+ self.send_error(500, 'Unable to process request')
+
+ raise e
def log_message(self, format, *args):
"""Overloads the default message logging method to allow messages to
@@ -133,13 +153,17 @@ class WebhookRequestHandler(BaseHTTPRequestHandler):
from wrappers import GitWrapper
from lock import Lock
import json
-
+
logger = logging.getLogger()
data = json.loads(request_body)
+ result = []
+
# Process each matching repository
for repo_config in repo_configs:
+ repo_result = {}
+
try:
# Verify that all filters matches the request (if any filters are specified)
if 'filters' in repo_config:
@@ -161,13 +185,15 @@ class WebhookRequestHandler(BaseHTTPRequestHandler):
# Filter does not match, do not process this repo config
continue
-
+
# In case there is no path configured for the repository, no pull will
# be made.
if not 'path' in repo_config:
- GitWrapper.deploy(repo_config)
+ res = GitWrapper.deploy(repo_config)
+ repo_result['deploy'] = res
+ result.append(repo_result)
continue
-
+
running_lock = Lock(os.path.join(repo_config['path'], 'status_running'))
waiting_lock = Lock(os.path.join(repo_config['path'], 'status_waiting'))
try:
@@ -186,12 +212,27 @@ class WebhookRequestHandler(BaseHTTPRequestHandler):
# Keep on attempting to obtain the status_running lock until we succeed
time.sleep(5)
+ #n = 4
+ #while 0 < n and 0 != GitWrapper.pull(repo_config):
+ # n -= 1
+
n = 4
- while 0 < n and 0 != GitWrapper.pull(repo_config):
+ res = None
+ while n > 0:
+
+ # Attempt to pull up a maximum of 4 times
+ res = GitWrapper.pull(repo_config)
+ repo_result['git pull'] = res
+
+ # Return code indicating success?
+ if res == 0:
+ break
+
n -= 1
if 0 < n:
- GitWrapper.deploy(repo_config)
+ res = GitWrapper.deploy(repo_config)
+ repo_result['deploy'] = res
except Exception as e:
logger.error('Error during \'pull\' or \'deploy\' operation on path: %s' % repo_config['path'])
@@ -206,3 +247,7 @@ class WebhookRequestHandler(BaseHTTPRequestHandler):
# Release the lock if it's ours
if waiting_lock.has_lock():
waiting_lock.release()
+
+ result.append(repo_result)
+
+ return result \ No newline at end of file