Skip to content
This repository has been archived by the owner on Mar 19, 2022. It is now read-only.

Default to lsb_release for detecting the Linux distro #242

Merged
merged 3 commits into from
May 9, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/knife-solo/bootstraps/linux.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
module KnifeSolo::Bootstraps
class Linux < Base

def issue
prepare.run_command("cat /etc/issue").stdout.strip || prepare.run_command("lsb_release -d -s").stdout.strip
commands = [
'lsb_release -d -s',
'cat /etc/redhat-release',
'cat /etc/issue'
]
result = prepare.run_with_fallbacks(commands)
result.success? ? result.stdout.strip : nil
end

def x86?
Expand Down Expand Up @@ -83,7 +88,7 @@ def distro
when %r{This is \\n\.\\O \(\\s \\m \\r\) \\t}
{:type => "emerge_gem"}
else
raise "Distro not recognized from looking at /etc/issue. Please fork https://github.com/matschaffer/knife-solo and add support for your distro."
raise "Distribution not recognized. Please run again with `-VV` option and file an issue: https://github.com/matschaffer/knife-solo/issues"
end
Chef::Log.debug("Distro detection yielded: #{@distro}")
@distro
Expand Down
16 changes: 14 additions & 2 deletions lib/knife-solo/ssh_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ def startup_script
class ExecResult
attr_accessor :stdout, :stderr, :exit_code

def initialize
def initialize(exit_code = nil)
@exit_code = exit_code
@stdout = ""
@stderr = ""
end
Expand Down Expand Up @@ -208,7 +209,7 @@ def processed_command(command, options = {})
command
end

def run_command(command, options={})
def run_command(command, options = {})
defaults = {:process_sudo => true}
options = defaults.merge(options)

Expand Down Expand Up @@ -254,6 +255,17 @@ def run_command(command, options={})
result
end

# Runs commands from the specified array until successful.
# Returns the result of the successful command or an ExecResult with
# exit_code 1 if all fail.
def run_with_fallbacks(commands, options = {})
commands.each do |command|
result = run_command(command, options)
return result if result.success?
end
ExecResult.new(1)
end

# TODO:
# - move this to a dedicated "portability" module?
# - use ruby in all cases instead?
Expand Down
27 changes: 27 additions & 0 deletions test/ssh_command_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,33 @@ def test_barks_without_atleast_a_hostname
assert_exits { cmd.validate_ssh_options! }
end

def test_run_with_fallbacks_returns_first_successful_result
cmds = sequence("cmds")
cmd = command
cmd.expects(:run_command).with("first", {}).returns(result(1, "fail")).in_sequence(cmds)
cmd.expects(:run_command).with("second", {}).returns(result(0, "w00t")).in_sequence(cmds)
cmd.expects(:run_command).never

res = cmd.run_with_fallbacks(["first", "second", "third"])
assert_equal "w00t", res.stdout
assert res.success?
end

def test_run_with_fallbacks_returns_error_if_all_fail
cmd = command
cmd.expects(:run_command).twice.returns(result(64, "fail"))

res = cmd.run_with_fallbacks(["foo", "bar"])
assert_equal "", res.stdout
assert_equal 1, res.exit_code
end

def result(code, stdout = "")
res = KnifeSolo::SshCommand::ExecResult.new(code)
res.stdout = stdout
res
end

def command(*args)
Net::SSH::Config.stubs(:default_files)
knife_command(DummySshCommand, *args)
Expand Down