1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
#!/usr/bin/env python
"""Script to build a Docker image of the AWS CLI"""
import argparse
import os
import shutil
import sys
from distutils.dir_util import copy_tree
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from utils import (
BadRCError,
cd,
extract_zip,
run,
save_to_zip,
tmp_dir,
update_metadata,
)
ROOT = os.path.dirname(
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
)
DOCKER_DIR = os.path.join(ROOT, 'docker')
DIST_DIR = os.path.join(ROOT, 'dist')
DEFAULT_EXE_ZIP = os.path.join(DIST_DIR, 'awscli-exe.zip')
DEFAULT_DOCKER_OUTPUT = os.path.join(DIST_DIR, 'aws-cli-docker.tar')
DEFAULT_TAGS = ['amazon/aws-cli']
DISTRIBUTION_SOURCE = 'docker'
def make_docker(exe, output, tags):
_ensure_docker_is_available()
with tmp_dir() as build_context:
_make_build_context(build_context, exe)
# Use os.path.basename to extract filename for the path
_docker_build(build_context, tags, os.path.basename(exe))
_docker_save(tags, output)
def _ensure_docker_is_available():
try:
run(['docker', '--version'])
except (OSError, BadRCError) as e:
raise RuntimeError(
'Docker must be installed to run this script. Received '
'following error from docker --version: %s' % e
)
def _make_build_context(build_context_dir, exe):
_copy_docker_dir_to_build_context(build_context_dir)
_copy_exe_to_build_context(build_context_dir, exe)
_update_exe_metadata(
os.path.join(build_context_dir, os.path.basename(exe))
)
def _update_exe_metadata(exe):
with tmp_dir() as tmp:
extract_zip(exe, tmp)
update_metadata(
os.path.join(tmp, 'aws', 'dist'),
distribution_source=DISTRIBUTION_SOURCE,
)
save_to_zip(tmp, exe)
def _copy_docker_dir_to_build_context(build_context_dir):
copy_tree(DOCKER_DIR, build_context_dir)
def _copy_exe_to_build_context(build_context_dir, exe):
build_context_exe_path = os.path.join(
build_context_dir, os.path.basename(exe)
)
shutil.copy(exe, build_context_exe_path)
def _docker_build(build_context_dir, tags, exe_filename):
with cd(build_context_dir):
docker_build_cmd = [
'docker',
'build',
'--build-arg',
f'EXE_FILENAME={exe_filename}',
'.',
]
for tag in tags:
docker_build_cmd.extend(['-t', tag])
print(run(docker_build_cmd))
def _docker_save(tags, output):
parent_dir = os.path.dirname(os.path.abspath(output))
if not os.path.exists(parent_dir):
os.makedirs(parent_dir)
run(['docker', 'save', '--output', output] + tags)
print('Saved image at: %s' % output)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
'--exe',
default=DEFAULT_EXE_ZIP,
help=(
'The name of the exe zip to build into the Docker image. By '
'default the exe located at: %s' % DEFAULT_EXE_ZIP
),
)
parser.add_argument(
'--output',
default=DEFAULT_DOCKER_OUTPUT,
help=(
'The name of the file to save the Docker image. By default, '
'this will be saved at: %s' % DEFAULT_DOCKER_OUTPUT
),
)
parser.add_argument(
'--tags',
nargs='*',
default=DEFAULT_TAGS,
help=(
'The tags to give the image. This is the value provided to the '
'`-t` flag when running `docker build`. By default, the image '
'will have the tags: %s' % ''.join(DEFAULT_TAGS)
),
)
parsed_args = parser.parse_args()
make_docker(parsed_args.exe, parsed_args.output, parsed_args.tags)
if __name__ == "__main__":
main()
|