diff --git a/Gemfile b/Gemfile index dcf3767ce..3c9d250c4 100644 --- a/Gemfile +++ b/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 diff --git a/bin/hobo-ssh-expect.sh b/bin/hobo-ssh-expect.sh new file mode 100755 index 000000000..9efaaf411 --- /dev/null +++ b/bin/hobo-ssh-expect.sh @@ -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 diff --git a/config/default.yml b/config/default.yml index eab6ccda1..cec31f04c 100644 --- a/config/default.yml +++ b/config/default.yml @@ -1 +1,4 @@ -:setting: 1 \ No newline at end of file +:ssh: + :uname: hobo + :pass: hobo + :host: localhost \ No newline at end of file diff --git a/lib/hobo.rb b/lib/hobo.rb index 83a98abd0..7f0921558 100644 --- a/lib/hobo.rb +++ b/lib/hobo.rb @@ -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 diff --git a/lib/hobo/env.rb b/lib/hobo/env.rb index 7489c95ce..94d156fda 100644 --- a/lib/hobo/env.rb +++ b/lib/hobo/env.rb @@ -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 + + 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 - - def load_config - ensure_directories - ensure_files - parsed = yield(CONFIG.keys.first) - Hobo.config!(parsed) end end end diff --git a/lib/hobo/ssh.rb b/lib/hobo/ssh.rb new file mode 100644 index 000000000..25618510c --- /dev/null +++ b/lib/hobo/ssh.rb @@ -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 diff --git a/test/hobo/env_test.rb b/test/hobo/env_test.rb index 823640c54..4bb2f915f 100644 --- a/test/hobo/env_test.rb +++ b/test/hobo/env_test.rb @@ -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 diff --git a/test/hobo/ssh_test.rb b/test/hobo/ssh_test.rb new file mode 100644 index 000000000..2a7e06c0d --- /dev/null +++ b/test/hobo/ssh_test.rb @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index 64d6c7c07..a273ba404 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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'} }