class DRbQS::Manage::SSHShell
Requirements:
-
bash
Constants
- DEFAULT_SHELL
Attributes
Public Class Methods
@param [String] dest Destination of SSH. @param [Hash] opts The options of SSH shell. @option opts [String] :shell The shell to use. @option opts [String] :keys Path of a ssh key. @option opts [IO] :io IO to output results of commands. @option opts [String] :directory Same as options DRbQS::Manage::SSHShell::RubyEnvironment
. @option opts [String] :rvm Same as options DRbQS::Manage::SSHShell::RubyEnvironment
. @option opts [String] :rvm_init Same as options DRbQS::Manage::SSHShell::RubyEnvironment
. @option opts [Hash] :env Same as options DRbQS::Manage::SSHShell::RubyEnvironment
.
# File lib/drbqs/manage/ssh_shell.rb, line 70 def initialize(dest, opts = {}) @user, @host, @port = split_destination(dest) if !(@host && @user) raise ArgumentError, "Invalid ssh server: host=#{@host.inspect}, user=#{@user.inspect}." end opts = opts.dup @keys = opts.delete(:keys) @shell = opts.delete(:shell) || DEFAULT_SHELL @out = opts.delete(:io) @ruby_environment = DRbQS::Manage::SSHShell::RubyEnvironment.new(opts.slice(:directory, :rvm, :rvm_init, :env)) @ssh = nil end
Public Instance Methods
# File lib/drbqs/manage/ssh_shell.rb, line 83 def directory @ruby_environment.directory end
# File lib/drbqs/manage/ssh_shell.rb, line 147 def exec(cmd, opts = {}) unless @ssh raise "Not connect." end if opts[:check] shell_exec_check(@ssh, cmd) else shell_exec(@ssh, cmd) end end
@param [Array] commands An array of list of commands. @param [Hash] opts Options @option opts [Boolean] :check Check exit codes of commands.
# File lib/drbqs/manage/ssh_shell.rb, line 176 def execute_all(commands, opts = {}) results = [] start do |ssh_shell| commands.each do |cmd| results << ssh_shell.exec(cmd, opts) end end results end
# File lib/drbqs/manage/ssh_shell.rb, line 186 def get_environment execute_all(@ruby_environment.get_environment_commands) end
# File lib/drbqs/manage/ssh_shell.rb, line 158 def start(&block) Net::SSH.start(@host, @user, :port => @port, :keys => @keys) do |ssh| ssh.shell(@shell) do |sh| @ruby_environment.setup_commands.each do |cmd| shell_exec_check(sh, cmd) end @ssh = sh yield(self) shell_exec(sh, "exit") end end ensure @ssh = nil end
Private Instance Methods
# File lib/drbqs/manage/ssh_shell.rb, line 107 def output_command(cmd, result) if @out @out.puts "#{@user}@#{@host}$ #{cmd}" @out.print result end end
# File lib/drbqs/manage/ssh_shell.rb, line 130 def shell_exec(sh, cmd) ary = shell_exec_get_output(sh, cmd) output_command(cmd, ary[1]) ary end
# File lib/drbqs/manage/ssh_shell.rb, line 137 def shell_exec_check(sh, cmd) ary = shell_exec(sh, cmd) n = ary[0].exit_status if n != 0 raise RuntimeError, "Can not execute properly on #{@host}.\nExit status: #{n}\ncommand: #{cmd}" end ary end
Return an array of a Net::SSH::Shell::Process object and a result string.
# File lib/drbqs/manage/ssh_shell.rb, line 116 def shell_exec_get_output(sh, cmd) result = '' pr_cmd = sh.execute!(cmd) do |sh_proc| sh_proc.on_output do |pr, data| result << data end sh_proc.on_error_output do |pr, data| result << data end end [pr_cmd, result] end
# File lib/drbqs/manage/ssh_shell.rb, line 87 def split_destination(dest) if (n = dest.index("@")) && n > 0 user = dest[0..(n - 1)] host_dir = dest[(n + 1)..-1] else raise "Not include '@': #{dest}" end if n = host_dir.index(':') host = host_dir[0..(n - 1)] port = host_dir[(n + 1)..-1] else host = host_dir port = nil end [user && user.size > 0 ? user : nil, host && host.size > 0 ? host : nil, port && port.size > 0 ? port.to_i : nil] end