Module: SSH_Utils
- Defined in:
- lib/ssh-utils.rb
Constant Summary collapse
- @@default_envars =
default envarlist
%w(PATH)
Instance Attribute Summary collapse
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#servers ⇒ Object
Returns the value of attribute servers.
Instance Method Summary collapse
- #_show_cmd(cmd) ⇒ Object
-
#as(user, opts = {}) ⇒ Object
as USER, :with => ENVARLIST, :debug => [true|false], :norun => [true|false].
-
#merge_env(opts) ⇒ Object
merge_env opts.
- #merge_opts(opts = {}) ⇒ Object
-
#merge_opts_with_env(opts = {}) ⇒ Object
merge_opts_with_env opts.
-
#on(servers, opts = {}) ⇒ Object
on SERVERLIST, :with => %w(PATH RUBYLIB), :debug => [true|false], :norun => [true|false].
-
#remote_run(cmd) ⇒ Object
(also: #run_remotely)
remote_run COMMAND run the remote command.
- #remote_run_with_output(cmd, opts = {}) ⇒ Object (also: #capture, #rrout)
- #ssh_command(cmd) ⇒ Object
-
#with(env, opts = {}) ⇒ Object
with ENVARLIST, :VAR1 => value, :debug => …
Instance Attribute Details
#server ⇒ Object (readonly)
Returns the value of attribute server.
25 26 27 |
# File 'lib/ssh-utils.rb', line 25 def server @server end |
#servers ⇒ Object
Returns the value of attribute servers.
26 27 28 |
# File 'lib/ssh-utils.rb', line 26 def servers @servers end |
Instance Method Details
#_show_cmd(cmd) ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/ssh-utils.rb', line 106 def _show_cmd cmd if @opts[:norun] || $norun talkf "(norun) %s\n", cmd elsif @opts[:debug] || $debug || @opts[:verbose] || $verbose talkf "--> %s\n", cmd end end |
#as(user, opts = {}) ⇒ Object
as USER, :with => ENVARLIST, :debug => [true|false], :norun => [true|false]
78 79 80 81 82 |
# File 'lib/ssh-utils.rb', line 78 def as user, opts = {} @user = user merge_opts_with_env opts yield if block_given? end |
#merge_env(opts) ⇒ Object
merge_env opts
merge envars. :with => ENVARS_TO_ADD, :without => ENVARS_TO_EXCLUDE
:with => %w(PATH RUBYLIB)
:without => %w(PATH)
Removes the :with and :without keys from the opts hash
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ssh-utils.rb', line 44 def merge_env opts @envars = @@default_envars if opts.key?(:with) @envars.concat(opts[:with]).uniq! opts.delete(:with) end if opts.key?(:without) @envars -= opts[:without] opts.delete(:without) end end |
#merge_opts(opts = {}) ⇒ Object
32 33 34 35 |
# File 'lib/ssh-utils.rb', line 32 def merge_opts opts = {} @opts ||= {} @opts.merge!(opts) unless opts.empty? end |
#merge_opts_with_env(opts = {}) ⇒ Object
merge_opts_with_env opts
Invokes merge_env, then merge_opts
60 61 62 63 |
# File 'lib/ssh-utils.rb', line 60 def merge_opts_with_env opts = {} merge_env (opts = opts.dup) merge_opts opts end |
#on(servers, opts = {}) ⇒ Object
on SERVERLIST, :with => %w(PATH RUBYLIB), :debug => [true|false], :norun => [true|false]
67 68 69 70 71 72 73 74 |
# File 'lib/ssh-utils.rb', line 67 def on servers, opts = {} merge_opts_with_env opts (@servers = servers).each do |server| @server = server talk("--> Running block for server #{server}..") if @opts[:debug] || $debug yield server end end |
#remote_run(cmd) ⇒ Object Also known as: run_remotely
remote_run COMMAND run the remote command
117 118 119 120 121 |
# File 'lib/ssh-utils.rb', line 117 def remote_run cmd ssh = ssh_command(cmd) _show_cmd ssh system(ssh) unless @opts[:norun] || $norun end |
#remote_run_with_output(cmd, opts = {}) ⇒ Object Also known as: capture, rrout
125 126 127 128 129 130 131 132 |
# File 'lib/ssh-utils.rb', line 125 def remote_run_with_output cmd, opts = {} merge_opts_with_env opts ssh = ssh_command cmd _show_cmd ssh out = nil IO.popen(ssh) {|f| out = f.read } out end |
#ssh_command(cmd) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/ssh-utils.rb', line 91 def ssh_command cmd ssh = "ssh -A" ssh += " -u #{@user}" unless @user.nil? ssh += " #{@server}" @envars.each do |env| # explicit values in the options list override the environment values val = @opts.key?(env) ? @opts[env] : ENV[env] if !(val.nil? || val.empty?) ssh += sprintf(" %s='%s'", env, val.gsub("'", "\'")) end end ssh += " " + cmd.to_s ssh end |
#with(env, opts = {}) ⇒ Object
with ENVARLIST, :VAR1 => value, :debug => …
86 87 88 89 |
# File 'lib/ssh-utils.rb', line 86 def with env, opts = {} merge_opts_with_env opts yield if block_given? end |