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
Returns the value of attribute full_stderr.
-
#full_stdout ⇒ Object
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.
-
#stderr ⇒ Object
Returns the value of attribute stderr.
-
#stdout ⇒ Object
Returns the value of attribute stdout.
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.
- #runtime ⇒ Object
- #should_map? ⇒ Boolean
- #started? ⇒ Boolean
- #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
- #within(&block) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Command
Initialize a new Command object
command name, with optional variadaric args nothing in stdin or stdout
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/sshkit/command.rb', line 47 def initialize(*args) raise ArgumentError, "May not pass no arguments to Command.new" if args.empty? @options = .merge(args.) @command = args.shift.to_s.strip.to_sym @args = args @options.symbolize_keys! sanitize_command! @stdout, @stderr = String.new, String.new @full_stdout, @full_stderr = String.new, String.new end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
35 36 37 |
# File 'lib/sshkit/command.rb', line 35 def args @args end |
#command ⇒ Object (readonly)
Returns the value of attribute command.
35 36 37 |
# File 'lib/sshkit/command.rb', line 35 def command @command end |
#exit_status ⇒ Object
Returns the value of attribute exit_status.
35 36 37 |
# File 'lib/sshkit/command.rb', line 35 def exit_status @exit_status end |
#full_stderr ⇒ Object
Returns the value of attribute full_stderr.
38 39 40 |
# File 'lib/sshkit/command.rb', line 38 def full_stderr @full_stderr end |
#full_stdout ⇒ Object
Returns the value of attribute full_stdout.
38 39 40 |
# File 'lib/sshkit/command.rb', line 38 def full_stdout @full_stdout end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
35 36 37 |
# File 'lib/sshkit/command.rb', line 35 def @options end |
#started ⇒ Object
Returns the value of attribute started.
35 36 37 |
# File 'lib/sshkit/command.rb', line 35 def started @started end |
#started_at ⇒ Object (readonly)
Returns the value of attribute started_at.
35 36 37 |
# File 'lib/sshkit/command.rb', line 35 def started_at @started_at end |
#stderr ⇒ Object
Returns the value of attribute stderr.
37 38 39 |
# File 'lib/sshkit/command.rb', line 37 def stderr @stderr end |
#stdout ⇒ Object
Returns the value of attribute stdout.
37 38 39 |
# File 'lib/sshkit/command.rb', line 37 def stdout @stdout end |
Instance Method Details
#complete? ⇒ Boolean Also known as: finished?
58 59 60 |
# File 'lib/sshkit/command.rb', line 58 def complete? !exit_status.nil? end |
#environment_hash ⇒ Object
149 150 151 |
# File 'lib/sshkit/command.rb', line 149 def environment_hash (SSHKit.config.default_env || {}).merge([:env] || {}) end |
#environment_string ⇒ Object
153 154 155 156 157 |
# File 'lib/sshkit/command.rb', line 153 def environment_string environment_hash.collect do |key,value| "#{key.to_s.upcase}=#{value}" end.join(' ') end |
#failure? ⇒ Boolean Also known as: failed?
81 82 83 |
# File 'lib/sshkit/command.rb', line 81 def failure? exit_status.to_i > 0 end |
#group(&block) ⇒ Object
179 180 181 182 183 184 |
# File 'lib/sshkit/command.rb', line 179 def group(&block) return yield unless [:group] "sg #{[:group]} -c \\\"%s\\\"" % %Q{#{yield}} # We could also use the so-called heredoc format perhaps: #"newgrp #{options[:group]} <<EOC \\\"%s\\\" EOC" % %Q{#{yield}} end |
#host ⇒ Object
125 126 127 |
# File 'lib/sshkit/command.rb', line 125 def host [:host] end |
#in_background(&block) ⇒ Object
169 170 171 172 |
# File 'lib/sshkit/command.rb', line 169 def in_background(&block) return yield unless [:run_in_background] "( nohup %s > /dev/null & )" % yield end |
#runtime ⇒ Object
101 102 103 104 |
# File 'lib/sshkit/command.rb', line 101 def runtime return nil unless complete? @finished_at - @started_at end |
#should_map? ⇒ Boolean
140 141 142 |
# File 'lib/sshkit/command.rb', line 140 def should_map? !command.match /\s/ end |
#started? ⇒ Boolean
63 64 65 |
# File 'lib/sshkit/command.rb', line 63 def started? started end |
#success? ⇒ Boolean Also known as: successful?
76 77 78 |
# File 'lib/sshkit/command.rb', line 76 def success? exit_status.nil? ? false : exit_status.to_i == 0 end |
#to_command ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/sshkit/command.rb', line 186 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
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/sshkit/command.rb', line 106 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
203 204 205 |
# File 'lib/sshkit/command.rb', line 203 def to_s [SSHKit.config.command_map[command.to_sym], *Array(args)].join(' ') end |
#umask(&block) ⇒ Object
174 175 176 177 |
# File 'lib/sshkit/command.rb', line 174 def umask(&block) return yield unless SSHKit.config.umask "umask #{SSHKit.config.umask} && %s" % yield end |
#user(&block) ⇒ Object
164 165 166 167 |
# File 'lib/sshkit/command.rb', line 164 def user(&block) return yield unless [:user] "sudo su #{[:user]} -c '%s'" % %Q{#{yield}} end |
#uuid ⇒ Object
72 73 74 |
# File 'lib/sshkit/command.rb', line 72 def uuid @uuid ||= Digest::SHA1.hexdigest(SecureRandom.random_bytes(10))[0..7] end |
#verbosity ⇒ Object
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/sshkit/command.rb', line 129 def verbosity if vb = [:verbosity] case vb.class.name when 'Symbol' then return Logger.const_get(vb.to_s.upcase) when 'Fixnum' then return vb end else Logger::INFO end end |
#with(&block) ⇒ Object
159 160 161 162 |
# File 'lib/sshkit/command.rb', line 159 def with(&block) return yield unless environment_hash.any? "( #{environment_string} %s )" % yield end |
#within(&block) ⇒ Object
144 145 146 147 |
# File 'lib/sshkit/command.rb', line 144 def within(&block) return yield unless [:in] "cd #{[:in]} && %s" % yield end |