Parent

SSHKit::Command

@author Lee Hambley

Constants

Failed

Attributes

args[R]
command[R]
exit_status[R]
full_stderr[RW]
full_stdout[RW]
options[R]
started[R]
started_at[R]
stderr[RW]
stdout[RW]

Public Class Methods

new(*args) click to toggle source

Initialize a new Command object

@param [Array] A list of arguments, the first is considered to be the command name, with optional variadaric args @return [Command] An un-started command object with no exit staus, and nothing in stdin or stdout

# 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

Public Instance Methods

complete?() click to toggle source
# File lib/sshkit/command.rb, line 58
def complete?
  !exit_status.nil?
end
Also aliased as: finished?
environment_hash() click to toggle source
# File lib/sshkit/command.rb, line 149
def environment_hash
  (SSHKit.config.default_env || {}).merge(options[:env] || {})
end
environment_string() click to toggle source
# File lib/sshkit/command.rb, line 153
def environment_string
  environment_hash.collect do |key,value|
    "#{key.to_s.upcase}=#{value}"
  end.join(' ')
end
exit_status=(new_exit_status) click to toggle source
# File lib/sshkit/command.rb, line 86
def exit_status=(new_exit_status)
  @finished_at = Time.now
  @exit_status = new_exit_status

  if options[:raise_on_non_zero_exit] && exit_status > 0
    message = ""
    message += "#{command} exit status: " + exit_status.to_s + "\n"
    message += "#{command} stdout: " + (stdout.strip.empty? ? "Nothing written" : stdout.strip) + "\n"

    stderr_message = [stderr.strip, full_stderr.strip].delete_if(&:empty?).first
    message += "#{command} stderr: " + (stderr_message || 'Nothing written') + "\n"
    raise Failed, message
  end
end
failed?() click to toggle source
Alias for: failure?
failure?() click to toggle source
# File lib/sshkit/command.rb, line 81
def failure?
  exit_status.to_i > 0
end
Also aliased as: failed?
finished?() click to toggle source
Alias for: complete?
group(&block) click to toggle source
# File lib/sshkit/command.rb, line 179
def group(&block)
  return yield unless options[:group]
  "sg #{options[:group]} -c \\\"%s\\\"" % %{#{yield}}
  # We could also use the so-called heredoc format perhaps:
  #"newgrp #{options[:group]} <<EOC \\\"%s\\\" EOC" % %Q{#{yield}}
end
host() click to toggle source
# File lib/sshkit/command.rb, line 125
def host
  options[:host]
end
in_background(&block) click to toggle source
# File lib/sshkit/command.rb, line 169
def in_background(&block)
  return yield unless options[:run_in_background]
  "( nohup %s > /dev/null & )" % yield
end
runtime() click to toggle source
# File lib/sshkit/command.rb, line 101
def runtime
  return nil unless complete?
  @finished_at - @started_at
end
should_map?() click to toggle source
# File lib/sshkit/command.rb, line 140
def should_map?
  !command.match /\s/
end
started=(new_started) click to toggle source
# File lib/sshkit/command.rb, line 67
def started=(new_started)
  @started_at = Time.now
  @started = new_started
end
started?() click to toggle source
# File lib/sshkit/command.rb, line 63
def started?
  started
end
success?() click to toggle source
# File lib/sshkit/command.rb, line 76
def success?
  exit_status.nil? ? false : exit_status.to_i == 0
end
Also aliased as: successful?
successful?() click to toggle source
Alias for: success?
to_command() click to toggle source
# 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() click to toggle source
# File lib/sshkit/command.rb, line 106
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_s() click to toggle source
# File lib/sshkit/command.rb, line 203
def to_s
  [SSHKit.config.command_map[command.to_sym], *Array(args)].join(' ')
end
umask(&block) click to toggle source
# 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) click to toggle source
# File lib/sshkit/command.rb, line 164
def user(&block)
  return yield unless options[:user]
  "sudo -u #{options[:user]} #{environment_string + " " unless environment_string.empty?}-- sh -c '%s'" % %{#{yield}}
end
uuid() click to toggle source
# File lib/sshkit/command.rb, line 72
def uuid
  @uuid ||= Digest::SHA1.hexdigest(SecureRandom.random_bytes(10))[0..7]
end
verbosity() click to toggle source
# File lib/sshkit/command.rb, line 129
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) click to toggle source
# File lib/sshkit/command.rb, line 159
def with(&block)
  return yield unless environment_hash.any?
  "( #{environment_string} %s )" % yield
end
within(&block) click to toggle source
# File lib/sshkit/command.rb, line 144
def within(&block)
  return yield unless options[:in]
  "cd #{options[:in]} && %s" % yield
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.