-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson2docker.rb
executable file
·296 lines (263 loc) · 7.39 KB
/
json2docker.rb
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
#!/usr/bin/env ruby
#
# Copyright (C) 2019 Open Source Robotics Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
require 'json'
require 'optparse'
require 'tempfile'
# Example JSON.
example_json=<<EOF
{
"circuit": "cave",
"world": "simple_cave_01",
"headless": "false",
"robots":
[
{
"name":"X1",
"type":"X1_SENSOR_CONFIG_1",
"image":"osrf/subt-virtual-testbed:subt_solution_latest"
},
{
"name":"X2",
"type":"X2_SENSOR_CONFIG_2",
"image":"osrf/subt-virtual-testbed:subt_solution_latest"
}
]
}
EOF
# Usage information.
usage =<<EOF
Convert a SubT JSON solution to a Docker Compose yaml file.
Usage:
subt-compose -f <json_file> -o <docker_yaml_file>
subt-compose [Options]
Options:
-b [--bridge] arg Set the cloudsim_bridge image.
-e [--example] Output an example JSON file.
-f [--file] arg The JSON file to convert.
-h [--help] Output this help message.
-o [--output] arg Specifiy output Docker Compose YAML file.
-s [--sim] arg Set the cloudsim_sim image.
-r [--run] Run the JSON file using docker-compose.
Example JSON submission:
#{example_json}
EOF
# Options hash
options = {
'file' => '',
'output' => '',
'sim_image' => 'osrf/subt-virtual-testbed:cloudsim_sim_latest',
'bridge_image' => 'osrf/subt-virtual-testbed:cloudsim_bridge_latest',
'run' => false
}
# Options parser
opt_parser = OptionParser.new do |opts|
opts.banner = usage
opts.on('-b', '--bridge [arg]', String, 'cloudsim_bridge image') do |b|
options['bridge_image'] = b
end
# Output the example json.
opts.on('-e', '--example') do
puts example_json
exit
end
# JSON input file
opts.on('-f', '--file [arg]', String, 'JSON file to convert') do |f|
options['file'] = f
end
# Output the usage.
opts.on('-h', '--help') do
puts usage
exit
end
# YAML output file
opts.on('-o', '--output [arg]', String, 'Output YAML file') do |f|
options['output'] = f
end
# Simulation image
opts.on('-s', '--sim [arg]', String, 'cloudsim_sim image') do |s|
options['sim_image'] = s
end
# Should the submission be run?
opts.on('-r', '--run') do
options['run'] = true
end
end
if ARGV.length < 1
puts usage
end
# Process the command line arguments.
opt_parser.parse!(ARGV)
# Check the input file is specified.
if options['file'] == '' || options['file'] == nil
puts "Missing filename argument."
exit
end
# Make sure the provided file exists.
if !File.exist?(options['file'])
puts "JSON file, #{options['file']}, does not exist."
exit
end
# Read the json file.
begin
submission = JSON.parse(File.read(options['file']))
rescue
puts "Unable to parse JSON file #{options['file']}."
puts "Output an example JSON file using the '-e' option."
exit
end
# Check the JSON contents.
if !submission.key?('circuit')
puts "JSON file does not contain a 'circuit' name."
puts "Output an example JSON file using the '-e' option."
exit
end
if !submission.key?('world')
puts "JSON file does not contain a 'world' name."
puts "Output an example JSON file using the '-e' option."
exit
end
if !submission.key?('robots')
puts "JSON file does not contain a 'robots' array."
puts "Output an example JSON file using the '-e' option."
exit
end
if !submission.key?('headless')
submission['headless'] = 'false'
end
# Construct a the robot string that is passed to the cloudsim_sim image.
robotStr = ""
submission['robots'].each_with_index do |robot, index|
if !robot.key?('name')
puts "A robot in the 'robots' array is missing a 'name'."
puts "Output an example JSON file using the '-e' option."
exit
end
if !robot.key?('type')
puts "A robot in the 'robots' array is missing a 'type'."
puts "Output an example JSON file using the '-e' option."
exit
end
if !robot.key?('image')
puts "A robot in the 'robots' array is missing a 'image'."
puts "Output an example JSON file using the '-e' option."
exit
end
robotStr += "robotName#{index+1}:=#{robot['name']} robotConfig#{index+1}:=#{robot['type']} "
end
marsupialChildren = []
if submission.key?('marsupials')
submission['marsupials'].each_with_index do |(parent, child), index|
robotStr += "marsupial#{index+1}:=#{parent}:#{child} "
marsupialChildren.append(child)
end
end
# Set where the output is going
if options['run']
output = Tempfile.new()
elsif options['output'] == '' || options['output'] == nil
output = $stdout.dup
else
output = File.open(options['output'], "w")
end
begin
# Output the simulation container
output.puts <<EOF
version: '2.4'
services:
# The sim container runs Gazebo and all simulation plugins.
# In this example, two robots are started with the names X1 and X2.
sim:
image: #{options['sim_image']}
command: cloudsim_sim.ign headless:=#{submission['headless']} circuit:=#{submission['circuit']} ros:=true worldName:=#{submission['world']} #{robotStr}
networks:
sim_net:
ipv4_address: 172.28.1.1
environment:
- DISPLAY
- QT_X11_NO_MITSHM=1
- XAUTHORITY=/tmp/.docker.xauth
- IGN_PARTITION=sim
- IGN_IP=172.28.1.1
privileged: true
runtime: nvidia
security_opt:
- seccomp=unconfined
volumes:
- "/tmp/.docker.xauth:/tmp/.docker.xauth"
- "/tmp/.X11-unix:/tmp/.X11-unix"
- "/etc/localtime:/etc/localtime:ro"
- "/dev/input:/dev/input"
EOF
# Output bridge and solution definitions
submission['robots'].each_with_index do |robot, index|
marsupialStr = "marsupial:=true" if marsupialChildren.include? robot['name']
output.puts <<EOF
bridge#{index+1}:
image: #{options['bridge_image']}
command: circuit:=#{submission['circuit']} worldName:=#{submission['world']} robotName1:=#{robot['name']} robotConfig1:=#{robot['type']} #{marsupialStr}
networks:
relay_net#{index+1}:
ipv4_address: 172.#{29+index}.1.1
sim_net:
ipv4_address: 172.28.1.#{index+2}
environment:
- IGN_PARTITION=sim
- IGN_IP=172.28.1.#{index+2}
- ROS_MASTER_URI=http://172.#{29+index}.1.1:11311
depends_on:
- "sim"
solution#{index+1}:
image: #{robot['image']}
networks:
relay_net#{index+1}:
ipv4_address: 172.#{29+index}.1.2
environment:
- ROS_MASTER_URI=http://172.#{29+index}.1.1:11311
runtime: nvidia
depends_on:
- "bridge#{index+1}"
EOF
end
# Output network definitions
output.puts <<EOF
networks:
sim_net:
ipam:
driver: default
config:
- subnet: 172.28.0.0/16
EOF
submission['robots'].each_with_index do |robot, index|
output.puts <<EOF
relay_net#{index+1}:
internal: true
ipam:
driver: default
config:
- subnet: 172.#{29+index}.0.0/16
EOF
end
if options['run']
output.close
system "docker-compose -f #{output.path} up"
system "docker-compose -f #{output.path} down"
output.unlink
end
ensure
output.close
end