forked from zilionis/dash
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dev
executable file
·411 lines (342 loc) · 10.2 KB
/
dev
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/usr/bin/env ruby
require 'open3'
require 'pathname'
require 'rbconfig'
require 'tempfile'
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'docker-api', '~> 2.0'
gem 'subprocess', '~> 1.5'
gem 'outstand-tty-command'
gem 'pastel'
end
require 'tty-command'
class DevTool
MAIN_HELP_BANNER = <<HEREDOC
Execute various commands within the developer environment
Usage:
dev [COMMAND] [ARGS...]"
dev -h|--help
Commands:
compose Manage containers (default)
machine Manage the virtual machine
update Update Dash developer environment
parallels Use Parallels
docker_desktop Use Docker Desktop
unset_docker_env Pass through eval to unset docker env vars
Machine subcommands:
destroy
up
create (overridden)
stop (overridden)
HEREDOC
TOP_COMMANDS=%w{compose machine update parallels docker_desktop unset_docker_env}
DOMAIN = 'test'
RESOLVER_DIR = Pathname.new("/etc/resolver")
CONFIG_DIR = Pathname.new("~/.config/dev").expand_path
SHARED_CONTAINERS_DIR = Pathname.new('/usr/local/shared_containers')
def initialize(args)
unless CONFIG_DIR.directory?
cmd.run("mkdir", "-p", CONFIG_DIR)
end
Bundler.with_clean_env do
if args.empty?
help
elsif TOP_COMMANDS.include?(args[0])
send(args.shift.to_sym, args)
else
send(:compose, args)
end
end
rescue TTY::Command::ExitError => e
puts pastel.red(e.message)
exit 1
end
def help
puts MAIN_HELP_BANNER
end
def compose(args)
if (Pathname.pwd + 'docker-compose.yml').file?
project_dir = '.'
else
project_dir = `git rev-parse --show-toplevel`.chomp
if $? != 0 || !(Pathname.new(project_dir).join('docker-compose.yml').file?)
puts pastel.red('ERROR: unable to automatically detect project dir')
exit 1
end
end
@compose_project_name = Pathname.new(project_dir).expand_path.basename.to_s
Dir.chdir(project_dir) do
if args.empty?
puts `docker-compose --help`
else
command = args.first
compose_files = []
if (Pathname.pwd + 'docker-compose.override.yml').file?
compose_files.unshift('-f', 'docker-compose.override.yml')
end
docker_host =
CONFIG_DIR.join('docker_host').open('r') do |f|
f.read
end
binary = ['docker-compose']
case docker_host
when 'parallels'
if (Pathname.pwd.join('docker', 'parallels.yml')).file?
compose_files.unshift('-f', 'docker/parallels.yml')
end
when 'docker-desktop'
if (Pathname.pwd.join('docker', 'docker-desktop.yml')).file?
compose_files.unshift('-f', 'docker/docker-desktop.yml')
end
binary = ['mutagen', 'compose']
else
raise 'Unable to detect docker host!'
end
if (Pathname.pwd + 'development.yml').file?
compose_files.unshift('-f', 'development.yml')
end
compose_files.unshift('-f', 'docker-compose.yml')
args.unshift(*compose_files)
args.unshift(*binary)
$stdout.sync
begin
Subprocess.check_call(
args,
stdin: $stdin,
stdout: $stdout,
stderr: $stderr
)
puts
if command == 'down'
puts "Removing log volumes"
remove_log_volumes
# TODO: Auto retry on ERROR: error while removing network: network app_default id <id> has active endpoints
puts "Removing sync volumes"
remove_mutagen_sync_volumes
end
rescue Subprocess::NonZeroExit => e
puts e.message
exit 1
rescue Interrupt
exit 1
end
end
end
end
def parallels(args)
puts "Setting up Parallels"
puts 'Terminating mutagen sync'
cmd.run('mutagen', 'sync', 'terminate', '-a')
stop_mutagen_project(dir: SHARED_CONTAINERS_DIR)
puts "Removing mutagen sync volumes"
remove_mutagen_sync_volumes(all: true)
puts "Saving config"
CONFIG_DIR.join('docker_host').open('w') do |f|
f.write('parallels')
end
end
def docker_desktop(args)
puts "Setting up Docker Desktop"
@resolver_file = RESOLVER_DIR.join(DOMAIN)
configure_resolver!
if ENV['DOCKER_HOST'] ||
ENV['DOCKER_CERT_PATH'] ||
ENV['DOCKER_TLS_VERIFY'] ||
ENV['DOCKER_MACHINE_NAME']
puts "ERROR: At least one docker env var is set!"
puts "You can unset them for this terminal session with:"
puts "eval $(dev unset_docker_env)"
exit 1
end
puts 'Terminating mutagen sync'
cmd.run('mutagen', 'sync', 'terminate', '-a')
puts "Removing mutagen sync volumes"
remove_mutagen_sync_volumes(all: true)
puts "Saving config"
CONFIG_DIR.join('docker_host').open('w') do |f|
f.write('docker-desktop')
end
puts "Starting shared containers"
start_shared_containers
end
def unset_docker_env(args)
puts <<~EOS
unset DOCKER_HOST
unset DOCKER_CERT_PATH
unset DOCKER_TLS_VERIFY
unset DOCKER_MACHINE_NAME
EOS
end
def machine(args)
if args.empty?
puts `docker-machine --help`
elsif args.first == 'env'
args.shift
machine_name = 'default'
unless args.first.nil?
machine_name = args.first
end
if `docker-machine status #{machine_name}` =~ /Running/
cmd.run("docker-machine env #{machine_name}")
else
$stderr.puts "#{machine_name} is not running"
end
elsif args.first == 'create'
puts 'Running `dinghy create` (ignoring args)'
cmd.run('sudo ls > /dev/null')
cmd.run('dinghy create')
unless cmd.run!("#{root_dir}/docker/provision-dinghy.sh").success?
$stderr.puts "Failed to provision!"
exit 1
end
cmd.run('dinghy restart')
cmd.run('eval $(dinghy env); docker start dns > /dev/null')
elsif args.first == 'up'
puts 'Running `dinghy up` (ignoring args)'
cmd.run('sudo ls > /dev/null')
exec('dinghy up')
elsif args.first == 'stop'
puts 'Running `dinghy stop` (ignoring args)'
cmd.run('sudo ls > /dev/null')
exec('dinghy stop')
elsif args.first == 'destroy'
puts 'Running `dinghy destroy` (ignoring args)'
cmd.run('sudo ls > /dev/null')
exec('dinghy destroy')
else
args.unshift('docker-machine')
exec(args.join(' '))
end
end
def update(args)
cmd.run('cd /usr/local/dev-env && git fetch && git reset --hard origin/master') unless args.first == '--no-pull'
exec("ansible-playbook #{root_dir}/ansible/mac.yml -i 127.0.0.1, -K") if mac?
exec("ansible-playbook #{root_dir}/ansible/linux.yml -i 127.0.0.1, -K -v") if linux?
end
private
def root_dir
'/usr/local/dev-env'
end
def mac?
RbConfig::CONFIG['host_os'] =~ /darwin/
end
def linux?
RbConfig::CONFIG['host_os'] =~ /linux/
end
def cmd
return @_cmd if defined?(@_cmd)
@_cmd =
TTY::Command.new(printer: :quiet)
end
def quiet_cmd
return @_quiet_cmd if defined?(@_quiet_cmd)
@_quiet_cmd =
TTY::Command.new(printer: :null)
end
def pastel
return @_pastel if defined?(@_pastel)
@_pastel =
Pastel.new
end
# Many thanks to codekitchen/dinghy for the following methods
def configure_resolver!
if resolver_configured?
puts "DNS resolution is correctly configured"
return
end
puts "setting up DNS resolution, this will require sudo"
unless RESOLVER_DIR.directory?
cmd.run("sudo", "mkdir", "-p", RESOLVER_DIR)
end
Tempfile.open('dinghy-dnsmasq') do |f|
f.write(resolver_contents)
f.close
cmd.run("sudo", "cp", f.path, @resolver_file)
cmd.run("sudo", "chmod", "644", @resolver_file)
end
cmd.run("sudo", "killall", "mDNSResponder")
end
def resolver_configured?
@resolver_file.exist? && File.read(@resolver_file) == resolver_contents
end
def resolver_contents; <<-EOS.gsub(/^ /, '')
# Generated by dev
nameserver 127.0.0.1
port 19322
EOS
end
def remove_mutagen_sync_volumes(all: false)
labels = {
'com.outstand.mutagen-sync' => true,
}
if all == false
labels.merge!('com.docker.compose.project' => @compose_project_name)
end
remove_labelled_volumes(
labels: labels
)
end
def remove_log_volumes
remove_labelled_volumes(
labels: {
'com.outstand.logs' => true,
'com.docker.compose.project' => @compose_project_name
}
)
end
def remove_labelled_volumes(labels:)
volumes = []
Docker::Volume.all.each do |volume|
volumes << volume if labels.all? do |label, value|
if value == true
volume.info.dig('Labels', label)
else
volume.info.dig('Labels', label) == value
end
end
end
unless volumes.empty?
puts "removing: #{volumes.map(&:id).join(", ")}"
end
volumes.each do |volume|
begin
volume.remove
rescue Docker::Error::ConflictError
puts pastel.yellow("#{volume.id}: volume is in use - unable to remove")
end
end
end
def start_shared_containers
unless SHARED_CONTAINERS_DIR.directory?
cmd.run("sudo", "mkdir", "-p", SHARED_CONTAINERS_DIR)
cmd.run("sudo", "chown", "#{ENV['USER']}:admin", SHARED_CONTAINERS_DIR)
puts 'Cloning shared_containers...'
cmd.run("git", "clone", "https://github.com/outstand/shared-containers", SHARED_CONTAINERS_DIR)
end
Dir.chdir(SHARED_CONTAINERS_DIR) do
cmd.run("git", "fetch")
cmd.run("git", "reset", "--hard", "origin/master")
cmd.run("docker-compose", "up", "-d")
start_mutagen_project(dir: SHARED_CONTAINERS_DIR)
end
end
def start_mutagen_project(dir:)
Dir.chdir(dir) do
result = quiet_cmd.run!('mutagen project list')
if result.failure? && result.err == "Error: project not running\n"
cmd.run("mutagen", "project", "start")
end
end
end
def stop_mutagen_project(dir:)
Dir.chdir(dir) do
result = quiet_cmd.run!('mutagen project list')
if result.success?
cmd.run("mutagen", "project", "terminate")
end
end
end
end
DevTool.new(ARGV) if __FILE__==$0