This repository has been archived by the owner on Mar 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
box.rb
283 lines (235 loc) · 6.26 KB
/
box.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
require 'rubygems'
require 'yaml'
require 'blimpy/helpers/state'
require 'blimpy/livery'
require 'blimpy/keys'
require 'blimpy/securitygroups'
require 'blimpy/boxes'
module Blimpy
class Box
include Blimpy::Helpers::State
attr_reader :allowed_regions, :region
attr_accessor :image_id, :flavor, :group, :ports
attr_accessor :dns, :internal_dns
attr_accessor :name, :tags, :fleet_id, :username, :ssh_port, :livery
attr_accessor :provision_on_start
def self.from_instance_id(an_id, data)
return if data[:type].nil?
name = data[:type].to_sym
return unless Blimpy::Boxes.const_defined? name
klass = Blimpy::Boxes.const_get(name)
server = klass.fog_server_for_instance(an_id, data)
return if server.nil?
box = klass.new(server)
box.with_data(an_id, data)
box
end
def initialize(server=nil)
@provision_on_start = true
@livery = nil
@group = nil
@name = 'Unnamed Box'
@tags = {}
@ports = []
@server = server
@fleet_id = 0
@ssh_connected = false
@exec_commands = true
end
def region=(newRegion)
unless (@allowed_regions.nil?) || (@allowed_regions.include?(newRegion))
raise InvalidRegionError
end
@region = newRegion
end
def online!
write_state_file
end
def validate!
raise NotImplementedError, '#validate! should be defined in a subclass of Blimpy::Box'
end
def prestart
end
def start
ensure_state_folder
prestart
@server = create_host
poststart
write_state_file
end
def poststart
end
def bootstrap
@exec_commands = false
if @livery.nil?
return
end
if @livery.respond_to? :new
@livery = @livery.new
end
wait_for_sshd
bootstrap_livery
end
# This is just here to make things more consistent from an API perspective
def provision
bootstrap
end
def stop
unless @server.nil?
@server.stop
end
end
def resume
unless @server.nil?
@server.start
end
end
def predestroy
end
def destroy
unless @server.nil?
predestroy
@server.destroy
postdestroy
File.unlink(state_file)
end
end
def postdestroy
end
def type
# We only really care about the class name as part of the Blimpy::Boxes
# module
self.class.to_s.split('::').last
end
def serializable_attributes
[:type, :name, :region, :dns, :internal_dns, :flavor, :tags]
end
def immutable_attributes
[:type]
end
def write_state_file
data = {}
serializable_attributes.each do |attr|
data[attr] = self.send(attr)
end
File.open(state_file, 'w') do |f|
f.write(data.to_yaml)
end
end
def state_file
if @server.nil?
raise Exception, "I can't make a state file without a @server!"
end
File.join(state_folder, "#{@server.id}.blimp")
end
def wait_for_state(until_state, &block)
if @server.nil?
return
end
@server.wait_for do
block.call
state == until_state
end
end
def with_data(ship_id, data)
data.each do |key, value|
next if immutable_attributes.include? key.to_sym
self.send("#{key}=", value)
end
end
def dns
@dns ||= begin
if @server.nil?
'no name'
else
@server.dns_name
end
end
end
def internal_dns
@internal_dns ||= begin
if @server.nil?
'no name'
else
@server.private_dns_name
end
end
end
def run_command(*args)
if @exec_commands
::Kernel.exec(*args)
else
::Kernel.system(*args)
end
end
def ssh_commands(*args)
['ssh', '-o', 'PasswordAuthentication=no',
'-o', 'StrictHostKeyChecking=no',
'-p', (ssh_port||22).to_s,
'-l', username, dns, *args]
end
def ssh_into(*args)
run_command(*ssh_commands(*args))
end
def scp_file(filename, directory='', *args)
filename = File.expand_path(filename)
run_command('scp', '-o', 'StrictHostKeyChecking=no',
filename, "#{username}@#{dns}:#{directory}", *args)
end
def scp_files(directory, files)
filename = File.expand_path(filename)
run_command(*['scp', '-o', 'StrictHostKeyChecking=no']+files+["#{username}@#{dns}:#{directory}"])
end
def bootstrap_livery
if @livery.kind_of? Symbol
raise Blimpy::InvalidLiveryException, 'Symbol liveries are unsupported!'
end
@livery.setup_on(self)
@livery.preflight(self)
@livery.flight(self)
@livery.postflight(self)
end
def wait_for_sshd
return if @ssh_connected
start = Time.now.to_i
use_exec = @exec_commands
# Even if we are supposed to use #exec here, we wait to disable it until
# after sshd(8) comes online
@exec_commands = false
$stdout.sync = true
need_nl = false
until @ssh_connected
# Run the `true` command and exit
@ssh_connected = ssh_into('-q', 'true')
# if SSH is killed, don't repeat
if $?.signaled?
if $?.termsig==2
# if Ctrl+C, report what we were doing
puts "Failed to connect. To try it yourself:\n#{ssh_commands('-v','true').join(' ')}"
end
raise Exception, "ssh was killed: #{$?}"
end
unless @ssh_connected
if !need_nl
p = ssh_port.nil? ? "" : ":#{ssh_port}"
print ">> Connecting #{username}@#{name}#{p}"
end
if (Time.now.to_i - start) < 60
print '.'
need_nl = true
sleep 1
end
end
end
puts if need_nl
@exec_commands = use_exec
end
def fog
raise NotImplementedError, '#fog should be implemented by cloud-specific subclasses'
end
private
def create_host
raise NotImplementedError, '#create_host should be implemented by a cloud-specific subclass'
end
end
end