From 3e98fc44e50585ab980f129d4533f219a9a940a3 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 31 Jan 2010 18:53:35 -0800 Subject: [PATCH] hobo-ssh --- bin/hobo-ssh | 26 ++++++++++++++++++++++++++ lib/hobo/ssh.rb | 19 ++++++------------- lib/hobo/vm.rb | 6 ++++++ test/hobo/vm_test.rb | 21 +++++++++++++++++++-- 4 files changed, 57 insertions(+), 15 deletions(-) create mode 100755 bin/hobo-ssh diff --git a/bin/hobo-ssh b/bin/hobo-ssh new file mode 100755 index 000000000..831242cee --- /dev/null +++ b/bin/hobo-ssh @@ -0,0 +1,26 @@ +#!/usr/bin/env ruby +begin + require File.expand_path(File.join(File.dirname(__FILE__), "../vendor/gems/ruby/1.8/environment")) +rescue LoadError +end + +require 'git-style-binary/command' + +# Get hobo +hobodir = File.join(File.dirname(__FILE__), '..', 'lib') +$:.unshift(hobodir) unless $:.include?(hobodir) +require 'hobo' + +GitStyleBinary.command do + short_desc "opens an SSH connection into the VM" + banner <<-EOS +Usage: #{command.full_name} #{all_options_string} + +Opens an SSH connection into the created VM. + +EOS + + run do |command| + Hobo::VM.ssh + end +end \ No newline at end of file diff --git a/lib/hobo/ssh.rb b/lib/hobo/ssh.rb index 418f98d6b..c5f35cbd9 100644 --- a/lib/hobo/ssh.rb +++ b/lib/hobo/ssh.rb @@ -1,21 +1,14 @@ module Hobo class SSH SCRIPT = File.join(File.dirname(__FILE__), '..', '..', 'script', 'hobo-ssh-expect.sh') - + def self.connect(opts={}) - Kernel.exec "#{SCRIPT} #{uname(opts)} #{pass(opts)} #{host(opts)} #{port(opts)}".strip - end - - private - module ClassMethods - private - [:port, :host, :pass, :uname].each do |method| - define_method(method) do |opts| - opts[method] || Hobo.config[:ssh][method] - end + options = {} + [:port, :host, :pass, :uname].each do |param| + options[param] = opts[param] || Hobo.config[:ssh][param] end - end - extend ClassMethods + Kernel.exec "#{SCRIPT} #{options[:uname]} #{options[:pass]} #{options[:host]} #{options[:port]}".strip + end end end diff --git a/lib/hobo/vm.rb b/lib/hobo/vm.rb index f0623694f..707bbf825 100644 --- a/lib/hobo/vm.rb +++ b/lib/hobo/vm.rb @@ -15,6 +15,12 @@ module Hobo Env.persisted_vm.destroy end + # SSHs into the VM and replaces the ruby process with the SSH process + def ssh + Env.require_persisted_vm + SSH.connect + end + # Finds a virtual machine by a given UUID and either returns # a Hobo::VM object or returns nil. def find(uuid) diff --git a/test/hobo/vm_test.rb b/test/hobo/vm_test.rb index 4600debfc..42d439856 100644 --- a/test/hobo/vm_test.rb +++ b/test/hobo/vm_test.rb @@ -4,13 +4,30 @@ class VMTest < Test::Unit::TestCase setup do @mock_vm = mock("vm") Hobo.config!(hobo_mock_config) + + @persisted_vm = mock("persisted_vm") + Hobo::Env.stubs(:persisted_vm).returns(@persisted_vm) + end + + context "hobo ssh" do + setup do + Hobo::SSH.stubs(:connect) + end + + should "require a persisted VM" do + Hobo::Env.expects(:require_persisted_vm).once + Hobo::VM.ssh + end + + should "connect to SSH" do + Hobo::SSH.expects(:connect).once + Hobo::VM.ssh + end end context "hobo down" do setup do - @persisted_vm = mock("persisted_vm") @persisted_vm.stubs(:destroy) - Hobo::Env.stubs(:persisted_vm).returns(@persisted_vm) end should "require a persisted VM" do