Class: SSHKit::Command
- Inherits:
-
Object
- Object
- SSHKit::Command
- Defined in:
- lib/sshkit/command.rb
Overview
Constant Summary collapse
- Failed =
Class.new(SSHKit::StandardError)
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#exit_status ⇒ Object
Returns the value of attribute exit_status.
-
#full_stderr ⇒ Object
readonly
Returns the value of attribute full_stderr.
-
#full_stdout ⇒ Object
readonly
Returns the value of attribute full_stdout.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#started ⇒ Object
Returns the value of attribute started.
-
#started_at ⇒ Object
readonly
Returns the value of attribute started_at.
Instance Method Summary collapse
- #complete? ⇒ Boolean (also: #finished?)
- #environment_hash ⇒ Object
- #environment_string ⇒ Object
- #failure? ⇒ Boolean (also: #failed?)
- #group(&_block) ⇒ Object
- #host ⇒ Object
- #in_background(&_block) ⇒ Object
-
#initialize(*args) ⇒ Command
constructor
Initialize a new Command object.
- #on_stderr(channel, data) ⇒ Object
- #on_stdout(channel, data) ⇒ Object
- #runtime ⇒ Object
- #should_map? ⇒ Boolean
- #started? ⇒ Boolean
- #stderr ⇒ Object
- #stderr=(new_value) ⇒ Object
- #stdout ⇒ Object
- #stdout=(new_value) ⇒ Object
- #success? ⇒ Boolean (also: #successful?)
- #to_command ⇒ Object
- #to_hash ⇒ Object
- #to_s ⇒ Object
- #umask(&_block) ⇒ Object
- #user(&_block) ⇒ Object
- #uuid ⇒ Object
- #verbosity ⇒ Object
- #with(&_block) ⇒ Object
- #with_redaction ⇒ Object
- #within(&_block) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Command
Initialize a new Command object
command name, with optional variadaric args nothing in stdin or stdout
21 22 23 24 25 26 27 28 |
# File 'lib/sshkit/command.rb', line 21 def initialize(*args) raise ArgumentError, "Must pass arguments to Command.new" if args.empty? @options = .merge(args.) @command = sanitize_command(args.shift) @args = args @options.symbolize_keys! @stdout, @stderr, @full_stdout, @full_stderr = String.new, String.new, String.new, String.new end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
12 13 14 |
# File 'lib/sshkit/command.rb', line 12 def args @args end |
#command ⇒ Object (readonly)
Returns the value of attribute command.
12 13 14 |
# File 'lib/sshkit/command.rb', line 12 def command @command end |
#exit_status ⇒ Object
Returns the value of attribute exit_status.
12 13 14 |
# File 'lib/sshkit/command.rb', line 12 def exit_status @exit_status end |
#full_stderr ⇒ Object (readonly)
Returns the value of attribute full_stderr.
12 13 14 |
# File 'lib/sshkit/command.rb', line 12 def full_stderr @full_stderr end |
#full_stdout ⇒ Object (readonly)
Returns the value of attribute full_stdout.
12 13 14 |
# File 'lib/sshkit/command.rb', line 12 def full_stdout @full_stdout end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
12 13 14 |
# File 'lib/sshkit/command.rb', line 12 def @options end |
#started ⇒ Object
Returns the value of attribute started.
12 13 14 |
# File 'lib/sshkit/command.rb', line 12 def started @started end |
#started_at ⇒ Object (readonly)
Returns the value of attribute started_at.
12 13 14 |
# File 'lib/sshkit/command.rb', line 12 def started_at @started_at end |
Instance Method Details
#complete? ⇒ Boolean Also known as: finished?
30 31 32 |
# File 'lib/sshkit/command.rb', line 30 def complete? !exit_status.nil? end |
#environment_hash ⇒ Object
151 152 153 |
# File 'lib/sshkit/command.rb', line 151 def environment_hash (SSHKit.config.default_env || {}).merge([:env] || {}) end |
#environment_string ⇒ Object
155 156 157 158 159 160 161 |
# File 'lib/sshkit/command.rb', line 155 def environment_string environment_hash.collect do |key,value| key_string = key.is_a?(Symbol) ? key.to_s.upcase : key.to_s escaped_value = value.to_s.gsub(/"/, '\"') %{#{key_string}="#{escaped_value}"} end.join(' ') end |
#failure? ⇒ Boolean Also known as: failed?
53 54 55 |
# File 'lib/sshkit/command.rb', line 53 def failure? exit_status.to_i > 0 end |
#group(&_block) ⇒ Object
183 184 185 186 187 188 |
# File 'lib/sshkit/command.rb', line 183 def group(&_block) return yield unless [:group] %Q(sg #{[:group]} -c "#{yield}") # We could also use the so-called heredoc format perhaps: #"newgrp #{options[:group]} <<EOC \\\"%s\\\" EOC" % %Q{#{yield}} end |
#host ⇒ Object
127 128 129 |
# File 'lib/sshkit/command.rb', line 127 def host [:host] end |
#in_background(&_block) ⇒ Object
173 174 175 176 |
# File 'lib/sshkit/command.rb', line 173 def in_background(&_block) return yield unless [:run_in_background] sprintf("( nohup %s > /dev/null & )", yield) end |
#on_stderr(channel, data) ⇒ Object
84 85 86 87 88 |
# File 'lib/sshkit/command.rb', line 84 def on_stderr(channel, data) @stderr = data @full_stderr += data call_interaction_handler(:stderr, data, channel) end |
#on_stdout(channel, data) ⇒ Object
78 79 80 81 82 |
# File 'lib/sshkit/command.rb', line 78 def on_stdout(channel, data) @stdout = data @full_stdout += data call_interaction_handler(:stdout, data, channel) end |
#runtime ⇒ Object
103 104 105 106 |
# File 'lib/sshkit/command.rb', line 103 def runtime return nil unless complete? @finished_at - @started_at end |
#should_map? ⇒ Boolean
142 143 144 |
# File 'lib/sshkit/command.rb', line 142 def should_map? !command.match(/\s/) end |
#started? ⇒ Boolean
35 36 37 |
# File 'lib/sshkit/command.rb', line 35 def started? started end |
#stderr ⇒ Object
68 69 70 71 |
# File 'lib/sshkit/command.rb', line 68 def stderr log_reader_deprecation('stderr') @stderr end |
#stderr=(new_value) ⇒ Object
73 74 75 76 |
# File 'lib/sshkit/command.rb', line 73 def stderr=(new_value) log_writer_deprecation('stderr') @stderr = new_value end |
#stdout ⇒ Object
58 59 60 61 |
# File 'lib/sshkit/command.rb', line 58 def stdout log_reader_deprecation('stdout') @stdout end |
#stdout=(new_value) ⇒ Object
63 64 65 66 |
# File 'lib/sshkit/command.rb', line 63 def stdout=(new_value) log_writer_deprecation('stdout') @stdout = new_value end |
#success? ⇒ Boolean Also known as: successful?
48 49 50 |
# File 'lib/sshkit/command.rb', line 48 def success? exit_status.nil? ? false : exit_status.to_i == 0 end |
#to_command ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/sshkit/command.rb', line 190 def to_command return command.to_s unless should_map? within do umask do with do user do in_background do group do to_s end end end end end end end |
#to_hash ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/sshkit/command.rb', line 108 def to_hash { command: self.to_s, args: args, options: , exit_status: exit_status, stdout: full_stdout, stderr: full_stderr, started_at: @started_at, finished_at: @finished_at, runtime: runtime, uuid: uuid, started: started?, finished: finished?, successful: successful?, failed: failed? } end |
#to_s ⇒ Object
214 215 216 217 218 219 220 |
# File 'lib/sshkit/command.rb', line 214 def to_s if should_map? [SSHKit.config.command_map[command.to_sym], *Array(args)].join(' ') else command.to_s end end |
#umask(&_block) ⇒ Object
178 179 180 181 |
# File 'lib/sshkit/command.rb', line 178 def umask(&_block) return yield unless SSHKit.config.umask sprintf("umask #{SSHKit.config.umask} && %s", yield) end |
#user(&_block) ⇒ Object
168 169 170 171 |
# File 'lib/sshkit/command.rb', line 168 def user(&_block) return yield unless [:user] "sudo -u #{[:user]} #{environment_string + " " unless environment_string.empty?}-- sh -c '#{yield}'" end |
#uuid ⇒ Object
44 45 46 |
# File 'lib/sshkit/command.rb', line 44 def uuid @uuid ||= Digest::SHA1.hexdigest(SecureRandom.random_bytes(10))[0..7] end |
#verbosity ⇒ Object
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/sshkit/command.rb', line 131 def verbosity if (vb = [:verbosity]) case vb when Symbol then return Logger.const_get(vb.to_s.upcase) when Integer then return vb end else Logger::INFO end end |
#with(&_block) ⇒ Object
163 164 165 166 |
# File 'lib/sshkit/command.rb', line 163 def with(&_block) return yield unless environment_hash.any? "( export #{environment_string} ; #{yield} )" end |
#with_redaction ⇒ Object
207 208 209 210 211 212 |
# File 'lib/sshkit/command.rb', line 207 def with_redaction new_args = args.map{|arg| arg.is_a?(Redaction) ? '[REDACTED]' : arg } redacted_cmd = dup redacted_cmd.instance_variable_set(:@args, new_args) redacted_cmd end |
#within(&_block) ⇒ Object
146 147 148 149 |
# File 'lib/sshkit/command.rb', line 146 def within(&_block) return yield unless [:in] sprintf("cd #{[:in]} && %s", yield) end |