Class: DRbQS::Manage::SSHShell

Inherits:
Object
  • Object
show all
Defined in:
lib/drbqs/manage/ssh_shell.rb

Overview

Requirements:

  • bash

Defined Under Namespace

Classes: RubyEnvironment

Constant Summary collapse

DEFAULT_SHELL =
'bash'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dest, opts = {}) ⇒ SSHShell

Returns a new instance of SSHShell.

Parameters:

  • dest (String)

    Destination of SSH.

  • opts (Hash) (defaults to: {})

    The options of SSH shell.

Options Hash (opts):

  • :shell (String)

    The shell to use.

  • :keys (String)

    Path of a ssh key.

  • :io (IO)

    IO to output results of commands.

  • :directory (String)

    Same as options DRbQS::Manage::SSHShell::RubyEnvironment.

  • :rvm (String)

    Same as options DRbQS::Manage::SSHShell::RubyEnvironment.

  • :rvm_init (String)

    Same as options DRbQS::Manage::SSHShell::RubyEnvironment.

  • :env (Hash)

    Same as options DRbQS::Manage::SSHShell::RubyEnvironment.



70
71
72
73
74
75
76
77
78
79
80
81
# 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

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



57
58
59
# File 'lib/drbqs/manage/ssh_shell.rb', line 57

def host
  @host
end

#keysObject (readonly)

Returns the value of attribute keys.



57
58
59
# File 'lib/drbqs/manage/ssh_shell.rb', line 57

def keys
  @keys
end

#portObject (readonly)

Returns the value of attribute port.



57
58
59
# File 'lib/drbqs/manage/ssh_shell.rb', line 57

def port
  @port
end

#userObject (readonly)

Returns the value of attribute user.



57
58
59
# File 'lib/drbqs/manage/ssh_shell.rb', line 57

def user
  @user
end

Instance Method Details

#directoryObject



83
84
85
# File 'lib/drbqs/manage/ssh_shell.rb', line 83

def directory
  @ruby_environment.directory
end

#exec(cmd, opts = {}) ⇒ Object



147
148
149
150
151
152
153
154
155
156
# 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

#execute_all(commands, opts = {}) ⇒ Object

Parameters:

  • commands (Array)

    An array of list of commands.

  • opts (Hash) (defaults to: {})

    Options

Options Hash (opts):

  • :check (Boolean)

    Check exit codes of commands.



176
177
178
179
180
181
182
183
184
# 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

#get_environmentObject



186
187
188
# File 'lib/drbqs/manage/ssh_shell.rb', line 186

def get_environment
  execute_all(@ruby_environment.get_environment_commands)
end

#start(&block) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# 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