lib/infrataster/server.rb in infrataster-0.1.0 vs lib/infrataster/server.rb in infrataster-0.1.1

- old
+ new

@@ -6,24 +6,32 @@ module Infrataster class Server Error = Class.new(StandardError) class << self - @@servers = [] def define(*args) @@servers << Server.new(*args) end + def defined_servers + @@servers + end + + def clear_defined_servers + @@servers = [] + end + def find_by_name(name) server = @@servers.find {|s| s.name == name } unless server raise Error, "Server definition for '#{name}' is not found." end server end + Server.clear_defined_servers end attr_reader :name, :address, :options def initialize(name, address, options = {}) @@ -68,28 +76,28 @@ end [port, finalize_proc] end end + def from_gateway_open(port) + if from + new_port, finalize_proc = from.gateway_open(@address, port) + Logger.debug("tunnel: localhost:#{new_port} -> #{from.address} -> #{@address}:#{port}") + ['127.0.0.1', new_port, finalize_proc] + else + [@address, port, nil] + end + end + def from_gateway(port) if from - if block_given? - from.gateway_open(@address, port) do |new_port| - Logger.debug("tunnel: localhost:#{new_port} -> #{from.address} -> #{@address}:#{port}") - yield '127.0.0.1', new_port - end - else - new_port, finalize_proc = from.gateway_open(@address, port) + from.gateway_open(@address, port) do |new_port| Logger.debug("tunnel: localhost:#{new_port} -> #{from.address} -> #{@address}:#{port}") - ['127.0.0.1', new_port, finalize_proc] + yield '127.0.0.1', new_port end else - if block_given? - yield @address, port - else - [@address, port, nil] - end + yield @address, port end end def ssh_start_args @ssh_start_args ||= _ssh_start_args @@ -109,10 +117,13 @@ vagrant_name = @name end Dir.mktmpdir do |dir| output = File.join(dir, 'ssh-config') - system("/usr/bin/vagrant ssh-config #{vagrant_name} > #{output}") + `/usr/bin/vagrant ssh-config #{vagrant_name} > #{output}` + if $?.exitstatus != 0 + raise Error, "`vagrant ssh-config` failed. Please check if VMs are running or not." + end config = Net::SSH::Config.for(@name.to_s, [output]) end else raise Error, "Can't get configuration to connect to the server via SSH. Please set ssh option or vagrant option." end