| 1 |
# Note that this script was written to be executable by either version |
| 2 |
# of python (checked by running pylint-2 and pylint-3 on it). Also |
| 3 |
# note that the python version this script gets executed by is decided |
| 4 |
# on by httpd/backends.cxx. |
| 5 |
|
| 6 |
"""Build a docker file and container image based on a JSON template.""" |
| 7 |
|
| 8 |
from __future__ import print_function |
| 9 |
import json |
| 10 |
import os |
| 11 |
import os.path |
| 12 |
import sys |
| 13 |
import string |
| 14 |
import tempfile |
| 15 |
import shutil |
| 16 |
|
| 17 |
def _eprint(*args, **kwargs): |
| 18 |
"""Print to stderr.""" |
| 19 |
print(*args, file=sys.stderr, **kwargs) |
| 20 |
|
| 21 |
def _usage(): |
| 22 |
"""Display command-line usage.""" |
| 23 |
_eprint('Usage: %s [-v] --distro-file DISTRO_JSON_FILE' |
| 24 |
' --build-file DATA_JSON_FILE --data-dir DATA_DIR' |
| 25 |
' --dest-dir DEST_DIR' |
| 26 |
% sys.argv[0]) |
| 27 |
sys.exit(1) |
| 28 |
|
| 29 |
def _handle_command_line(): |
| 30 |
import getopt |
| 31 |
|
| 32 |
verbose = 0 |
| 33 |
ivars = {} |
| 34 |
|
| 35 |
# Make sure the command line looks reasonable. |
| 36 |
if len(sys.argv) < 5: |
| 37 |
_usage() |
| 38 |
try: |
| 39 |
(opts, pargs) = getopt.getopt(sys.argv[1:], 'v', |
| 40 |
['distro-file=', 'build-file=', |
| 41 |
'data-dir=', 'dest-dir=']) |
| 42 |
except getopt.GetoptError as err: |
| 43 |
_eprint("Error: %s" % err) |
| 44 |
_usage() |
| 45 |
for (opt, value) in opts: |
| 46 |
if opt == '-v': |
| 47 |
verbose += 1 |
| 48 |
elif opt == '--distro-file': |
| 49 |
ivars['DISTRO_FILE'] = value |
| 50 |
elif opt == '--build-file': |
| 51 |
ivars['BUILD_FILE'] = value |
| 52 |
elif opt == '--data-dir': |
| 53 |
ivars['DATA_DIR'] = value |
| 54 |
elif opt == '--dest-dir': |
| 55 |
ivars['DEST_DIR'] = value |
| 56 |
|
| 57 |
if len(pargs) != 0: |
| 58 |
_eprint("Extra argument found.") |
| 59 |
_usage() |
| 60 |
if 'DISTRO_FILE' not in ivars or 'BUILD_FILE' not in ivars \ |
| 61 |
or 'DATA_DIR' not in ivars or 'DEST_DIR' not in ivars: |
| 62 |
_eprint("Arguments '--distro-file', '--build-file'," |
| 63 |
" '--data-dir', and '--dest-dir' are required.") |
| 64 |
_usage() |
| 65 |
return (verbose, ivars) |
| 66 |
|
| 67 |
def _load_distro_file(distro_path): |
| 68 |
# Read in the distro file. |
| 69 |
try: |
| 70 |
jfile = open(distro_path) |
| 71 |
except (OSError, IOError) as err: |
| 72 |
_eprint("Error opening file %s: %s" % (distro_path, err)) |
| 73 |
sys.exit(1) |
| 74 |
try: |
| 75 |
distro_json = json.load(jfile) |
| 76 |
except ValueError as err: |
| 77 |
_eprint("Error: Invalid JSON input in file %s: %s" |
| 78 |
% (distro_path, err)) |
| 79 |
jfile.close() |
| 80 |
sys.exit(1) |
| 81 |
jfile.close() |
| 82 |
|
| 83 |
# We need to validate the distro JSON data here, making sure we've |
| 84 |
# got everything we need. |
| 85 |
if 'docker_stages' not in distro_json: |
| 86 |
_eprint("Error: Missing 'docker_stages' data in %s." % distro_path) |
| 87 |
sys.exit(1) |
| 88 |
if 'distro_package_installer' not in distro_json: |
| 89 |
_eprint("Error: Missing 'distro_package_installer' data in %s." |
| 90 |
% distro_path) |
| 91 |
sys.exit(1) |
| 92 |
return distro_json |
| 93 |
|
| 94 |
def _load_build_file(build_path): |
| 95 |
# Read in the build file. |
| 96 |
try: |
| 97 |
jfile = open(build_path) |
| 98 |
except (OSError, IOError) as err: |
| 99 |
_eprint("Error opening file %s: %s" % (build_path, err)) |
| 100 |
sys.exit(1) |
| 101 |
try: |
| 102 |
build_json = json.load(jfile) |
| 103 |
except ValueError as err: |
| 104 |
_eprint("Error: Invalid JSON input in file %s: %s" |
| 105 |
% (build_path, err)) |
| 106 |
jfile.close() |
| 107 |
sys.exit(1) |
| 108 |
jfile.close() |
| 109 |
|
| 110 |
# We need to validate the build JSON data here, making sure we've |
| 111 |
# got everything we need. |
| 112 |
if 'file_info' not in build_json: |
| 113 |
_eprint("Error: Missing 'file_info' data in %s." % build_path) |
| 114 |
sys.exit(1) |
| 115 |
if 'distro_version' not in build_json: |
| 116 |
_eprint("Error: Missing 'distro_version' data in %s." % build_path) |
| 117 |
sys.exit(1) |
| 118 |
return build_json |
| 119 |
|
| 120 |
def main(): |
| 121 |
"""Main function.""" |
| 122 |
|
| 123 |
(verbose, ivars) = _handle_command_line() |
| 124 |
|
| 125 |
# Read the distro file. |
| 126 |
distro_json = _load_distro_file(ivars['DISTRO_FILE']) |
| 127 |
|
| 128 |
# Read the build file. |
| 129 |
build_json = _load_build_file(ivars['BUILD_FILE']) |
| 130 |
ivars['DVER'] = build_json['distro_version'] |
| 131 |
|
| 132 |
# If we've got a distro-specific script needed to install |
| 133 |
# packages, copy it to the destination directory, since the docker |
| 134 |
# 'COPY' directive only works on paths relative to the destination |
| 135 |
# directory. |
| 136 |
if 'distro_package_installer' in distro_json: |
| 137 |
try: |
| 138 |
src_path = os.path.join(ivars['DATA_DIR'], |
| 139 |
distro_json['distro_package_installer']) |
| 140 |
shutil.copy(src_path, ivars['DEST_DIR']) |
| 141 |
except (shutil.Error, IOError) as err: |
| 142 |
_eprint("Error: copy failed: %s" % err) |
| 143 |
sys.exit(1) |
| 144 |
|
| 145 |
dockerfile_data = '' |
| 146 |
if 'header' in distro_json['docker_stages']: |
| 147 |
# Now treat the data as a template, and substitute the |
| 148 |
# information we've got. Why aren't we using docker's 'ARG' |
| 149 |
# directive? There are places, like in 'FROM' directives, that |
| 150 |
# you can't use 'ARG' variables. So, we're rolling our own. |
| 151 |
orig_data = '\n'.join(distro_json['docker_stages']['header']) |
| 152 |
orig_data += '\n' |
| 153 |
template = string.Template(orig_data) |
| 154 |
dockerfile_data += template.substitute(ivars) |
| 155 |
|
| 156 |
# Now add an item for each file to be installed. |
| 157 |
orig_data = '\n'.join(distro_json['docker_stages']['install']) |
| 158 |
orig_data += '\n' |
| 159 |
template = string.Template(orig_data) |
| 160 |
for file_info in build_json['file_info']: |
| 161 |
ivars['NAME'] = file_info['name'] |
| 162 |
ivars['PKG'] = file_info['pkg'] |
| 163 |
ivars['BUILD_ID'] = file_info['build_id'] |
| 164 |
dockerfile_data += template.substitute(ivars) |
| 165 |
|
| 166 |
if 'footer' in distro_json['docker_stages']: |
| 167 |
# See the 'header' discussion for why we're doing this. |
| 168 |
orig_data = '\n'.join(distro_json['docker_stages']['footer']) |
| 169 |
orig_data += '\n' |
| 170 |
template = string.Template(orig_data) |
| 171 |
dockerfile_data += template.substitute(ivars) |
| 172 |
|
| 173 |
# Write the dockerfile data to a file. |
| 174 |
dockerfile_path = ("%s/base.docker" % ivars['DEST_DIR']) |
| 175 |
tmpfile = open(dockerfile_path, "w") |
| 176 |
tmpfile.write(dockerfile_data) |
| 177 |
tmpfile.close() |
| 178 |
print("created file in %s\n" % ivars['DEST_DIR']) |
| 179 |
|
| 180 |
# At this point we've created the docker file, so we're done. |
| 181 |
sys.exit(0) |
| 182 |
|
| 183 |
if __name__ == '__main__': |
| 184 |
main() |