lib/cuken/api/ssh.rb in cuken-0.1.13 vs lib/cuken/api/ssh.rb in cuken-0.1.15

- old
+ new

@@ -1,20 +1,56 @@ -require 'aruba/api' unless defined? Aruba::Api +#require 'aruba/api' unless defined? Aruba::Api require 'cuken/api/ssh-forever' unless defined? SecureShellForever module Cuken module Api module Ssh def ssh_init_password_less - @ssh_forever_options ||= ssh_forever_default_options - cmd = ssh_forever(@ssh_forever_options) - run_simple(unescape(cmd)) + @ssh_options ||= ssh_default_options + cmd = ssh_cmd(@ssh_options) + announce_or_puts(unescape(cmd)) end def ssh_init_password_less_batch(table=nil) if table == :default + return @ssh_options = ssh_default_options + elsif table.nil? + return @ssh_options ||={} + end + @ssh_options ||={} + table.hashes.each do |hash| + hsh = parse_ssh_options(hash) + cmd = ssh_cmd(hsh) + ssh_run(cmd) + end + end + + def ssh_init_password_less_interactive(table=nil) + if table == :default + return @ssh_options = ssh_default_options + elsif table.nil? + return @ssh_options ||={} + end + @ssh_options ||={} + table.hashes.each do |hash| + hsh = parse_ssh_options(hash) + cmd = ssh_cmd(hsh) + ssh_login_interactive(cmd) + end + end + + def ssh_run(cmd) + run_simple(unescape(cmd)) + end + + def ssh_login_interactive(cmd) + run_interactive(unescape(cmd)) + end + + def ssh_forever_init_password_less_batch(table=nil) + if table == :default return @ssh_forever_options = ssh_forever_default_options elsif table.nil? return @ssh_forever_options ||={} end table.hashes.each do |hash| @@ -57,10 +93,30 @@ def ssh_forever(hsh) "ssh-forever #{hsh[:user]}@#{hsh[:hostname]} -p #{hsh[:port]} -i ~/.ssh/id_rsa_cuken -n #{hsh[:name]} -b" end + def ssh_cmd(hsh) + if hsh[:alias] + #append_ssh_config unless existing_ssh_config? + login_command = "ssh #{hsh[:alias]} #{ssh_args(hsh)}" + else + login_command = "ssh #{hsh[:user]}@#{hsh[:hostname]}#{ssh_args(hsh)}" + end + login_command + end + + def ssh_args(hsh) + [ ' ', + ("-i #{hsh[:identity_file]}" if hsh[:identity_file]), + ("-F #{hsh[:config_file]}" if hsh[:config_file]), + ("-p #{hsh[:port]}" if hsh[:port] =~ /^\d+$/), + (hsh[:stricthostkeychecking] == 'yes' ? "-o stricthostkeychecking=yes" : "-o stricthostkeychecking=no"), + (hsh[:debug] ? "-vvv" : "-q") + ].compact.join(' ') + end + def parse_ssh_forever_options(hsh) new_hsh = hsh.inject({}) { |h, (k, v)| h[k.intern] = v; h } ssh_forever_default_options.merge(new_hsh).inject({}) do |h, (k, v)| v=v.to_s if v[/\:default/] @@ -76,13 +132,38 @@ h[k]=v h end end + def parse_ssh_options(hsh) + new_hsh = hsh.inject({}) { |h, (k, v)| h[k.intern] = v; h } + ssh_default_options.merge(new_hsh).inject({}) do |h, (k, v)| + v=v.to_s + if v[/\:default/] + v = ssh_default_options[k.to_sym] + h[k]=v + next h + end + if v[/`(.*)`/] + cmd=v.gsub(/`/,'') + run_simple(unescape(cmd)) + v = output_from(cmd).strip + end + h[k]=v + h + end + end + def ssh_forever_default_options {:user => ssh_client_username, :hostname => ssh_client_hostname, :port => 22, :name => 'cuken', :identity_file => '~/.ssh/id_rsa_cuken', :auto => true, :intense => true, :quiet => true } + end + + def ssh_default_options + {:user => ssh_client_username, :hostname => ssh_client_hostname, + :port => 22, :identity_file => '~/.ssh/id_rsa', + :quiet => true } end end end end