/
main.py
319 lines (277 loc) · 13.8 KB
/
main.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
"""
Copyright (c) 2015 Red Hat, Inc
All rights reserved.
This software may be modified and distributed under the terms
of the BSD license. See the LICENSE file for details.
"""
import json
import argparse
import logging
import os
import sys
import pkg_resources
import locale
from atomic_reactor import set_logging
from osbs import set_logging as set_logging_osbs
from atomic_reactor.api import (build_image_here, build_image_in_privileged_container,
build_image_using_hosts_docker)
from atomic_reactor.constants import DESCRIPTION, PROG
from atomic_reactor.buildimage import BuildImageBuilder
from atomic_reactor.inner import build_inside, BuildResults
from atomic_reactor.util import (process_substitutions, setup_introspection_signal_handler,
exception_message)
logger = logging.getLogger('atomic_reactor')
def cli_create_build_image(args):
b = BuildImageBuilder(reactor_tarball_path=args.reactor_tarball_path,
reactor_local_path=args.reactor_local_path,
reactor_remote_path=args.reactor_remote_git,
use_official_reactor_git=args.reactor_latest)
try:
b.create_image(args.dockerfile_dir_path, args.image, use_cache=args.use_cache)
except RuntimeError:
logger.error("build failed")
sys.exit(1)
sys.exit(0)
def cli_build_image(args):
if args.plugin_files:
args.plugin_files = [os.path.abspath(f) for f in args.plugin_files]
if args.source__provider == 'json':
with open(args.json_path) as json_fp:
common_kwargs = json.load(json_fp)
if args.substitute:
process_substitutions(common_kwargs, args.substitute)
else:
common_kwargs = construct_kwargs(**vars(args))
response = BuildResults()
if args.method == "hostdocker":
response = build_image_using_hosts_docker(args.build_image, **common_kwargs)
elif args.method == "privileged":
response = build_image_in_privileged_container(args.build_image, **common_kwargs)
elif args.method == 'here':
build_result = build_image_here(plugin_files=args.plugin_files, **common_kwargs)
if build_result.is_failed():
response.return_code = -1
else:
response.return_code = 0
if response.return_code != 0:
logger.error("build failed")
sys.exit(response.return_code)
def construct_kwargs(**kwargs):
ret = {}
ret['source'] = {'provider_params': {}}
# extend this when adding more args that should be passed to build_* functions
recognized_kwargs = ['image', 'target_registries', 'target_registries_insecure',
'dont_pull_base_image']
def is_recognized_kwarg(x):
return x in recognized_kwargs or x.startswith('source__')
for k, v in kwargs.items():
if is_recognized_kwarg(k):
if k.startswith('source__provider_params__'):
ret['source']['provider_params'][k.split('__')[-1]] = v
elif k.startswith('source__'):
ret['source'][k.split('__')[-1]] = v
else:
ret[k] = v
return ret
def cli_inside_build(args):
build_inside(input_method=args.input, input_args=args.input_arg,
substitutions=args.substitute)
class CLI(object):
def __init__(self, formatter_class=argparse.HelpFormatter, prog=PROG):
self.parser = argparse.ArgumentParser(
prog=prog,
description=DESCRIPTION,
formatter_class=formatter_class,
)
self.build_parser = None
self.bi_parser = None
self.ib_parser = None
self.source_types_parsers = None
locale.setlocale(locale.LC_ALL, '')
def set_arguments(self):
try:
version = pkg_resources.get_distribution("atomic_reactor").version
except pkg_resources.DistributionNotFound:
version = "GIT"
exclusive_group = self.parser.add_mutually_exclusive_group()
exclusive_group.add_argument("-q", "--quiet", action="store_true")
exclusive_group.add_argument("-v", "--verbose", action="store_true")
exclusive_group.add_argument("-V", "--version", action="version", version=version)
subparsers = self.parser.add_subparsers(help='commands')
# BUILDING IMAGES
self.build_parser = subparsers.add_parser(
'build',
usage="%s [OPTIONS] build" % PROG,
description='This command enables you to build images. '
'Currently, you can build images from git repo, local path or '
'a json file with full build specification. '
'There are several methods for performing the build: '
'inside a build container using docker from host, '
'inside a build container using new instance of docker, '
'or within current environment.'
)
def cli_build_with_source(args):
if args.source__provider is None:
self.build_parser.print_help()
else:
cli_build_image(args)
self.build_parser.set_defaults(func=cli_build_with_source)
self.generate_source_types_subparsers()
# CREATE BUILD IMAGE
self.bi_parser = subparsers.add_parser('create-build-image',
usage="%s [OPTIONS] create-build-image" % PROG,
description='Create build image; Atomic Reactor '
'installs itself inside and is capable of '
'building images within this image.')
self.bi_parser.set_defaults(func=cli_create_build_image)
reactor_source = self.bi_parser.add_mutually_exclusive_group()
reactor_source.add_argument("--reactor-latest", action='store_true',
help="put latest Atomic Reactor inside (from public git)")
reactor_source.add_argument("--reactor-remote-git", action='store',
help="URL to git repo with Atomic Reactor (has to contain "
"setup.py)")
reactor_source.add_argument("--reactor-local-path", action='store',
help="path to directory with Atomic Reactor (has to contain "
"setup.py)")
reactor_source.add_argument("--reactor-tarball-path", action='store',
help="path to distribution tarball with Atomic Reactor")
self.bi_parser.add_argument("dockerfile_dir_path", action="store",
metavar="DOCKERFILE_DIR_PATH",
help="path to directory with Dockerfile")
self.bi_parser.add_argument("image", action='store', metavar="IMAGE",
help="name under the image will be accessible")
self.bi_parser.add_argument("--use-cache", action='store_true', default=False,
help="use cache to build image (may be faster, but not up "
"to date)")
# inside build
self.ib_parser = subparsers.add_parser(
'inside-build',
usage="%s [OPTIONS] inside-build" % PROG,
description="build inside a container, taking build JSON input "
"from the source specified by the --input option.")
self.ib_parser.add_argument("--input", action='store', default="auto",
help="input plugin name (determined automatically unless "
"given)")
self.ib_parser.add_argument("--input-arg", action='append',
help="argument for input plugin (in form of 'key=value'), "
"see input plugins to know what arguments they accept (can "
"be specified multiple times)")
self.ib_parser.add_argument("--dont-pull-base-image", action='store_true',
help="don't pull or update base image specified in "
"dockerfile")
self.ib_parser.add_argument("--substitute", action='append',
help="substitute values in build json (key=value, or "
"plugin_type.plugin_name.key=value)")
self.ib_parser.set_defaults(func=cli_inside_build)
def generate_source_types_subparsers(self):
build_subparsers = self.build_parser.add_subparsers(help='select source provider to use',
dest='source__provider')
self.source_types_parsers = {
'git': build_subparsers.add_parser(
'git',
help='build from a git repo',
usage="%s [OPTIONS] build git" % PROG),
'path': build_subparsers.add_parser(
'path',
help='build from a local path',
usage="%s [OPTIONS] build path" % PROG),
}
source_types_uri_help = {
'git': 'URI of the git repo',
'path': 'URI of the directory with source (e.g. "file:///foo/bar") or\
path (e.g. "/foo/bar"); path can be relative',
}
source_types_df_path_help = {
'git': 'path to Dockerfile within git repo (default is ./)',
'path': 'path to Dockerfile within given directory (default is ./)',
}
# add arguments that are common to non-json source types
for st, stp in self.source_types_parsers.items():
stp.add_argument(
"--image", action='store', required=True,
help="name under which the image will be accessible")
stp.add_argument(
"--uri", action='store', metavar="URI", required=True,
help=source_types_uri_help[st], dest='source__uri')
stp.add_argument(
"--df-path", action='store', help=source_types_df_path_help[st],
dest='source__dockerfile_path')
stp.add_argument(
"--source-registry", action='store', metavar="REGISTRY",
help="registry to pull base image from")
stp.add_argument(
"--source-registry-insecure", action='store_true',
help="allow connecting to source registry over plain http")
stp.add_argument(
"--target-registries", action='store', nargs="*", metavar="REGISTRY",
help="list of registries to push image to")
stp.add_argument(
"--target-registries-insecure", action='store_true',
help="allow connecting to target registries over plain http")
stp.add_argument(
"--dont-pull-base-image", action='store_true',
help="don't pull or update base image specified in dockerfile")
# add the "json" subparser that allows to build from a json build file
self.source_types_parsers['json'] = build_subparsers.add_parser(
'json',
help='load build configuration from json file',
usage="%s [OPTIONS] build json" % PROG)
# add arguments common to both non-json and json source types
for stp in self.source_types_parsers.values():
stp.add_argument(
"--build-image", action='store',
help="name of build image to use (build image type has to match method)")
stp.add_argument(
"--load-plugin", action="store", nargs="*", metavar="PLUGIN_FILE",
dest="plugin_files", help="list of files where plugins live")
stp.add_argument(
"--method", action='store', required=True,
choices=["hostdocker", "privileged", "here"],
help="choose method for building image: "
"'hostdocker' mounts socket inside privileged container to use docker from "
"host, 'privileged' runs separate docker instance inside privileged "
"container and 'here' executes build in current environment")
# add sourcetype-specific arguments now
self.source_types_parsers['git'].add_argument(
'--commit', action='store',
dest='source__provider_params__git_commit',
help="checkout this commit (default is master)")
self.source_types_parsers['json'].add_argument(
'json_path', metavar='JSON_PATH',
help='path to the build json')
self.source_types_parsers['json'].add_argument(
'--substitute', nargs='*', metavar='SUBSTITUTE',
help='provide substitutions for json in form "foo.bar=spam"')
def run(self):
self.set_arguments()
args = self.parser.parse_args()
logging.captureWarnings(True)
if args.verbose:
set_logging(level=logging.DEBUG)
set_logging_osbs(level=logging.DEBUG)
elif args.quiet:
set_logging(level=logging.WARNING)
set_logging_osbs(level=logging.WARNING)
else:
set_logging(level=logging.INFO)
set_logging_osbs(level=logging.INFO)
try:
args.func(args)
except AttributeError:
if hasattr(args, 'func'):
raise
else:
self.parser.print_help()
except KeyboardInterrupt:
pass
except Exception as ex:
if args.verbose:
raise
else:
logger.error("exception caught: %s", exception_message(ex))
def run():
cli = CLI()
setup_introspection_signal_handler()
cli.run()
if __name__ == '__main__':
run()