More deploy stuff
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
from .gitautodeploy import *
|
||||
from .cli import *
|
||||
from .parsers import *
|
||||
@@ -0,0 +1,17 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
if __package__ != 'gitautodeploy':
|
||||
import sys
|
||||
print("Critical - GAD must be started as a python module, for example using python -m gitautodeploy")
|
||||
sys.exit()
|
||||
|
||||
from gitautodeploy import main
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
|
||||
main()
|
||||
|
||||
else:
|
||||
from gitautodeploy import main
|
||||
@@ -0,0 +1 @@
|
||||
from .config import *
|
||||
@@ -0,0 +1,503 @@
|
||||
class ConfigFileNotFoundException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class ConfigFileInvalidException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def get_config_defaults():
|
||||
"""Get the default configuration values."""
|
||||
|
||||
config = {}
|
||||
|
||||
# Supress console output
|
||||
config['quiet'] = False
|
||||
|
||||
# Run in daemon mode
|
||||
config['daemon-mode'] = False
|
||||
|
||||
# File containing additional config options
|
||||
config['config'] = None
|
||||
|
||||
# File to store a copy of the console output
|
||||
config['log-file'] = None
|
||||
|
||||
# File to store the process id (pid)
|
||||
config['pid-file'] = '~/.gitautodeploy.pid'
|
||||
|
||||
# HTTP server options
|
||||
config['http-enabled'] = True
|
||||
config['http-host'] = '0.0.0.0'
|
||||
config['http-port'] = 8001
|
||||
|
||||
# HTTPS server options
|
||||
config['https-enabled'] = True
|
||||
config['https-host'] = '0.0.0.0'
|
||||
config['https-port'] = 8002
|
||||
|
||||
# Web socket server options (used by web UI for real time updates)
|
||||
config['wss-enabled'] = False # Disabled by default until authentication is in place
|
||||
config['wss-host'] = '0.0.0.0'
|
||||
config['wss-port'] = 8003
|
||||
|
||||
# TLS/SSL cert (necessary for HTTPS and web socket server to work)
|
||||
config['ssl-key'] = None # If specified, holds the private key
|
||||
config['ssl-cert'] = '~/cert.pem' # Holds the public key or both the private and public keys
|
||||
|
||||
# Web user interface options
|
||||
config['web-ui-enabled'] = False # Disabled by default until authentication is in place
|
||||
config['web-ui-username'] = None
|
||||
config['web-ui-password'] = None
|
||||
config['web-ui-whitelist'] = ['127.0.0.1']
|
||||
config['web-ui-require-https'] = True
|
||||
config['web-ui-auth-enabled'] = True
|
||||
config['web-ui-prevent-root'] = True
|
||||
|
||||
# Record all log levels by default
|
||||
config['log-level'] = 'NOTSET'
|
||||
|
||||
# Other options
|
||||
config['intercept-stdout'] = True
|
||||
config['ssh-keyscan'] = False
|
||||
config['allow-root-user'] = False
|
||||
|
||||
# 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 rename_legacy_attribute_names(config):
|
||||
import logging
|
||||
logger = logging.getLogger()
|
||||
|
||||
rewrite_map = {
|
||||
'ssl': 'https-enabled',
|
||||
'ssl-pem-file': 'ssl-cert',
|
||||
'host': 'http-host',
|
||||
'port': 'http-port',
|
||||
'pidfilepath': 'pid-file',
|
||||
'logfilepath': 'log-file'
|
||||
}
|
||||
|
||||
for item in rewrite_map.items():
|
||||
old_name, new_name = item
|
||||
if old_name in config:
|
||||
config[new_name] = config[old_name]
|
||||
del config[old_name]
|
||||
print("Config option '%s' is deprecated. Please use '%s' instead." % (old_name, new_name))
|
||||
|
||||
return config
|
||||
|
||||
|
||||
def get_config_from_environment():
|
||||
"""Get configuration values provided as environment variables."""
|
||||
import os
|
||||
|
||||
config = {}
|
||||
|
||||
if 'GAD_QUIET' in os.environ:
|
||||
config['quiet'] = True
|
||||
|
||||
if 'GAD_DAEMON_MODE' in os.environ:
|
||||
config['daemon-mode'] = True
|
||||
|
||||
if 'GAD_CONFIG' in os.environ:
|
||||
config['config'] = os.environ['GAD_CONFIG']
|
||||
|
||||
if 'GAD_SSH_KEYSCAN' in os.environ:
|
||||
config['ssh-keyscan'] = True
|
||||
|
||||
if 'GAD_SSL_KEY' in os.environ:
|
||||
config['ssl-key'] = os.environ['GAD_SSL_KEY']
|
||||
|
||||
if 'GAD_SSL_CERT' in os.environ:
|
||||
config['ssl-cert'] = os.environ['GAD_SSL_CERT']
|
||||
|
||||
if 'GAD_PID_FILE' in os.environ:
|
||||
config['pid-file'] = os.environ['GAD_PID_FILE']
|
||||
|
||||
if 'GAD_LOG_FILE' in os.environ:
|
||||
config['log-file'] = os.environ['GAD_LOG_FILE']
|
||||
|
||||
if 'GAD_HOST' in os.environ:
|
||||
config['http-host'] = os.environ['GAD_HOST']
|
||||
|
||||
if 'GAD_HTTP_HOST' in os.environ:
|
||||
config['http-host'] = os.environ['GAD_HTTP_HOST']
|
||||
|
||||
if 'GAD_HTTPS_HOST' in os.environ:
|
||||
config['https-host'] = os.environ['GAD_HTTPS_HOST']
|
||||
|
||||
if 'GAD_PORT' in os.environ:
|
||||
config['http-port'] = int(os.environ['GAD_PORT'])
|
||||
|
||||
if 'GAD_HTTP_PORT' in os.environ:
|
||||
config['http-port'] = int(os.environ['GAD_HTTP_PORT'])
|
||||
|
||||
if 'GAD_HTTPS_PORT' in os.environ:
|
||||
config['https-port'] = int(os.environ['GAD_HTTPS_PORT'])
|
||||
|
||||
return config
|
||||
|
||||
|
||||
def get_config_from_argv(argv):
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
|
||||
parser.add_argument("-d", "--daemon-mode",
|
||||
help="run in background (daemon mode)",
|
||||
dest="daemon-mode",
|
||||
default=None,
|
||||
action="store_true")
|
||||
|
||||
parser.add_argument("-q", "--quiet",
|
||||
help="supress console output",
|
||||
dest="quiet",
|
||||
default=None,
|
||||
action="store_true")
|
||||
|
||||
parser.add_argument("-c", "--config",
|
||||
help="custom configuration file",
|
||||
dest="config",
|
||||
type=str)
|
||||
|
||||
parser.add_argument("--ssh-keyscan",
|
||||
help="scan repository hosts for ssh keys",
|
||||
dest="ssh-keyscan",
|
||||
default=None,
|
||||
action="store_true")
|
||||
|
||||
parser.add_argument("--pid-file",
|
||||
help="specify a custom pid file",
|
||||
dest="pid-file",
|
||||
type=str)
|
||||
|
||||
parser.add_argument("--log-file",
|
||||
help="specify a log file",
|
||||
dest="log-file",
|
||||
type=str)
|
||||
|
||||
parser.add_argument("--log-level",
|
||||
help="specify log level",
|
||||
dest="log-level",
|
||||
type=str)
|
||||
|
||||
parser.add_argument("--host",
|
||||
help="address to bind http server to",
|
||||
dest="http-host",
|
||||
type=str)
|
||||
|
||||
#parser.add_argument("--http-host",
|
||||
# help="address to bind http server to",
|
||||
# dest="http-host",
|
||||
# type=str)
|
||||
|
||||
#parser.add_argument("--https-host",
|
||||
# help="address to bind https server to",
|
||||
# dest="https-host",
|
||||
# type=str)
|
||||
|
||||
parser.add_argument("--port",
|
||||
help="port to bind http server to",
|
||||
dest="http-port",
|
||||
type=int)
|
||||
|
||||
#parser.add_argument("--http-port",
|
||||
# help="port to bind http server to",
|
||||
# dest="http-port",
|
||||
# type=int)
|
||||
|
||||
#parser.add_argument("--https-port",
|
||||
# help="port to bind http server to",
|
||||
# dest="https-port",
|
||||
# type=int)
|
||||
|
||||
parser.add_argument("--ws-port",
|
||||
help="port to bind web socket server to",
|
||||
dest="web-ui-web-socket-port",
|
||||
type=int)
|
||||
|
||||
parser.add_argument("--ssl",
|
||||
help="enable https",
|
||||
dest="https-enabled",
|
||||
default=None,
|
||||
action="store_true")
|
||||
|
||||
parser.add_argument("--ssl-key",
|
||||
help="path to ssl key file",
|
||||
dest="ssl-key",
|
||||
type=str)
|
||||
|
||||
parser.add_argument("--ssl-cert",
|
||||
help="path to ssl cert file",
|
||||
dest="ssl-cert",
|
||||
type=str)
|
||||
|
||||
parser.add_argument("--allow-root-user",
|
||||
help="allow running as root user",
|
||||
dest="allow-root-user",
|
||||
default=None,
|
||||
action="store_true")
|
||||
|
||||
|
||||
config = vars(parser.parse_args(argv))
|
||||
|
||||
# Delete entries for unprovided arguments
|
||||
del_keys = []
|
||||
for key in config:
|
||||
if config[key] is None:
|
||||
del_keys.append(key)
|
||||
|
||||
for key in del_keys:
|
||||
del config[key]
|
||||
|
||||
return config
|
||||
|
||||
|
||||
def find_config_file(target_directories=None):
|
||||
"""Attempt to find a path to a config file. Provided paths are scanned
|
||||
for *.conf(ig)?.json files."""
|
||||
import os
|
||||
import re
|
||||
import logging
|
||||
logger = logging.getLogger()
|
||||
|
||||
if not target_directories:
|
||||
return
|
||||
|
||||
# Remove duplicates
|
||||
target_directories = list(set(target_directories))
|
||||
|
||||
# Look for a *conf.json or *config.json
|
||||
for dir in target_directories:
|
||||
|
||||
if not os.access(dir, os.R_OK):
|
||||
continue
|
||||
|
||||
for item in os.listdir(dir):
|
||||
if re.match(r".*conf(ig)?\.json$", item):
|
||||
path = os.path.realpath(os.path.join(dir, item))
|
||||
logger.info("Using '%s' as config" % path)
|
||||
return path
|
||||
|
||||
|
||||
def get_config_from_file(path):
|
||||
"""Get configuration values from config file."""
|
||||
import logging
|
||||
import os
|
||||
logger = logging.getLogger()
|
||||
|
||||
config_file_path = os.path.realpath(path)
|
||||
logger.info('Using custom configuration file \'%s\'' % config_file_path)
|
||||
|
||||
# Read config data from json file
|
||||
if config_file_path:
|
||||
config_data = read_json_file(config_file_path)
|
||||
else:
|
||||
logger.info('No configuration file found or specified. Using default values.')
|
||||
config_data = {}
|
||||
|
||||
return config_data
|
||||
|
||||
|
||||
def read_json_file(file_path):
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
import errno
|
||||
|
||||
try:
|
||||
json_string = open(file_path).read()
|
||||
|
||||
except IOError as e:
|
||||
if e.errno == errno.ENOENT:
|
||||
raise ConfigFileNotFoundException(file_path)
|
||||
else:
|
||||
raise e
|
||||
|
||||
except Exception as e:
|
||||
raise e
|
||||
|
||||
try:
|
||||
# Remove commens from JSON (makes sample config options easier)
|
||||
regex = r'\s*(#|\/{2}).*$'
|
||||
regex_inline = r'(:?(?:\s)*([A-Za-z\d\.{}]*)|((?<=\").*\"),?)(?:\s)*(((#|(\/{2})).*)|)$'
|
||||
lines = json_string.split('\n')
|
||||
|
||||
for index, line in enumerate(lines):
|
||||
if re.search(regex, line):
|
||||
if re.search(r'^' + regex, line, re.IGNORECASE):
|
||||
lines[index] = ""
|
||||
elif re.search(regex_inline, line):
|
||||
lines[index] = re.sub(regex_inline, r'\1', line)
|
||||
|
||||
data = json.loads('\n'.join(lines))
|
||||
|
||||
except ValueError as e:
|
||||
raise ConfigFileInvalidException(file_path)
|
||||
|
||||
except Exception as e:
|
||||
raise e
|
||||
|
||||
return data
|
||||
|
||||
|
||||
def init_config(config):
|
||||
"""Initialize config by filling out missing values etc."""
|
||||
|
||||
import os
|
||||
import re
|
||||
import logging
|
||||
from ..models import Project
|
||||
logger = logging.getLogger()
|
||||
|
||||
# Translate any ~ in the path into /home/<user>
|
||||
if 'pid-file' in config and config['pid-file']:
|
||||
config['pid-file'] = os.path.expanduser(config['pid-file'])
|
||||
|
||||
if 'log-file' in config and config['log-file']:
|
||||
config['log-file'] = os.path.expanduser(config['log-file'])
|
||||
|
||||
if 'ssl-cert' in config and config['ssl-cert']:
|
||||
config['ssl-cert'] = os.path.expanduser(config['ssl-cert'])
|
||||
|
||||
if 'ssl-key' in config and config['ssl-key']:
|
||||
config['ssl-key'] = os.path.expanduser(config['ssl-key'])
|
||||
|
||||
if 'repositories' not in config:
|
||||
config['repositories'] = []
|
||||
|
||||
deserialized = []
|
||||
|
||||
for repo_config in config['repositories']:
|
||||
|
||||
# Setup branch if missing
|
||||
if 'branch' not in repo_config:
|
||||
repo_config['branch'] = "master"
|
||||
|
||||
# Setup remote if missing
|
||||
if 'remote' not in repo_config:
|
||||
repo_config['remote'] = "origin"
|
||||
|
||||
# Setup deploy commands list if not present
|
||||
if 'deploy_commands' not in repo_config:
|
||||
repo_config['deploy_commands'] = []
|
||||
|
||||
# Check if any global pre deploy commands is specified
|
||||
if 'global_deploy' in config and len(config['global_deploy']) > 0 and len(config['global_deploy'][0]) is not 0:
|
||||
repo_config['deploy_commands'].insert(0, config['global_deploy'][0])
|
||||
|
||||
# Check if any repo specific deploy command is specified
|
||||
if 'deploy' in repo_config:
|
||||
repo_config['deploy_commands'].append(repo_config['deploy'])
|
||||
|
||||
# Check if any global post deploy command is specified
|
||||
if 'global_deploy' in config and len(config['global_deploy']) > 1 and len(config['global_deploy'][1]) is not 0:
|
||||
repo_config['deploy_commands'].append(config['global_deploy'][1])
|
||||
|
||||
# If a repository is configured with embedded credentials, we create an alternate URL
|
||||
# without these credentials that cen be used when comparing the URL with URLs referenced
|
||||
# in incoming web hook requests.
|
||||
if 'url' in repo_config:
|
||||
regexp = re.search(r"^(https?://)([^@]+)@(.+)$", repo_config['url'])
|
||||
if regexp:
|
||||
repo_config['url_without_usernme'] = regexp.group(1) + regexp.group(3)
|
||||
|
||||
# Translate any ~ in the path into /home/<user>
|
||||
if 'path' in repo_config:
|
||||
repo_config['path'] = os.path.expanduser(repo_config['path'])
|
||||
|
||||
# Support for legacy config format
|
||||
if 'filters' in repo_config:
|
||||
repo_config['payload-filter'] = repo_config['filters']
|
||||
del repo_config['filters']
|
||||
|
||||
if 'payload-filter' not in repo_config:
|
||||
repo_config['payload-filter'] = []
|
||||
|
||||
if 'header-filter' not in repo_config:
|
||||
repo_config['header-filter'] = {}
|
||||
|
||||
# Rewrite some legacy filter config syntax
|
||||
for filter in repo_config['payload-filter']:
|
||||
|
||||
# Legacy config syntax?
|
||||
if ('kind' in filter and filter['kind'] == 'pull-request-handler') or ('type' in filter and filter['type'] == 'pull-request-filter'):
|
||||
|
||||
# Reset legacy values
|
||||
filter['kind'] = None
|
||||
filter['type'] = None
|
||||
|
||||
if 'ref' in filter:
|
||||
filter['pull_request.base.ref'] = filter['ref']
|
||||
filter['ref'] = None
|
||||
|
||||
filter['pull_request'] = True
|
||||
|
||||
project = Project(repo_config)
|
||||
deserialized.append(project)
|
||||
|
||||
config['repositories'] = deserialized
|
||||
|
||||
return config
|
||||
|
||||
|
||||
def get_repo_config_from_environment():
|
||||
"""Look for repository config in any defined environment variables. If
|
||||
found, import to main config."""
|
||||
import logging
|
||||
import os
|
||||
|
||||
if 'GAD_REPO_URL' not in os.environ:
|
||||
return
|
||||
|
||||
logger = logging.getLogger()
|
||||
|
||||
repo_config = {
|
||||
'url': os.environ['GAD_REPO_URL']
|
||||
}
|
||||
|
||||
logger.info("Added configuration for '%s' found in environment variables" % os.environ['GAD_REPO_URL'])
|
||||
|
||||
if 'GAD_REPO_BRANCH' in os.environ:
|
||||
repo_config['branch'] = os.environ['GAD_REPO_BRANCH']
|
||||
|
||||
if 'GAD_REPO_REMOTE' in os.environ:
|
||||
repo_config['remote'] = os.environ['GAD_REPO_REMOTE']
|
||||
|
||||
if 'GAD_REPO_PATH' in os.environ:
|
||||
repo_config['path'] = os.environ['GAD_REPO_PATH']
|
||||
|
||||
if 'GAD_REPO_DEPLOY' in os.environ:
|
||||
repo_config['deploy'] = os.environ['GAD_REPO_DEPLOY']
|
||||
|
||||
return repo_config
|
||||
|
||||
|
||||
def get_config_file_path(env_config, argv_config, search_target):
|
||||
import os
|
||||
|
||||
# Config file path provided in argument vector?
|
||||
if 'config' in argv_config and argv_config['config']:
|
||||
config_file_path = os.path.realpath(argv_config['config'])
|
||||
|
||||
# Config file path provided in environment variable?
|
||||
elif 'config' in env_config and env_config['config']:
|
||||
config_file_path = os.path.realpath(env_config['config'])
|
||||
|
||||
# Search file system
|
||||
else:
|
||||
|
||||
# Directories to scan for config files
|
||||
target_directories = [
|
||||
os.getcwd(), # cwd
|
||||
search_target # script path
|
||||
]
|
||||
|
||||
config_file_path = find_config_file(target_directories)
|
||||
|
||||
return config_file_path
|
||||
@@ -0,0 +1,64 @@
|
||||
{
|
||||
// HTTP server options
|
||||
//"http-enabled": true,
|
||||
//"http-host": "0.0.0.0",
|
||||
//"http-port": 8001,
|
||||
|
||||
// HTTPS server options
|
||||
//"https-enabled": true,
|
||||
//"https-host": "0.0.0.0",
|
||||
//"https-port": 8002,
|
||||
|
||||
// Web socket server options (used by web UI for real time updates)
|
||||
//"wss-enabled": false,
|
||||
//"wss-host": "0.0.0.0",
|
||||
//"wss-port": 8003,
|
||||
|
||||
// Web user interface options
|
||||
//"web-ui-enabled": false,
|
||||
//"web-ui-username": null,
|
||||
//"web-ui-password": null,
|
||||
//"web-ui-whitelist": ["127.0.0.1"],
|
||||
|
||||
// TLS/SSL cert (necessary for HTTPS and web socket server to work)
|
||||
//"ssl-key": null, // If specified, holds the private key
|
||||
"ssl-cert": "/etc/git-auto-deploy/cert.pem", // Holds the public key or both the private and public keys
|
||||
|
||||
// File to store a copy of the console output
|
||||
"log-file": "/var/log/git-auto-deploy.log",
|
||||
|
||||
// Record all log levels by default
|
||||
//"log-level": "NOTSET",
|
||||
|
||||
// Deploy commands that should be executed for all projects
|
||||
//"global_deploy": [
|
||||
// "echo Deploy started!",
|
||||
// "echo Deploy completed!"
|
||||
//],
|
||||
|
||||
// Project configs
|
||||
"repositories": [
|
||||
{
|
||||
"url": "https://github.com/olipo186/Git-Auto-Deploy.git",
|
||||
"branch": "master",
|
||||
"remote": "origin",
|
||||
"path": "/var/lib/git-auto-deploy/Git-Auto-Deploy",
|
||||
"deploy": "echo deploying"
|
||||
},
|
||||
{
|
||||
"url": "https://github.com/github/gitignore",
|
||||
"path": "/var/lib/git-auto-deploy/gitignore"
|
||||
}
|
||||
// ,{
|
||||
// "url": "https://api.github.com/repos/olipo186/Git-Auto-Deploy",
|
||||
// "deploy": "echo deploying after pull request",
|
||||
// "filters": [
|
||||
// {
|
||||
// "type": "pull-request-filter",
|
||||
// "action": "closed",
|
||||
// "ref": "testing-branch"
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
class SystemEvent(object):
|
||||
|
||||
def __init__(self, name=None):
|
||||
import logging
|
||||
|
||||
self.logger = logging.getLogger()
|
||||
self.hub = None
|
||||
self.messages = []
|
||||
self.name = name
|
||||
self.id = None
|
||||
self.waiting = None
|
||||
self.success = None
|
||||
|
||||
def __repr__(self):
|
||||
if self.id:
|
||||
return "<SystemEvent:%s>" % self.id
|
||||
else:
|
||||
return "<SystemEvent>"
|
||||
|
||||
def dict_repr(self):
|
||||
from time import time
|
||||
return {
|
||||
"id": self.id,
|
||||
"type": type(self).__name__,
|
||||
"timestamp": time(),
|
||||
"messages": self.messages,
|
||||
"waiting": self.waiting,
|
||||
"success": self.success
|
||||
}
|
||||
|
||||
def register_hub(self, hub):
|
||||
self.hub = hub
|
||||
|
||||
def register_message(self, message, level="INFO"):
|
||||
self.messages.append(message)
|
||||
self.hub.notify_observers(type="event-updated", event=self.dict_repr())
|
||||
|
||||
def set_id(self, id):
|
||||
self.id = id
|
||||
|
||||
def get_id(self):
|
||||
return self.id
|
||||
|
||||
def set_waiting(self, value):
|
||||
self.waiting = value
|
||||
self.hub.notify_observers(type="event-updated", event=self.dict_repr())
|
||||
|
||||
def set_success(self, value):
|
||||
self.success = value
|
||||
self.hub.notify_observers(type="event-updated", event=self.dict_repr())
|
||||
self.hub.notify_observers(type="event-success", id=self.id, success=value)
|
||||
|
||||
def log_debug(self, message):
|
||||
self.logger.debug(message)
|
||||
self.register_message(message, "DEBUG")
|
||||
|
||||
def log_info(self, message):
|
||||
self.logger.info(message)
|
||||
self.register_message(message, "INFO")
|
||||
|
||||
def log_warning(self, message):
|
||||
self.logger.warning(message)
|
||||
self.register_message(message, "WARNING")
|
||||
|
||||
def log_error(self, message):
|
||||
self.logger.error(message)
|
||||
self.register_message(message, "ERROR")
|
||||
|
||||
def log_critical(self, message):
|
||||
self.logger.critical(message)
|
||||
self.register_message(message, "CRITICAL")
|
||||
|
||||
#def update(self):
|
||||
# self.hub.notify_observers(type="event-updated", event=self.dict_repr())
|
||||
|
||||
|
||||
class WebhookAction(SystemEvent):
|
||||
"""Represents a webhook request event and keeps a copy of all incoming and outgoing data for monitoring purposes."""
|
||||
|
||||
def __init__(self, client_address, request_headers, request_body):
|
||||
self.client_address = client_address
|
||||
self.request_headers = request_headers
|
||||
self.request_body = request_body
|
||||
super(WebhookAction, self).__init__()
|
||||
|
||||
def __repr__(self):
|
||||
return "<WebhookAction>"
|
||||
|
||||
def dict_repr(self):
|
||||
data = super(WebhookAction, self).dict_repr()
|
||||
data['client-address'] = self.client_address[0]
|
||||
data['client-port'] = self.client_address[1]
|
||||
data['request-headers'] = self.request_headers
|
||||
data['request-body'] = self.request_body
|
||||
return data
|
||||
|
||||
|
||||
class DeployEvent(SystemEvent):
|
||||
|
||||
def __init__(self, project):
|
||||
self.project = project
|
||||
super(DeployEvent, self).__init__()
|
||||
|
||||
def __repr__(self):
|
||||
return "<WebhookAction>"
|
||||
|
||||
def dict_repr(self):
|
||||
data = super(DeployEvent, self).dict_repr()
|
||||
data['name'] = self.project.get_name()
|
||||
return data
|
||||
|
||||
|
||||
class StartupEvent(SystemEvent):
|
||||
|
||||
def __init__(self, http_address=None, http_port=None, ws_address=None, ws_port=None):
|
||||
self.http_address = http_address
|
||||
self.http_port = http_port
|
||||
self.http_started = None
|
||||
self.ws_address = ws_address
|
||||
self.ws_port = ws_port
|
||||
self.ws_started = None
|
||||
self.waiting = True
|
||||
super(StartupEvent, self).__init__()
|
||||
|
||||
def __repr__(self):
|
||||
return "<StartupEvent>"
|
||||
|
||||
def dict_repr(self):
|
||||
data = super(StartupEvent, self).dict_repr()
|
||||
data['http-address'] = self.http_address
|
||||
data['http-port'] = self.http_port
|
||||
data['http-started'] = self.http_started
|
||||
data['ws-address'] = self.ws_address
|
||||
data['ws-port'] = self.ws_port
|
||||
data['ws-started'] = self.ws_started
|
||||
return data
|
||||
|
||||
def set_http_started(self, value):
|
||||
self.http_started = value
|
||||
self.hub.notify_observers(type="event-updated", event=self.dict_repr())
|
||||
self.validate_success()
|
||||
|
||||
def set_ws_started(self, value):
|
||||
self.ws_started = value
|
||||
self.hub.notify_observers(type="event-updated", event=self.dict_repr())
|
||||
self.validate_success()
|
||||
|
||||
def validate_success(self):
|
||||
if self.http_started is not False and self.ws_started is not False:
|
||||
self.set_waiting(False)
|
||||
self.set_success(True)
|
||||
|
||||
|
||||
class EventStore(object):
|
||||
|
||||
def __init__(self):
|
||||
self.actions = []
|
||||
self.observers = []
|
||||
self.next_id = 0
|
||||
|
||||
def register_observer(self, observer):
|
||||
self.observers.append(observer)
|
||||
|
||||
def unregister_observer(self, observer):
|
||||
if observer in self.observers:
|
||||
self.observers.remove(observer)
|
||||
|
||||
def notify_observers(self, *args, **kwargs):
|
||||
for observer in self.observers:
|
||||
observer.update(*args, **kwargs)
|
||||
|
||||
def register_action(self, event):
|
||||
event.set_id(self.next_id)
|
||||
event.register_hub(self)
|
||||
self.next_id = self.next_id + 1
|
||||
self.actions.append(event)
|
||||
self.notify_observers(type="new-event", event=event.dict_repr())
|
||||
|
||||
# Store max 100 actions
|
||||
if len(self.actions) > 100:
|
||||
self.actions.pop(0)
|
||||
|
||||
def dict_repr(self):
|
||||
action_repr = []
|
||||
for action in self.actions:
|
||||
action_repr.append(action.dict_repr())
|
||||
return action_repr
|
||||
@@ -0,0 +1,679 @@
|
||||
if __name__ == '__main__':
|
||||
import sys
|
||||
print("Critical - GAD must be started as a python module, for example using python -m gitautodeploy")
|
||||
sys.exit()
|
||||
|
||||
|
||||
class LogInterface(object):
|
||||
"""Interface that functions as a stdout and stderr handler and directs the
|
||||
output to the logging module, which in turn will output to either console,
|
||||
file or both."""
|
||||
|
||||
def __init__(self, level=None):
|
||||
import logging
|
||||
self.level = (level if level else logging.getLogger().info)
|
||||
|
||||
def write(self, msg):
|
||||
for line in msg.strip().split("\n"):
|
||||
self.level(line)
|
||||
|
||||
def flush(self):
|
||||
pass
|
||||
|
||||
|
||||
from .wsserver import WebSocketClientHandlerFactory
|
||||
from .httpserver import WebhookRequestHandlerFactory
|
||||
|
||||
|
||||
class GitAutoDeploy(object):
|
||||
_instance = None
|
||||
_http_server = None
|
||||
_https_server = None
|
||||
_https_server_unwrapped_socket = None
|
||||
_config = {}
|
||||
_server_status = {}
|
||||
_pid = None
|
||||
_event_store = None
|
||||
_default_stdout = None
|
||||
_default_stderr = None
|
||||
_startup_event = None
|
||||
_ws_clients = []
|
||||
_http_port = None
|
||||
|
||||
def __new__(cls, *args, **kwargs):
|
||||
"""Overload constructor to enable singleton access"""
|
||||
if not cls._instance:
|
||||
cls._instance = super(GitAutoDeploy, cls).__new__(
|
||||
cls, *args, **kwargs)
|
||||
return cls._instance
|
||||
|
||||
def __init__(self):
|
||||
from .events import EventStore, StartupEvent
|
||||
|
||||
# Setup an event store instance that can keep a global record of events
|
||||
self._event_store = EventStore()
|
||||
self._event_store.register_observer(self)
|
||||
|
||||
# Create a startup event that can hold status and any error messages
|
||||
# from the startup process
|
||||
self._startup_event = StartupEvent()
|
||||
self._event_store.register_action(self._startup_event)
|
||||
|
||||
def clone_all_repos(self):
|
||||
"""Iterates over all configured repositories and clones them to their
|
||||
configured paths."""
|
||||
import os
|
||||
import re
|
||||
import logging
|
||||
from .wrappers import GitWrapper
|
||||
logger = logging.getLogger()
|
||||
|
||||
if 'repositories' not in self._config:
|
||||
return
|
||||
|
||||
# Iterate over all configured repositories
|
||||
for repo_config in self._config['repositories']:
|
||||
|
||||
# Only clone repositories with a configured path
|
||||
if 'url' not in repo_config:
|
||||
logger.critical("Repository has no configured URL")
|
||||
self.exit()
|
||||
return
|
||||
|
||||
# Only clone repositories with a configured path
|
||||
if 'path' not in repo_config:
|
||||
logger.debug("Repository %s will not be cloned (no path configured)" % repo_config['url'])
|
||||
continue
|
||||
|
||||
if os.path.isdir(repo_config['path']) and os.path.isdir(repo_config['path']+'/.git'):
|
||||
GitWrapper.init(repo_config)
|
||||
else:
|
||||
GitWrapper.clone(repo_config)
|
||||
|
||||
def ssh_key_scan(self):
|
||||
import re
|
||||
import logging
|
||||
from .wrappers import ProcessWrapper
|
||||
logger = logging.getLogger()
|
||||
|
||||
for repository in self._config['repositories']:
|
||||
|
||||
if 'url' not in repository:
|
||||
continue
|
||||
|
||||
logger.info("Scanning repository: %s" % repository['url'])
|
||||
m = re.match('[^\@]+\@([^\:\/]+)(:(\d+))?', repository['url'])
|
||||
|
||||
if m is not None:
|
||||
host = m.group(1)
|
||||
port = m.group(3)
|
||||
port_arg = '' if port is None else ('-p %s ' % port)
|
||||
cmd = 'ssh-keyscan %s%s >> $HOME/.ssh/known_hosts' % (port_arg, host)
|
||||
ProcessWrapper().call([cmd], shell=True)
|
||||
|
||||
else:
|
||||
logger.error('Could not find regexp match in path: %s' % repository['url'])
|
||||
|
||||
def create_pid_file(self):
|
||||
import os
|
||||
|
||||
with open(self._config['pid-file'], 'w') as f:
|
||||
f.write(str(os.getpid()))
|
||||
|
||||
def read_pid_file(self):
|
||||
with open(self._config['pid-file'], 'r') as f:
|
||||
return f.readlines()
|
||||
|
||||
def remove_pid_file(self):
|
||||
import os
|
||||
import errno
|
||||
if 'pid-file' in self._config and self._config['pid-file']:
|
||||
try:
|
||||
os.remove(self._config['pid-file'])
|
||||
except OSError as e:
|
||||
# errno.ENOENT = no such file or directory
|
||||
if e.errno != errno.ENOENT:
|
||||
raise
|
||||
|
||||
@staticmethod
|
||||
def create_daemon():
|
||||
import os
|
||||
|
||||
try:
|
||||
# Spawn first child. Returns 0 in the child and pid in the parent.
|
||||
pid = os.fork()
|
||||
except OSError as e:
|
||||
raise Exception("%s [%d]" % (e.strerror, e.errno))
|
||||
|
||||
# First child
|
||||
if pid == 0:
|
||||
os.setsid()
|
||||
|
||||
try:
|
||||
# Spawn second child
|
||||
pid = os.fork()
|
||||
|
||||
except OSError as e:
|
||||
raise Exception("%s [%d]" % (e.strerror, e.errno))
|
||||
|
||||
if pid == 0:
|
||||
os.umask(0)
|
||||
else:
|
||||
# Kill first child
|
||||
os._exit(0)
|
||||
else:
|
||||
# Kill parent of first child
|
||||
os._exit(0)
|
||||
|
||||
return 0
|
||||
|
||||
def update(self, *args, **kwargs):
|
||||
import json
|
||||
data = json.dumps(kwargs).encode('utf-8')
|
||||
for client in self._ws_clients:
|
||||
client.sendMessage(data)
|
||||
|
||||
def get_log_formatter(self):
|
||||
import logging
|
||||
return logging.Formatter("%(asctime)s [%(levelname)-5.5s] %(message)s")
|
||||
|
||||
def setup_console_logger(self):
|
||||
import logging
|
||||
|
||||
# Set up logging
|
||||
logger = logging.getLogger()
|
||||
|
||||
consoleHandler = logging.StreamHandler()
|
||||
consoleHandler.setFormatter(self.get_log_formatter())
|
||||
|
||||
# Check if a stream handler is already present (will be if GAD is started by test script)
|
||||
handler_present = False
|
||||
for handler in logger.handlers:
|
||||
if isinstance(handler, type(consoleHandler)):
|
||||
handler_present = True
|
||||
break
|
||||
|
||||
if not handler_present:
|
||||
logger.addHandler(consoleHandler)
|
||||
|
||||
def setup(self, config):
|
||||
"""Setup an instance of GAD based on the provided config object."""
|
||||
import sys
|
||||
import socket
|
||||
import os
|
||||
import logging
|
||||
import base64
|
||||
from .lock import Lock
|
||||
import getpass
|
||||
|
||||
# This solves https://github.com/olipo186/Git-Auto-Deploy/issues/118
|
||||
try:
|
||||
from logging import NullHandler
|
||||
except ImportError:
|
||||
from logging import Handler
|
||||
|
||||
class NullHandler(Handler):
|
||||
def emit(self, record):
|
||||
pass
|
||||
|
||||
# Attatch config values to this instance
|
||||
self._config = config
|
||||
|
||||
# Set up logging
|
||||
logger = logging.getLogger()
|
||||
logFormatter = self.get_log_formatter()
|
||||
|
||||
# Enable console output?
|
||||
if ('quiet' in self._config and self._config['quiet']) or ('daemon-mode' in self._config and self._config['daemon-mode']):
|
||||
|
||||
# Add a default null handler that suppresses any console output
|
||||
logger.addHandler(NullHandler())
|
||||
|
||||
else:
|
||||
|
||||
# Set up console logger if not already present
|
||||
self.setup_console_logger()
|
||||
|
||||
# Set logging level
|
||||
if 'log-level' in self._config:
|
||||
level = logging.getLevelName(self._config['log-level'])
|
||||
logger.setLevel(level)
|
||||
|
||||
if 'log-file' in self._config and self._config['log-file']:
|
||||
# Translate any ~ in the path into /home/<user>
|
||||
fileHandler = logging.FileHandler(self._config['log-file'])
|
||||
fileHandler.setFormatter(logFormatter)
|
||||
logger.addHandler(fileHandler)
|
||||
|
||||
# Display a warning when trying to run as root
|
||||
if not self._config['allow-root-user'] and getpass.getuser() == 'root':
|
||||
logger.critical("Refusing to start as root. This application shouldn't run as a privileged used. Please run it as a different user. To disregard this warning and start anyway, set the config option \"allow-root-user\" to true, or use the command line argument --allow-root-user")
|
||||
sys.exit()
|
||||
|
||||
if 'ssh-keyscan' in self._config and self._config['ssh-keyscan']:
|
||||
self._startup_event.log_info('Scanning repository hosts for ssh keys...')
|
||||
self.ssh_key_scan()
|
||||
|
||||
# Clone all repos once initially
|
||||
self.clone_all_repos()
|
||||
|
||||
# Set default stdout and stderr to our logging interface (that writes
|
||||
# to file and console depending on user preference)
|
||||
if 'intercept-stdout' in self._config and self._config['intercept-stdout']:
|
||||
self._default_stdout = sys.stdout
|
||||
self._default_stderr = sys.stderr
|
||||
sys.stdout = LogInterface(logger.info)
|
||||
sys.stderr = LogInterface(logger.error)
|
||||
|
||||
if 'daemon-mode' in self._config and self._config['daemon-mode']:
|
||||
self._startup_event.log_info('Starting Git Auto Deploy in daemon mode')
|
||||
GitAutoDeploy.create_daemon()
|
||||
|
||||
self._pid = os.getpid()
|
||||
self.create_pid_file()
|
||||
|
||||
# Generate auth key to protect the web socket server
|
||||
self._server_status['auth-key'] = base64.b64encode(os.urandom(32))
|
||||
|
||||
# Clear any existing lock files, with no regard to possible ongoing processes
|
||||
for repo_config in self._config['repositories']:
|
||||
|
||||
# Do we have a physical repository?
|
||||
if 'path' in repo_config:
|
||||
Lock(os.path.join(repo_config['path'], 'status_running')).clear()
|
||||
Lock(os.path.join(repo_config['path'], 'status_waiting')).clear()
|
||||
|
||||
#if 'daemon-mode' not in self._config or not self._config['daemon-mode']:
|
||||
# self._startup_event.log_info('Git Auto Deploy started')
|
||||
|
||||
def serve_http(self, serve_forever=True):
|
||||
"""Starts a HTTP server that listens for webhook requests and serves the web ui."""
|
||||
import sys
|
||||
import socket
|
||||
import os
|
||||
from .events import SystemEvent
|
||||
|
||||
try:
|
||||
from BaseHTTPServer import HTTPServer
|
||||
except ImportError as e:
|
||||
from http.server import HTTPServer
|
||||
|
||||
if not self._config['http-enabled']:
|
||||
return
|
||||
|
||||
# Setup
|
||||
try:
|
||||
|
||||
# Create web hook request handler class
|
||||
WebhookRequestHandler = WebhookRequestHandlerFactory(self._config, self._event_store, self._server_status, is_https=False)
|
||||
|
||||
# Create HTTP server
|
||||
self._http_server = HTTPServer((self._config['http-host'],
|
||||
self._config['http-port']),
|
||||
WebhookRequestHandler)
|
||||
|
||||
# Setup SSL for HTTP server
|
||||
sa = self._http_server.socket.getsockname()
|
||||
self._http_port = sa[1]
|
||||
self._server_status['http-uri'] = "http://%s:%s" % (self._config['http-host'], sa[1])
|
||||
self._startup_event.log_info("Listening for connections on %s" % self._server_status['http-uri'])
|
||||
self._startup_event.http_address = sa[0]
|
||||
self._startup_event.http_port = sa[1]
|
||||
self._startup_event.set_http_started(True)
|
||||
|
||||
except socket.error as e:
|
||||
self._startup_event.log_critical("Unable to start HTTP server: %s" % e)
|
||||
return
|
||||
|
||||
if not serve_forever:
|
||||
return
|
||||
|
||||
# Run forever
|
||||
try:
|
||||
self._http_server.serve_forever()
|
||||
|
||||
except socket.error as e:
|
||||
event = SystemEvent()
|
||||
self._event_store.register_action(event)
|
||||
event.log_critical("Error on socket: %s" % e)
|
||||
sys.exit(1)
|
||||
|
||||
except KeyboardInterrupt as e:
|
||||
event = SystemEvent()
|
||||
self._event_store.register_action(event)
|
||||
event.log_info('Requested close by keyboard interrupt signal')
|
||||
self.stop()
|
||||
self.exit()
|
||||
|
||||
event = SystemEvent()
|
||||
self._event_store.register_action(event)
|
||||
event.log_info('HTTP server did quit')
|
||||
|
||||
def serve_https(self):
|
||||
"""Starts a HTTPS server that listens for webhook requests and serves the web ui."""
|
||||
import sys
|
||||
import socket
|
||||
import os
|
||||
import ssl
|
||||
from .events import SystemEvent
|
||||
|
||||
try:
|
||||
from BaseHTTPServer import HTTPServer
|
||||
except ImportError as e:
|
||||
from http.server import HTTPServer
|
||||
|
||||
if not self._config['https-enabled']:
|
||||
return
|
||||
|
||||
if not os.path.isfile(self._config['ssl-cert']):
|
||||
self._startup_event.log_critical("Unable to activate SSL: File does not exist: %s" % self._config['ssl-cert'])
|
||||
return
|
||||
|
||||
# Setup
|
||||
try:
|
||||
|
||||
# Create web hook request handler class
|
||||
WebhookRequestHandler = WebhookRequestHandlerFactory(self._config, self._event_store, self._server_status, is_https=True)
|
||||
|
||||
# Create HTTP server
|
||||
self._https_server = HTTPServer((self._config['https-host'],
|
||||
self._config['https-port']),
|
||||
WebhookRequestHandler)
|
||||
|
||||
# Setup SSL for HTTP server
|
||||
self._https_server_unwrapped_socket = self._https_server.socket
|
||||
self._https_server.socket = ssl.wrap_socket(self._https_server.socket,
|
||||
keyfile=self._config['ssl-key'],
|
||||
certfile=self._config['ssl-cert'],
|
||||
server_side=True)
|
||||
|
||||
sa = self._https_server.socket.getsockname()
|
||||
self._http_port = sa[1]
|
||||
self._server_status['https-uri'] = "https://%s:%s" % (self._config['https-host'], sa[1])
|
||||
|
||||
self._startup_event.log_info("Listening for connections on %s" % self._server_status['https-uri'])
|
||||
self._startup_event.http_address = sa[0]
|
||||
self._startup_event.http_port = sa[1]
|
||||
self._startup_event.set_http_started(True)
|
||||
|
||||
except socket.error as e:
|
||||
self._startup_event.log_critical("Unable to start HTTPS server: %s" % e)
|
||||
return
|
||||
|
||||
# Run forever
|
||||
try:
|
||||
self._https_server.serve_forever()
|
||||
|
||||
except socket.error as e:
|
||||
event = SystemEvent()
|
||||
self._event_store.register_action(event)
|
||||
event.log_critical("Error on socket: %s" % e)
|
||||
sys.exit(1)
|
||||
|
||||
except KeyboardInterrupt as e:
|
||||
event = SystemEvent()
|
||||
self._event_store.register_action(event)
|
||||
event.log_info('Requested close by keyboard interrupt signal')
|
||||
self.stop()
|
||||
self.exit()
|
||||
|
||||
event = SystemEvent()
|
||||
self._event_store.register_action(event)
|
||||
event.log_info('HTTPS server did quit')
|
||||
|
||||
def serve_wss(self):
|
||||
"""Start a web socket server over SSL, used by the web UI to get notifications about updates."""
|
||||
import os
|
||||
from .events import SystemEvent
|
||||
|
||||
# Start a web socket server if the web UI is enabled
|
||||
if not self._config['web-ui-enabled']:
|
||||
return
|
||||
|
||||
if not self._config['wss-enabled']:
|
||||
return
|
||||
|
||||
if not os.path.isfile(self._config['ssl-cert']):
|
||||
self._startup_event.log_critical("Unable to activate SSL: File does not exist: %s" % self._config['ssl-cert'])
|
||||
return
|
||||
|
||||
try:
|
||||
import os
|
||||
from autobahn.websocket import WebSocketServerProtocol, WebSocketServerFactory
|
||||
from twisted.internet import reactor, ssl
|
||||
from twisted.internet.error import BindError
|
||||
|
||||
# Create a WebSocketClientHandler instance
|
||||
WebSocketClientHandler = WebSocketClientHandlerFactory(self._config, self._ws_clients, self._event_store, self._server_status)
|
||||
|
||||
uri = u"ws://%s:%s" % (self._config['wss-host'], self._config['wss-port'])
|
||||
factory = WebSocketServerFactory(uri)
|
||||
factory.protocol = WebSocketClientHandler
|
||||
# factory.setProtocolOptions(maxConnections=2)
|
||||
|
||||
# note to self: if using putChild, the child must be bytes...
|
||||
if self._config['ssl-key'] and self._config['ssl-cert']:
|
||||
contextFactory = ssl.DefaultOpenSSLContextFactory(privateKeyFileName=self._config['ssl-key'], certificateFileName=self._config['ssl-cert'])
|
||||
else:
|
||||
contextFactory = ssl.DefaultOpenSSLContextFactory(privateKeyFileName=self._config['ssl-cert'], certificateFileName=self._config['ssl-cert'])
|
||||
|
||||
|
||||
self._ws_server_port = reactor.listenSSL(self._config['wss-port'], factory, contextFactory)
|
||||
# self._ws_server_port = reactor.listenTCP(self._config['wss-port'], factory)
|
||||
|
||||
self._server_status['wss-uri'] = "wss://%s:%s" % (self._config['wss-host'], self._config['wss-port'])
|
||||
|
||||
self._startup_event.log_info("Listening for connections on %s" % self._server_status['wss-uri'])
|
||||
self._startup_event.ws_address = self._config['wss-host']
|
||||
self._startup_event.ws_port = self._config['wss-port']
|
||||
self._startup_event.set_ws_started(True)
|
||||
|
||||
# Serve forever (until reactor.stop())
|
||||
reactor.run(installSignalHandlers=False)
|
||||
|
||||
except BindError as e:
|
||||
self._startup_event.log_critical("Unable to start web socket server: %s" % e)
|
||||
|
||||
except ImportError:
|
||||
self._startup_event.log_error("Unable to start web socket server due to missing dependency.")
|
||||
|
||||
event = SystemEvent()
|
||||
self._event_store.register_action(event)
|
||||
event.log_info('WSS server did quit')
|
||||
|
||||
def serve_forever(self):
|
||||
"""Start HTTP and web socket servers."""
|
||||
import sys
|
||||
import socket
|
||||
import logging
|
||||
import os
|
||||
from .events import SystemEvent
|
||||
import threading
|
||||
|
||||
try:
|
||||
from autobahn.websocket import WebSocketServerProtocol, WebSocketServerFactory
|
||||
from twisted.internet import reactor
|
||||
|
||||
# Given that the nessecary dependencies are present, notify the
|
||||
# event that we expect the web socket server to be started
|
||||
self._startup_event.ws_started = False
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
# Notify the event that we expect the http server to be started
|
||||
self._startup_event.http_started = False
|
||||
|
||||
# Add script dir to sys path, allowing us to import sub modules even after changing cwd
|
||||
sys.path.insert(1, os.path.dirname(os.path.realpath(__file__)))
|
||||
|
||||
# Set CWD to public www folder. This makes the http server serve files from the wwwroot directory.
|
||||
wwwroot = os.path.join(os.path.dirname(os.path.realpath(__file__)), "wwwroot")
|
||||
os.chdir(wwwroot)
|
||||
|
||||
threads = [
|
||||
# HTTP server
|
||||
threading.Thread(target=self.serve_http),
|
||||
|
||||
# HTTPS server
|
||||
threading.Thread(target=self.serve_https),
|
||||
|
||||
# Web socket SSL server
|
||||
threading.Thread(target=self.serve_wss)
|
||||
]
|
||||
|
||||
# Start all threads
|
||||
for thread in threads:
|
||||
thread.start()
|
||||
|
||||
# Wait for each thread to finish
|
||||
for thread in threads:
|
||||
|
||||
# Wait for thread to finish without blocking main thread
|
||||
while thread.is_alive():
|
||||
thread.join(5)
|
||||
|
||||
def signal_handler(self, signum, frame):
|
||||
from .events import SystemEvent
|
||||
self.stop()
|
||||
|
||||
event = SystemEvent()
|
||||
self._event_store.register_action(event)
|
||||
|
||||
# Reload configuration on SIGHUP events (conventional for daemon processes)
|
||||
if signum == 1:
|
||||
self.setup(self._config)
|
||||
self.serve_forever()
|
||||
return
|
||||
|
||||
# Keyboard interrupt signal
|
||||
elif signum == 2:
|
||||
event.log_info('Recieved keyboard interrupt signal (%s) from the OS, shutting down.' % signum)
|
||||
|
||||
else:
|
||||
event.log_info('Recieved signal (%s) from the OS, shutting down.' % signum)
|
||||
|
||||
self.exit()
|
||||
|
||||
def stop(self):
|
||||
"""Stop all running TCP servers (HTTP and web socket servers)"""
|
||||
|
||||
# Stop HTTP server if running
|
||||
if self._http_server is not None:
|
||||
|
||||
# Shut down the underlying TCP server
|
||||
self._http_server.shutdown()
|
||||
|
||||
# Close the socket
|
||||
self._http_server.socket.close()
|
||||
|
||||
# Stop HTTPS server if running
|
||||
if self._https_server is not None:
|
||||
|
||||
# Shut down the underlying TCP server
|
||||
self._https_server.shutdown()
|
||||
|
||||
# Close the socket
|
||||
self._https_server.socket.close()
|
||||
|
||||
if self._https_server_unwrapped_socket is not None:
|
||||
|
||||
self._https_server_unwrapped_socket.close()
|
||||
|
||||
# Stop web socket server if running
|
||||
try:
|
||||
from twisted.internet import reactor
|
||||
reactor.callFromThread(reactor.stop)
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
def exit(self):
|
||||
import sys
|
||||
import logging
|
||||
logger = logging.getLogger()
|
||||
logger.info('Goodbye')
|
||||
|
||||
# Delete PID file
|
||||
self.remove_pid_file()
|
||||
|
||||
# Restore stdin and stdout
|
||||
if 'intercept-stdout' in self._config and self._config['intercept-stdout']:
|
||||
sys.stdout = self._default_stdout
|
||||
sys.stderr = self._default_stderr
|
||||
|
||||
|
||||
def main():
|
||||
import signal
|
||||
from gitautodeploy import GitAutoDeploy
|
||||
from cli.config import get_config_defaults, get_config_from_environment
|
||||
from cli.config import get_config_from_argv, find_config_file
|
||||
from cli.config import get_config_from_file, get_repo_config_from_environment
|
||||
from cli.config import init_config, get_config_file_path, rename_legacy_attribute_names
|
||||
from cli.config import ConfigFileNotFoundException, ConfigFileInvalidException
|
||||
import logging
|
||||
import sys
|
||||
import os
|
||||
|
||||
logger = logging.getLogger()
|
||||
|
||||
app = GitAutoDeploy()
|
||||
|
||||
if hasattr(signal, 'SIGHUP'):
|
||||
signal.signal(signal.SIGHUP, app.signal_handler)
|
||||
if hasattr(signal, 'SIGINT'):
|
||||
signal.signal(signal.SIGINT, app.signal_handler)
|
||||
if hasattr(signal, 'SIGABRT'):
|
||||
signal.signal(signal.SIGABRT, app.signal_handler)
|
||||
if hasattr(signal, 'SIGPIPE') and hasattr(signal, 'SIG_IGN'):
|
||||
signal.signal(signal.SIGPIPE, signal.SIG_IGN)
|
||||
|
||||
# Get default config values
|
||||
config = get_config_defaults()
|
||||
|
||||
# Get config values from environment variables and commadn line arguments
|
||||
environment_config = get_config_from_environment()
|
||||
argv_config = get_config_from_argv(sys.argv[1:])
|
||||
|
||||
# Merge config values from environment variables
|
||||
config.update(environment_config)
|
||||
|
||||
search_target = os.path.dirname(os.path.realpath(__file__))
|
||||
config_file_path = get_config_file_path(environment_config, argv_config, search_target)
|
||||
|
||||
# Config file path provided or found?
|
||||
if config_file_path:
|
||||
|
||||
try:
|
||||
file_config = get_config_from_file(config_file_path)
|
||||
except ConfigFileNotFoundException as e:
|
||||
app.setup_console_logger()
|
||||
logger.critical("No config file not found at '%s'" % e)
|
||||
return
|
||||
except ConfigFileInvalidException as e:
|
||||
app.setup_console_logger()
|
||||
logger.critical("Unable to read config file due to invalid JSON format in '%s'" % e)
|
||||
return
|
||||
|
||||
# Merge config values from config file (overrides environment variables)
|
||||
config.update(file_config)
|
||||
|
||||
# Merge config value from command line (overrides environment variables and config file)
|
||||
config.update(argv_config)
|
||||
|
||||
# Rename legacy config option names
|
||||
config = rename_legacy_attribute_names(config)
|
||||
|
||||
# Extend config data with any repository defined by environment variables
|
||||
repo_config = get_repo_config_from_environment()
|
||||
|
||||
if repo_config:
|
||||
|
||||
if not 'repositories' in config:
|
||||
config['repositories'] = []
|
||||
|
||||
config['repositories'].append(repo_config)
|
||||
|
||||
# Initialize config by expanding with missing values
|
||||
init_config(config)
|
||||
|
||||
app.setup(config)
|
||||
app.serve_forever()
|
||||
@@ -0,0 +1,346 @@
|
||||
from __future__ import absolute_import
|
||||
from .events import WebhookAction
|
||||
from .parsers import get_service_handler
|
||||
|
||||
|
||||
def WebhookRequestHandlerFactory(config, event_store, server_status, is_https=False):
|
||||
"""Factory method for webhook request handler class"""
|
||||
try:
|
||||
from SimpleHTTPServer import SimpleHTTPRequestHandler
|
||||
except ImportError as e:
|
||||
from http.server import SimpleHTTPRequestHandler
|
||||
|
||||
class WebhookRequestHandler(SimpleHTTPRequestHandler, object):
|
||||
"""Extends the BaseHTTPRequestHandler class and handles the incoming
|
||||
HTTP requests."""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self._config = config
|
||||
self._event_store = event_store
|
||||
self._server_status = server_status
|
||||
self._is_https = is_https
|
||||
super(WebhookRequestHandler, self).__init__(*args, **kwargs)
|
||||
|
||||
def end_headers(self):
|
||||
self.send_header('Access-Control-Allow-Origin', '*')
|
||||
SimpleHTTPRequestHandler.end_headers(self)
|
||||
|
||||
def do_HEAD(self):
|
||||
|
||||
# Web UI needs to be enabled
|
||||
if not self.validate_web_ui_enabled():
|
||||
return
|
||||
|
||||
# Web UI might require HTTPS
|
||||
if not self.validate_web_ui_https():
|
||||
return
|
||||
|
||||
# Client needs to be whitelisted
|
||||
if not self.validate_web_ui_whitelist():
|
||||
return
|
||||
|
||||
# Client needs to authenticate
|
||||
if not self.validate_web_ui_basic_auth():
|
||||
return
|
||||
|
||||
return SimpleHTTPRequestHandler.do_HEAD(self)
|
||||
|
||||
def do_GET(self):
|
||||
|
||||
# Web UI needs to be enabled
|
||||
if not self.validate_web_ui_enabled():
|
||||
return
|
||||
|
||||
# Web UI might require HTTPS
|
||||
if not self.validate_web_ui_https():
|
||||
return
|
||||
|
||||
# Client needs to be whitelisted
|
||||
if not self.validate_web_ui_whitelist():
|
||||
return
|
||||
|
||||
# Client needs to authenticate
|
||||
if not self.validate_web_ui_basic_auth():
|
||||
return
|
||||
|
||||
# Handle status API call
|
||||
if self.path == "/api/status":
|
||||
self.handle_status_api()
|
||||
return
|
||||
|
||||
# Serve static file
|
||||
return SimpleHTTPRequestHandler.do_GET(self)
|
||||
|
||||
def handle_status_api(self):
|
||||
import json
|
||||
from os import urandom
|
||||
from base64 import b64encode
|
||||
|
||||
data = {
|
||||
'events': self._event_store.dict_repr(),
|
||||
'auth-key': self._server_status['auth-key']
|
||||
}
|
||||
|
||||
data.update(self.get_server_status())
|
||||
|
||||
self.send_response(200, 'OK')
|
||||
self.send_header('Content-type', 'application/json')
|
||||
self.end_headers()
|
||||
self.wfile.write(json.dumps(data).encode('utf-8'))
|
||||
|
||||
def do_POST(self):
|
||||
"""Invoked on incoming POST requests"""
|
||||
from threading import Timer
|
||||
import logging
|
||||
import json
|
||||
import threading
|
||||
from urlparse import parse_qs
|
||||
|
||||
logger = logging.getLogger()
|
||||
|
||||
content_length = int(self.headers.get('content-length'))
|
||||
request_body = self.rfile.read(content_length).decode('utf-8')
|
||||
|
||||
# Extract request headers and make all keys to lowercase (makes them easier to compare)
|
||||
request_headers = dict(self.headers)
|
||||
request_headers = dict((k.lower(), v) for k, v in request_headers.items())
|
||||
|
||||
action = WebhookAction(self.client_address, request_headers, request_body)
|
||||
self._event_store.register_action(action)
|
||||
action.set_waiting(True)
|
||||
|
||||
action.log_info('Incoming request from %s:%s' % (self.client_address[0], self.client_address[1]))
|
||||
|
||||
# Payloads from GitHub can be delivered as form data. Test the request for this pattern and extract json payload
|
||||
if request_headers['content-type'] == 'application/x-www-form-urlencoded':
|
||||
res = parse_qs(request_body.decode('utf-8'))
|
||||
if 'payload' in res and len(res['payload']) == 1:
|
||||
request_body = res['payload'][0]
|
||||
|
||||
# Test case debug data
|
||||
test_case = {
|
||||
'headers': dict(self.headers),
|
||||
'payload': json.loads(request_body),
|
||||
'config': {},
|
||||
'expected': {'status': 200, 'data': [{'deploy': 0}]}
|
||||
}
|
||||
|
||||
try:
|
||||
|
||||
# Will raise a ValueError exception if it fails
|
||||
ServiceRequestHandler = get_service_handler(request_headers, request_body, action)
|
||||
|
||||
# Unable to identify the source of the request
|
||||
if not ServiceRequestHandler:
|
||||
self.send_error(400, 'Unrecognized service')
|
||||
test_case['expected']['status'] = 400
|
||||
action.log_error("Unable to find appropriate handler for request. The source service is not supported")
|
||||
action.set_waiting(False)
|
||||
action.set_success(False)
|
||||
return
|
||||
|
||||
service_handler = ServiceRequestHandler(self._config)
|
||||
|
||||
action.log_info("Handling the request with %s" % ServiceRequestHandler.__name__)
|
||||
|
||||
# Could be GitHubParser, GitLabParser or other
|
||||
projects = service_handler.get_matching_projects(request_headers, request_body, action)
|
||||
|
||||
action.log_info("%s candidates matches the request" % len(projects))
|
||||
|
||||
# request_filter = WebhookRequestFilter()
|
||||
|
||||
if len(projects) == 0:
|
||||
self.send_error(400, 'Bad request')
|
||||
test_case['expected']['status'] = 400
|
||||
action.log_error("No matching projects")
|
||||
action.set_waiting(False)
|
||||
action.set_success(False)
|
||||
return
|
||||
|
||||
# Apply filters
|
||||
matching_projects = []
|
||||
for project in projects:
|
||||
if project.apply_filters(request_headers, request_body, action):
|
||||
matching_projects.append(project)
|
||||
|
||||
# Only keep projects that matches
|
||||
projects = matching_projects
|
||||
|
||||
action.log_info("%s candidates matches after applying filters" % len(projects))
|
||||
|
||||
if not service_handler.validate_request(request_headers, request_body, projects, action):
|
||||
self.send_error(400, 'Bad request')
|
||||
test_case['expected']['status'] = 400
|
||||
action.log_warning("Request was rejected due to a secret token mismatch")
|
||||
action.set_waiting(False)
|
||||
action.set_success(False)
|
||||
return
|
||||
|
||||
test_case['expected']['status'] = 200
|
||||
|
||||
self.send_response(200, 'OK')
|
||||
self.send_header('Content-type', 'text/plain')
|
||||
self.end_headers()
|
||||
|
||||
if len(projects) == 0:
|
||||
action.set_waiting(False)
|
||||
action.set_success(False)
|
||||
return
|
||||
|
||||
action.log_info("Proceeding with %s candidates" % len(projects))
|
||||
action.set_waiting(False)
|
||||
action.set_success(True)
|
||||
|
||||
for project in projects:
|
||||
|
||||
# Schedule the execution of the webhook (git pull and trigger deploy etc)
|
||||
thread = threading.Thread(target=project.execute_webhook, args=[self._event_store])
|
||||
thread.start()
|
||||
|
||||
# Add additional test case data
|
||||
test_case['config'] = {
|
||||
'url': 'url' in project and project['url'],
|
||||
'branch': 'branch' in project and project['branch'],
|
||||
'remote': 'remote' in project and project['remote'],
|
||||
'deploy': 'echo test!'
|
||||
}
|
||||
|
||||
except ValueError as e:
|
||||
self.send_error(400, 'Unprocessable request')
|
||||
action.log_warning('Unable to process incoming request from %s:%s' % (self.client_address[0], self.client_address[1]))
|
||||
test_case['expected']['status'] = 400
|
||||
action.set_waiting(False)
|
||||
action.set_success(False)
|
||||
return
|
||||
|
||||
except Exception as e:
|
||||
self.send_error(500, 'Unable to process request')
|
||||
test_case['expected']['status'] = 500
|
||||
action.log_warning("Unable to process request")
|
||||
action.set_waiting(False)
|
||||
action.set_success(False)
|
||||
|
||||
raise e
|
||||
|
||||
finally:
|
||||
|
||||
# Save the request as a test case
|
||||
if 'log-test-case' in self._config and self._config['log-test-case']:
|
||||
self.save_test_case(test_case)
|
||||
|
||||
def log_message(self, format, *args):
|
||||
"""Overloads the default message logging method to allow messages to
|
||||
go through our custom logger instead."""
|
||||
import logging
|
||||
logger = logging.getLogger()
|
||||
logger.info("%s - %s" % (self.client_address[0], format%args))
|
||||
|
||||
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()
|
||||
|
||||
def get_server_status(self):
|
||||
"""Generate a copy of the server status object that contains the public IP or hostname."""
|
||||
|
||||
server_status = {}
|
||||
for item in self._server_status.items():
|
||||
key, value = item
|
||||
public_host = self.headers.get('host').split(':')[0]
|
||||
|
||||
if key == 'http-uri':
|
||||
server_status[key] = value.replace(self._config['http-host'], public_host)
|
||||
|
||||
if key == 'https-uri':
|
||||
server_status[key] = value.replace(self._config['https-host'], public_host)
|
||||
|
||||
if key == 'wss-uri':
|
||||
server_status[key] = value.replace(self._config['wss-host'], public_host)
|
||||
|
||||
return server_status
|
||||
|
||||
def validate_web_ui_enabled(self):
|
||||
"""Verify that the Web UI is enabled"""
|
||||
|
||||
if self._config['web-ui-enabled']:
|
||||
return True
|
||||
|
||||
self.send_error(403, "Web UI is not enabled")
|
||||
return False
|
||||
|
||||
def validate_web_ui_https(self):
|
||||
"""Verify that the request is made over HTTPS"""
|
||||
|
||||
if self._is_https:
|
||||
return True
|
||||
|
||||
if not self._config['web-ui-require-https']:
|
||||
return True
|
||||
|
||||
# Attempt to redirect the request to HTTPS
|
||||
server_status = self.get_server_status()
|
||||
if 'https-uri' in server_status:
|
||||
self.send_response(307)
|
||||
self.send_header('Location', '%s%s' % (server_status['https-uri'], self.path))
|
||||
self.end_headers()
|
||||
return False
|
||||
|
||||
self.send_error(403, "Web UI is only accessible through HTTPS")
|
||||
return False
|
||||
|
||||
def validate_web_ui_whitelist(self):
|
||||
"""Verify that the client address is whitelisted"""
|
||||
|
||||
# Allow all if whitelist is empty
|
||||
if len(self._config['web-ui-whitelist']) == 0:
|
||||
return True
|
||||
|
||||
# Verify that client IP is whitelisted
|
||||
if self.client_address[0] in self._config['web-ui-whitelist']:
|
||||
return True
|
||||
|
||||
self.send_error(403, "%s is not allowed access" % self.client_address[0])
|
||||
return False
|
||||
|
||||
def validate_web_ui_basic_auth(self):
|
||||
"""Authenticate the user"""
|
||||
import base64
|
||||
|
||||
if not self._config['web-ui-auth-enabled']:
|
||||
return True
|
||||
|
||||
# Verify that a username and password is specified in the config
|
||||
if self._config['web-ui-username'] is None or self._config['web-ui-password'] is None:
|
||||
self.send_error(403, "Authentication credentials missing in config")
|
||||
return False
|
||||
|
||||
# Verify that the provided username and password matches the ones in the config
|
||||
key = base64.b64encode("%s:%s" % (self._config['web-ui-username'], self._config['web-ui-password']))
|
||||
if self.headers.getheader('Authorization') == 'Basic ' + key:
|
||||
return True
|
||||
|
||||
# Let the client know that authentication is required
|
||||
self.send_response(401)
|
||||
self.send_header('WWW-Authenticate', 'Basic realm=\"GAD\"')
|
||||
self.send_header('Content-type', 'text/html')
|
||||
self.end_headers()
|
||||
self.wfile.write('Not authenticated')
|
||||
return False
|
||||
|
||||
return WebhookRequestHandler
|
||||
@@ -0,0 +1,54 @@
|
||||
|
||||
class Lock():
|
||||
"""Simple implementation of a mutex lock using the file systems. Works on
|
||||
*nix systems."""
|
||||
|
||||
path = None
|
||||
lock = None
|
||||
|
||||
def __init__(self, path):
|
||||
try:
|
||||
from lockfile import LockFile
|
||||
except ImportError:
|
||||
from lockfile import FileLock
|
||||
# Different naming in older versions of lockfile
|
||||
LockFile = FileLock
|
||||
|
||||
self.path = path
|
||||
self.lock = LockFile(path)
|
||||
|
||||
def obtain(self):
|
||||
import os
|
||||
import logging
|
||||
from lockfile import AlreadyLocked
|
||||
logger = logging.getLogger()
|
||||
|
||||
try:
|
||||
self.lock.acquire(0)
|
||||
logger.debug("Successfully obtained lock: %s" % self.path)
|
||||
except AlreadyLocked:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def release(self):
|
||||
import os
|
||||
import logging
|
||||
logger = logging.getLogger()
|
||||
|
||||
if not self.has_lock():
|
||||
raise Exception("Unable to release lock that is owned by another process")
|
||||
|
||||
self.lock.release()
|
||||
logger.debug("Successfully released lock: %s" % self.path)
|
||||
|
||||
def has_lock(self):
|
||||
return self.lock.i_am_locking()
|
||||
|
||||
def clear(self):
|
||||
import os
|
||||
import logging
|
||||
logger = logging.getLogger()
|
||||
|
||||
self.lock.break_lock()
|
||||
logger.debug("Successfully cleared lock: %s" % self.path)
|
||||
@@ -0,0 +1 @@
|
||||
from .project import *
|
||||
@@ -0,0 +1,210 @@
|
||||
import collections
|
||||
from ..wrappers import GitWrapper
|
||||
from ..lock import Lock
|
||||
from ..wrappers import GitWrapper
|
||||
from ..events import DeployEvent
|
||||
|
||||
|
||||
class Project(collections.MutableMapping):
|
||||
|
||||
"""A dictionary that applies an arbitrary key-altering
|
||||
function before accessing the keys"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.store = dict()
|
||||
self.update(dict(*args, **kwargs)) # use the free update to set keys
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self.store[self.__keytransform__(key)]
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
self.store[self.__keytransform__(key)] = value
|
||||
|
||||
def __delitem__(self, key):
|
||||
del self.store[self.__keytransform__(key)]
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self.store)
|
||||
|
||||
def __len__(self):
|
||||
return len(self.store)
|
||||
|
||||
def __keytransform__(self, key):
|
||||
return key
|
||||
|
||||
def get_name(self):
|
||||
return self['url'].split('/')[-1].split('.git')[0]
|
||||
|
||||
def passes_payload_filter(self, payload, action):
|
||||
|
||||
# At least one filter must match
|
||||
for filter in self['payload-filter']:
|
||||
|
||||
# All options specified in the filter must match
|
||||
for filter_key, filter_value in filter.items():
|
||||
|
||||
# Ignore filters with value None (let them pass)
|
||||
if filter_value == None:
|
||||
continue
|
||||
|
||||
# Interpret dots in filter name as path notations
|
||||
node_value = payload
|
||||
for node_key in filter_key.split('.'):
|
||||
|
||||
# If the path is not valid the filter does not match
|
||||
if not node_key in node_value:
|
||||
action.log_info("Filter '%s' does not match since the path is invalid" % (filter_key))
|
||||
|
||||
# Filter does not match, do not process this repo config
|
||||
return False
|
||||
|
||||
node_value = node_value[node_key]
|
||||
|
||||
if filter_value == node_value:
|
||||
continue
|
||||
|
||||
# If the filter value is set to True. the filter
|
||||
# will pass regardless of the actual value
|
||||
if filter_value == True:
|
||||
continue
|
||||
|
||||
action.log_debug("Filter '%s' does not match ('%s' != '%s')" % (filter_key, filter_value, (str(node_value)[:75] + '..') if len(str(node_value)) > 75 else str(node_value)))
|
||||
|
||||
# Filter does not match, do not process this repo config
|
||||
return False
|
||||
|
||||
# Filter does match, proceed
|
||||
return True
|
||||
|
||||
def passes_header_filter(self, request_headers):
|
||||
|
||||
# At least one filter must match
|
||||
for key in self['header-filter']:
|
||||
|
||||
# Verify that the request has the required header attribute
|
||||
if key.lower() not in request_headers:
|
||||
return False
|
||||
|
||||
# "True" indicates that any header value is accepted
|
||||
if self['header-filter'][key] is True:
|
||||
continue
|
||||
|
||||
# Verify that the request has the required header value
|
||||
if self['header-filter'][key] != request_headers[key.lower()]:
|
||||
return False
|
||||
|
||||
# Filter does match, proceed
|
||||
return True
|
||||
|
||||
def apply_filters(self, request_headers, request_body, action):
|
||||
"""Verify that the suggested repositories has matching settings and
|
||||
issue git pull and/or deploy commands."""
|
||||
import os
|
||||
import time
|
||||
import json
|
||||
|
||||
payload = json.loads(request_body)
|
||||
|
||||
# Verify that all payload filters matches the request (if any payload filters are specified)
|
||||
if 'payload-filter' in self and not self.passes_payload_filter(payload, action):
|
||||
|
||||
# Filter does not match, do not process this repo config
|
||||
return False
|
||||
|
||||
# Verify that all header filters matches the request (if any header filters are specified)
|
||||
if 'header-filter' in self and not self.passes_header_filter(request_headers):
|
||||
|
||||
# Filter does not match, do not process this repo config
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def execute_webhook(self, event_store):
|
||||
"""Verify that the suggested repositories has matching settings and
|
||||
issue git pull and/or deploy commands."""
|
||||
import os
|
||||
import time
|
||||
import json
|
||||
|
||||
event = DeployEvent(self)
|
||||
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 self:
|
||||
res = GitWrapper.deploy(self)
|
||||
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(self['path']):
|
||||
event.log_error("The repository '%s' does not exist locally. Make sure it was pulled properly without errors by reviewing the log." % self['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(self['path'], os.W_OK):
|
||||
event.log_error("The path '%s' is not writable. Make sure that GAD has write access to that path." % self['path'])
|
||||
event.set_waiting(False)
|
||||
event.set_success(False)
|
||||
return
|
||||
|
||||
running_lock = Lock(os.path.join(self['path'], 'status_running'))
|
||||
waiting_lock = Lock(os.path.join(self['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(self)
|
||||
|
||||
# Return code indicating success?
|
||||
if res == 0:
|
||||
break
|
||||
|
||||
n -= 1
|
||||
|
||||
if 0 < n:
|
||||
res = GitWrapper.deploy(self)
|
||||
|
||||
#except Exception as e:
|
||||
# logger.error('Error during \'pull\' or \'deploy\' operation on path: %s' % self['path'])
|
||||
# logger.error(e)
|
||||
# raise e
|
||||
|
||||
finally:
|
||||
|
||||
# Release the lock if it's ours
|
||||
if running_lock.has_lock():
|
||||
running_lock.release()
|
||||
|
||||
# Release the lock if it's ours
|
||||
if waiting_lock.has_lock():
|
||||
waiting_lock.release()
|
||||
|
||||
event.log_info("Deploy commands were executed")
|
||||
event.set_waiting(False)
|
||||
event.set_success(True)
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
from .bitbucket import BitBucketRequestParser
|
||||
from .github import GitHubRequestParser
|
||||
from .gitlab import GitLabRequestParser
|
||||
from .gitlabci import GitLabCIRequestParser
|
||||
from .generic import GenericRequestParser
|
||||
from .coding import CodingRequestParser
|
||||
|
||||
|
||||
def get_service_handler(request_headers, request_body, action):
|
||||
"""Parses the incoming request and attempts to determine whether
|
||||
it originates from GitHub, GitLab or any other known service."""
|
||||
import json
|
||||
|
||||
payload = json.loads(request_body)
|
||||
|
||||
if not isinstance(payload, dict):
|
||||
raise ValueError("Invalid JSON object")
|
||||
|
||||
user_agent = 'user-agent' in request_headers and request_headers['user-agent']
|
||||
content_type = 'content-type' in request_headers and request_headers['content-type']
|
||||
|
||||
# Assume Coding if the X-Coding-Event HTTP header is set
|
||||
if 'x-coding-event' in request_headers:
|
||||
return CodingRequestParser
|
||||
|
||||
# Assume GitLab if the X-Gitlab-Event HTTP header is set
|
||||
elif 'x-gitlab-event' in request_headers:
|
||||
|
||||
# Special Case for Gitlab CI
|
||||
if content_type == "application/json" and "build_status" in payload:
|
||||
return GitLabCIRequestParser
|
||||
else:
|
||||
return GitLabRequestParser
|
||||
|
||||
# Assume GitHub if the X-GitHub-Event HTTP header is set
|
||||
elif 'x-github-event' in request_headers:
|
||||
|
||||
return GitHubRequestParser
|
||||
|
||||
# Assume BitBucket if the User-Agent HTTP header is set to
|
||||
# 'Bitbucket-Webhooks/2.0' (or something similar)
|
||||
elif user_agent and user_agent.lower().find('bitbucket') != -1:
|
||||
|
||||
return BitBucketRequestParser
|
||||
|
||||
# This handles old GitLab requests and Gogs requests for example.
|
||||
elif content_type == "application/json":
|
||||
|
||||
action.log_info("Received event from unknown origin.")
|
||||
return GenericRequestParser
|
||||
|
||||
action.log_error("Unable to recognize request origin. Don't know how to handle the request.")
|
||||
return
|
||||
@@ -0,0 +1,29 @@
|
||||
class WebhookRequestParserBase(object):
|
||||
"""Abstract parent class for git service parsers. Contains helper
|
||||
methods."""
|
||||
|
||||
def __init__(self, config):
|
||||
self._config = config
|
||||
|
||||
def get_matching_repo_configs(self, urls, action):
|
||||
"""Iterates over the various repo URLs provided as argument (git://,
|
||||
ssh:// and https:// for the repo) and compare them to any repo URL
|
||||
specified in the config"""
|
||||
|
||||
configs = []
|
||||
for url in urls:
|
||||
for repo_config in self._config['repositories']:
|
||||
if repo_config in configs:
|
||||
continue
|
||||
if repo_config.get('match-url', repo_config.get('url')) == url:
|
||||
configs.append(repo_config)
|
||||
elif 'url_without_usernme' in repo_config and repo_config['url_without_usernme'] == url:
|
||||
configs.append(repo_config)
|
||||
|
||||
if len(configs) == 0:
|
||||
action.log_warning('The URLs references in the webhook did not match any repository entry in the config. For this webhook to work, make sure you have at least one repository configured with one of the following URLs; %s' % ', '.join(urls))
|
||||
|
||||
return configs
|
||||
|
||||
def validate_request(self, request_headers, request_body, repo_configs, action):
|
||||
return True
|
||||
@@ -0,0 +1,44 @@
|
||||
from .base import WebhookRequestParserBase
|
||||
|
||||
|
||||
class BitBucketRequestParser(WebhookRequestParserBase):
|
||||
|
||||
def get_matching_projects(self, request_headers, request_body, action):
|
||||
import json
|
||||
|
||||
data = json.loads(request_body)
|
||||
|
||||
repo_urls = []
|
||||
|
||||
action.log_debug("Received event from BitBucket")
|
||||
|
||||
if 'repository' not in data:
|
||||
action.log_error("Unable to recognize data format")
|
||||
return []
|
||||
|
||||
# One repository may posses multiple URLs for different protocols
|
||||
for k in ['url', 'git_url', 'clone_url', 'ssh_url']:
|
||||
if k in data['repository']:
|
||||
repo_urls.append(data['repository'][k])
|
||||
|
||||
if 'full_name' in data['repository']:
|
||||
repo_urls.append('git@bitbucket.org:%s.git' % data['repository']['full_name'])
|
||||
|
||||
# Add a simplified version of the bitbucket HTTPS URL - without the username@bitbucket.com part. This is
|
||||
# needed since the configured repositories might be configured using a different username.
|
||||
repo_urls.append('https://bitbucket.org/%s.git' % (data['repository']['full_name']))
|
||||
|
||||
if 'fullName' in data['repository']:
|
||||
# For Bitbucket Server add repository name (OWNER/REPO_NAME).
|
||||
# Support "Post Webhooks for Bitbucket".
|
||||
repo_urls.append(data['repository']['fullName'])
|
||||
|
||||
if 'slug' in data['repository']:
|
||||
# For Bitbucket Server add repository slug.
|
||||
# Support "Post-Receive WebHooks" and "Post Webhooks for Bitbucket".
|
||||
repo_urls.append(data['repository']['slug'])
|
||||
|
||||
# Get a list of configured repositories that matches the incoming web hook reqeust
|
||||
repo_configs = self.get_matching_repo_configs(repo_urls, action)
|
||||
|
||||
return repo_configs
|
||||
@@ -0,0 +1,40 @@
|
||||
from .base import WebhookRequestParserBase
|
||||
|
||||
|
||||
class CodingRequestParser(WebhookRequestParserBase):
|
||||
|
||||
def get_matching_projects(self, request_headers, request_body, action):
|
||||
import json
|
||||
|
||||
data = json.loads(request_body)
|
||||
|
||||
repo_urls = []
|
||||
|
||||
coding_event = 'x-coding-event' in request_headers and request_headers['x-coding-event']
|
||||
|
||||
if 'repository' not in data:
|
||||
action.log_error("Unable to recognize data format")
|
||||
return []
|
||||
|
||||
# One repository may posses multiple URLs for different protocols
|
||||
for k in ['web_url', 'https_url', 'ssh_url']:
|
||||
if k in data['repository']:
|
||||
repo_urls.append(data['repository'][k])
|
||||
|
||||
# Get a list of configured repositories that matches the incoming web hook reqeust
|
||||
items = self.get_matching_repo_configs(repo_urls, action)
|
||||
|
||||
repo_configs = []
|
||||
for repo_config in items:
|
||||
# Validate secret token if present
|
||||
if 'secret-token' in repo_config:
|
||||
if 'token' not in data or not self.verify_token(repo_config['secret-token'], data['token']):
|
||||
action.log_warning("Request token does not match the 'secret-token' configured for repository %s." % repo_config['url'])
|
||||
continue
|
||||
|
||||
repo_configs.append(repo_config)
|
||||
|
||||
return repo_configs
|
||||
|
||||
def verify_token(self, secret_token, request_token):
|
||||
return secret_token == request_token
|
||||
@@ -0,0 +1,28 @@
|
||||
from .base import WebhookRequestParserBase
|
||||
|
||||
|
||||
class GenericRequestParser(WebhookRequestParserBase):
|
||||
|
||||
def get_matching_projects(self, request_headers, request_body, action):
|
||||
import json
|
||||
|
||||
data = json.loads(request_body)
|
||||
|
||||
repo_urls = []
|
||||
|
||||
action.log_info("Received event from unknown origin. Assume generic data format.")
|
||||
|
||||
if 'repository' not in data:
|
||||
action.log_error("Unable to recognize data format")
|
||||
return []
|
||||
|
||||
# One repository may posses multiple URLs for different protocols
|
||||
for k in ['url', 'git_http_url', 'git_ssh_url', 'http_url', 'ssh_url']:
|
||||
if k in data['repository']:
|
||||
repo_urls.append(data['repository'][k])
|
||||
|
||||
# Get a list of configured repositories that matches the incoming web hook reqeust
|
||||
repo_configs = self.get_matching_repo_configs(repo_urls, action)
|
||||
|
||||
return repo_configs
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
from .base import WebhookRequestParserBase
|
||||
|
||||
|
||||
class GitHubRequestParser(WebhookRequestParserBase):
|
||||
|
||||
def get_matching_projects(self, request_headers, request_body, action):
|
||||
import json
|
||||
|
||||
data = json.loads(request_body)
|
||||
|
||||
repo_urls = []
|
||||
|
||||
github_event = 'x-github-event' in request_headers and request_headers['x-github-event']
|
||||
|
||||
action.log_info("Received '%s' event from GitHub" % github_event)
|
||||
|
||||
if 'repository' not in data:
|
||||
action.log_error("Unable to recognize data format")
|
||||
return []
|
||||
|
||||
# One repository may posses multiple URLs for different protocols
|
||||
for k in ['url', 'git_url', 'clone_url', 'ssh_url']:
|
||||
if k in data['repository']:
|
||||
repo_urls.append(data['repository'][k])
|
||||
|
||||
# Get a list of configured repositories that matches the incoming web hook reqeust
|
||||
repo_configs = self.get_matching_repo_configs(repo_urls, action)
|
||||
|
||||
return repo_configs
|
||||
|
||||
def validate_request(self, request_headers, request_body, repo_configs, action):
|
||||
|
||||
for repo_config in repo_configs:
|
||||
|
||||
# Validate secret token if present
|
||||
if 'secret-token' in repo_config and 'x-hub-signature' in request_headers:
|
||||
if not self.verify_signature(repo_config['secret-token'], request_body, request_headers['x-hub-signature']):
|
||||
action.log_info("Request signature does not match the 'secret-token' configured for repository %s." % repo_config['url'])
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def verify_signature(self, token, body, signature):
|
||||
import hashlib
|
||||
import hmac
|
||||
|
||||
result = "sha1=" + hmac.new(str(token), body, hashlib.sha1).hexdigest()
|
||||
return result == signature
|
||||
@@ -0,0 +1,42 @@
|
||||
from .base import WebhookRequestParserBase
|
||||
|
||||
|
||||
class GitLabRequestParser(WebhookRequestParserBase):
|
||||
|
||||
def get_matching_projects(self, request_headers, request_body, action):
|
||||
import json
|
||||
|
||||
data = json.loads(request_body)
|
||||
|
||||
repo_urls = []
|
||||
|
||||
gitlab_event = 'x-gitlab-event' in request_headers and request_headers['x-gitlab-event']
|
||||
|
||||
action.log_info("Received '%s' event from GitLab" % gitlab_event)
|
||||
|
||||
if 'repository' not in data:
|
||||
action.log_error("Unable to recognize data format")
|
||||
return []
|
||||
|
||||
# One repository may posses multiple URLs for different protocols
|
||||
for k in ['url', 'git_http_url', 'git_ssh_url']:
|
||||
if k in data['repository']:
|
||||
repo_urls.append(data['repository'][k])
|
||||
|
||||
# Get a list of configured repositories that matches the incoming web hook reqeust
|
||||
repo_configs = self.get_matching_repo_configs(repo_urls, action)
|
||||
|
||||
return repo_configs
|
||||
|
||||
def validate_request(self, request_headers, request_body, repo_configs, action):
|
||||
|
||||
for repo_config in repo_configs:
|
||||
|
||||
# Validate secret token if present
|
||||
if 'secret-token' in repo_config and 'x-gitlab-token' in request_headers:
|
||||
|
||||
if repo_config['secret-token'] != request_headers['x-gitlab-token']:
|
||||
action.log_info("Request token does not match the 'secret-token' configured for repository %s." % repo_config['url'])
|
||||
return False
|
||||
|
||||
return True
|
||||
@@ -0,0 +1,31 @@
|
||||
from .base import WebhookRequestParserBase
|
||||
|
||||
|
||||
class GitLabCIRequestParser(WebhookRequestParserBase):
|
||||
|
||||
def get_matching_projects(self, request_headers, request_body, action):
|
||||
import json
|
||||
|
||||
data = json.loads(request_body)
|
||||
|
||||
repo_urls = []
|
||||
|
||||
action.log_info('Received event from Gitlab CI')
|
||||
|
||||
if 'repository' not in data:
|
||||
action.log_error("Unable to recognize data format")
|
||||
return []
|
||||
|
||||
# Only add repositories if the build is successful. Ignore it in other case.
|
||||
if data['build_status'] == "success":
|
||||
for k in ['url', 'git_http_url', 'git_ssh_url']:
|
||||
for n in ['repository', 'project']:
|
||||
if n in data and k in data[n]:
|
||||
repo_urls.append(data[n][k])
|
||||
else:
|
||||
action.log_warning("Gitlab CI build '%d' has status '%s'. Not pull will be done" % (data['build_id'], data['build_status']))
|
||||
|
||||
# Get a list of configured repositories that matches the incoming web hook reqeust
|
||||
repo_configs = self.get_matching_repo_configs(repo_urls, action)
|
||||
|
||||
return repo_configs
|
||||
@@ -0,0 +1,2 @@
|
||||
from .git import *
|
||||
from .process import *
|
||||
@@ -0,0 +1,158 @@
|
||||
class GitWrapper():
|
||||
"""Wraps the git client. Currently uses git through shell command
|
||||
invocations."""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def init(repo_config):
|
||||
"""Init remote url of the repo from the git server"""
|
||||
import logging
|
||||
from .process import ProcessWrapper
|
||||
import os
|
||||
import platform
|
||||
|
||||
logger = logging.getLogger()
|
||||
logger.info("Initializing repository %s" % repo_config['path'])
|
||||
|
||||
commands = []
|
||||
|
||||
# On Windows, bash command needs to be run using bash.exe. This assumes bash.exe
|
||||
# (typically installed under C:\Program Files\Git\bin) is in the system PATH.
|
||||
if platform.system().lower() == "windows":
|
||||
commands.append('bash -c "cd \\"' + repo_config['path'] + '\\" && unset GIT_DIR"')
|
||||
else:
|
||||
commands.append('unset GIT_DIR')
|
||||
|
||||
commands.append('git remote set-url ' + repo_config['remote'] + " " + repo_config['url'])
|
||||
commands.append('git fetch ' + repo_config['remote'])
|
||||
commands.append('git checkout -f -B ' + repo_config['branch'] + ' -t ' + repo_config['remote'] + '/' + repo_config['branch'])
|
||||
commands.append('git submodule update --init --recursive')
|
||||
|
||||
# All commands need to success
|
||||
for command in commands:
|
||||
res = ProcessWrapper().call(command, cwd=repo_config['path'], shell=True, supressStderr=True)
|
||||
|
||||
if res != 0:
|
||||
logger.error("Command '%s' failed with exit code %s" % (command, res))
|
||||
break
|
||||
|
||||
if res == 0 and os.path.isdir(repo_config['path']):
|
||||
logger.info("Repository %s successfully initialized" % repo_config['path'])
|
||||
else:
|
||||
logger.error("Unable to init repository %s" % repo_config['path'])
|
||||
|
||||
return int(res)
|
||||
|
||||
@staticmethod
|
||||
def pull(repo_config):
|
||||
"""Pulls the latest version of the repo from the git server"""
|
||||
import logging
|
||||
from .process import ProcessWrapper
|
||||
import os
|
||||
import platform
|
||||
|
||||
logger = logging.getLogger()
|
||||
logger.info("Updating repository %s" % repo_config['path'])
|
||||
|
||||
# Only pull if there is actually a local copy of the repository
|
||||
if 'path' not in repo_config:
|
||||
logger.info('No local repository path configured, no pull will occure')
|
||||
return 0
|
||||
|
||||
commands = []
|
||||
|
||||
# On Windows, bash command needs to be run using bash.exe. This assumes bash.exe
|
||||
# (typically installed under C:\Program Files\Git\bin) is in the system PATH.
|
||||
if platform.system().lower() == "windows":
|
||||
commands.append('bash -c "cd \\"' + repo_config['path'] + '\\" && unset GIT_DIR"')
|
||||
else:
|
||||
commands.append('unset GIT_DIR')
|
||||
|
||||
if "prepull" in repo_config:
|
||||
commands.append(repo_config['prepull'])
|
||||
|
||||
commands.append('git fetch ' + repo_config['remote'])
|
||||
commands.append('git reset --hard ' + repo_config['remote'] + "/" + repo_config['branch'])
|
||||
commands.append('git submodule update --init --recursive')
|
||||
|
||||
if "postpull" in repo_config:
|
||||
commands.append(repo_config['postpull'])
|
||||
|
||||
# All commands need to success
|
||||
for command in commands:
|
||||
res = ProcessWrapper().call(command, cwd=repo_config['path'], shell=True, supressStderr=True)
|
||||
|
||||
if res != 0:
|
||||
logger.error("Command '%s' failed with exit code %s" % (command, res))
|
||||
break
|
||||
|
||||
if res == 0 and os.path.isdir(repo_config['path']):
|
||||
logger.info("Repository %s successfully updated" % repo_config['path'])
|
||||
else:
|
||||
logger.error("Unable to update repository %s" % repo_config['path'])
|
||||
|
||||
return int(res)
|
||||
|
||||
@staticmethod
|
||||
def clone(repo_config):
|
||||
"""Clones the latest version of the repo from the git server"""
|
||||
import logging
|
||||
from .process import ProcessWrapper
|
||||
import os
|
||||
import platform
|
||||
|
||||
logger = logging.getLogger()
|
||||
logger.info("Cloning repository %s" % repo_config['path'])
|
||||
|
||||
# Only pull if there is actually a local copy of the repository
|
||||
if 'path' not in repo_config:
|
||||
logger.info('No local repository path configured, no clone will occure')
|
||||
return 0
|
||||
|
||||
commands = []
|
||||
commands.append('unset GIT_DIR')
|
||||
commands.append('git clone --recursive ' + repo_config['url'] + ' -b ' + repo_config['branch'] + ' ' + repo_config['path'])
|
||||
|
||||
# All commands need to success
|
||||
for command in commands:
|
||||
res = ProcessWrapper().call(command, shell=True)
|
||||
|
||||
if res != 0:
|
||||
logger.error("Command '%s' failed with exit code %s" % (command, res))
|
||||
break
|
||||
|
||||
if res == 0 and os.path.isdir(repo_config['path']):
|
||||
logger.info("Repository %s successfully cloned" % repo_config['url'])
|
||||
else:
|
||||
logger.error("Unable to clone repository %s" % repo_config['url'])
|
||||
|
||||
return int(res)
|
||||
|
||||
@staticmethod
|
||||
def deploy(repo_config):
|
||||
"""Executes any supplied post-pull deploy command"""
|
||||
from .process import ProcessWrapper
|
||||
import logging
|
||||
logger = logging.getLogger()
|
||||
|
||||
if 'path' in repo_config:
|
||||
path = repo_config['path']
|
||||
|
||||
if not 'deploy_commands' in repo_config or len(repo_config['deploy_commands']) == 0:
|
||||
logger.info('No deploy commands configured')
|
||||
return []
|
||||
|
||||
logger.info('Executing %s deploy commands' % str(len(repo_config['deploy_commands'])))
|
||||
|
||||
# Use repository path as default cwd when executing deploy commands
|
||||
cwd = (repo_config['path'] if 'path' in repo_config else None)
|
||||
|
||||
res = []
|
||||
for cmd in repo_config['deploy_commands']:
|
||||
res.append(ProcessWrapper().call([cmd], cwd=cwd, shell=True))
|
||||
|
||||
logger.info('%s commands executed with status; %s' % (str(len(res)), str(res)))
|
||||
|
||||
return res
|
||||
@@ -0,0 +1,43 @@
|
||||
class ProcessWrapper():
|
||||
"""Wraps the subprocess popen method and provides logging."""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def call(*popenargs, **kwargs):
|
||||
"""Run command with arguments. Wait for command to complete. Sends
|
||||
output to logging module. The arguments are the same as for the Popen
|
||||
constructor."""
|
||||
|
||||
from subprocess import Popen, PIPE
|
||||
import logging
|
||||
logger = logging.getLogger()
|
||||
|
||||
kwargs['stdout'] = PIPE
|
||||
kwargs['stderr'] = PIPE
|
||||
|
||||
supressStderr = None
|
||||
if 'supressStderr' in kwargs:
|
||||
supressStderr = kwargs['supressStderr']
|
||||
del kwargs['supressStderr']
|
||||
|
||||
p = Popen(*popenargs, **kwargs)
|
||||
stdout, stderr = p.communicate()
|
||||
|
||||
# Decode bytes to string (assume utf-8 encoding)
|
||||
stdout = stdout.decode("utf-8")
|
||||
stderr = stderr.decode("utf-8")
|
||||
|
||||
if stdout:
|
||||
for line in stdout.strip().split("\n"):
|
||||
logger.info(line)
|
||||
|
||||
if stderr:
|
||||
for line in stderr.strip().split("\n"):
|
||||
if supressStderr:
|
||||
logger.info(line)
|
||||
else:
|
||||
logger.error(line)
|
||||
|
||||
return p.returncode
|
||||
@@ -0,0 +1,117 @@
|
||||
from .events import SystemEvent
|
||||
|
||||
try:
|
||||
from autobahn.websocket import WebSocketServerProtocol
|
||||
except ImportError:
|
||||
WebSocketServerProtocol = object
|
||||
|
||||
def WebSocketClientHandlerFactory(config, clients, event_store, server_status):
|
||||
"""Factory method for webhook request handler class"""
|
||||
|
||||
class WebSocketClientHandler(WebSocketServerProtocol, object):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self._config = config
|
||||
self.clients = clients
|
||||
self._event_store = event_store
|
||||
self._server_status = server_status
|
||||
import logging
|
||||
self.logger = logging.getLogger()
|
||||
super(WebSocketClientHandler, self).__init__(*args, **kwargs)
|
||||
|
||||
def onConnect(self, request):
|
||||
self.logger.info("Client connecting: {0}".format(request.peer))
|
||||
|
||||
# Web UI needs to be enabled
|
||||
if not self.validate_web_ui_enabled():
|
||||
return
|
||||
|
||||
# Client needs to be whitelisted
|
||||
if not self.validate_web_ui_whitelist():
|
||||
return
|
||||
|
||||
def onOpen(self):
|
||||
self.logger.info("WebSocket connection open.")
|
||||
|
||||
def onMessage(self, payload, isBinary):
|
||||
import json
|
||||
|
||||
if isBinary:
|
||||
return
|
||||
|
||||
try:
|
||||
data = json.loads(payload)
|
||||
|
||||
# Handle authentication requests
|
||||
if 'type' in data and data['type'] == 'authenticate':
|
||||
|
||||
# Verify auth key
|
||||
if 'auth-key' in data and data['auth-key'] == self._server_status['auth-key']:
|
||||
self.clients.append(self)
|
||||
|
||||
# Let the client know that they are authenticated
|
||||
self.sendMessage(json.dumps({
|
||||
"type": "authenticated"
|
||||
}))
|
||||
return
|
||||
|
||||
else:
|
||||
self.logger.error("Recieved bad auth key.")
|
||||
|
||||
self.sendMessage(json.dumps({
|
||||
"type": "bad-auth-key"
|
||||
}))
|
||||
return
|
||||
|
||||
except Exception as e:
|
||||
|
||||
self.logger.error("Unable to interpret incoming message: %s" % e)
|
||||
|
||||
if self not in self.clients:
|
||||
self.logger.error("Recieved message form unauthenticated client, closing connection.")
|
||||
self.sendClose()
|
||||
return
|
||||
|
||||
#self.logger.info("WebSocket connection open.")
|
||||
#if isBinary:
|
||||
# self.logger.info("Binary message received: {0} bytes".format(len(payload)))
|
||||
#else:
|
||||
# self.logger.info("Text message received: {0}".format(payload.decode('utf8')))
|
||||
|
||||
#for client in self.clients:
|
||||
# client.sendMessage(payload, isBinary)
|
||||
|
||||
# echo back message verbatim
|
||||
#self.sendMessage(payload, isBinary)
|
||||
|
||||
def onClose(self, wasClean, code, reason):
|
||||
self.logger.info("WebSocket connection closed: {0}".format(reason))
|
||||
|
||||
if self in self.clients:
|
||||
self.clients.remove(self)
|
||||
|
||||
def validate_web_ui_enabled(self):
|
||||
"""Verify that the Web UI is enabled"""
|
||||
|
||||
if self._config['web-ui-enabled']:
|
||||
return True
|
||||
|
||||
self.sendClose()
|
||||
return False
|
||||
|
||||
def validate_web_ui_whitelist(self):
|
||||
"""Verify that the client address is whitelisted"""
|
||||
|
||||
# Allow all if whitelist is empty
|
||||
if len(self._config['web-ui-whitelist']) == 0:
|
||||
return True
|
||||
|
||||
# Verify that client IP is whitelisted
|
||||
if self.peer.host in self._config['web-ui-whitelist']:
|
||||
return True
|
||||
|
||||
self.sendClose()
|
||||
logger.info("Unautorized connection attempt from %s" % self.peer.host)
|
||||
return False
|
||||
|
||||
return WebSocketClientHandler
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"main.css": "static/css/main.e20565bf.css",
|
||||
"main.css.map": "static/css/main.e20565bf.css.map",
|
||||
"main.js": "static/js/main.58631280.js",
|
||||
"main.js.map": "static/js/main.58631280.js.map"
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="shortcut icon" href="/favicon.ico"><title>Git-Auto-Deploy Web UI</title><link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet"><link href="https://fonts.googleapis.com/css?family=Lato:100,100i,300,300i,400,400i,700,700i,900,900i" rel="stylesheet"><link rel="stylesheet" href="//cdn.materialdesignicons.com/1.7.22/css/materialdesignicons.min.css"><link href="/static/css/main.e20565bf.css" rel="stylesheet"></head><body><div id="root"></div><script type="text/javascript" src="/static/js/main.58631280.js"></script></body></html>
|
||||
@@ -0,0 +1,2 @@
|
||||
body,html{height:100%;margin:0;padding:0}body{background:#f2f2f2}
|
||||
/*# sourceMappingURL=main.e20565bf.css.map*/
|
||||
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":[],"names":[],"mappings":"","file":"static/css/main.e20565bf.css","sourceRoot":""}
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user