forked from zilionis/dash
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dev
executable file
·496 lines (410 loc) · 12.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#!/usr/bin/env ruby
require 'pathname'
require 'rbconfig'
require 'tempfile'
require 'optparse'
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'docker-api', '~> 2.0'
gem 'subprocess', '~> 1.5'
gem 'tty-command', '~> 0.10.1'
gem 'tty-prompt'
gem 'pastel'
end
require_relative "../lib/state"
require_relative "../lib/util"
class DevTool
include State::Methods
MAIN_HELP_BANNER = <<HEREDOC
Execute various commands within the developer environment
Usage:
dev [COMMAND] [ARGS...]"
dev -h|--help
Commands:
compose Manage containers (default)
update Update Dash developer environment
unset_docker_env Pass through eval to unset docker env vars
mkcert Use mkcert to set up development CA
provision_secrets Pull in developer secrets
deploy Deploy wrapper for fly
setup_network Install systemd networking
clean Clean the current project
HEREDOC
TOP_COMMANDS=%w{compose update post_update unset_docker_env mkcert provision_secrets clean deploy setup_network}
CONFIG_DIR = Pathname.new("~/.config/dev").expand_path
SHARED_CONTAINERS_DIR = Pathname.new("/opt/shared_containers")
INSTALL_DIR = Pathname.new("/opt/dev-env")
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)
@project_dir = find_project_dir!
@compose_project_name = @project_dir.basename.to_s
Dir.chdir(@project_dir) do
if args.empty?
puts `docker compose --help`
else
command = args.first
if command == 'kick'
raise ArgumentError, 'one or more services required' if args.size < 2
services = args[1..-1]
compose_command!(['rm', '-sf'] + services)
compose_command!(['up', '-d'] + services)
else
compose_command!(args)
end
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
end
end
end
end
private def find_project_dir!
project_dir = nil
if (Pathname.pwd + 'compose.yml').file? ||
(Pathname.pwd + 'docker-compose.yml').file?
project_dir = '.'
else
project_dir = `git rev-parse --show-toplevel`.chomp
if !($? == 0 && ((Pathname.new(project_dir).join('compose.yml').file?) || (Pathname.new(project_dir).join('docker-compose.yml').file?)))
puts pastel.red('ERROR: unable to automatically detect project dir')
exit 1
end
end
return Pathname.new(project_dir).expand_path
end
def compose_command!(args)
compose_files = []
if (Pathname.pwd + 'docker-compose.override.yml').file?
compose_files.unshift('-f', 'docker-compose.override.yml')
end
binary = ['docker', 'compose']
case docker_host
when 'native'
if (Pathname.pwd.join('docker', 'native.yml')).file?
compose_files.unshift('-f', 'docker/native.yml')
end
else
raise 'Unable to detect docker host!'
end
if (Pathname.pwd + 'development.yml').file?
compose_files.unshift('-f', 'development.yml')
end
# Prefer the newer compose.yml
if (Pathname.pwd + 'compose.yml').file?
compose_files.unshift('-f', 'compose.yml')
else
compose_files.unshift('-f', 'docker-compose.yml')
end
args.unshift(*compose_files)
args.unshift(*binary)
begin
run_it
.subprocess(
args,
env: {
"WORKSPACE_DIR" => @project_dir.to_s
},
preserve_env: true
)
rescue Subprocess::NonZeroExit => e
puts e.message
exit 1
rescue Interrupt
exit 1
end
end
def native_docker(args)
puts "Setting up native docker"
puts "Starting shared containers"
start_shared_containers
puts "Saving config"
CONFIG_DIR.join('docker_host').open('w') do |f|
f.write('native')
end
end
def unset_docker_env(args)
puts <<~EOS
unset DOCKER_HOST
unset DOCKER_CERT_PATH
unset DOCKER_TLS_VERIFY
EOS
end
def mkcert(*args)
options = {}
OptionParser.new do |opts|
opts.on("--domain=DOMAIN")
end.order_recognized!(*args, into: options)
%w[mkcert cacerts certs].each do |dir|
dir = CONFIG_DIR.join(dir)
unless dir.directory?
dir.mkdir
end
chown_dir_for_user(dir: dir)
end
caroot = CONFIG_DIR.join('mkcert')
ENV['CAROOT'] = caroot.to_s
if caroot.empty?
puts pastel.green('Installing local CA')
cmd.run('mkcert -install')
end
public_caroot = CONFIG_DIR.join('cacerts')
if public_caroot.empty?
puts pastel.green('Copying local CA to public directory')
FileUtils.cp caroot.join('rootCA.pem'), public_caroot.join('dev-mkcert.crt')
end
cert_dir = CONFIG_DIR.join('certs')
cert_created = false
if options.has_key?(:domain)
cert_created =
mkcert_for(domain: options[:domain], cert_dir: cert_dir)
else
domains = ['s3.test', 'app.outstand.test', 'webpacker.test', 'ember.test', 'kibana.test', 'mailhog.test', 'portainer.test', 'atlas.test', 'public-site.test', 'webhooks.test', 'recipient-events.test', 'wealthwave.test', 'howmoneyworks.test', 'www.howmoneyworks.test', 'mpoweringamerica.test', 'livingbenefitsexperts.test', 'whyequis.test']
cert_created =
domains.any? do |domain|
mkcert_for(domain: domain, cert_dir: cert_dir)
end
end
if cert_created
puts pastel.yellow('WARNING: Restarting http-proxy')
Dir.chdir(SHARED_CONTAINERS_DIR) do
compose(['kick', 'http-proxy'])
end
end
end
private def mkcert_for(domain:, cert_dir:)
cert_created = false
if !cert_dir.join("#{domain}.crt").file? || !cert_dir.join("#{domain}.key").file?
puts pastel.green("Generating certs for #{domain}")
Dir.chdir(cert_dir) do
cmd.run("mkcert -cert-file #{domain}.crt -key-file #{domain}.key #{domain} *.#{domain}")
cert_created = true
end
end
cert_created
end
def provision_secrets(_args = nil)
zsh_dir = Pathname.new("~/.zsh.after").expand_path
zsh_config = zsh_dir + "mailhog.zsh"
if zsh_config.file?
puts pastel.yellow("Skipping mailhog secret provisioning; file already exists")
return
end
puts pastel.green("Provisioning mailhog secrets")
# TODO: the following commands do not exit 1 on error. I need to grep for "An error occurred"
out, err = quiet_cmd.run("aws secretsmanager get-secret-value --secret-id /dev/mailhog/user | jq -r '.SecretString'")
user = out.strip
out, err = quiet_cmd.run("aws secretsmanager get-secret-value --secret-id /dev/mailhog/password | jq -r '.SecretString'")
password = out.strip
lines = []
lines << "export SENDGRID_SMTP_USERNAME=#{user}"
lines << "export SENDGRID_SMTP_PASSWORD=#{password}"
zsh_dir.mkpath
zsh_config.write(lines.join("\n"))
end
def setup_network(_args = nil)
network_dir = Pathname.new("/etc/systemd/network")
dummy_netdev = network_dir + "10-dummy0.netdev"
dummy_network = network_dir + "10-dummy0.network"
create_system_file(
file: dummy_netdev,
chmod: "644",
contents: netdev_contents
)
create_system_file(
file: dummy_network,
chmod: "644",
contents: network_contents
)
end
private def netdev_contents
<<~EOS
[NetDev]
Description=Dummy interface for docker and dns
Name=dummy0
Kind=dummy
EOS
end
private def network_contents
<<~EOS
[Match]
Kind=dummy
Name=dummy0
[Network]
Description=Dummy network for docker and dns
Address=169.254.1.1/32
DNS=169.254.1.1:53
Domains=~test
EOS
end
def clean(_args = nil)
return if prompt.no?("This command will remove all volumes and state data! Do you want to continue?")
@project_dir = find_project_dir!
compose_command!(["down", "--remove-orphans", "-v"])
Dir.chdir(@project_dir) do
puts pastel.yellow("Listing files/directories to be removed:")
cmd.run("git clean -X -n -d -ff")
if prompt.yes?("Do you want to remove the above?")
cmd.run("git clean -X -f -d -ff")
end
end
end
def deploy(args)
@project_dir = find_project_dir!
Dir.chdir(@project_dir) do
exec((INSTALL_DIR + 'tools/deploy').to_s, *args)
end
end
def update(args)
if Util.mac?
puts pastel.red("dev update should only be run on your remote VM!")
exit 1
end
cmd.run("cd #{INSTALL_DIR} && git fetch && git reset --hard origin/main") unless args.include?("--no-pull")
exec((INSTALL_DIR + 'bin/dev').to_s, 'post_update', *args)
end
def post_update(args)
unless args.include?("--no-ansible")
ansible_cmd = nil
if Util.mac?
ansible_cmd = ['ansible-playbook', "#{INSTALL_DIR}/ansible/mac.yml", '-i', '127.0.0.1', '-K']
elsif Util.linux?
ansible_cmd = ['ansible-playbook', "#{INSTALL_DIR}/ansible/linux.yml", '-i', '127.0.0.1', '-K', '-v']
else
raise "Unknown platform!"
end
$stdout.sync
begin
Subprocess.check_call(
ansible_cmd,
stdin: $stdin,
stdout: $stdout,
stderr: $stderr
)
puts
rescue Subprocess::NonZeroExit => e
puts e.message
exit 1
rescue Interrupt
exit 1
end
end
native_docker [] if Util.linux?
mkcert
provision_secrets
setup_network
end
private
def state
return @state if defined?(@state)
@state =
State.new(log_level: log_level)
end
def log_level
Logger::INFO
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)
end
chown_system_dir_for_user(dir: SHARED_CONTAINERS_DIR)
if SHARED_CONTAINERS_DIR.empty?
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/main")
cmd.run("docker", "compose", "up", "-d")
end
end
def docker_host
file = CONFIG_DIR.join('docker_host')
if file.exist?
file.open('r') do |f|
f.read
end
else
nil
end
end
def prompt
return @prompt if defined?(@prompt)
@prompt =
TTY::Prompt.new.tap do |p|
# vim keys
p.on(:keypress) do |event|
if event.value == "j"
prompt.trigger(:keydown)
end
if event.value == "k"
prompt.trigger(:keyup)
end
end
end
end
end
class OptionParser
# Like order!, but leave any unrecognized --switches alone
def order_recognized!(argv = default_argv, into: nil)
extra_opts = []
begin
order!(argv, into: into) { |a| extra_opts << a }
rescue OptionParser::InvalidOption => e
extra_opts << e.args[0]
retry
end
argv[0,0] = extra_opts
end
end
DevTool.new(ARGV) if __FILE__==$0