diff options
author | Oliver Poignant <oliver@poignant.se> | 2016-08-26 22:48:24 +0200 |
---|---|---|
committer | Oliver Poignant <oliver@poignant.se> | 2016-08-26 22:48:24 +0200 |
commit | d23233780c4223404b9585b52d3bc993b5626b67 (patch) | |
tree | 00653bb61ffc0d03d92d3187d87a3178c4a60122 | |
parent | 4a40a946b8ff0da5d119fce57d872b597b8377b3 (diff) | |
parent | 0526f410b5dad1f97efc7c651e3afa55c831fb03 (diff) | |
download | Git-Auto-Deploy-d23233780c4223404b9585b52d3bc993b5626b67.zip Git-Auto-Deploy-d23233780c4223404b9585b52d3bc993b5626b67.tar.gz Git-Auto-Deploy-d23233780c4223404b9585b52d3bc993b5626b67.tar.bz2 |
Merge branch 'feature/update-branch-when-changed-in-config' of git://github.com/letorbi/Git-Auto-Deploy into letorbi-feature/update-branch-when-changed-in-config
-rw-r--r-- | gitautodeploy/cli/config.py | 14 | ||||
-rw-r--r-- | gitautodeploy/gitautodeploy.py | 30 | ||||
-rw-r--r-- | gitautodeploy/wrappers/git.py | 78 |
3 files changed, 86 insertions, 36 deletions
diff --git a/gitautodeploy/cli/config.py b/gitautodeploy/cli/config.py index b23135f..302bcae 100644 --- a/gitautodeploy/cli/config.py +++ b/gitautodeploy/cli/config.py @@ -246,6 +246,16 @@ def init_config(config): for repo_config in config['repositories']: + # Only clone repositories with a configured path + if 'url' not in repo_config: + logger.critical("Repository has no configured URL") + return 1 + + # Only clone repositories with a configured path + if 'path' not in repo_config: + logger.critical("Repository has no configured path") + return 2 + # Setup branch if missing if 'branch' not in repo_config: repo_config['branch'] = "master" @@ -301,7 +311,7 @@ def init_config(config): filter['pull_request'] = True - return config + return 0 def get_repo_config_from_environment(): """Look for repository config in any defined environment variables. If @@ -332,4 +342,4 @@ def get_repo_config_from_environment(): if 'GAD_REPO_DEPLOY' in os.environ: repo_config['deploy'] = os.environ['GAD_REPO_DEPLOY'] - return repo_config
\ No newline at end of file + return repo_config diff --git a/gitautodeploy/gitautodeploy.py b/gitautodeploy/gitautodeploy.py index 21f1f16..5d67851 100644 --- a/gitautodeploy/gitautodeploy.py +++ b/gitautodeploy/gitautodeploy.py @@ -97,31 +97,10 @@ class GitAutoDeploy(object): # 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.close() - 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'): - logger.debug("Repository %s already present" % repo_config['url']) - continue - - logger.info("Repository %s not present and needs to be cloned" % repo_config['url']) - - # Clone repository - ret = GitWrapper.clone(url=repo_config['url'], branch=repo_config['branch'], path=repo_config['path']) - - if ret == 0 and os.path.isdir(repo_config['path']): - logger.info("Repository %s successfully cloned" % repo_config['url']) + GitWrapper.init(repo_config) else: - logger.error("Unable to clone %s branch of repository %s" % (repo_config['branch'], repo_config['url'])) + GitWrapper.clone(repo_config) def ssh_key_scan(self): import re @@ -476,7 +455,10 @@ def main(): config['repositories'].append(repo_config) # Initialize config by expanding with missing values - init_config(config) + if init_config(config) != 0: + app.close() + app.exit() + return app.setup(config) app.serve_forever() diff --git a/gitautodeploy/wrappers/git.py b/gitautodeploy/wrappers/git.py index e600d9e..9bca474 100644 --- a/gitautodeploy/wrappers/git.py +++ b/gitautodeploy/wrappers/git.py @@ -6,6 +6,46 @@ class GitWrapper(): 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) + + 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 @@ -16,11 +56,6 @@ class GitWrapper(): 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 @@ -31,11 +66,10 @@ class GitWrapper(): commands.append('unset GIT_DIR') commands.append('git fetch ' + repo_config['remote']) - commands.append('git reset --hard ' + repo_config['remote'] + '/' + repo_config['branch']) + commands.append('git reset --hard ' + repo_config['remote'] + "/" + repo_config['branch']) commands.append('git submodule update --init --recursive') - #commands.append('git update-index --refresh') - # All commands needs to success + # All commands need to success for command in commands: res = ProcessWrapper().call(command, cwd=repo_config['path'], shell=True) @@ -51,9 +85,33 @@ class GitWrapper(): return int(res) @staticmethod - def clone(url, branch, path): + def clone(repo_config): + """Clones the latest version of the repo from the git server""" + import logging from process import ProcessWrapper - res = ProcessWrapper().call(['git clone --recursive ' + url + ' -b ' + branch + ' ' + path], shell=True) + import os + import platform + + logger = logging.getLogger() + logger.info("Cloning repository %s" % repo_config['path']) + + 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 |