devbox_launcher 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +20 -0
- data/Gemfile.lock +8 -8
- data/lib/devbox_launcher.rb +2 -0
- data/lib/devbox_launcher/box.rb +214 -0
- data/lib/devbox_launcher/cli.rb +1 -175
- data/lib/devbox_launcher/models/box.rb +222 -0
- data/lib/devbox_launcher/models/description.rb +32 -0
- data/lib/devbox_launcher/models/mutagen.rb +23 -0
- data/lib/devbox_launcher/version.rb +1 -1
- metadata +11 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eb4a378b2f76aac6847c19aa98173e0ec1eaa4dd94ee5983dce3b2b2025ff989
|
4
|
+
data.tar.gz: e0185ccc0c83c43091399da51229fe7f677b6bf295362449b81deca893a359a5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 00dcd892ade882a93cc908451b6ed9dedb229160f55f28a9aa2735e573aafae7732e6df4221fbf06d0669060f5ec4082d331de908917ae172a0157c84cef001b
|
7
|
+
data.tar.gz: dee55da84e14e426fdc605201725f3ddb0d9525e9838edbae1a8af62438420040d51a48cda06cac2e898383b3b85bfa8d62355148f63ab2870373cb942198901
|
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,26 @@ All notable changes to this project will be documented in this file.
|
|
4
4
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
5
5
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
6
|
|
7
|
+
## [0.4.0]
|
8
|
+
### Added
|
9
|
+
- Sync mutagen with two-way-resolved
|
10
|
+
|
11
|
+
## [0.3.5]
|
12
|
+
### Fixed
|
13
|
+
- When running commands, also rescue from whitelist of exceptions, and retry
|
14
|
+
|
15
|
+
## [0.3.4]
|
16
|
+
### Added
|
17
|
+
- Rescue from `Errno::ECONNREFUSED` and retry
|
18
|
+
|
19
|
+
## [0.3.3]
|
20
|
+
### Fixed
|
21
|
+
- Support launching multiple boxes at the same time
|
22
|
+
|
23
|
+
## [0.3.2]
|
24
|
+
### Fixed
|
25
|
+
- Fix: add missing file
|
26
|
+
|
7
27
|
## [0.3.1]
|
8
28
|
### Fixed
|
9
29
|
- Recover when the devbox is in a shutdown cycle
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
devbox_launcher (0.3.
|
4
|
+
devbox_launcher (0.3.5)
|
5
5
|
activesupport (~> 6.0)
|
6
6
|
bcrypt_pbkdf (~> 1.0)
|
7
7
|
ed25519 (~> 1.2)
|
@@ -14,7 +14,7 @@ PATH
|
|
14
14
|
GEM
|
15
15
|
remote: https://rubygems.org/
|
16
16
|
specs:
|
17
|
-
activesupport (6.0.3.
|
17
|
+
activesupport (6.0.3.4)
|
18
18
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
19
19
|
i18n (>= 0.7, < 2)
|
20
20
|
minitest (~> 5.1)
|
@@ -23,15 +23,15 @@ GEM
|
|
23
23
|
bcrypt_pbkdf (1.0.1)
|
24
24
|
byebug (11.0.1)
|
25
25
|
coderay (1.1.2)
|
26
|
-
concurrent-ruby (1.1.
|
26
|
+
concurrent-ruby (1.1.7)
|
27
27
|
diff-lcs (1.3)
|
28
28
|
ed25519 (1.2.4)
|
29
|
-
i18n (1.8.
|
29
|
+
i18n (1.8.5)
|
30
30
|
concurrent-ruby (~> 1.0)
|
31
31
|
method_source (0.9.2)
|
32
|
-
minitest (5.14.
|
32
|
+
minitest (5.14.2)
|
33
33
|
net-ssh (5.2.0)
|
34
|
-
os (1.1.
|
34
|
+
os (1.1.1)
|
35
35
|
pry (0.12.2)
|
36
36
|
coderay (~> 1.1.0)
|
37
37
|
method_source (~> 0.9.0)
|
@@ -56,9 +56,9 @@ GEM
|
|
56
56
|
ssh-config (0.1.3)
|
57
57
|
thor (1.0.1)
|
58
58
|
thread_safe (0.3.6)
|
59
|
-
tzinfo (1.2.
|
59
|
+
tzinfo (1.2.8)
|
60
60
|
thread_safe (~> 0.1)
|
61
|
-
zeitwerk (2.
|
61
|
+
zeitwerk (2.4.1)
|
62
62
|
|
63
63
|
PLATFORMS
|
64
64
|
ruby
|
data/lib/devbox_launcher.rb
CHANGED
@@ -0,0 +1,214 @@
|
|
1
|
+
module DevboxLauncher
|
2
|
+
class Box
|
3
|
+
|
4
|
+
WAIT_BOOT_RESCUED_EXCEPTIONS = [
|
5
|
+
Net::SSH::ConnectionTimeout,
|
6
|
+
Net::SSH::Disconnect,
|
7
|
+
Errno::ECONNRESET,
|
8
|
+
Errno::ETIMEDOUT,
|
9
|
+
Errno::ECONNREFUSED,
|
10
|
+
]
|
11
|
+
WAIT_BOOT_IN_SECONDS = 10.freeze
|
12
|
+
DEFAULT_IDENTIFY_FILE_PATH = "~/.ssh/google_compute_engine".freeze
|
13
|
+
SSH_CONFIG_PATH = File.expand_path("~/.ssh/config").freeze
|
14
|
+
CONFIG_PATH = File.expand_path("~/.devbox_launcher.yml").freeze
|
15
|
+
CONFIG = YAML.load_file(CONFIG_PATH).freeze
|
16
|
+
|
17
|
+
attr_reader :account
|
18
|
+
|
19
|
+
def initialize(account)
|
20
|
+
@account = account
|
21
|
+
end
|
22
|
+
|
23
|
+
def start
|
24
|
+
start_stdout, start_stderr, start_status =
|
25
|
+
run_command(start_cmd)
|
26
|
+
|
27
|
+
set_ssh_config!(hostname, {
|
28
|
+
username: username,
|
29
|
+
ip: description.ip,
|
30
|
+
})
|
31
|
+
|
32
|
+
wait_boot
|
33
|
+
|
34
|
+
reset_mutagen_session(
|
35
|
+
mutagen_config: config[:mutagen],
|
36
|
+
hostname: hostname,
|
37
|
+
)
|
38
|
+
end
|
39
|
+
|
40
|
+
def start_cmd
|
41
|
+
args = {
|
42
|
+
project: config[:project],
|
43
|
+
account: account,
|
44
|
+
}.map do |(key, val)|
|
45
|
+
["--#{key}", val].join("=")
|
46
|
+
end.join(" ")
|
47
|
+
|
48
|
+
[
|
49
|
+
"gcloud",
|
50
|
+
"compute",
|
51
|
+
"instances",
|
52
|
+
"start",
|
53
|
+
name,
|
54
|
+
args
|
55
|
+
].join(" ")
|
56
|
+
end
|
57
|
+
|
58
|
+
def wait_boot(tries: 1)
|
59
|
+
Net::SSH.start(hostname, username, timeout: WAIT_BOOT_IN_SECONDS) do |ssh|
|
60
|
+
puts "[#{ssh.exec!('date').chomp}] Machine booted"
|
61
|
+
end
|
62
|
+
rescue *WAIT_BOOT_RESCUED_EXCEPTIONS
|
63
|
+
puts "Not booted. Waiting #{WAIT_BOOT_IN_SECONDS} seconds before trying again..."
|
64
|
+
|
65
|
+
sleep WAIT_BOOT_IN_SECONDS
|
66
|
+
|
67
|
+
description = describe(name)
|
68
|
+
if !description.running?
|
69
|
+
puts "Detected that the machine is not running " \
|
70
|
+
"(status is #{description.status}). Booting it..."
|
71
|
+
start_box name, username
|
72
|
+
end
|
73
|
+
|
74
|
+
wait_boot name, username, tries: tries+1
|
75
|
+
end
|
76
|
+
|
77
|
+
def description(reload: false)
|
78
|
+
return @description if !reload && @description
|
79
|
+
|
80
|
+
puts "Fetching box's description..."
|
81
|
+
|
82
|
+
describe_command = %Q(gcloud compute instances describe #{name})
|
83
|
+
describe_stdout, describe_stderr, describe_status =
|
84
|
+
run_command(describe_command)
|
85
|
+
|
86
|
+
if !describe_status.success?
|
87
|
+
msg = "Problem fetching the description of #{name}. "
|
88
|
+
msg += "Please ensure you can call `#{describe_command}`.\n"
|
89
|
+
msg += "Error:\n"
|
90
|
+
msg += describe_stderr
|
91
|
+
fail msg
|
92
|
+
end
|
93
|
+
|
94
|
+
@description = Description.new(describe_stdout)
|
95
|
+
end
|
96
|
+
|
97
|
+
def set_ssh_config!(hostname, username:, ip:)
|
98
|
+
FileUtils.touch(SSH_CONFIG_PATH)
|
99
|
+
config = ConfigFile.new
|
100
|
+
args = {
|
101
|
+
"HostName" => ip,
|
102
|
+
"User" => username,
|
103
|
+
"IdentityFile" => DEFAULT_IDENTIFY_FILE_PATH,
|
104
|
+
}
|
105
|
+
args.each do |key, value|
|
106
|
+
config.set(hostname, key, value)
|
107
|
+
end
|
108
|
+
config.save
|
109
|
+
end
|
110
|
+
|
111
|
+
def reset_mutagen_session
|
112
|
+
mutagen_config = config[:mutagen]
|
113
|
+
return if mutagen_config.nil?
|
114
|
+
|
115
|
+
alpha_dir = mutagen_config[:alpha]
|
116
|
+
beta_dir = mutagen_config[:beta]
|
117
|
+
|
118
|
+
return if alpha_dir.nil? || beta_dir.nil?
|
119
|
+
|
120
|
+
terminate_mutagen_session
|
121
|
+
|
122
|
+
create_mutagen_session(
|
123
|
+
alpha_dir: alpha_dir,
|
124
|
+
beta_dir: beta_dir,
|
125
|
+
hostname: hostname,
|
126
|
+
username: username,
|
127
|
+
)
|
128
|
+
|
129
|
+
if OS.linux?
|
130
|
+
watch_alpha(alpha_dir: alpha_dir, hostname: hostname)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def terminate_mutagen_session(username)
|
135
|
+
puts "Terminating mutagen session..."
|
136
|
+
terminate_mutagen_command =
|
137
|
+
%Q(mutagen terminate --label-selector=#{username})
|
138
|
+
terminate_mutagen_stdout,
|
139
|
+
terminate_mutagen_stderr,
|
140
|
+
terminate_mutagen_status =
|
141
|
+
run_command(terminate_mutagen_command)
|
142
|
+
|
143
|
+
if not terminate_mutagen_status.success?
|
144
|
+
# mutagen prints to stdout and stderr
|
145
|
+
msg = "Failed to terminate mutagen sessions: " \
|
146
|
+
"#{terminate_mutagen_stdout} -" \
|
147
|
+
"#{terminate_mutagen_stderr}"
|
148
|
+
fail msg
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
def create_mutagen_session(alpha_dir:, beta_dir:, hostname:, username:)
|
153
|
+
puts "Create mutagen session syncing local #{alpha_dir} " \
|
154
|
+
"with #{hostname} #{beta_dir}"
|
155
|
+
|
156
|
+
create_mutagen_command = [
|
157
|
+
"mutagen sync create",
|
158
|
+
alpha_dir,
|
159
|
+
"#{hostname}:#{beta_dir}",
|
160
|
+
"--label=#{username}",
|
161
|
+
"--sync-mode=two-way-resolved",
|
162
|
+
]
|
163
|
+
create_mutagen_command << "--watch-mode-alpha=no-watch" if OS.linux?
|
164
|
+
|
165
|
+
create_mutagen_stdout,
|
166
|
+
create_mutagen_stderr,
|
167
|
+
create_mutagen_status =
|
168
|
+
run_command(create_mutagen_command.join(" "))
|
169
|
+
|
170
|
+
if not create_mutagen_status.success?
|
171
|
+
# mutagen prints to stdout and stderr
|
172
|
+
msg = "Failed to create mutagen sessions: " \
|
173
|
+
"#{create_mutagen_stdout} -" \
|
174
|
+
"#{create_mutagen_stderr}"
|
175
|
+
fail msg
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
def watch_alpha(alpha_dir:, hostname:)
|
180
|
+
watchman = Watchman.new(dir: alpha_dir)
|
181
|
+
watchman.trigger("mutagen sync flush --label-selector=#{hostname}")
|
182
|
+
end
|
183
|
+
|
184
|
+
def name
|
185
|
+
@name ||= config[:box]
|
186
|
+
end
|
187
|
+
|
188
|
+
def hostname
|
189
|
+
[name, username, "devbox"].join("-")
|
190
|
+
end
|
191
|
+
|
192
|
+
def username
|
193
|
+
@username ||= account.gsub(/\W/, "_")
|
194
|
+
end
|
195
|
+
|
196
|
+
def config
|
197
|
+
return @config if @config
|
198
|
+
|
199
|
+
if not CONFIG.has_key?(account)
|
200
|
+
fail "No config in #{CONFIG_PATH} found for #{account}"
|
201
|
+
end
|
202
|
+
|
203
|
+
@config = CONFIG[account].with_indifferent_access
|
204
|
+
end
|
205
|
+
|
206
|
+
def run_command(command, tries: 0)
|
207
|
+
Open3.capture3(command)
|
208
|
+
rescue *WAIT_BOOT_RESCUED_EXCEPTIONS
|
209
|
+
sleep WAIT_BOOT_IN_SECONDS
|
210
|
+
run_command(command, tries+1)
|
211
|
+
end
|
212
|
+
|
213
|
+
end
|
214
|
+
end
|
data/lib/devbox_launcher/cli.rb
CHANGED
@@ -6,186 +6,12 @@ module DevboxLauncher
|
|
6
6
|
SSH_CONFIG_PATH = File.expand_path("~/.ssh/config").freeze
|
7
7
|
CONFIG_PATH = File.expand_path("~/.devbox_launcher.yml").freeze
|
8
8
|
CONFIG = YAML.load_file(CONFIG_PATH).freeze
|
9
|
-
LABEL = "devbox".freeze
|
10
9
|
|
11
10
|
desc "start configured box for account", "Start a devbox by account"
|
12
11
|
option :mosh, type: :boolean, desc: "Mosh in"
|
13
12
|
|
14
13
|
def start(account)
|
15
|
-
|
16
|
-
fail "No config in #{CONFIG_PATH} found for #{account}"
|
17
|
-
end
|
18
|
-
|
19
|
-
config = CONFIG[account].with_indifferent_access
|
20
|
-
name = config[:box]
|
21
|
-
|
22
|
-
username = account.gsub(/\W/, "_")
|
23
|
-
|
24
|
-
puts "Starting #{name}..."
|
25
|
-
|
26
|
-
set_account_command = %Q(gcloud config set account #{account})
|
27
|
-
set_account_stdout, set_account_stderr, set_account_status =
|
28
|
-
Open3.capture3(set_account_command)
|
29
|
-
|
30
|
-
set_project_command = %Q(gcloud config set project #{config[:project]})
|
31
|
-
set_project_stdout, set_project_stderr, set_project_status =
|
32
|
-
Open3.capture3(set_project_command)
|
33
|
-
|
34
|
-
start_box name, username
|
35
|
-
|
36
|
-
wait_boot(name, username)
|
37
|
-
|
38
|
-
hostname = hostname_for(name)
|
39
|
-
|
40
|
-
reset_mutagen_session(
|
41
|
-
mutagen_config: config[:mutagen],
|
42
|
-
hostname: hostname,
|
43
|
-
)
|
44
|
-
|
45
|
-
if options[:mosh]
|
46
|
-
mosh_command = %Q(mosh #{hostname})
|
47
|
-
system(mosh_command)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
no_commands do
|
52
|
-
def wait_boot(name, username, tries: 1)
|
53
|
-
hostname = hostname_for(name)
|
54
|
-
|
55
|
-
Net::SSH.start(hostname, username, timeout: WAIT_BOOT_IN_SECONDS) do |ssh|
|
56
|
-
puts "[#{ssh.exec!('date').chomp}] Machine booted"
|
57
|
-
end
|
58
|
-
rescue Net::SSH::ConnectionTimeout, Net::SSH::Disconnect, Errno::ECONNRESET
|
59
|
-
puts "Not booted. Waiting #{WAIT_BOOT_IN_SECONDS} seconds before trying again..."
|
60
|
-
|
61
|
-
sleep WAIT_BOOT_IN_SECONDS
|
62
|
-
|
63
|
-
description = describe(name)
|
64
|
-
if !description.running?
|
65
|
-
puts "Detected that the machine is not running " \
|
66
|
-
"(status is #{description.status}). Booting it..."
|
67
|
-
start_box name, username
|
68
|
-
end
|
69
|
-
|
70
|
-
wait_boot name, username, tries: tries+1
|
71
|
-
end
|
72
|
-
|
73
|
-
def set_ssh_config!(hostname, username:, ip:)
|
74
|
-
FileUtils.touch(SSH_CONFIG_PATH)
|
75
|
-
config = ConfigFile.new
|
76
|
-
args = {
|
77
|
-
"HostName" => ip,
|
78
|
-
"User" => username,
|
79
|
-
"IdentityFile" => DEFAULT_IDENTIFY_FILE_PATH,
|
80
|
-
}
|
81
|
-
args.each do |key, value|
|
82
|
-
config.set(hostname, key, value)
|
83
|
-
end
|
84
|
-
config.save
|
85
|
-
end
|
86
|
-
|
87
|
-
def reset_mutagen_session(mutagen_config:, hostname:)
|
88
|
-
return if mutagen_config.nil?
|
89
|
-
alpha_dir = mutagen_config[:alpha]
|
90
|
-
beta_dir = mutagen_config[:beta]
|
91
|
-
|
92
|
-
return if alpha_dir.nil? || beta_dir.nil?
|
93
|
-
|
94
|
-
terminate_mutagen_session
|
95
|
-
create_mutagen_session(
|
96
|
-
alpha_dir: alpha_dir,
|
97
|
-
beta_dir: beta_dir,
|
98
|
-
hostname: hostname,
|
99
|
-
)
|
100
|
-
|
101
|
-
if OS.linux?
|
102
|
-
watch_alpha(alpha_dir: alpha_dir)
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
def terminate_mutagen_session
|
107
|
-
puts "Terminating mutagen session..."
|
108
|
-
terminate_mutagen_command =
|
109
|
-
%Q(mutagen terminate --label-selector=#{LABEL})
|
110
|
-
terminate_mutagen_stdout,
|
111
|
-
terminate_mutagen_stderr,
|
112
|
-
terminate_mutagen_status =
|
113
|
-
Open3.capture3(terminate_mutagen_command)
|
114
|
-
|
115
|
-
if not terminate_mutagen_status.success?
|
116
|
-
# mutagen prints to stdout and stderr
|
117
|
-
msg = "Failed to terminate mutagen sessions: " \
|
118
|
-
"#{terminate_mutagen_stdout} -" \
|
119
|
-
"#{terminate_mutagen_stderr}"
|
120
|
-
fail msg
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
|
-
def create_mutagen_session(alpha_dir:, beta_dir:, hostname:)
|
125
|
-
puts "Create mutagen session syncing local #{alpha_dir} " \
|
126
|
-
"with #{hostname} #{beta_dir}"
|
127
|
-
|
128
|
-
create_mutagen_command = [
|
129
|
-
"mutagen sync create",
|
130
|
-
alpha_dir,
|
131
|
-
"#{hostname}:#{beta_dir}",
|
132
|
-
"--label=#{LABEL}",
|
133
|
-
]
|
134
|
-
create_mutagen_command << "--watch-mode-alpha=no-watch" if OS.linux?
|
135
|
-
|
136
|
-
create_mutagen_stdout,
|
137
|
-
create_mutagen_stderr,
|
138
|
-
create_mutagen_status =
|
139
|
-
Open3.capture3(create_mutagen_command.join(" "))
|
140
|
-
|
141
|
-
if not create_mutagen_status.success?
|
142
|
-
# mutagen prints to stdout and stderr
|
143
|
-
msg = "Failed to create mutagen sessions: " \
|
144
|
-
"#{create_mutagen_stdout} -" \
|
145
|
-
"#{create_mutagen_stderr}"
|
146
|
-
fail msg
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
|
-
def watch_alpha(alpha_dir:)
|
151
|
-
watchman = Watchman.new(dir: alpha_dir)
|
152
|
-
watchman.trigger("mutagen sync flush --label-selector=#{LABEL}")
|
153
|
-
end
|
154
|
-
|
155
|
-
def start_box(name, username)
|
156
|
-
start_command = %Q(gcloud compute instances start #{name})
|
157
|
-
start_stdout, start_stderr, start_status = Open3.capture3(start_command)
|
158
|
-
|
159
|
-
desc = describe(name)
|
160
|
-
ip = desc.ip
|
161
|
-
|
162
|
-
set_ssh_config!(hostname_for(name), {
|
163
|
-
username: username,
|
164
|
-
ip: ip,
|
165
|
-
})
|
166
|
-
end
|
167
|
-
|
168
|
-
def describe(name)
|
169
|
-
puts "Fetching box's description..."
|
170
|
-
|
171
|
-
describe_command = %Q(gcloud compute instances describe #{name})
|
172
|
-
describe_stdout, describe_stderr, describe_status =
|
173
|
-
Open3.capture3(describe_command)
|
174
|
-
|
175
|
-
if !describe_status.success?
|
176
|
-
msg = "Problem fetching the description of #{name}. "
|
177
|
-
msg += "Please ensure you can call `#{describe_command}`.\n"
|
178
|
-
msg += "Error:\n"
|
179
|
-
msg += describe_stderr
|
180
|
-
fail msg
|
181
|
-
end
|
182
|
-
|
183
|
-
Description.new(describe_stdout)
|
184
|
-
end
|
185
|
-
|
186
|
-
def hostname_for(name)
|
187
|
-
[name, "devbox"].join("-")
|
188
|
-
end
|
14
|
+
Box.new(account, options).start
|
189
15
|
end
|
190
16
|
|
191
17
|
end
|
@@ -0,0 +1,222 @@
|
|
1
|
+
module DevboxLauncher
|
2
|
+
class Box
|
3
|
+
|
4
|
+
WAIT_BOOT_RESCUED_EXCEPTIONS = [
|
5
|
+
Net::SSH::ConnectionTimeout,
|
6
|
+
Net::SSH::Disconnect,
|
7
|
+
Errno::ECONNRESET,
|
8
|
+
Errno::ETIMEDOUT,
|
9
|
+
]
|
10
|
+
WAIT_BOOT_IN_SECONDS = 10.freeze
|
11
|
+
MAX_BOOT_RETRIES = 10
|
12
|
+
DEFAULT_IDENTIFY_FILE_PATH = "~/.ssh/google_compute_engine".freeze
|
13
|
+
SSH_CONFIG_PATH = File.expand_path("~/.ssh/config").freeze
|
14
|
+
CONFIG_PATH = File.expand_path("~/.devbox_launcher.yml").freeze
|
15
|
+
CONFIG = YAML.load_file(CONFIG_PATH).freeze
|
16
|
+
|
17
|
+
attr_reader :account, :options
|
18
|
+
|
19
|
+
def initialize(account, options)
|
20
|
+
@account = account
|
21
|
+
@options = options
|
22
|
+
end
|
23
|
+
|
24
|
+
def start
|
25
|
+
start_stdout, start_stderr, start_status =
|
26
|
+
Open3.capture3(start_cmd)
|
27
|
+
|
28
|
+
set_ssh_config!
|
29
|
+
|
30
|
+
wait_boot
|
31
|
+
|
32
|
+
reset_mutagen_session
|
33
|
+
|
34
|
+
connect_mosh
|
35
|
+
end
|
36
|
+
|
37
|
+
def start_cmd
|
38
|
+
args = {
|
39
|
+
project: config[:project],
|
40
|
+
account: account,
|
41
|
+
}.map do |(key, val)|
|
42
|
+
["--#{key}", val].join("=")
|
43
|
+
end.join(" ")
|
44
|
+
|
45
|
+
[
|
46
|
+
"gcloud",
|
47
|
+
"compute",
|
48
|
+
"instances",
|
49
|
+
"start",
|
50
|
+
name,
|
51
|
+
args
|
52
|
+
].join(" ")
|
53
|
+
end
|
54
|
+
|
55
|
+
def connect_mosh
|
56
|
+
return if options[:mosh].nil?
|
57
|
+
|
58
|
+
mosh_cmd = %Q(mosh #{hostname})
|
59
|
+
system(mosh_cmd)
|
60
|
+
end
|
61
|
+
|
62
|
+
def wait_boot(tries: 1)
|
63
|
+
Net::SSH.start(hostname, username, timeout: WAIT_BOOT_IN_SECONDS) do |ssh|
|
64
|
+
puts "[#{ssh.exec!('date').chomp}] Machine booted"
|
65
|
+
end
|
66
|
+
rescue *WAIT_BOOT_RESCUED_EXCEPTIONS
|
67
|
+
puts "Not booted. Waiting #{WAIT_BOOT_IN_SECONDS} seconds before trying again..."
|
68
|
+
|
69
|
+
sleep WAIT_BOOT_IN_SECONDS
|
70
|
+
|
71
|
+
if !description(reload: true).running?
|
72
|
+
puts "Detected that the machine is not running " \
|
73
|
+
"(status is #{description.status}). Booting it..."
|
74
|
+
start
|
75
|
+
end
|
76
|
+
|
77
|
+
fail if tries >= MAX_BOOT_RETRIES
|
78
|
+
|
79
|
+
wait_boot tries: tries+1
|
80
|
+
end
|
81
|
+
|
82
|
+
def description(reload: false)
|
83
|
+
return @description if !reload && @description
|
84
|
+
|
85
|
+
puts "Fetching box's description..."
|
86
|
+
|
87
|
+
describe_stdout, describe_stderr, describe_status =
|
88
|
+
Open3.capture3(describe_cmd)
|
89
|
+
|
90
|
+
if !describe_status.success?
|
91
|
+
msg = "Problem fetching the description of #{name}. "
|
92
|
+
msg += "Please ensure you can call `#{describe_cmd}`.\n"
|
93
|
+
msg += "Error:\n"
|
94
|
+
msg += describe_stderr
|
95
|
+
fail msg
|
96
|
+
end
|
97
|
+
|
98
|
+
@description = Description.new(describe_stdout)
|
99
|
+
end
|
100
|
+
|
101
|
+
def describe_cmd
|
102
|
+
args = {
|
103
|
+
project: config[:project],
|
104
|
+
account: account,
|
105
|
+
}.map do |(key, val)|
|
106
|
+
["--#{key}", val].join("=")
|
107
|
+
end.join(" ")
|
108
|
+
|
109
|
+
[
|
110
|
+
"gcloud",
|
111
|
+
"compute",
|
112
|
+
"instances",
|
113
|
+
"describe",
|
114
|
+
name,
|
115
|
+
args
|
116
|
+
].join(" ")
|
117
|
+
end
|
118
|
+
|
119
|
+
def set_ssh_config!
|
120
|
+
FileUtils.touch(SSH_CONFIG_PATH)
|
121
|
+
config = ConfigFile.new
|
122
|
+
args = {
|
123
|
+
"HostName" => description.ip,
|
124
|
+
"User" => username,
|
125
|
+
"IdentityFile" => DEFAULT_IDENTIFY_FILE_PATH,
|
126
|
+
}
|
127
|
+
args.each do |key, value|
|
128
|
+
config.set(hostname, key, value)
|
129
|
+
end
|
130
|
+
config.save
|
131
|
+
end
|
132
|
+
|
133
|
+
def reset_mutagen_session
|
134
|
+
return if !mutagen_config.configured?
|
135
|
+
|
136
|
+
terminate_mutagen_session
|
137
|
+
create_mutagen_session
|
138
|
+
watch_alpha if OS.linux?
|
139
|
+
end
|
140
|
+
|
141
|
+
def terminate_mutagen_session
|
142
|
+
puts "Terminating mutagen session..."
|
143
|
+
terminate_mutagen_cmd =
|
144
|
+
%Q(mutagen terminate --label-selector=#{label})
|
145
|
+
terminate_mutagen_stdout,
|
146
|
+
terminate_mutagen_stderr,
|
147
|
+
terminate_mutagen_status =
|
148
|
+
Open3.capture3(terminate_mutagen_cmd)
|
149
|
+
|
150
|
+
if not terminate_mutagen_status.success?
|
151
|
+
# mutagen prints to stdout and stderr
|
152
|
+
msg = "Failed to terminate mutagen sessions: " \
|
153
|
+
"#{terminate_mutagen_stdout} -" \
|
154
|
+
"#{terminate_mutagen_stderr}"
|
155
|
+
fail msg
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
def label
|
160
|
+
"#{username}=#{name}"
|
161
|
+
end
|
162
|
+
|
163
|
+
def create_mutagen_session
|
164
|
+
puts "Create mutagen session syncing local " \
|
165
|
+
"#{mutagen_config.alpha_dir} with " \
|
166
|
+
"#{hostname} #{mutagen_config.beta_dir}"
|
167
|
+
|
168
|
+
create_mutagen_cmd = [
|
169
|
+
"mutagen sync create",
|
170
|
+
mutagen_config.alpha_dir,
|
171
|
+
"#{hostname}:#{mutagen_config.beta_dir}",
|
172
|
+
"--label=#{label}",
|
173
|
+
]
|
174
|
+
create_mutagen_cmd << "--watch-mode-alpha=no-watch" if OS.linux?
|
175
|
+
|
176
|
+
create_mutagen_stdout,
|
177
|
+
create_mutagen_stderr,
|
178
|
+
create_mutagen_status =
|
179
|
+
Open3.capture3(create_mutagen_cmd.join(" "))
|
180
|
+
|
181
|
+
if not create_mutagen_status.success?
|
182
|
+
# mutagen prints to stdout and stderr
|
183
|
+
msg = "Failed to create mutagen sessions: " \
|
184
|
+
"#{create_mutagen_stdout} -" \
|
185
|
+
"#{create_mutagen_stderr}"
|
186
|
+
fail msg
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
def watch_alpha
|
191
|
+
watchman = Watchman.new(dir: mutagen_config.alpha_dir)
|
192
|
+
watchman.trigger("mutagen sync flush --label-selector=#{label}")
|
193
|
+
end
|
194
|
+
|
195
|
+
def name
|
196
|
+
@name ||= config[:box]
|
197
|
+
end
|
198
|
+
|
199
|
+
def hostname
|
200
|
+
[name, username, "devbox"].join("-")
|
201
|
+
end
|
202
|
+
|
203
|
+
def username
|
204
|
+
@username ||= account.gsub(/\W/, "_")
|
205
|
+
end
|
206
|
+
|
207
|
+
def config
|
208
|
+
return @config if @config
|
209
|
+
|
210
|
+
if not CONFIG.has_key?(account)
|
211
|
+
fail "No config in #{CONFIG_PATH} found for #{account}"
|
212
|
+
end
|
213
|
+
|
214
|
+
@config = CONFIG[account].with_indifferent_access
|
215
|
+
end
|
216
|
+
|
217
|
+
def mutagen_config
|
218
|
+
@mutagen_config ||= Mutagen.new(config[:mutagen])
|
219
|
+
end
|
220
|
+
|
221
|
+
end
|
222
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module DevboxLauncher
|
2
|
+
class Description
|
3
|
+
|
4
|
+
def initialize(yaml)
|
5
|
+
@desc = YAML.load(yaml)
|
6
|
+
end
|
7
|
+
|
8
|
+
def ip
|
9
|
+
return @ip if @ip
|
10
|
+
network_interface = network_interfaces.first
|
11
|
+
access_configs = network_interface["accessConfigs"]
|
12
|
+
|
13
|
+
access_config = access_configs.find do |c|
|
14
|
+
c["kind"] == "compute#accessConfig"
|
15
|
+
end
|
16
|
+
@ip = access_config["natIP"]
|
17
|
+
end
|
18
|
+
|
19
|
+
def status
|
20
|
+
@status ||= @desc["status"]
|
21
|
+
end
|
22
|
+
|
23
|
+
def network_interfaces
|
24
|
+
@network_interfaces ||= @desc["networkInterfaces"]
|
25
|
+
end
|
26
|
+
|
27
|
+
def running?
|
28
|
+
status == "RUNNING"
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module DevboxLauncher
|
2
|
+
class Mutagen
|
3
|
+
|
4
|
+
attr_reader :config
|
5
|
+
|
6
|
+
def initialize(config)
|
7
|
+
@config = config
|
8
|
+
end
|
9
|
+
|
10
|
+
def configured?
|
11
|
+
[config, alpha_dir, beta_dir].none?(&:nil?)
|
12
|
+
end
|
13
|
+
|
14
|
+
def alpha_dir
|
15
|
+
config[:alpha]
|
16
|
+
end
|
17
|
+
|
18
|
+
def beta_dir
|
19
|
+
config[:beta]
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: devbox_launcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ramon Tayag
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-02-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -164,7 +164,7 @@ dependencies:
|
|
164
164
|
- - '='
|
165
165
|
- !ruby/object:Gem::Version
|
166
166
|
version: 0.0.2
|
167
|
-
description:
|
167
|
+
description:
|
168
168
|
email:
|
169
169
|
- ramon.tayag@gmail.com
|
170
170
|
executables:
|
@@ -189,7 +189,11 @@ files:
|
|
189
189
|
- bin/setup
|
190
190
|
- devbox_launcher.gemspec
|
191
191
|
- lib/devbox_launcher.rb
|
192
|
+
- lib/devbox_launcher/box.rb
|
192
193
|
- lib/devbox_launcher/cli.rb
|
194
|
+
- lib/devbox_launcher/models/box.rb
|
195
|
+
- lib/devbox_launcher/models/description.rb
|
196
|
+
- lib/devbox_launcher/models/mutagen.rb
|
193
197
|
- lib/devbox_launcher/version.rb
|
194
198
|
- lib/devbox_launcher/watchman.rb
|
195
199
|
homepage: https://github.com/bloom-solutions/devbox_launcher
|
@@ -200,7 +204,7 @@ metadata:
|
|
200
204
|
homepage_uri: https://github.com/bloom-solutions/devbox_launcher
|
201
205
|
source_code_uri: https://github.com/bloom-solutions/devbox_launcher
|
202
206
|
changelog_uri: https://github.com/bloom-solutions/devbox_launcher/blob/master/CHANGELOG.md
|
203
|
-
post_install_message:
|
207
|
+
post_install_message:
|
204
208
|
rdoc_options: []
|
205
209
|
require_paths:
|
206
210
|
- lib
|
@@ -215,8 +219,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
215
219
|
- !ruby/object:Gem::Version
|
216
220
|
version: '0'
|
217
221
|
requirements: []
|
218
|
-
rubygems_version: 3.
|
219
|
-
signing_key:
|
222
|
+
rubygems_version: 3.1.4
|
223
|
+
signing_key:
|
220
224
|
specification_version: 4
|
221
225
|
summary: Conveniently launch your devbox
|
222
226
|
test_files: []
|