diff options
author | Oliver Poignant <oliver@poignant.se> | 2017-01-08 01:06:24 +0100 |
---|---|---|
committer | Oliver Poignant <oliver@poignant.se> | 2017-01-08 01:06:24 +0100 |
commit | 324c3518d4b98da5bb660c62fa1503c47139c088 (patch) | |
tree | ab97540187b22d07a5df35a3b7da07b32dad52e6 /gitautodeploy/webhook.py | |
parent | 98b6a63e343f2a6a1888f8373845b0b1bd3c89d4 (diff) | |
download | Git-Auto-Deploy-324c3518d4b98da5bb660c62fa1503c47139c088.zip Git-Auto-Deploy-324c3518d4b98da5bb660c62fa1503c47139c088.tar.gz Git-Auto-Deploy-324c3518d4b98da5bb660c62fa1503c47139c088.tar.bz2 |
Warning when running as root. Improved logging.
Diffstat (limited to 'gitautodeploy/webhook.py')
-rw-r--r-- | gitautodeploy/webhook.py | 162 |
1 files changed, 77 insertions, 85 deletions
diff --git a/gitautodeploy/webhook.py b/gitautodeploy/webhook.py index 5c027e4..752717e 100644 --- a/gitautodeploy/webhook.py +++ b/gitautodeploy/webhook.py @@ -51,105 +51,97 @@ class WebbhookRequestProcessor(object): action.log_error("Unable to recognize request origin. Don't know how to handle the request.") return - def execute_webhook(self, repo_configs, request_headers, request_body, action): + def execute_webhook(self, repo_config, event_store): """Verify that the suggested repositories has matching settings and issue git pull and/or deploy commands.""" import os import time - import logging from .wrappers import GitWrapper + from .events import DeployEvent from .lock import Lock import json - logger = logging.getLogger() - payload = json.loads(request_body) - - result = [] - - # Process each matching repository - for repo_config in repo_configs: - - repo_result = {} - - # In case there is no path configured for the repository, no pull will - # be made. - if 'path' not in repo_config: + event = DeployEvent(repo_config) + event_store.register_action(event) + event.set_waiting(True) + event.log_info("Running deploy commands") + + # In case there is no path configured for the repository, no pull will + # be made. + if 'path' not in repo_config: + res = GitWrapper.deploy(repo_config) + event.log_info("%s" % res) + event.set_waiting(False) + event.set_success(True) + return + + # If the path does not exist, a warning will be raised and no pull or + # deploy will be made. + if not os.path.isdir(repo_config['path']): + event.log_error("The repository '%s' does not exist locally. Make sure it was pulled properly without errors by reviewing the log." % repo_config['path']) + event.set_waiting(False) + event.set_success(False) + return + + # If the path is not writable, a warning will be raised and no pull or + # deploy will be made. + if not os.access(repo_config['path'], os.W_OK): + event.log_error("The path '%s' is not writable. Make sure that GAD has write access to that path." % repo_config['path']) + event.set_waiting(False) + event.set_success(False) + return + + running_lock = Lock(os.path.join(repo_config['path'], 'status_running')) + waiting_lock = Lock(os.path.join(repo_config['path'], 'status_waiting')) + try: + + # Attempt to obtain the status_running lock + while not running_lock.obtain(): + + # If we're unable, try once to obtain the status_waiting lock + if not waiting_lock.has_lock() and not waiting_lock.obtain(): + event.log_error("Unable to obtain the status_running lock nor the status_waiting lock. Another process is already waiting, so we'll ignore the request.") + + # If we're unable to obtain the waiting lock, ignore the request + break + + # Keep on attempting to obtain the status_running lock until we succeed + time.sleep(5) + + n = 4 + res = None + while n > 0: + + # Attempt to pull up a maximum of 4 times + res = GitWrapper.pull(repo_config) + + # Return code indicating success? + if res == 0: + break + + n -= 1 + + if 0 < n: res = GitWrapper.deploy(repo_config) - repo_result['deploy'] = res - result.append(repo_result) - continue - - # If the path does not exist, a warning will be raised and no pull or - # deploy will be made. - if not os.path.isdir(repo_config['path']): - action.log_error("The repository '%s' does not exist locally. Make sure it was pulled properly without errors by reviewing the log." % repo_config['path']) - result.append(repo_result) - continue - - # If the path is not writable, a warning will be raised and no pull or - # deploy will be made. - if not os.access(repo_config['path'], os.W_OK): - action.log_error("The path '%s' is not writable. Make sure that GAD has write access to that path." % repo_config['path']) - 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: - - # Attempt to obtain the status_running lock - while not running_lock.obtain(): - - # If we're unable, try once to obtain the status_waiting lock - if not waiting_lock.has_lock() and not waiting_lock.obtain(): - action.log_error("Unable to obtain the status_running lock nor the status_waiting lock. Another process is already waiting, so we'll ignore the request.") - - # If we're unable to obtain the waiting lock, ignore the request - break - - # Keep on attempting to obtain the status_running lock until we succeed - time.sleep(5) - - n = 4 - 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: - 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']) - # logger.error(e) - # raise e - - finally: - # Release the lock if it's ours - if running_lock.has_lock(): - running_lock.release() + #except Exception as e: + # logger.error('Error during \'pull\' or \'deploy\' operation on path: %s' % repo_config['path']) + # logger.error(e) + # raise e - # Release the lock if it's ours - if waiting_lock.has_lock(): - waiting_lock.release() + finally: - result.append(repo_result) + # Release the lock if it's ours + if running_lock.has_lock(): + running_lock.release() - action.log_info("Deploy commands were executed") - action.set_waiting(False) - action.set_success(True) + # Release the lock if it's ours + if waiting_lock.has_lock(): + waiting_lock.release() - return result + event.log_info("Deploy commands were executed") + event.set_waiting(False) + event.set_success(True) class WebhookRequestFilter(object): |