|
# This script processes the dependencies.json file in a cookbook and either adds |
|
# or updates the submodule information in the master repo, this is run in a job |
|
# per cookbook "Execute Python" build step *after* the cookbook-update.sh build |
|
# step |
|
|
|
import json |
|
import os |
|
import subprocess |
|
import sys |
|
|
|
base_path = os.environ['WORKSPACE'] + '/' + 'repo' |
|
|
|
cookbook = os.environ['JOB_NAME'] |
|
cookbook_path = '%s/cookbooks/%s' % (base_path, cookbook) |
|
dependencies = '%s/dependencies.json' % cookbook_path |
|
|
|
|
|
def execute(command): |
|
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
|
stdout, stderr = p.communicate() |
|
if p.returncode: |
|
print 'Command failure [%s] returned %s' % (' '.join(command), |
|
p.returncode) |
|
print |
|
print stderr |
|
print |
|
sys.exit(p.returncode) |
|
return stdout |
|
|
|
os.chdir(base_path) |
|
if os.path.exists(dependencies): |
|
with open(dependencies, 'r') as handle: |
|
data = json.load(handle) |
|
|
|
cookbooks = data.get('cookbooks', dict()) |
|
for name in cookbooks: |
|
print 'Processing dependency cookbook %s' % name |
|
dependency_path = '%s/cookbooks/%s' % (base_path, name) |
|
if not os.path.exists(dependency_path): |
|
print 'Adding dependency cookbook %s' % name |
|
execute(['git', 'submodule', 'add', cookbooks[name]['url'], 'cookbooks/%s' % name]) |
|
print 'Committing addition of dependency cookbook %s' % name |
|
execute(['git', 'commit', '-m', 'Adding dependency cookbook %s' % name, |
|
'.gitmodules', 'cookbooks/%s' % name]) |
|
|
|
print 'Updating dependency cookbook %s' % name |
|
execute(['git', 'submodule', 'update', '--init', 'cookbooks/%s' % name]) |
|
|
|
os.chdir(dependency_path) |
|
if cookbooks[name].get('branch'): |
|
execute(['git', 'checkout', '-b', cookbooks[name].get('branch', 'master')]) |
|
execute(['git', 'pull', '-f', '-u', 'origin', cookbooks[name].get('branch', 'master')]) |
|
os.chdir(base_path) |
|
|
|
if 'revision' in cookbooks[name]: |
|
# Change to that revision |
|
os.chdir(dependency_path) |
|
execute(['git', 'reset', '--hard', cookbooks[name]['revision']]) |
|
|
|
# Commit the specific revision for that cookbook |
|
os.chdir(base_path) |
|
stdout = execute(['git', 'status', '--porcelain']) |
|
if stdout: |
|
print 'Committing updates to dependency cookbook "%s"' % name |
|
revision = cookbooks[name].get('revision', 'HEAD') |
|
execute(['git', 'commit', '-m', 'Updating dependency cookbook %s to %s' % (name, revision), |
|
'.gitmodules', 'cookbooks/%s' % name]) |
|
else: |
|
print 'No changes to commit for dependency cookbook %s' % name |