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



113
114
115
# File 'lib/net/ssh/session_helpers.rb', line 113

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



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

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



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

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:

  • file (String)

    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

#has_group?(name) ⇒ Boolean

Check if group exists

Parameters:

  • group (String)

    name

Returns:

  • (Boolean)


89
90
91
# File 'lib/net/ssh/session_helpers.rb', line 89

def has_group?(name)
  run("id -g #{name}").success?
end

#has_user?(name, options = {}) ⇒ Boolean

Check if user exists

Parameters:

  • username (String)

Returns:

  • (Boolean)


82
83
84
# File 'lib/net/ssh/session_helpers.rb', line 82

def has_user?(name, options={})
  run("id #{name}").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



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

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



119
120
121
# File 'lib/net/ssh/session_helpers.rb', line 119

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



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

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

Check if a symbilic link exists

Parameters:

  • file (String)

    path

Returns:

  • (Boolean)


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

def symlink_exists?(path)
  run("test -h #{path}").success?
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



126
127
128
129
130
# File 'lib/net/ssh/session_helpers.rb', line 126

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