ssh and ssh expect script added
This commit is contained in:
parent
bb3496c6a6
commit
3fd2ef5e2b
2
Gemfile
2
Gemfile
|
@ -1,5 +1,7 @@
|
|||
# Gems required for the lib to even run
|
||||
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
|
||||
# gem bundle test
|
||||
|
|
|
@ -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
|
|
@ -1 +1,4 @@
|
|||
:setting: 1
|
||||
:ssh:
|
||||
:uname: hobo
|
||||
:pass: hobo
|
||||
:host: localhost
|
|
@ -8,6 +8,8 @@ require 'logger'
|
|||
require 'hobo/config'
|
||||
require 'hobo/env'
|
||||
require 'hobo/virtual_box'
|
||||
require 'hobo/ssh'
|
||||
|
||||
# TODO: Make this configurable
|
||||
HOBO_LOGGER = Logger.new(STDOUT)
|
||||
Hobo::Env.load_config
|
||||
|
|
|
@ -7,23 +7,25 @@ module Hobo
|
|||
:dirs => [HOME] #additional dirs go mhia!
|
||||
}
|
||||
|
||||
def ensure_directories
|
||||
ENSURE[:dirs].each do |name|
|
||||
Dir.mkdir(name) unless File.exists?(name)
|
||||
class << self
|
||||
def ensure_directories
|
||||
ENSURE[:dirs].each do |name|
|
||||
Dir.mkdir(name) unless File.exists?(name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def ensure_files
|
||||
ENSURE[:files].each do |target, default|
|
||||
File.copy(File.join(PROJECT_ROOT, default), target) unless File.exists?(target)
|
||||
def ensure_files
|
||||
ENSURE[:files].each do |target, default|
|
||||
File.copy(File.join(PROJECT_ROOT, default), target) unless File.exists?(target)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def load_config
|
||||
ensure_directories
|
||||
ensure_files
|
||||
parsed = yield(CONFIG.keys.first)
|
||||
Hobo.config!(parsed)
|
||||
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
|
||||
end
|
||||
|
|
|
@ -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
|
|
@ -4,7 +4,7 @@ class EnvTest < Test::Unit::TestCase
|
|||
|
||||
context "Hobo environment handler" do
|
||||
setup do
|
||||
@handler = Hobo::Env.new
|
||||
@handler = Hobo::Env
|
||||
@ensure = Hobo::Env::ENSURE
|
||||
end
|
||||
|
||||
|
@ -42,15 +42,20 @@ class EnvTest < Test::Unit::TestCase
|
|||
@handler.ensure_files
|
||||
end
|
||||
|
||||
test "should load configuration" do
|
||||
test "should load configuration with a block" do
|
||||
@handler.expects(:ensure_directories).once
|
||||
@handler.expects(:ensure_files).once
|
||||
@handler.load_config do |file|
|
||||
assert_equal file, Hobo::Env::CONFIG.keys.first
|
||||
{ :setting => 1 }
|
||||
HOBO_MOCK_CONFIG
|
||||
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
|
||||
|
|
|
@ -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
|
|
@ -21,3 +21,5 @@ rescue LoadError; end
|
|||
require File.join(File.dirname(__FILE__), '..', 'lib', 'hobo')
|
||||
require 'contest'
|
||||
require 'mocha'
|
||||
|
||||
HOBO_MOCK_CONFIG = { :ssh => { :uname => 'foo'} }
|
||||
|
|
Loading…
Reference in New Issue