class Blimpy::Box
Attributes
allowed_regions[R]
dns[RW]
flavor[RW]
fleet_id[RW]
group[RW]
image_id[RW]
internal_dns[RW]
livery[RW]
name[RW]
ports[RW]
provision_on_start[RW]
region[R]
ssh_port[RW]
username[RW]
Public Class Methods
from_instance_id(an_id, data)
click to toggle source
# File lib/blimpy/box.rb, line 19 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
new(server=nil)
click to toggle source
# File lib/blimpy/box.rb, line 35 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
Public Instance Methods
bootstrap()
click to toggle source
# File lib/blimpy/box.rb, line 77 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
bootstrap_livery()
click to toggle source
# File lib/blimpy/box.rb, line 222 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
destroy()
click to toggle source
# File lib/blimpy/box.rb, line 111 def destroy unless @server.nil? predestroy @server.destroy postdestroy File.unlink(state_file) end end
fog()
click to toggle source
# File lib/blimpy/box.rb, line 272 def fog raise NotImplementedError, '#fog should be implemented by cloud-specific subclasses' end
immutable_attributes()
click to toggle source
# File lib/blimpy/box.rb, line 133 def immutable_attributes [:type] end
online!()
click to toggle source
# File lib/blimpy/box.rb, line 55 def online! write_state_file end
postdestroy()
click to toggle source
# File lib/blimpy/box.rb, line 120 def postdestroy end
poststart()
click to toggle source
# File lib/blimpy/box.rb, line 74 def poststart end
predestroy()
click to toggle source
# File lib/blimpy/box.rb, line 108 def predestroy end
prestart()
click to toggle source
# File lib/blimpy/box.rb, line 63 def prestart end
provision()
click to toggle source
This is just here to make things more consistent from an API perspective
# File lib/blimpy/box.rb, line 92 def provision bootstrap end
region=(newRegion)
click to toggle source
# File lib/blimpy/box.rb, line 48 def region=(newRegion) unless (@allowed_regions.nil?) || (@allowed_regions.include?(newRegion)) raise InvalidRegionError end @region = newRegion end
resume()
click to toggle source
# File lib/blimpy/box.rb, line 102 def resume unless @server.nil? @server.start end end
run_command(*args)
click to toggle source
# File lib/blimpy/box.rb, line 192 def run_command(*args) if @exec_commands ::Kernel.exec(*args) else ::Kernel.system(*args) end end
scp_file(filename, directory='', *args)
click to toggle source
# File lib/blimpy/box.rb, line 211 def scp_file(filename, directory='', *args) filename = File.expand_path(filename) run_command('scp', '-o', 'StrictHostKeyChecking=no', filename, "#{username}@#{dns}:#{directory}", *args) end
scp_files(directory, files)
click to toggle source
# File lib/blimpy/box.rb, line 217 def scp_files(directory, files) filename = File.expand_path(filename) run_command(*['scp', '-o', 'StrictHostKeyChecking=no']+files+["#{username}@#{dns}:#{directory}"]) end
serializable_attributes()
click to toggle source
# File lib/blimpy/box.rb, line 129 def serializable_attributes [:type, :name, :region, :dns, :internal_dns, :flavor, :tags] end
ssh_commands(*args)
click to toggle source
# File lib/blimpy/box.rb, line 200 def ssh_commands(*args) ['ssh', '-o', 'PasswordAuthentication=no', '-o', 'StrictHostKeyChecking=no', '-p', (ssh_port||22).to_s, '-l', username, dns, *args] end
ssh_into(*args)
click to toggle source
# File lib/blimpy/box.rb, line 207 def ssh_into(*args) run_command(*ssh_commands(*args)) end
start()
click to toggle source
# File lib/blimpy/box.rb, line 66 def start ensure_state_folder prestart @server = create_host poststart write_state_file end
state_file()
click to toggle source
# File lib/blimpy/box.rb, line 147 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
stop()
click to toggle source
# File lib/blimpy/box.rb, line 96 def stop unless @server.nil? @server.stop end end
type()
click to toggle source
# File lib/blimpy/box.rb, line 123 def type # We only really care about the class name as part of the Blimpy::Boxes # module self.class.to_s.split('::').last end
validate!()
click to toggle source
# File lib/blimpy/box.rb, line 59 def validate! raise NotImplementedError, '#validate! should be defined in a subclass of Blimpy::Box' end
wait_for_sshd()
click to toggle source
# File lib/blimpy/box.rb, line 233 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
wait_for_state(until_state, &block)
click to toggle source
# File lib/blimpy/box.rb, line 154 def wait_for_state(until_state, &block) if @server.nil? return end @server.wait_for do block.call state == until_state end end
with_data(ship_id, data)
click to toggle source
# File lib/blimpy/box.rb, line 165 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
write_state_file()
click to toggle source
# File lib/blimpy/box.rb, line 137 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
Private Instance Methods
create_host()
click to toggle source
# File lib/blimpy/box.rb, line 279 def create_host raise NotImplementedError, '#create_host should be implemented by a cloud-specific subclass' end