Module: Net::SSH::SessionHelpers
- Included in:
- Session
- Defined in:
- lib/net/ssh/session_helpers.rb
Instance Method Summary collapse
-
#capture(command) ⇒ String
Execute command and capture any output.
-
#chdir(path) ⇒ Boolean
Swith current directory.
-
#directory_exists?(path) ⇒ Boolean
Check if remote directory exists.
-
#env(key) ⇒ String
Get an environment variable.
-
#export(key, value) ⇒ Boolean
Export an environment variable.
-
#export_hash(data = {}) ⇒ Boolean
Export environment vars from hash.
-
#file_exists?(path) ⇒ Boolean
Check if remote file exists.
-
#has_group?(name) ⇒ Boolean
Check if group exists.
-
#has_user?(name, options = {}) ⇒ Boolean
Check if user exists.
-
#kill_process(pid, signal = 'SIGTERM') ⇒ Boolean
Kill a process with the signal.
-
#last_exit_code ⇒ Integer
Get last executed command exit code.
-
#process_exists?(pid) ⇒ Boolean
Check if process with PID is running.
-
#pwd ⇒ String
Get current directory.
-
#read_file(path) ⇒ String
Read remote file contents.
-
#sudo(command, options = {}) ⇒ SessionCommand
Execute command with sudo.
-
#symlink_exists?(path) ⇒ Boolean
Check if a symbilic link exists.
-
#with_timeout(time, &block) ⇒ Object
Set a timeout context for execution.
Instance Method Details
#capture(command) ⇒ String
Execute command and capture any output
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
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
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
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
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
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
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
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
82 83 84 |
# File 'lib/net/ssh/session_helpers.rb', line 82 def has_user?(name, ={}) run("id #{name}").success? end |
#kill_process(pid, signal = 'SIGTERM') ⇒ Boolean
Kill a process with the signal
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_code ⇒ Integer
Get last executed command 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
66 67 68 |
# File 'lib/net/ssh/session_helpers.rb', line 66 def process_exists?(pid) run("ps -p #{pid}").success? end |
#pwd ⇒ String
Get current directory
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
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
10 11 12 |
# File 'lib/net/ssh/session_helpers.rb', line 10 def sudo(command, ={}) run("sudo #{command}", ) end |
#symlink_exists?(path) ⇒ Boolean
Check if a symbilic link exists
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
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 |