Module: Net::SSH::SessionHelpers

Included in:
Session
Defined in:
lib/net/ssh/session_helpers.rb

Instance Method Summary collapse

Instance Method Details

#capture(command) ⇒ String

Execute command and capture any output

Parameters:

  • command (String)

    command to execute

Returns:

  • (String)

    execution result



30
31
32
# File 'lib/net/ssh/session_helpers.rb', line 30

def capture(command)
  run(command).output.strip
end

#chdir(path) ⇒ Boolean

Swith current directory

Parameters:

  • path (String)

    directory path

Returns:

  • (Boolean)

    execution result



17
18
19
# File 'lib/net/ssh/session_helpers.rb', line 17

def chdir(path)
  run("cd #{path}").success?
end

#directory_exists?(path) ⇒ Boolean

Check if remote directory exists

Parameters:

  • path (String)

    directory path

Returns:

  • (Boolean)

    execution result



45
46
47
# File 'lib/net/ssh/session_helpers.rb', line 45

def directory_exists?(path)
  run("test -d #{path}").success?
end

#env(key) ⇒ String

Get an environment variable

Parameters:

  • key (String)

    variable name

Returns:

  • (String)

    variable value



92
93
94
# File 'lib/net/ssh/session_helpers.rb', line 92

def env(key)
  capture("echo $#{key}")
end

#export(key, value) ⇒ Boolean

Export an environment variable

Parameters:

  • key (String)

    variable name

  • value (String)

    variable value

Returns:

  • (Boolean)

    execution result



76
77
78
# File 'lib/net/ssh/session_helpers.rb', line 76

def export(key, value)
  run("export #{key}=#{value}").success?
end

#export_hash(data = {}) ⇒ Boolean

Export environment vars from hash

Parameters:

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

Returns:

  • (Boolean)

    execution result



83
84
85
86
87
# File 'lib/net/ssh/session_helpers.rb', line 83

def export_hash(data={})
  data.each_pair do |k, v|
    export(k, v)
  end
end

#file_exists?(path) ⇒ Boolean

Check if remote file exists

Parameters:

  • path (String)

    file path

Returns:

  • (Boolean)

    execution result



52
53
54
# File 'lib/net/ssh/session_helpers.rb', line 52

def file_exists?(path)
  run("test -f #{path}").success?
end

#kill_process(pid, signal = 'SIGTERM') ⇒ Boolean

Kill a process with the signal

Parameters:

  • pid (String)

    process id

  • signal (String) (defaults to: 'SIGTERM')

    signal to send

Returns:

  • (Boolean)

    exection result



67
68
69
70
# File 'lib/net/ssh/session_helpers.rb', line 67

def kill_process(pid, signal='SIGTERM')
  run("kill -#{signal} #{pid}")
  process_exists?(pid)
end

#last_exit_codeInteger

Get last executed command exit code

Returns:

  • (Integer)

    exit code



98
99
100
# File 'lib/net/ssh/session_helpers.rb', line 98

def last_exit_code
  Integer(capture("echo $?"))
end

#process_exists?(pid) ⇒ Boolean

Check if process with PID is running

Parameters:

  • pid (String)

    process id

Returns:

  • (Boolean)

    execution result



59
60
61
# File 'lib/net/ssh/session_helpers.rb', line 59

def process_exists?(pid)
  run("ps -p #{pid}").success?
end

#pwdString

Get current directory

Returns:

  • (String)


23
24
25
# File 'lib/net/ssh/session_helpers.rb', line 23

def pwd
  capture("pwd")
end

#read_file(path) ⇒ String

Read remote file contents

Returns:

  • (String)

    file contents



37
38
39
40
# File 'lib/net/ssh/session_helpers.rb', line 37

def read_file(path)
  result = run("cat #{path}")
  result.success? ? result.output : ''
end

#sudo(command, options = {}) ⇒ SessionCommand

Execute command with sudo

Parameters:

  • command (String)

    string

  • execution (Hash)

    options

Returns:



10
11
12
# File 'lib/net/ssh/session_helpers.rb', line 10

def sudo(command, options={})
  run("sudo #{command}", options)
end

#with_timeout(time, &block) ⇒ Object

Set a timeout context for execution

Parameters:

  • time (Integer)

    max time for execution in seconds

  • block (Block)

    block to execute



105
106
107
108
109
# File 'lib/net/ssh/session_helpers.rb', line 105

def with_timeout(time, &block)
  Timeout.timeout(time) do
    block.call(self)
  end
end