Class: Toolshed::ServerAdministration::SSH

Inherits:
Object
  • Object
show all
Includes:
Password
Defined in:
lib/toolshed/server_administration/ssh.rb

Overview

SSH class that can ssh to a host and perform commands

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Password

#password_from_config

Constructor Details

#initialize(options = nil) ⇒ SSH

rubocop:disable AbcSize, CyclomaticComplexity, PerceivedComplexity, LineLength



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/toolshed/server_administration/ssh.rb', line 16

def initialize(options = nil) # rubocop:disable AbcSize, CyclomaticComplexity, PerceivedComplexity, LineLength
  options ||= {}
  @password = options[:password] || ''
  @sudo_password = options[:sudo_password] || ''
  @keys = options[:keys] || ''
  @host = options[:host] || ''
  @user = options[:user] || ''
  @ssh_options = options[:ssh_options] || {}
  @commands = options[:commands] || ''
  @password = options[:password] || ''
  @data = []
  @silent = options[:silent] || false
  timeout_period = options[:timeout_period].to_i || 30
  @timeout = Toolshed::Timeout.new(timeout_period: timeout_period)

  set_ssh_options
end

Instance Attribute Details

#channelObject

rubocop:disable LineLength



13
14
15
# File 'lib/toolshed/server_administration/ssh.rb', line 13

def channel
  @channel
end

#commandsObject

rubocop:disable LineLength



13
14
15
# File 'lib/toolshed/server_administration/ssh.rb', line 13

def commands
  @commands
end

#dataObject

rubocop:disable LineLength



13
14
15
# File 'lib/toolshed/server_administration/ssh.rb', line 13

def data
  @data
end

#hostObject

rubocop:disable LineLength



13
14
15
# File 'lib/toolshed/server_administration/ssh.rb', line 13

def host
  @host
end

#keysObject

rubocop:disable LineLength



13
14
15
# File 'lib/toolshed/server_administration/ssh.rb', line 13

def keys
  @keys
end

#passwordObject

rubocop:disable LineLength



13
14
15
# File 'lib/toolshed/server_administration/ssh.rb', line 13

def password
  @password
end

#silentObject (readonly)

Returns the value of attribute silent.



14
15
16
# File 'lib/toolshed/server_administration/ssh.rb', line 14

def silent
  @silent
end

#ssh_optionsObject

rubocop:disable LineLength



13
14
15
# File 'lib/toolshed/server_administration/ssh.rb', line 13

def ssh_options
  @ssh_options
end

#sudo_passwordObject

rubocop:disable LineLength



13
14
15
# File 'lib/toolshed/server_administration/ssh.rb', line 13

def sudo_password
  @sudo_password
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



14
15
16
# File 'lib/toolshed/server_administration/ssh.rb', line 14

def timeout
  @timeout
end

#userObject

rubocop:disable LineLength



13
14
15
# File 'lib/toolshed/server_administration/ssh.rb', line 13

def user
  @user
end

Instance Method Details

#executeObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/toolshed/server_administration/ssh.rb', line 34

def execute
  begin
    timeout.start do
      Net::SSH.start(host, user, ssh_options) do |ssh|
        ssh.open_channel do |channel|
          self.channel = channel
          request_pty
          run_commands
        end
        ssh.loop
      end
    end
  rescue Toolshed::Timeout::Error => e
    Toolshed.logger.fatal e.message
    raise SSHResponseException, "Unable to handle response for #{data.last}"
  end
  data
end