diff --git a/add-apt-s3-repository b/add-apt-s3-repository new file mode 100755 index 0000000..d72462b --- /dev/null +++ b/add-apt-s3-repository @@ -0,0 +1,69 @@ +#!/usr/bin/python2 +import sys +import argparse +import copy +from os import path, devnull +from subprocess import check_call, check_output, call, CalledProcessError + +# install dependencies +with open(devnull, "w") as f: + try: + package = 'python-software-properties' + check_call(['dpkg', '-s', package], stdout=f) + except CalledProcessError as e: + print 'installing package \'{}\''.format(package) + check_call(['apt-get', 'update']) + check_call(['apt-get', 'install', '-y', package]) + + try: + package = 'apt-transport-s3' + check_call(['dpkg', '-s', package], stdout=f) + except CalledProcessError as e: + print 'installing package \'{}\''.format(package) + check_call(['apt-add-repository', '-y', 'ppa:mxmops/packages'], stdout=f) + check_call(['apt-get', 'update']) + check_call(['apt-get', 'install', '-y', package]) + + +def s3_deb_uri(arch, access_id, secret_key, bucket, distribution, + components, region=None): + values = copy.deepcopy(locals()) + if region is not None: + values['s3_host'] = "s3-{0}.amazonaws.com".format(region) + else: + values['s3_host'] = "s3.amazonaws.com" + + return "deb [arch={arch}] " \ + "s3://{access_id}:[{secret_key}]@{s3_host}/{bucket} " \ + "{distribution} {components}".format(**values) + +parser = argparse.ArgumentParser(description='Add apt S3 url\'s to /etc/apt/sources.list.d/') + +parser.add_argument('architectures', type=str) +parser.add_argument('access_id', type=str) +parser.add_argument('secret_key', type=str) +parser.add_argument('region', type=str) +parser.add_argument('bucket', type=str) +parser.add_argument('distribution', type=str) +parser.add_argument('components', type=str) +parser.add_argument('gpg_id', type=str) +args = parser.parse_args() + +s3_url = s3_deb_uri(args.architectures, args.access_id, args.secret_key, args.bucket, args.distribution, args.components, args.region) + +apt_list_path = '/etc/apt/sources.list.d/{}.s3.list'.format(args.bucket) +if path.isdir(apt_list_path): + os.remove(apt_list_path) + +with open(apt_list_path, 'w') as f: + f.write('{}\n'.format(s3_url)) + +key = args.gpg_id +if key is not None: + with open(devnull, "w") as f: + key_list = check_output(['apt-key', 'list']) + if key not in key_list: + print 'adding gpg key \'{}\''.format(key) + check_call(['apt-key', 'adv', '--keyserver', 'keys.gnupg.net', '--recv-keys', key], stdout=f) + +print 'Added S3 apt repository \'{}\''.format(args.bucket) diff --git a/bootstrap b/bootstrap new file mode 100644 index 0000000..45710ba --- /dev/null +++ b/bootstrap @@ -0,0 +1,3 @@ +#! /bin/bash -e + +apt-get update -qq && apt-get install curl