Class: SSHKit::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/sshkit/command.rb

Overview

Author:

  • Lee Hambley

Constant Summary collapse

Failed =
Class.new(SSHKit::StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Command

Initialize a new Command object

command name, with optional variadaric args nothing in stdin or stdout

Parameters:

  • A (Array)

    list of arguments, the first is considered to be the

Raises:

  • (ArgumentError)


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 = default_options.merge(args.extract_options!)
  @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

#argsObject (readonly)

Returns the value of attribute args.



35
36
37
# File 'lib/sshkit/command.rb', line 35

def args
  @args
end

#commandObject (readonly)

Returns the value of attribute command.



35
36
37
# File 'lib/sshkit/command.rb', line 35

def command
  @command
end

#exit_statusObject

Returns the value of attribute exit_status.



35
36
37
# File 'lib/sshkit/command.rb', line 35

def exit_status
  @exit_status
end

#full_stderrObject

Returns the value of attribute full_stderr.



38
39
40
# File 'lib/sshkit/command.rb', line 38

def full_stderr
  @full_stderr
end

#full_stdoutObject

Returns the value of attribute full_stdout.



38
39
40
# File 'lib/sshkit/command.rb', line 38

def full_stdout
  @full_stdout
end

#optionsObject (readonly)

Returns the value of attribute options.



35
36
37
# File 'lib/sshkit/command.rb', line 35

def options
  @options
end

#startedObject

Returns the value of attribute started.



35
36
37
# File 'lib/sshkit/command.rb', line 35

def started
  @started
end

#started_atObject (readonly)

Returns the value of attribute started_at.



35
36
37
# File 'lib/sshkit/command.rb', line 35

def started_at
  @started_at
end

#stderrObject

Returns the value of attribute stderr.



37
38
39
# File 'lib/sshkit/command.rb', line 37

def stderr
  @stderr
end

#stdoutObject

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?

Returns:

  • (Boolean)


58
59
60
# File 'lib/sshkit/command.rb', line 58

def complete?
  !exit_status.nil?
end

#environment_hashObject



147
148
149
# File 'lib/sshkit/command.rb', line 147

def environment_hash
  (SSHKit.config.default_env || {}).merge(options[:env] || {})
end

#environment_stringObject



151
152
153
154
155
156
157
158
159
# File 'lib/sshkit/command.rb', line 151

def environment_string
  environment_hash.collect do |key,value|
    if key.is_a? Symbol
      "#{key.to_s.upcase}=#{value}"
    else
      "#{key.to_s}=#{value}"
    end
  end.join(' ')
end

#failure?Boolean Also known as: failed?

Returns:

  • (Boolean)


81
82
83
# File 'lib/sshkit/command.rb', line 81

def failure?
  exit_status.to_i > 0
end

#group(&block) ⇒ Object



181
182
183
184
185
186
# File 'lib/sshkit/command.rb', line 181

def group(&block)
  return yield unless options[:group]
  "sg #{options[:group]} -c \\\"%s\\\"" % %Q{#{yield}}
  # We could also use the so-called heredoc format perhaps:
  #"newgrp #{options[:group]} <<EOC \\\"%s\\\" EOC" % %Q{#{yield}}
end

#hostObject



123
124
125
# File 'lib/sshkit/command.rb', line 123

def host
  options[:host]
end

#in_background(&block) ⇒ Object



171
172
173
174
# File 'lib/sshkit/command.rb', line 171

def in_background(&block)
  return yield unless options[:run_in_background]
  "( nohup %s > /dev/null & )" % yield
end

#runtimeObject



99
100
101
102
# File 'lib/sshkit/command.rb', line 99

def runtime
  return nil unless complete?
  @finished_at - @started_at
end

#should_map?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/sshkit/command.rb', line 138

def should_map?
  !command.match /\s/
end

#started?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/sshkit/command.rb', line 63

def started?
  started
end

#success?Boolean Also known as: successful?

Returns:

  • (Boolean)


76
77
78
# File 'lib/sshkit/command.rb', line 76

def success?
  exit_status.nil? ? false : exit_status.to_i == 0
end

#to_commandObject



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/sshkit/command.rb', line 188

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_hashObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/sshkit/command.rb', line 104

def to_hash
  {
    command:     self.to_s,
    args:        args,
    options:     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_sObject



205
206
207
# File 'lib/sshkit/command.rb', line 205

def to_s
  [SSHKit.config.command_map[command.to_sym], *Array(args)].join(' ')
end

#umask(&block) ⇒ Object



176
177
178
179
# File 'lib/sshkit/command.rb', line 176

def umask(&block)
  return yield unless SSHKit.config.umask
  "umask #{SSHKit.config.umask} && %s" % yield
end

#user(&block) ⇒ Object



166
167
168
169
# File 'lib/sshkit/command.rb', line 166

def user(&block)
  return yield unless options[:user]
  "sudo -u #{options[:user]} #{environment_string + " " unless environment_string.empty?}-- sh -c '%s'" % %Q{#{yield}}
end

#uuidObject



72
73
74
# File 'lib/sshkit/command.rb', line 72

def uuid
  @uuid ||= Digest::SHA1.hexdigest(SecureRandom.random_bytes(10))[0..7]
end

#verbosityObject



127
128
129
130
131
132
133
134
135
136
# File 'lib/sshkit/command.rb', line 127

def verbosity
  if vb = options[: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



161
162
163
164
# File 'lib/sshkit/command.rb', line 161

def with(&block)
  return yield unless environment_hash.any?
  "( #{environment_string} %s )" % yield
end

#within(&block) ⇒ Object



142
143
144
145
# File 'lib/sshkit/command.rb', line 142

def within(&block)
  return yield unless options[:in]
  "cd #{options[:in]} && %s" % yield
end