Implement `forward_port` method for VMs in configuration
This commit is contained in:
parent
b2b2cfd68b
commit
7971d656fe
|
@ -3,11 +3,12 @@ Hobo::Config.run do |config|
|
|||
config.ssh.uname = "hobo"
|
||||
config.ssh.pass = "hobo"
|
||||
config.ssh.host = "localhost"
|
||||
config.ssh.port = 2222
|
||||
config.ssh.forwarded_port_key = "ssh"
|
||||
config.ssh.max_tries = 10
|
||||
|
||||
config.dotfile_name = ".hobo"
|
||||
|
||||
config.vm.base = "~/.hobo/base/base.ovf"
|
||||
config.vm.base_mac = "0800279C2E41"
|
||||
config.vm.forward_port("ssh", 22, 2222)
|
||||
end
|
|
@ -41,13 +41,26 @@ module Hobo
|
|||
attr_accessor :uname
|
||||
attr_accessor :pass
|
||||
attr_accessor :host
|
||||
attr_accessor :port
|
||||
attr_accessor :forwarded_port_key
|
||||
attr_accessor :max_tries
|
||||
end
|
||||
|
||||
class VMConfig < Base
|
||||
attr_accessor :base
|
||||
attr_accessor :base_mac
|
||||
attr_reader :forwarded_ports
|
||||
|
||||
def initialize
|
||||
@forwarded_ports = {}
|
||||
end
|
||||
|
||||
def forward_port(name, guestport, hostport, protocol="TCP")
|
||||
forwarded_ports[name] = {
|
||||
:guestport => guestport,
|
||||
:hostport => hostport,
|
||||
:protocol => protocol
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
class Top < Base
|
||||
|
|
|
@ -5,21 +5,26 @@ module Hobo
|
|||
class << self
|
||||
def connect(opts={})
|
||||
options = {}
|
||||
[:port, :host, :pass, :uname].each do |param|
|
||||
[:host, :pass, :uname].each do |param|
|
||||
options[param] = opts[param] || Hobo.config.ssh.send(param)
|
||||
end
|
||||
|
||||
# The port is special
|
||||
options[:port] = opts[:port] || Hobo.config.vm.forwarded_ports[Hobo.config.ssh.forwarded_port_key][:hostport]
|
||||
|
||||
Kernel.exec "#{SCRIPT} #{options[:uname]} #{options[:pass]} #{options[:host]} #{options[:port]}".strip
|
||||
end
|
||||
|
||||
def execute
|
||||
Net::SSH.start("localhost", Hobo.config[:ssh][:uname], :port => Hobo.config[:ssh][:port], :password => Hobo.config[:ssh][:pass]) do |ssh|
|
||||
port = Hobo.config.vm.forwarded_ports[Hobo.config.ssh.forwarded_port_key][:hostport]
|
||||
Net::SSH.start("localhost", Hobo.config[:ssh][:uname], :port => port, :password => Hobo.config[:ssh][:pass]) do |ssh|
|
||||
yield ssh
|
||||
end
|
||||
end
|
||||
|
||||
def up?
|
||||
Ping.pingecho "localhost", 1, Hobo.config[:ssh][:port]
|
||||
port = Hobo.config.vm.forwarded_ports[Hobo.config.ssh.forwarded_port_key][:hostport]
|
||||
Ping.pingecho "localhost", 1, port
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -23,7 +23,7 @@ module Hobo
|
|||
SSH.connect
|
||||
end
|
||||
|
||||
|
||||
|
||||
# Save the state of the current hobo environment to disk
|
||||
def suspend
|
||||
Env.require_persisted_vm
|
||||
|
@ -61,7 +61,7 @@ error
|
|||
import
|
||||
persist
|
||||
setup_mac_address
|
||||
forward_ssh
|
||||
forward_ports
|
||||
setup_shared_folder
|
||||
start
|
||||
end
|
||||
|
@ -92,13 +92,18 @@ error
|
|||
@vm.save(true)
|
||||
end
|
||||
|
||||
def forward_ssh
|
||||
HOBO_LOGGER.info "Forwarding SSH ports..."
|
||||
port = VirtualBox::ForwardedPort.new
|
||||
port.name = "ssh"
|
||||
port.hostport = Hobo.config[:ssh][:port]
|
||||
port.guestport = 22
|
||||
@vm.forwarded_ports << port
|
||||
def forward_ports
|
||||
HOBO_LOGGER.info "Forwarding ports..."
|
||||
|
||||
Hobo.config.vm.forwarded_ports.each do |name, options|
|
||||
HOBO_LOGGER.info "Forwarding \"#{name}\": #{options[:guestport]} => #{options[:hostport]}"
|
||||
port = VirtualBox::ForwardedPort.new
|
||||
port.name = name
|
||||
port.hostport = options[:hostport]
|
||||
port.guestport = options[:guestport]
|
||||
@vm.forwarded_ports << port
|
||||
end
|
||||
|
||||
@vm.save(true)
|
||||
end
|
||||
|
||||
|
@ -117,11 +122,11 @@ error
|
|||
|
||||
# Now we have to wait for the boot to be successful
|
||||
HOBO_LOGGER.info "Waiting for VM to boot..."
|
||||
|
||||
|
||||
Hobo.config[:ssh][:max_tries].to_i.times do |i|
|
||||
sleep 5 unless ENV['HOBO_ENV'] == 'test'
|
||||
HOBO_LOGGER.info "Trying to connect (attempt ##{i+1} of #{Hobo.config[:ssh][:max_tries]})..."
|
||||
|
||||
|
||||
if Hobo::SSH.up?
|
||||
HOBO_LOGGER.info "VM booted and ready for use!"
|
||||
return true
|
||||
|
@ -133,7 +138,7 @@ error
|
|||
end
|
||||
|
||||
def saved?; @vm.saved? end
|
||||
|
||||
|
||||
def save_state(errs); @vm.save_state(errs) end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,15 +1,19 @@
|
|||
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
||||
|
||||
class SshTest < Test::Unit::TestCase
|
||||
setup do
|
||||
hobo_mock_config
|
||||
end
|
||||
|
||||
context "hobo ssh" do
|
||||
setup do
|
||||
@script = Hobo::SSH::SCRIPT
|
||||
hobo_mock_config
|
||||
end
|
||||
|
||||
test "should call exec with defaults when no options are supplied" do
|
||||
ssh = Hobo.config.ssh
|
||||
Kernel.expects(:exec).with("#{@script} #{ssh[:uname]} #{ssh[:pass]} #{ssh[:host]} #{ssh[:port]}")
|
||||
port = Hobo.config.vm.forwarded_ports[ssh.forwarded_port_key][:hostport]
|
||||
Kernel.expects(:exec).with("#{@script} #{ssh[:uname]} #{ssh[:pass]} #{ssh[:host]} #{port}")
|
||||
Hobo::SSH.connect
|
||||
end
|
||||
|
||||
|
@ -26,4 +30,12 @@ class SshTest < Test::Unit::TestCase
|
|||
Hobo::SSH.execute
|
||||
end
|
||||
end
|
||||
|
||||
context "checking if host is up" do
|
||||
should "pingecho the server" do
|
||||
port = Hobo.config.vm.forwarded_ports[Hobo.config.ssh.forwarded_port_key][:hostport]
|
||||
Ping.expects(:pingecho).with("localhost", 1, port).once
|
||||
Hobo::SSH.up?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -77,7 +77,7 @@ class VMTest < Test::Unit::TestCase
|
|||
@vm.expects(:import).in_sequence(create_seq)
|
||||
@vm.expects(:persist).in_sequence(create_seq)
|
||||
@vm.expects(:setup_mac_address).in_sequence(create_seq)
|
||||
@vm.expects(:forward_ssh).in_sequence(create_seq)
|
||||
@vm.expects(:forward_ports).in_sequence(create_seq)
|
||||
@vm.expects(:setup_shared_folder).in_sequence(create_seq)
|
||||
@vm.expects(:start).in_sequence(create_seq)
|
||||
@vm.create
|
||||
|
@ -157,7 +157,7 @@ class VMTest < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
context "forwarding SSH" do
|
||||
context "forwarding ports" do
|
||||
should "create a port forwarding for the VM" do
|
||||
# TODO: Test the actual port value to make sure it has the
|
||||
# correct attributes
|
||||
|
@ -165,7 +165,7 @@ class VMTest < Test::Unit::TestCase
|
|||
forwarded_ports.expects(:<<)
|
||||
@mock_vm.expects(:forwarded_ports).returns(forwarded_ports)
|
||||
@mock_vm.expects(:save).with(true).once
|
||||
@vm.forward_ssh
|
||||
@vm.forward_ports
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -184,7 +184,7 @@ class VMTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
context "suspending and resuming a vm" do
|
||||
should "put the vm in a suspended state" do
|
||||
should "put the vm in a suspended state" do
|
||||
saved_state_expectation(false)
|
||||
save_expectation
|
||||
Hobo::VM.suspend
|
||||
|
@ -197,7 +197,7 @@ class VMTest < Test::Unit::TestCase
|
|||
Hobo::VM.suspend
|
||||
end
|
||||
|
||||
should "start a vm in a suspended state" do
|
||||
should "start a vm in a suspended state" do
|
||||
saved_state_expectation(true)
|
||||
start_expectation
|
||||
Hobo::VM.resume
|
||||
|
@ -209,12 +209,12 @@ class VMTest < Test::Unit::TestCase
|
|||
# TODO research the matter of mocking exit
|
||||
Hobo::VM.expects(:error_and_exit)
|
||||
Hobo::VM.resume
|
||||
end
|
||||
end
|
||||
|
||||
def saved_state_expectation(saved)
|
||||
@persisted_vm.expects(:saved?).returns(saved)
|
||||
end
|
||||
|
||||
|
||||
def save_expectation
|
||||
@persisted_vm.expects(:save_state).with(true)
|
||||
end
|
||||
|
|
|
@ -37,11 +37,12 @@ class Test::Unit::TestCase
|
|||
config.ssh.uname = "foo"
|
||||
config.ssh.pass = "bar"
|
||||
config.ssh.host = "baz"
|
||||
config.ssh.port = "bak"
|
||||
config.ssh.forwarded_port_key = "ssh"
|
||||
config.ssh.max_tries = 10
|
||||
|
||||
config.vm.base = "foo"
|
||||
config.vm.base_mac = "42"
|
||||
config.vm.forward_port("ssh", 22, 2222)
|
||||
end
|
||||
|
||||
Hobo::Config.execute!
|
||||
|
|
Loading…
Reference in New Issue