forked from ElixirSeattle/tanx
-
Notifications
You must be signed in to change notification settings - Fork 6
/
.toys.rb
186 lines (167 loc) · 5.09 KB
/
.toys.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
# Temporary hack: bypass asdf 0.7.0 because its shims are eating signals.
# See https://github.com/asdf-vm/asdf/issues/475
elixir_path = (`asdf which elixir` rescue "").strip
erl_path = (`asdf which erl` rescue "").strip
unless elixir_path.empty? || erl_path.empty?
elixir_path = ::File.dirname(elixir_path)
erl_path = ::File.dirname(erl_path)
ENV["PATH"] = "#{erl_path}:#{elixir_path}:#{ENV['PATH']}"
end
tool "launch" do
flag :base_port, accept: Integer, default: 4000
include :exec, exit_on_nonzero_status: true
include :gems
include :terminal
def nodename(port)
"tanxport-#{port}@localhost"
end
def start(port = nil)
unless port
port = base_port
port += 1 while @controllers.key?(port)
end
if @controllers.key?(port)
puts("Port #{port} is already running.", :bold, :red)
else
start!(port)
end
end
def start!(port)
cmd = ["elixir", "--sname", nodename(port), "-S", "mix", "phx.server"]
env = {"PORT" => port.to_s}
unless @controllers.empty?
env["TANX_CONNECT_NODE"] = nodename(@controllers.keys.first)
end
controller = exec(cmd, env: env, background: true, out: :controller)
spinner(leading_text: "Starting port #{port} with pid #{controller.pid} ...",
final_text: "Done\n") do
log_name = "tmp/#{port}.log"
log_file = File.open(log_name, "a")
loop do
line = controller.out.gets
log_file.puts line
if line =~ /Running TanxWeb\.Endpoint/
log_file.close
controller.redirect_out(log_name, "a")
break
end
end
end
@controllers[port] = controller
end
def kill(port)
if @controllers.key?(port)
controller = @controllers[port]
spinner(leading_text: "Killing port #{port} with pid #{controller.pid} ...",
final_text: "Done\n") do
controller.kill("SIGTERM")
begin
loop do
controller.kill(0)
sleep(0.2)
end
rescue ::Errno::ESRCH
# Done
end
end
@controllers.delete(port)
else
puts("Port #{port} is not running.", :bold, :red)
end
end
def killall
if @controllers.empty?
puts("No ports are running.", :bold, :red)
else
@controllers.each do |port, controller|
puts("Killing port #{port} with pid #{controller.pid} ...")
controller.kill("SIGTERM")
end
spinner(leading_text: "Waiting for completion ...",
final_text: "Done\n") do
@controllers.each do |port, controller|
begin
loop do
controller.kill(0)
sleep(0.2)
end
rescue ::Errno::ESRCH
# Done
end
end
end
@controllers = {}
end
end
def quit
killall
puts("EXITING", :bold)
exit
end
def run
gem "tty-prompt", "~> 0.16"
require "tty-prompt"
prompt = TTY::Prompt.new
exec(["mix", "compile"])
@controllers = {}
start
loop do
puts("\nCurrent ports: #{@controllers.keys.sort.inspect}", :bold)
choices = ["Start port"] +
@controllers.keys.sort.map{ |p| "Kill port #{p}" } +
["Kill all", "Quit"]
choice = prompt.select("What now?", choices)
case choice
when /Kill port (\d+)/
kill($1.to_i)
when /Kill all/
killall
when /Start port/
start
when /Quit/
quit
else
puts("Unrecognized choice: #{choice.inspect}", :bold, :red)
end
end
end
end
tool "predeploy" do
flag :project, "--project=VALUE", "-p VALUE"
flag :name, "--name=VALUE", "-n VALUE", default: "tanx"
flag :yes, "--yes", "-y"
include :exec, exit_on_nonzero_status: true
include :terminal
def run
project = get(:project) || capture(["gcloud", "config", "get-value", "project"]).strip
exit(1) unless yes || confirm("Prebuild tanx dependencies in #{project}? ", default: true)
puts("Building base images...", :bold, :cyan)
exec(["gcloud", "builds", "submit",
"--project", project,
"--config", "deploy/build-base.yml",
"."])
puts("Done", :bold, :cyan)
end
end
tool "deploy" do
flag :project, "--project=VALUE", "-p VALUE"
flag :tag, "--tag=VALUE", "-t VALUE", default: ::Time.now.strftime("%Y-%m-%d-%H%M%S")
flag :name, "--name=VALUE", "-n VALUE", default: "tanx"
flag :yes, "--yes", "-y"
include :exec, exit_on_nonzero_status: true
include :terminal
def run
project = get(:project) || capture(["gcloud", "config", "get-value", "project"]).strip
exit(1) unless yes || confirm("Deploy build #{tag} in project #{project}? ", default: true)
image = "gcr.io/#{project}/#{name}:#{tag}"
puts("Building image: #{image} ...", :bold, :cyan)
exec(["gcloud", "builds", "submit",
"--project", project,
"--config", "deploy/build-tanx.yml",
"--substitutions", "_BUILD_ID=#{tag}",
"."])
puts("Updating deployment...", :bold, :cyan)
exec(["kubectl", "set", "image", "deployment/#{name}", "#{name}=#{image}"])
puts("Done", :bold, :cyan)
end
end