Show More
@@ -1,111 +1,116 b'' | |||
|
1 | 1 | #!/usr/bin/env python3 |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2018 Gregory Szorc <gregory.szorc@gmail.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | import argparse |
|
9 | 9 | import pathlib |
|
10 | 10 | import shutil |
|
11 | 11 | import subprocess |
|
12 | 12 | import sys |
|
13 | 13 | |
|
14 | 14 | def get_docker() -> str: |
|
15 | 15 | docker = shutil.which('docker.io') or shutil.which('docker') |
|
16 | 16 | if not docker: |
|
17 | 17 | print('could not find docker executable') |
|
18 | 18 | return 1 |
|
19 | 19 | |
|
20 | 20 | try: |
|
21 | 21 | out = subprocess.check_output([docker, '-h'], stderr=subprocess.STDOUT) |
|
22 | 22 | |
|
23 | 23 | if b'Jansens' in out: |
|
24 | 24 | print('%s is the Docking System Tray; try installing docker.io' % |
|
25 | 25 | docker) |
|
26 | 26 | sys.exit(1) |
|
27 | 27 | except subprocess.CalledProcessError as e: |
|
28 | 28 | print('error calling `%s -h`: %s' % (docker, e.output)) |
|
29 | 29 | sys.exit(1) |
|
30 | 30 | |
|
31 | 31 | out = subprocess.check_output([docker, 'version'], |
|
32 | 32 | stderr=subprocess.STDOUT) |
|
33 | 33 | |
|
34 | 34 | lines = out.splitlines() |
|
35 | 35 | if not any(l.startswith((b'Client:', b'Client version:')) for l in lines): |
|
36 | 36 | print('`%s version` does not look like Docker' % docker) |
|
37 | 37 | sys.exit(1) |
|
38 | 38 | |
|
39 | 39 | if not any(l.startswith((b'Server:', b'Server version:')) for l in lines): |
|
40 | 40 | print('`%s version` does not look like Docker' % docker) |
|
41 | 41 | sys.exit(1) |
|
42 | 42 | |
|
43 | 43 | return docker |
|
44 | 44 | |
|
45 | 45 | def get_dockerfile(path: pathlib.Path, args: list) -> bytes: |
|
46 | 46 | with path.open('rb') as fh: |
|
47 | 47 | df = fh.read() |
|
48 | 48 | |
|
49 | 49 | for k, v in args: |
|
50 | 50 | df = df.replace(b'%%%s%%' % k, v) |
|
51 | 51 | |
|
52 | 52 | return df |
|
53 | 53 | |
|
54 | 54 | def build_docker_image(dockerfile: pathlib.Path, params: list, tag: str): |
|
55 | 55 | """Build a Docker image from a templatized Dockerfile.""" |
|
56 | 56 | docker = get_docker() |
|
57 | 57 | |
|
58 | 58 | dockerfile_path = pathlib.Path(dockerfile) |
|
59 | 59 | |
|
60 | 60 | dockerfile = get_dockerfile(dockerfile_path, params) |
|
61 | 61 | |
|
62 | 62 | print('building Dockerfile:') |
|
63 | 63 | print(dockerfile.decode('utf-8', 'replace')) |
|
64 | 64 | |
|
65 | 65 | args = [ |
|
66 | 66 | docker, |
|
67 | 67 | 'build', |
|
68 | 68 | '--build-arg', 'http_proxy', |
|
69 | 69 | '--build-arg', 'https_proxy', |
|
70 | 70 | '--tag', tag, |
|
71 | 71 | '-', |
|
72 | 72 | ] |
|
73 | 73 | |
|
74 | 74 | print('executing: %r' % args) |
|
75 | subprocess.run(args, input=dockerfile, check=True) | |
|
75 | p = subprocess.Popen(args, stdin=subprocess.PIPE) | |
|
76 | p.communicate(input=dockerfile) | |
|
77 | if p.returncode: | |
|
78 | raise subprocess.CalledProcessException( | |
|
79 | p.returncode, 'failed to build docker image: %s %s' \ | |
|
80 | % (p.stdout, p.stderr)) | |
|
76 | 81 | |
|
77 | 82 | def command_build(args): |
|
78 | 83 | build_args = [] |
|
79 | 84 | for arg in args.build_arg: |
|
80 | 85 | k, v = arg.split('=', 1) |
|
81 | 86 | build_args.append((k.encode('utf-8'), v.encode('utf-8'))) |
|
82 | 87 | |
|
83 | 88 | build_docker_image(pathlib.Path(args.dockerfile), |
|
84 | 89 | build_args, |
|
85 | 90 | args.tag) |
|
86 | 91 | |
|
87 | 92 | def command_docker(args): |
|
88 | 93 | print(get_docker()) |
|
89 | 94 | |
|
90 | 95 | def main() -> int: |
|
91 | 96 | parser = argparse.ArgumentParser() |
|
92 | 97 | |
|
93 | 98 | subparsers = parser.add_subparsers(title='subcommands') |
|
94 | 99 | |
|
95 | 100 | build = subparsers.add_parser('build', help='Build a Docker image') |
|
96 | 101 | build.set_defaults(func=command_build) |
|
97 | 102 | build.add_argument('--build-arg', action='append', default=[], |
|
98 | 103 | help='Substitution to perform in Dockerfile; ' |
|
99 | 104 | 'format: key=value') |
|
100 | 105 | build.add_argument('dockerfile', help='path to Dockerfile to use') |
|
101 | 106 | build.add_argument('tag', help='Tag to apply to created image') |
|
102 | 107 | |
|
103 | 108 | docker = subparsers.add_parser('docker-path', help='Resolve path to Docker') |
|
104 | 109 | docker.set_defaults(func=command_docker) |
|
105 | 110 | |
|
106 | 111 | args = parser.parse_args() |
|
107 | 112 | |
|
108 | 113 | return args.func(args) |
|
109 | 114 | |
|
110 | 115 | if __name__ == '__main__': |
|
111 | 116 | sys.exit(main()) |
General Comments 0
You need to be logged in to leave comments.
Login now