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
# 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
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



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

def args
  @args
end

#commandObject (readonly)

Returns the value of attribute command.



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

def command
  @command
end

#exit_statusObject

Returns the value of attribute exit_status.



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

def exit_status
  @exit_status
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#startedObject

Returns the value of attribute started.



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

def started
  @started
end

#started_atObject (readonly)

Returns the value of attribute started_at.



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

def started_at
  @started_at
end

#stderrObject

Returns the value of attribute stderr.



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

def stderr
  @stderr
end

#stdoutObject

Returns the value of attribute stdout.



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

def stdout
  @stdout
end

Instance Method Details

#complete?Boolean Also known as: finished?

Returns:

  • (Boolean)


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

def complete?
  !exit_status.nil?
end

#failure?Boolean Also known as: failed?

Returns:

  • (Boolean)


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

def failure?
  exit_status.to_i > 0
end

#hostObject



120
121
122
# File 'lib/sshkit/command.rb', line 120

def host
  options[:host]
end

#runtimeObject



96
97
98
99
# File 'lib/sshkit/command.rb', line 96

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

#started?Boolean

Returns:

  • (Boolean)


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

def started?
  started
end

#success?Boolean Also known as: successful?

Returns:

  • (Boolean)


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

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

#to_hashObject



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

def to_hash
  {
    command:     self.to_s,
    args:        args,
    options:     options,
    exit_status: exit_status,
    stdout:      stdout,
    stderr:      stderr,
    started_at:  @started_at,
    finished_at: @finished_at,
    runtime:     runtime,
    uuid:        uuid,
    started:     started?,
    finished:    finished?,
    successful:  successful?,
    failed:      failed?
  }
end

#to_sObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/sshkit/command.rb', line 124

def to_s
  return command.to_s if command.match /\s/
  String.new.tap do |cs|
    if options[:in]
      cs << sprintf("cd %s && ", options[:in])
    end
    unless SSHKit.config.default_env.empty?
      if options[:env].is_a? Hash
        options[:env] = SSHKit.config.default_env.merge(options[:env])
      end
    end
    if options[:env]
      cs << '( '
      options[:env].each do |k,v|
        cs << k.to_s.upcase
        cs << "="
        cs << v.to_s
        cs << ' '
      end
    end
    if options[:user]
      cs << "sudo su #{options[:user]} -c \""
    end
    if options[:run_in_background]
      cs << 'nohup '
    end
    if umask = SSHKit.config.umask
      cs << "umask #{umask} && "
    end
    cs << SSHKit.config.command_map[command.to_sym]
    if args.any?
      cs << ' '
      cs << args.join(' ')
    end
    if options[:run_in_background]
      cs << ' > /dev/null &'
    end
    if options[:user]
      cs << "\""
    end
    if options[:env]
      cs << ' )'
    end
  end
end

#uuidObject



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

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