diff options
Diffstat (limited to 'gitautodeploy')
-rw-r--r-- | gitautodeploy/cli/config.py | 4 | ||||
-rw-r--r-- | gitautodeploy/gitautodeploy.py | 3 | ||||
-rw-r--r-- | gitautodeploy/httpserver.py | 44 |
3 files changed, 44 insertions, 7 deletions
diff --git a/gitautodeploy/cli/config.py b/gitautodeploy/cli/config.py index bdbe12f..d4d98fb 100644 --- a/gitautodeploy/cli/config.py +++ b/gitautodeploy/cli/config.py @@ -23,6 +23,10 @@ def get_config_defaults(): # response. config['detailed-response'] = True + # Log incoming webhook requests in a way they can be used as test cases + config['log-test-case'] = False + config['log-test-case-dir'] = None + return config def get_config_from_environment(): diff --git a/gitautodeploy/gitautodeploy.py b/gitautodeploy/gitautodeploy.py index 98ebeb1..3b7660e 100644 --- a/gitautodeploy/gitautodeploy.py +++ b/gitautodeploy/gitautodeploy.py @@ -28,6 +28,7 @@ class GitAutoDeploy(object): @staticmethod def debug_diagnosis(port): + """Display information about what process is using the specified port.""" import logging logger = logging.getLogger() @@ -43,6 +44,7 @@ class GitAutoDeploy(object): @staticmethod def get_pid_on_port(port): + """Determine what process (PID) is using a specific port.""" import os with open("/proc/net/tcp", 'r') as f: @@ -137,6 +139,7 @@ class GitAutoDeploy(object): logger.error('Could not find regexp match in path: %s' % url) def kill_conflicting_processes(self): + """Attempt to kill any process already using the configured port.""" import os import logging import signal diff --git a/gitautodeploy/httpserver.py b/gitautodeploy/httpserver.py index 29343c3..90fc72c 100644 --- a/gitautodeploy/httpserver.py +++ b/gitautodeploy/httpserver.py @@ -53,7 +53,6 @@ class WebhookRequestHandler(BaseHTTPRequestHandler): # 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("Event details - ref: %s; action: %s" % (ref or "master", action)) #if success: @@ -62,14 +61,11 @@ class WebhookRequestHandler(BaseHTTPRequestHandler): # print "Unable to handle request using %s" % ServiceHandler.__name__ if len(repo_configs) == 0: + self.send_error(400, 'Bad request') 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() - + # Make git pulls and trigger deploy commands res = self.process_repositories(repo_configs, ref, action, request_body) if 'detailed-response' in self._config and self._config['detailed-response']: @@ -86,6 +82,20 @@ class WebhookRequestHandler(BaseHTTPRequestHandler): raise e + # Save the request as a test case + if 'log-test-case' in self._config and self._config['log-test-case']: + self.save_test_case({ + 'headers': dict(self.headers), + 'payload': json.loads(request_body), + 'config': { + 'url': repo_configs[0]['url'], + 'branch': repo_configs[0]['branch'], + 'remote': repo_configs[0]['remote'], + 'deploy': 'echo test!' + }, + 'expected': [{'deploy': 0}] + }) + def log_message(self, format, *args): """Overloads the default message logging method to allow messages to go through our custom logger instead.""" @@ -250,4 +260,24 @@ class WebhookRequestHandler(BaseHTTPRequestHandler): result.append(repo_result) - return result
\ No newline at end of file + return result + + def save_test_case(self, test_case): + """Log request information in a way it can be used as a test case.""" + import time + import json + import os + + # Mask some header values + masked_headers = ['x-github-delivery', 'x-hub-signature'] + for key in test_case['headers']: + if key in masked_headers: + test_case['headers'][key] = 'xxx' + + target = '%s-%s.tc.json' % (self.client_address[0], time.strftime("%Y%m%d%H%M%S")) + if 'log-test-case-dir' in self._config and self._config['log-test-case-dir']: + target = os.path.join(self._config['log-test-case-dir'], target) + + file = open(target, 'w') + file.write(json.dumps(test_case, sort_keys=True, indent=4)) + file.close()
\ No newline at end of file |