ssh and ssh expect script added

This commit is contained in:
John Bender 2010-01-26 00:01:17 -08:00
parent bb3496c6a6
commit 3fd2ef5e2b
9 changed files with 102 additions and 19 deletions

View File

@ -1,5 +1,7 @@
# Gems required for the lib to even run # Gems required for the lib to even run
gem "net-ssh", ">= 2.0.19" gem "net-ssh", ">= 2.0.19"
gem "jashmenn-git-style-binaries", ">= 0.1.10"
source "http://gems.github.com"
# Gems required for testing only. To install run # Gems required for testing only. To install run
# gem bundle test # gem bundle test

16
bin/hobo-ssh-expect.sh Executable file
View File

@ -0,0 +1,16 @@
#!/usr/bin/expect
set uname [lrange $argv 0 0]
set password [lrange $argv 1 1]
set host [lrange $argv 2 2]
spawn ssh $uname@$host
expect "*password: " {
sleep 1
send "$password\r"
} timeout {
send_user "Error connecting"
}
interact

View File

@ -1 +1,4 @@
:setting: 1 :ssh:
:uname: hobo
:pass: hobo
:host: localhost

View File

@ -8,6 +8,8 @@ require 'logger'
require 'hobo/config' require 'hobo/config'
require 'hobo/env' require 'hobo/env'
require 'hobo/virtual_box' require 'hobo/virtual_box'
require 'hobo/ssh'
# TODO: Make this configurable # TODO: Make this configurable
HOBO_LOGGER = Logger.new(STDOUT) HOBO_LOGGER = Logger.new(STDOUT)
Hobo::Env.load_config

View File

@ -7,23 +7,25 @@ module Hobo
:dirs => [HOME] #additional dirs go mhia! :dirs => [HOME] #additional dirs go mhia!
} }
def ensure_directories class << self
ENSURE[:dirs].each do |name| def ensure_directories
Dir.mkdir(name) unless File.exists?(name) ENSURE[:dirs].each do |name|
Dir.mkdir(name) unless File.exists?(name)
end
end end
end
def ensure_files def ensure_files
ENSURE[:files].each do |target, default| ENSURE[:files].each do |target, default|
File.copy(File.join(PROJECT_ROOT, default), target) unless File.exists?(target) File.copy(File.join(PROJECT_ROOT, default), target) unless File.exists?(target)
end
end
def load_config
ensure_directories
ensure_files
parsed = block_given? ? yield(CONFIG.keys.first) : YAML.load_file(CONFIG.keys.first)
Hobo.config!(parsed)
end end
end
def load_config
ensure_directories
ensure_files
parsed = yield(CONFIG.keys.first)
Hobo.config!(parsed)
end end
end end
end end

28
lib/hobo/ssh.rb Normal file
View File

@ -0,0 +1,28 @@
module Hobo
class SSH
SCRIPT = File.join(File.dirname(__FILE__), '..', '..', 'bin', 'hobo-ssh-expect.sh')
class << self
def connect(opts={})
Kernel.exec "#{SCRIPT} #{opts[:uname] || uname_default} #{opts[:pass] || pass_default} #{opts[:host] || host_default}".strip
end
private
def port_default
Hobo.config[:ssh][:port]
end
def host_default
Hobo.config[:ssh][:host]
end
def pass_default
Hobo.config[:ssh][:pass]
end
def uname_default
Hobo.config[:ssh][:uname]
end
end
end
end

View File

@ -4,7 +4,7 @@ class EnvTest < Test::Unit::TestCase
context "Hobo environment handler" do context "Hobo environment handler" do
setup do setup do
@handler = Hobo::Env.new @handler = Hobo::Env
@ensure = Hobo::Env::ENSURE @ensure = Hobo::Env::ENSURE
end end
@ -42,15 +42,20 @@ class EnvTest < Test::Unit::TestCase
@handler.ensure_files @handler.ensure_files
end end
test "should load configuration" do test "should load configuration with a block" do
@handler.expects(:ensure_directories).once @handler.expects(:ensure_directories).once
@handler.expects(:ensure_files).once @handler.expects(:ensure_files).once
@handler.load_config do |file| @handler.load_config do |file|
assert_equal file, Hobo::Env::CONFIG.keys.first assert_equal file, Hobo::Env::CONFIG.keys.first
{ :setting => 1 } HOBO_MOCK_CONFIG
end end
assert_equal Hobo.config[:ssh], HOBO_MOCK_CONFIG[:ssh]
end
assert_equal Hobo.config[:setting], 1 test "should default to haml load of the default, without a block" do
YAML.expects(:load_file).with(Hobo::Env::CONFIG.keys.first).returns(HOBO_MOCK_CONFIG)
@handler.load_config
assert_equal Hobo.config[:ssh], HOBO_MOCK_CONFIG[:ssh]
end end
end end
end end

23
test/hobo/ssh_test.rb Normal file
View File

@ -0,0 +1,23 @@
require File.join(File.dirname(__FILE__), '..', 'test_helper')
class SshTest < Test::Unit::TestCase
context "Hobo ssh" do
setup do
@handler = Hobo::SSH
@script = Hobo::SSH::SCRIPT
end
test "should call exec with defaults when no options are supplied" do
# NOTE HOBO_MOCK_CONFIG only contains the :uname at this stage, adding further params will break this test
Kernel.expects(:exec).with("#{@script} #{HOBO_MOCK_CONFIG[:ssh][:uname]}")
Hobo::SSH.connect
end
test "should call exec with supplied params" do
args = {:uname => 'bar', :pass => 'baz', :host => 'bak'}
Kernel.expects(:exec).with("#{@script} #{args[:uname]} #{args[:pass]} #{args[:host]}")
Hobo::SSH.connect(args)
end
end
end

View File

@ -21,3 +21,5 @@ rescue LoadError; end
require File.join(File.dirname(__FILE__), '..', 'lib', 'hobo') require File.join(File.dirname(__FILE__), '..', 'lib', 'hobo')
require 'contest' require 'contest'
require 'mocha' require 'mocha'
HOBO_MOCK_CONFIG = { :ssh => { :uname => 'foo'} }