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