diff --git a/Gemfile b/Gemfile index b80e2bb0415643de56a5ee56e2f068cee4e9fdc9..778247058e8337ae0858c91798bb8e286bf4e728 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,4 @@ source :rubygems gem 'json_pure', '~> 1.5' +gem 'net-ssh' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 817082ac31c0080750e3e3d3b34fa5ee1d499fd2..50f1ecdcddc992d0c32339703a17fb5836bc37ea 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,9 +2,11 @@ GEM remote: http://rubygems.org/ specs: json_pure (1.5.1) + net-ssh (2.1.0) PLATFORMS ruby DEPENDENCIES json_pure (~> 1.5) + net-ssh diff --git a/generators/lib/g5k_generator.rb b/generators/lib/g5k_generator.rb index 835b9f0220943d37ed10529575183c60fa095b1f..0dc161aedc774f50a1ff33420ca583e9ac8a1f88 100755 --- a/generators/lib/g5k_generator.rb +++ b/generators/lib/g5k_generator.rb @@ -63,7 +63,49 @@ module G5K def dns_lookup(network_address) Resolv.getaddress(network_address) end + + # Remotly execute commands, and retrieve stdout, stderr and exit code. + def ssh_exec!(ssh, command) + stdout_data = "" + stderr_data = "" + exit_code = nil + exit_signal = nil + ssh.open_channel do |channel| + channel.exec(command) do |ch, success| + unless success + abort "FAILED: couldn't execute command (ssh.channel.exec)" + end + channel.on_data do |ch,data| + stdout_data+=data + end + + channel.on_extended_data do |ch,type,data| + stderr_data+=data + end + + channel.on_request("exit-status") do |ch,data| + exit_code = data.read_long + end + + channel.on_request("exit-signal") do |ch, data| + exit_signal = data.read_long + end + end + end + ssh.loop + [stdout_data, stderr_data, exit_code, exit_signal] + end + # Get the IP address corresponding to the host fqdn throught ssh channel + def dns_lookup_through_ssh(ssh,fqdn) + results = ssh_exec! ssh, "host #{fqdn}" + if results[2] == 0 + results[0].split(" ").reverse[0] + else + fail "Failed to get ip address of '#{fqdn}' : #{results[1]}" + end + end + # Lookup a key in one of the configuration files passed to the generator # # Usage: