2 # Licensed under the Apache License, Version 2.0 (the "License"); you may
3 # not use this file except in compliance with the License. You may obtain
4 # a copy of the License at
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 # License for the specific language governing permissions and limitations
20 docker_cmd = '/bin/docker'
22 # Tool to start docker containers as configured via
23 # tripleo-heat-templates.
25 # This tool reads data from a json file generated from heat when the
26 # TripleO stack is run. All the configuration data used to start the
27 # containerized services is in this file.
29 # By default this tool lists all the containers that are started and
32 # If you wish to see the command line used to start a given container,
33 # specify it by name using the --container argument. --run can then be
34 # used with this to actually execute docker to run the container.\n
36 # Other options listed allow you to modify this command line for
37 # debugging purposes. For example:
39 # docker-toool -c swift-proxy -r -e /bin/bash -u root -i -n test
41 # will run the swift proxy container as user root, executing /bin/bash,
43 # named 'test', and will run interactively (eg -ti).
47 parser = argparse.ArgumentParser("Tool to start docker containers via "
48 "TripleO configurations")
49 parser.add_argument('-f', '--config',
50 help="""File to use as docker startup configuration data.""",
51 default='/var/lib/docker-container-startup-configs.json')
52 parser.add_argument('-r', '--run',
54 help="""Run the container as specified with --container.""",
56 parser.add_argument('-e', '--command',
57 help="""Override the command used to run the container.""",
59 parser.add_argument('-c', '--container',
60 help="""Specify a container to run or show the command for.""",
62 parser.add_argument('-u', '--user',
63 help="""User to run container as.""",
65 parser.add_argument('-n', '--name',
66 help="""Name of container.""",
68 parser.add_argument('-i', '--interactive',
70 help="""Start docker container interactively (-ti).""",
72 opts = parser.parse_args(argv[1:])
76 def docker_arg_map(key, value):
77 value = str(value).encode('ascii', 'ignore')
82 'environment': "--env=%s" % value,
84 'net': "--net=%s" % value,
85 'pid': "--pid=%s" % value,
86 'privileged': "--privileged=%s" % value.lower(),
87 #'restart': "--restart=%s" % "false",
88 'user': "--user=%s" % value,
89 'volumes': "--volume=%s" % value,
90 'volumes_from': "--volumes-from=%s" % value,
93 def run_docker_container(opts, container_name):
94 container_found = False
96 with open(opts.config) as f:
97 json_data = json.load(f)
99 for step in (json_data or []):
102 for container in (json_data[step] or []):
103 if container == container_name:
104 print('container found: %s' % container)
105 container_found = True
106 # A few positional arguments:
114 opts.name or container
116 for container_data in (json_data[step][container] or []):
117 if container_data == "environment":
118 for env in (json_data[step][container][container_data] or []):
119 arg = docker_arg_map("environment", env)
122 elif container_data == "volumes":
123 for volume in (json_data[step][container][container_data] or []):
124 arg = docker_arg_map("volumes", volume)
127 elif container_data == "volumes_from":
128 for volume in (json_data[step][container][container_data] or []):
129 arg = docker_arg_map("volumes_from", volume)
132 elif container_data == 'command':
133 command = json_data[step][container][container_data]
134 elif container_data == 'image':
135 image = json_data[step][container][container_data]
137 # Only add a restart if we're not interactive
138 if container_data == 'restart':
141 if container_data == 'user':
144 arg = docker_arg_map(container_data,
145 json_data[step][container][container_data])
151 cmd.append(opts.user)
154 # May as well remove it when we're done too
158 cmd.append(opts.command)
165 os.execl(docker_cmd, *cmd)
167 if not container_found:
168 print("Container '%s' not found!" % container_name)
170 def list_docker_containers(opts):
172 with open(opts.config) as f:
173 json_data = json.load(f)
175 for step in (json_data or []):
179 for container in (json_data[step] or []):
180 print('\tcontainer: %s' % container)
181 for container_data in (json_data[step][container] or []):
182 #print('\t\tcontainer_data: %s' % container_data)
183 if container_data == "start_order":
184 print('\t\tstart_order: %s' % json_data[step][container][container_data])
186 opts = parse_opts(sys.argv)
189 run_docker_container(opts, opts.container)
191 list_docker_containers(opts)