ovn: Add missing configurations required
[apex-tripleo-heat-templates.git] / docker / docker-toool
1 #!/usr/bin/env python
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
5 #
6 #         http://www.apache.org/licenses/LICENSE-2.0
7 #
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
12 #    under the License.
13
14 import argparse
15 import os
16 import shutil
17 import sys
18 import json
19
20 docker_cmd = '/bin/docker'
21
22 # Tool to start docker containers as configured via
23 # tripleo-heat-templates.
24 #
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.
28 #
29 # By default this tool lists all the containers that are started and
30 # their start order.
31 #
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
35 #
36 # Other options listed allow you to modify this command line for
37 # debugging purposes.  For example:
38 #
39 # docker-toool -c swift-proxy -r -e /bin/bash -u root -i -n test
40 #
41 # will run the swift proxy container as user root, executing /bin/bash,
42 #
43 # named 'test', and will run interactively (eg -ti).
44
45
46 def parse_opts(argv):
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',
53                         action='store_true',
54                         help="""Run the container as specified with --container.""",
55                         default=False)
56     parser.add_argument('-e', '--command',
57                         help="""Override the command used to run the container.""",
58                         default='')
59     parser.add_argument('-c', '--container',
60                         help="""Specify a container to run or show the command for.""",
61                         default='')
62     parser.add_argument('-u', '--user',
63                         help="""User to run container as.""",
64                         default='')
65     parser.add_argument('-n', '--name',
66                         help="""Name of container.""",
67                         default='')
68     parser.add_argument('-i', '--interactive',
69                         action='store_true',
70                         help="""Start docker container interactively (-ti).""",
71                         default=False)
72     opts = parser.parse_args(argv[1:])
73
74     return opts
75
76 def docker_arg_map(key, value):
77     value = str(value).encode('ascii', 'ignore')
78     return {
79         'environment': "--env=%s" % value,
80         # 'image': value,
81         'net': "--net=%s" % value,
82         'pid': "--pid=%s" % value,
83         'privileged': "--privileged=%s" % value.lower(),
84         #'restart': "--restart=%s" % "false",
85         'user': "--user=%s" % value,
86         'volumes': "--volume=%s" % value,
87         'volumes_from': "--volumes-from=%s" % value,
88     }.get(key, None)
89
90 def run_docker_container(opts, container_name):
91     container_found = False
92
93     with open(opts.config) as f:
94         json_data = json.load(f)
95
96     for step in (json_data or []):
97         if step is None:
98             continue
99         for container in (json_data[step] or []):
100             if container == container_name:
101                 print('container found: %s' % container)
102                 container_found = True
103                 # A few positional arguments:
104                 command = ''
105                 image = ''
106
107                 cmd = [
108                     docker_cmd,
109                     'run',
110                     '--name',
111                     opts.name or container
112                 ]
113                 for container_data in (json_data[step][container] or []):
114                     if container_data == "environment":
115                         for env in (json_data[step][container][container_data] or []):
116                             arg = docker_arg_map("environment", env)
117                             if arg:
118                                 cmd.append(arg)
119                     elif container_data == "volumes":
120                         for volume in (json_data[step][container][container_data] or []):
121                             arg = docker_arg_map("volumes", volume)
122                             if arg:
123                                 cmd.append(arg)
124                     elif container_data == "volumes_from":
125                         for volume in (json_data[step][container][container_data] or []):
126                             arg = docker_arg_map("volumes_from", volume)
127                             if arg:
128                                 cmd.append(arg)
129                     elif container_data == 'command':
130                         command = json_data[step][container][container_data]
131                     elif container_data == 'image':
132                         image = json_data[step][container][container_data]
133                     else:
134                         # Only add a restart if we're not interactive
135                         if container_data == 'restart':
136                             if opts.interactive:
137                                 continue
138                         if container_data == 'user':
139                             if opts.user:
140                                 continue
141                         arg = docker_arg_map(container_data,
142                                 json_data[step][container][container_data])
143                         if arg:
144                             cmd.append(arg)
145
146                 if opts.user:
147                     cmd.append('--user')
148                     cmd.append(opts.user)
149                 if opts.interactive:
150                     cmd.append('-ti')
151                     # May as well remove it when we're done too
152                     cmd.append('--rm')
153                 cmd.append(image)
154                 if opts.command:
155                     cmd.append(opts.command)
156                 elif command:
157                     cmd.extend(command)
158
159                 print ' '.join(cmd)
160
161                 if opts.run:
162                     os.execl(docker_cmd, *cmd)
163
164     if not container_found:
165         print("Container '%s' not found!" % container_name)
166
167 def list_docker_containers(opts):
168     print opts
169     with open(opts.config) as f:
170         json_data = json.load(f)
171
172     for step in (json_data or []):
173         if step is None:
174             continue
175         print step
176         for container in (json_data[step] or []):
177             print('\tcontainer: %s' % container)
178             for container_data in (json_data[step][container] or []):
179                 #print('\t\tcontainer_data: %s' % container_data)
180                 if container_data == "start_order":
181                     print('\t\tstart_order: %s' % json_data[step][container][container_data])
182
183 opts = parse_opts(sys.argv)
184
185 if opts.container:
186     run_docker_container(opts, opts.container)
187 else:
188     list_docker_containers(opts)
189