Merge "Add detach to docker-toool"

This commit is contained in:
Jenkins 2017-06-30 03:52:38 +00:00 committed by Gerrit Code Review
commit a759deedf0
1 changed files with 10 additions and 6 deletions

View File

@ -69,10 +69,15 @@ def parse_opts(argv):
action='store_true', action='store_true',
help="""Start docker container interactively (-ti).""", help="""Start docker container interactively (-ti).""",
default=False) default=False)
parser.add_argument('-d', '--detach',
action='store_true',
help="""Start container detached.""",
default=False)
opts = parser.parse_args(argv[1:]) opts = parser.parse_args(argv[1:])
return opts return opts
def docker_arg_map(key, value): def docker_arg_map(key, value):
value = str(value).encode('ascii', 'ignore') value = str(value).encode('ascii', 'ignore')
if len(value) == 0: if len(value) == 0:
@ -84,12 +89,12 @@ def docker_arg_map(key, value):
'net': "--net=%s" % value, 'net': "--net=%s" % value,
'pid': "--pid=%s" % value, 'pid': "--pid=%s" % value,
'privileged': "--privileged=%s" % value.lower(), 'privileged': "--privileged=%s" % value.lower(),
#'restart': "--restart=%s" % "false",
'user': "--user=%s" % value, 'user': "--user=%s" % value,
'volumes': "--volume=%s" % value, 'volumes': "--volume=%s" % value,
'volumes_from': "--volumes-from=%s" % value, 'volumes_from': "--volumes-from=%s" % value,
}.get(key, None) }.get(key, None)
def run_docker_container(opts, container_name): def run_docker_container(opts, container_name):
container_found = False container_found = False
@ -142,13 +147,15 @@ def run_docker_container(opts, container_name):
if opts.user: if opts.user:
continue continue
arg = docker_arg_map(container_data, arg = docker_arg_map(container_data,
json_data[step][container][container_data]) json_data[step][container][container_data])
if arg: if arg:
cmd.append(arg) cmd.append(arg)
if opts.user: if opts.user:
cmd.append('--user') cmd.append('--user')
cmd.append(opts.user) cmd.append(opts.user)
if opts.detach:
cmd.append('--detach')
if opts.interactive: if opts.interactive:
cmd.append('-ti') cmd.append('-ti')
# May as well remove it when we're done too # May as well remove it when we're done too
@ -167,19 +174,17 @@ def run_docker_container(opts, container_name):
if not container_found: if not container_found:
print("Container '%s' not found!" % container_name) print("Container '%s' not found!" % container_name)
def list_docker_containers(opts): def list_docker_containers(opts):
print opts
with open(opts.config) as f: with open(opts.config) as f:
json_data = json.load(f) json_data = json.load(f)
for step in (json_data or []): for step in (json_data or []):
if step is None: if step is None:
continue continue
print step
for container in (json_data[step] or []): for container in (json_data[step] or []):
print('\tcontainer: %s' % container) print('\tcontainer: %s' % container)
for container_data in (json_data[step][container] or []): for container_data in (json_data[step][container] or []):
#print('\t\tcontainer_data: %s' % container_data)
if container_data == "start_order": if container_data == "start_order":
print('\t\tstart_order: %s' % json_data[step][container][container_data]) print('\t\tstart_order: %s' % json_data[step][container][container_data])
@ -189,4 +194,3 @@ if opts.container:
run_docker_container(opts, opts.container) run_docker_container(opts, opts.container)
else: else:
list_docker_containers(opts) list_docker_containers(opts)