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)


21
22
23
24
25
26
27
28
29
# File 'lib/sshkit/command.rb', line 21

def initialize(*args)
  raise ArgumentError, "Must pass 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, @full_stdout, @full_stderr = String.new, String.new, String.new, String.new
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



12
13
14
# File 'lib/sshkit/command.rb', line 12

def args
  @args
end

#commandObject (readonly)

Returns the value of attribute command.



12
13
14
# File 'lib/sshkit/command.rb', line 12

def command
  @command
end

#exit_statusObject

Returns the value of attribute exit_status.



12
13
14
# File 'lib/sshkit/command.rb', line 12

def exit_status
  @exit_status
end

#full_stderrObject (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_stdoutObject (readonly)

Returns the value of attribute full_stdout.



12
13
14
# File 'lib/sshkit/command.rb', line 12

def full_stdout
  @full_stdout
end

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/sshkit/command.rb', line 12

def options
  @options
end

#startedObject

Returns the value of attribute started.



12
13
14
# File 'lib/sshkit/command.rb', line 12

def started
  @started
end

#started_atObject (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?

Returns:

  • (Boolean)


31
32
33
# File 'lib/sshkit/command.rb', line 31

def complete?
  !exit_status.nil?
end

#environment_hashObject



152
153
154
# File 'lib/sshkit/command.rb', line 152

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

#environment_stringObject



156
157
158
159
160
161
162
# File 'lib/sshkit/command.rb', line 156

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?

Returns:

  • (Boolean)


54
55
56
# File 'lib/sshkit/command.rb', line 54

def failure?
  exit_status.to_i > 0
end

#group(&_block) ⇒ Object



184
185
186
187
188
189
# File 'lib/sshkit/command.rb', line 184

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



128
129
130
# File 'lib/sshkit/command.rb', line 128

def host
  options[:host]
end

#in_background(&_block) ⇒ Object



174
175
176
177
# File 'lib/sshkit/command.rb', line 174

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

#on_stderr(channel, data) ⇒ Object



85
86
87
88
89
# File 'lib/sshkit/command.rb', line 85

def on_stderr(channel, data)
  @stderr = data
  @full_stderr += data
  call_interaction_handler(:stderr, data, channel)
end

#on_stdout(channel, data) ⇒ Object



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

def on_stdout(channel, data)
  @stdout = data
  @full_stdout += data
  call_interaction_handler(:stdout, data, channel)
end

#runtimeObject



104
105
106
107
# File 'lib/sshkit/command.rb', line 104

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

#should_map?Boolean

Returns:

  • (Boolean)


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

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

#started?Boolean

Returns:

  • (Boolean)


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

def started?
  started
end

#stderrObject



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

def stderr
  log_reader_deprecation('stderr')
  @stderr
end

#stderr=(new_value) ⇒ Object



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

def stderr=(new_value)
  log_writer_deprecation('stderr')
  @stderr = new_value
end

#stdoutObject



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

def stdout
  log_reader_deprecation('stdout')
  @stdout
end

#stdout=(new_value) ⇒ Object



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

def stdout=(new_value)
  log_writer_deprecation('stdout')
  @stdout = new_value
end

#success?Boolean Also known as: successful?

Returns:

  • (Boolean)


49
50
51
# File 'lib/sshkit/command.rb', line 49

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

#to_commandObject



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

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/sshkit/command.rb', line 109

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



208
209
210
211
212
213
214
# File 'lib/sshkit/command.rb', line 208

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



179
180
181
182
# File 'lib/sshkit/command.rb', line 179

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

#user(&_block) ⇒ Object



169
170
171
172
# File 'lib/sshkit/command.rb', line 169

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



45
46
47
# File 'lib/sshkit/command.rb', line 45

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

#verbosityObject



132
133
134
135
136
137
138
139
140
141
# File 'lib/sshkit/command.rb', line 132

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



164
165
166
167
# File 'lib/sshkit/command.rb', line 164

def with(&_block)
  return yield unless environment_hash.any?
  sprintf("( export #{environment_string} ; %s )", yield)
end

#within(&_block) ⇒ Object



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

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