-
Notifications
You must be signed in to change notification settings - Fork 20
/
manof.py
296 lines (253 loc) · 8.35 KB
/
manof.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
import argparse
import sys
from twisted.internet import reactor
import core
import clients.logging
def _run(args, known_arg_options):
retval = 1
logger = clients.logging.Client(
"manof",
initial_severity=args.log_severity,
initial_console_severity=args.log_console_severity,
initial_file_severity=args.log_file_severity,
output_stdout=not args.log_disable_stdout,
output_dir=args.log_output_dir,
max_log_size_mb=args.log_file_rotate_max_file_size,
max_num_log_files=args.log_file_rotate_num_files,
log_file_name=args.log_file_name,
log_colors=args.log_colors,
).logger
# start root logger with kwargs and create manof
manof_instance = core.Manof(logger, args, known_arg_options)
d = manof_instance.execute_command()
# after run and possibly re-run, stop the reactor
d.addBoth(lambda _: reactor.callFromThread(reactor.stop))
reactor.run()
if logger.first_error is None:
retval = 0
return retval
def _register_arguments(parser):
# main command subparser, to which we'll add subparsers below
subparsers = parser.add_subparsers(
dest="command",
title="subcommands",
description=(
"To print additional help on a subcommand, run manof <subcmd> --help"
),
)
# global options for manof
clients.logging.Client.register_arguments(parser)
parser.add_argument(
"-mp", "--manofest-path", help="Location of manofest.py", default="manofest.py"
)
parser.add_argument(
"--num-retries",
help="Set number of retires for push and pull operations after first try fails",
type=int,
default=2,
)
# don't actually run any commands
parser.add_argument(
"-dr",
"--dry-run",
help="Don't actually run any commands, just log",
action="store_true",
)
parser.add_argument(
"-p",
"--parallel",
action="store",
help="Set how many commands to run in parallel",
type=int,
)
# update
subparsers.add_parser("update", help="Updates Manof")
# base sub parser
base_command_parent_parser = argparse.ArgumentParser(add_help=False)
base_command_parent_parser.add_argument("targets", nargs="+")
base_command_parent_parser.add_argument(
"-e",
"--exclude",
help="Exclude targets when running manof cmd (comma-delimited, no spaces)",
default="",
)
# TODO: Change default to 'docker.io'. Currently default is None for backwards compatibility
base_command_parent_parser.add_argument(
"-r",
"--repository",
help="The repository from which images shall be taken from or pushed to",
default=None,
)
# image based commands
provision_parent_command = argparse.ArgumentParser(add_help=False)
provision_parent_command.add_argument(
"-n", "--no-cache", help="Don't use cache images on build", action="store_true"
)
provision_parent_command.add_argument(
"--force-rm",
help=(
"Image: Always remove intermediate containers. "
"NamedVolume: Delete existing before creation"
),
action="store_true",
)
provision_parent_command.add_argument(
"-tl",
"--skip-tag-local",
help=(
"If no context is given, provision will perform pull and "
"skip tagging the image with its local repository (default: False)"
),
dest="tag_local",
action="store_false",
)
# provision
subparsers.add_parser(
"provision",
help="Build or pull target images",
parents=[base_command_parent_parser, provision_parent_command],
)
run_parent_parser = argparse.ArgumentParser(add_help=False)
run_parent_parser.add_argument(
"--privileged",
action="store_true",
help="Give extended privileges to these containers",
)
run_parent_parser.add_argument(
"--device",
help="Add a host device to the containers (can be used multiple times)",
action="append",
dest="devices",
)
run_parent_parser.add_argument(
"--device-cgroup-rule",
help="Add a rule to the cgroup allowed devices list (e.g. c 42:* rmw)",
)
run_parent_parser.add_argument(
"--device-read-bps",
help="Limit read rate (bytes per second) from a device (e.g. /dev/sda:50mb)",
)
run_parent_parser.add_argument(
"--device-read-iops",
help="Limit read rate (IO per second) from a device (e.g. /dev/sda:50)",
)
run_parent_parser.add_argument(
"--device-write-bps",
help="Limit write rate (bytes per second) to a device (e.g. /dev/sda:50mb)",
)
run_parent_parser.add_argument(
"--device-write-iops",
help="Limit write rate (IO per second) to a device (e.g. /dev/sda:50)",
)
run_parent_parser.add_argument(
"--cap-add", help="Add capability to the container", action="append"
)
run_parent_parser.add_argument(
"--cap-drop", help="Drop capability from the container", action="append"
)
run_parent_parser.add_argument(
"-dv",
"--delete-volumes",
help="Image: Delete named_volumes that are used by this image",
action="store_true",
)
run_parent_parser.add_argument(
"-pco",
"--print-command-only",
help="Will enforce dry run and print the run command only, no logs at all",
action="store_true",
)
run_parent_parser.add_argument(
"-prmd5o",
"--print-run-md5-only",
help="Will print the run command md5 only. no logs at all",
action="store_true",
)
# run
subparsers.add_parser(
"run",
help="Run target containers",
parents=[base_command_parent_parser, run_parent_parser],
)
# stop
stop_command = subparsers.add_parser(
"stop", help="Stop target containers", parents=[base_command_parent_parser]
)
stop_command.add_argument(
"-t",
"--time",
help="Seconds to wait for stop before killing it (default=10)",
type=int,
default=10,
)
# rm
rm_command = subparsers.add_parser(
"rm", help="Remove targets", parents=[base_command_parent_parser]
)
rm_command.add_argument(
"-f",
"--force",
help="Kill targets even if they are running",
action="store_true",
)
rm_command.add_argument(
"-v",
"--volumes",
help="Remove the volumes associated with the container",
action="store_true",
)
# serialize
subparsers.add_parser(
"serialize",
help="Get a JSON representation of the targets",
parents=[base_command_parent_parser],
)
# push
push_command = subparsers.add_parser(
"push", help="Push targets", parents=[base_command_parent_parser]
)
push_command.add_argument(
"-nc",
"--no-cleanup",
help="After pushing, delete the tagged image created to push",
action="store_true",
)
# pull
pull_parent_parser = argparse.ArgumentParser(add_help=False)
pull_parent_parser.add_argument(
"-tl",
"--tag-local",
help="After pulling, tag the image with its local repository",
action="store_true",
)
subparsers.add_parser(
"pull",
help="Pull targets",
parents=[base_command_parent_parser, pull_parent_parser],
)
# lift
subparsers.add_parser(
"lift",
help="Provision and run targets",
parents=[
base_command_parent_parser,
provision_parent_command,
run_parent_parser,
],
)
known_option_strings = list(parser._option_string_actions.keys())
for subparser in subparsers.choices.values():
known_option_strings += list(subparser._option_string_actions.keys())
# unique
return set(known_option_strings)
def run():
# create an argument parser
ap = argparse.ArgumentParser()
# register all arguments and sub-commands
known_option_strings = _register_arguments(ap)
# parse the known args, seeing how the targets may add arguments of their own and re-parse
retval = _run(ap.parse_known_args()[0], known_option_strings)
# return value
return retval
if __name__ == "__main__":
sys.exit(run())