From fb7c4214ced3b0533316e3eebd90ac07fe7b2933 Mon Sep 17 00:00:00 2001 From: shin- Date: Mon, 12 Aug 2013 19:45:12 +0200 Subject: [PATCH 1/5] brew: Reuse repositories when possible --- contrib/brew/brew/brew.py | 12 ++++++++++-- contrib/brew/brew/git.py | 17 ++++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/contrib/brew/brew/brew.py b/contrib/brew/brew/brew.py index 8cbbaca06a..352d20c779 100644 --- a/contrib/brew/brew/brew.py +++ b/contrib/brew/brew/brew.py @@ -14,6 +14,7 @@ logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level='INFO') client = docker.Client() processed = {} +processed_folders = [] def build_library(repository=None, branch=None, namespace=None, push=False, @@ -92,20 +93,27 @@ def build_library(repository=None, branch=None, namespace=None, push=False, f.close() if dst_folder != repository: rmtree(dst_folder, True) + for d in processed_folders: + rmtree(d, True) summary.print_summary(logger) def build_repo(repository, ref, docker_repo, docker_tag, namespace, push, registry): docker_repo = '{0}/{1}'.format(namespace or 'library', docker_repo) img_id = None + dst_folder = None if '{0}@{1}'.format(repository, ref) not in processed.keys(): logger.info('Cloning {0} (ref: {1})'.format(repository, ref)) - dst_folder = git.clone(repository, ref) + if repository not in processed: + rep, dst_folder = git.clone(repository, ref) + processed[repository] = rep + processed_folders.append(dst_folder) + else: + dst_folder = git.checkout(processed[repository], ref) if not 'Dockerfile' in os.listdir(dst_folder): raise RuntimeError('Dockerfile not found in cloned repository') logger.info('Building using dockerfile...') img_id, logs = client.build(path=dst_folder, quiet=True) - rmtree(dst_folder, True) else: img_id = processed['{0}@{1}'.format(repository, ref)] logger.info('Committing to {0}:{1}'.format(docker_repo, diff --git a/contrib/brew/brew/git.py b/contrib/brew/brew/git.py index 40cae8753b..e45e995458 100644 --- a/contrib/brew/brew/git.py +++ b/contrib/brew/brew/git.py @@ -16,6 +16,21 @@ def clone_tag(repo_url, tag, folder=None): return clone(repo_url, 'refs/tags/' + tag, folder) +def checkout(rep, ref=None): + is_commit = False + if ref is None: + ref = 'refs/heads/master' + elif not ref.startswith('refs/'): + is_commit = True + if is_commit: + rep['HEAD'] = rep.commit(ref) + else: + rep['HEAD'] = rep.refs[ref] + indexfile = rep.index_path() + tree = rep["HEAD"].tree + index.build_index_from_tree(rep.path, indexfile, rep.object_store, tree) + return rep.path + def clone(repo_url, ref=None, folder=None): is_commit = False if ref is None: @@ -45,4 +60,4 @@ def clone(repo_url, ref=None, folder=None): tree = rep["HEAD"].tree index.build_index_from_tree(rep.path, indexfile, rep.object_store, tree) logger.debug("done") - return folder + return rep, folder From 79fc90b6463d9b20391b4edd1540bc0a8e84da6f Mon Sep 17 00:00:00 2001 From: shin- Date: Mon, 12 Aug 2013 19:52:09 +0200 Subject: [PATCH 2/5] brew: Don't build if docker daemon can't be reached --- contrib/brew/brew/brew.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/contrib/brew/brew/brew.py b/contrib/brew/brew/brew.py index 352d20c779..e07fdfdbbb 100644 --- a/contrib/brew/brew/brew.py +++ b/contrib/brew/brew/brew.py @@ -32,6 +32,15 @@ def build_library(repository=None, branch=None, namespace=None, push=False, logger.info('Repository provided assumed to be a local path') dst_folder = repository + try: + client.version() + except Exception as e: + logger.error('Could not reach the docker daemon. Please make sure it ' + 'is running.') + logger.warning('Also make sure you have access to the docker UNIX ' + 'socket (use sudo)') + return + #FIXME: set destination folder and only pull latest changes instead of # cloning the whole repo everytime if not dst_folder: From e5f1b6b9a4b934eab9c42d6534fe52672c018405 Mon Sep 17 00:00:00 2001 From: shin- Date: Tue, 13 Aug 2013 20:12:44 +0200 Subject: [PATCH 3/5] brew: Updated requirements --- contrib/brew/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/brew/requirements.txt b/contrib/brew/requirements.txt index 78a574953d..6100b01d01 100644 --- a/contrib/brew/requirements.txt +++ b/contrib/brew/requirements.txt @@ -1,2 +1,2 @@ dulwich==0.9.0 -docker-py==0.1.3 \ No newline at end of file +docker-py==0.1.4 \ No newline at end of file From 2cebe09924c9afea47bb1f2444ba1cd8fc423669 Mon Sep 17 00:00:00 2001 From: shin- Date: Tue, 13 Aug 2013 20:28:06 +0200 Subject: [PATCH 4/5] brew: Display a clear error message when the path is invalid --- contrib/brew/brew/brew.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/contrib/brew/brew/brew.py b/contrib/brew/brew/brew.py index e07fdfdbbb..22fe5b7b40 100644 --- a/contrib/brew/brew/brew.py +++ b/contrib/brew/brew/brew.py @@ -53,7 +53,13 @@ def build_library(repository=None, branch=None, namespace=None, push=False, logger.error('Source repository could not be fetched. Check ' 'that the address is correct and the branch exists.') return - for buildfile in os.listdir(os.path.join(dst_folder, 'library')): + try: + dirlist = os.listdir(os.path.join(dst_folder, 'library')) + except OSError as e: + logger.error('The path provided ({0}) could not be found or didn\'t' + 'contain a library/ folder.'.format(dst_folder)) + return + for buildfile in dirlist: if buildfile == 'MAINTAINERS': continue f = open(os.path.join(dst_folder, 'library', buildfile)) From 62c0f433fa794092fe2a795784f36fbac7b9feec Mon Sep 17 00:00:00 2001 From: Joffrey F Date: Wed, 14 Aug 2013 05:05:44 +0200 Subject: [PATCH 5/5] brew: Fixed a bug with remote repository builds --- contrib/brew/brew/brew.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contrib/brew/brew/brew.py b/contrib/brew/brew/brew.py index 22fe5b7b40..0d9f0f3e98 100644 --- a/contrib/brew/brew/brew.py +++ b/contrib/brew/brew/brew.py @@ -47,7 +47,7 @@ def build_library(repository=None, branch=None, namespace=None, push=False, logger.info('Cloning docker repo from {0}, branch: {1}'.format( repository, branch)) try: - dst_folder = git.clone_branch(repository, branch) + rep, dst_folder = git.clone_branch(repository, branch) except Exception as e: logger.exception(e) logger.error('Source repository could not be fetched. Check ' @@ -182,4 +182,4 @@ class Summary(object): if logger: logger.info(s + success + details) else: - print s, success, details \ No newline at end of file + print s, success, details