From 620be3013685d14d306a60cd79a9a785fcb21150 Mon Sep 17 00:00:00 2001 From: Gaetan SIMO <gaetan.simo@inria.fr> Date: Wed, 8 Jun 2011 14:50:27 +0200 Subject: [PATCH] [lib] Enabling dns resolution over ssh. --- Gemfile | 1 + Gemfile.lock | 2 ++ generators/lib/g5k_generator.rb | 42 +++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/Gemfile b/Gemfile index b80e2bb041..778247058e 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 817082ac31..50f1ecdcdd 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 835b9f0220..0dc161aedc 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: -- GitLab