Module: BlackStack::Deployer::CommandModule

Included in:
Command
Defined in:
lib/blackstack-deployer.rb

Overview

define attributes and methods of a routine’s command

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#backgroundObject

Returns the value of attribute background.



193
194
195
# File 'lib/blackstack-deployer.rb', line 193

def background
  @background
end

#commandObject

Returns the value of attribute command.



193
194
195
# File 'lib/blackstack-deployer.rb', line 193

def command
  @command
end

#matchesObject

Returns the value of attribute matches.



193
194
195
# File 'lib/blackstack-deployer.rb', line 193

def matches
  @matches
end

#nomatchesObject

Returns the value of attribute nomatches.



193
194
195
# File 'lib/blackstack-deployer.rb', line 193

def nomatches
  @nomatches
end

#sudoObject

Returns the value of attribute sudo.



193
194
195
# File 'lib/blackstack-deployer.rb', line 193

def sudo
  @sudo
end

Class Method Details

.descriptor_errors(c) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/blackstack-deployer.rb', line 195

def self.descriptor_errors(c)
  errors = []

  # validate: h is a hash
  errors << "The command descriptor is not a hash" unless c.is_a?(Hash)

  # validate: the hash c has a key :command
  errors << "The command descriptor does not have a key :command" unless c.has_key?(:command)

  # validate: the value of c[:command] is a string or symbol
  errors << "The value of c[:command] is not a string and is not a symbol" unless c[:command].is_a?(String) || c[:command].is_a?(Symbol)

  # validate: if the key :sudo exists, then its value is a boolean
  if c.has_key?(:sudo)
    errors << "The value of c[:sudo] is not a boolean" unless c[:sudo].is_a?(TrueClass) || c[:sudo].is_a?(FalseClass)
  end

  # if the parameter h[:name] is a symbol
  if c[:command].is_a?(Symbol)
    if c[:command] == :reboot
      # :reboot is a reserved word, so it is fine to call :reboot
    else
      # validate: existis a routine with a the value c[:command].to_s on its :name key
      errors << "The routine with the name #{c[:command].to_s} does not exist" unless BlackStack::Deployer::routines.select { |r| r.name == c[:command].to_s }.size > 0
    end
  else
    # validate: each line of the :command value must finish with ;
    #errors << "Each line in the :command value must finish with `;`.\nCommand: #{c[:command]}.\nRefer https://github.com/leandrosardi/blackstack-deployer#67-running-commands-in-background for more details." unless c[:command].strip.split("\n").select { |l| l.strip.strip[-1,1] != ';' }.size == 0
    c[:command].strip.split("\n").each { |l| 
      l.strip!
      if l.strip[-1,1] != ';'
        errors << "Each line in the :command value must finish with `;`.\nCommand: #{l}.\nRefer https://github.com/leandrosardi/blackstack-deployer#67-running-commands-in-background for more details."
      end
    }
  end

  # if c[:matches] exists
  if c.has_key?(:matches)
    # validate: the value of c[:matches] must by a regex or an array
    errors << "The value of the key :matches is not a regex nor an array" unless c[:matches].is_a?(Regexp) || c[:matches].is_a?(Array)
    # if c[:matches] is a array
    if c[:matches].is_a?(Array)
      # validate: each element in the the array c[:matches] is a regex
      c[:matches].each do |m|
        errors += BlackStack::Deployer::MatchModule.descriptor_errors(m)
      end # each
    end # if c[:matches].is_a?(Array)
  end # if :matches exists

  # if c[:nomatches] exists
  if c.has_key?(:nomatches)
    # validate: the value of c[:nomatches] must by a regex or an array
    errors << "The value of the key :nomatches is not a regex nor an array" unless c[:nomatches].is_a?(Regexp) || c[:nomatches].is_a?(Array)
    # if c[:nomatches] is a array
    if c[:nomatches].is_a?(Array)
      # validate: each element in the the array c[:nomatches] is a hash
      c[:nomatches].each do |m|
        errors += BlackStack::Deployer::NoMatchModule.descriptor_errors(m)
      end # each
    end # if c[:matches].is_a?(Array)
  end # if :matches exists

  # if c[:background] exists, it must be a boolean
  if c.has_key?(:background)
    errors << "The value of the key :background is not a boolean" unless c[:background].is_a?(TrueClass) || c[:background].is_a?(FalseClass)
  end

  # 
  errors.uniq
end

Instance Method Details

#code(node) ⇒ Object

return the code to exectute the command, after applying modifications requested by some parameters like ‘:show_outut` or `:background`.



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/blackstack-deployer.rb', line 321

def code(node)
  ret = self.command
#puts
#puts
#puts "self.command: #{self.command}"
  # replacing parameters
  ret.scan(/%[a-zA-Z0-9\_]+%/).each do |p|
    if p == '%eth0_ip%' # reserved parameter
      # TODO: move the method eth0_ip to the blackstack-nodes library
      ret.gsub!(p, node.eth0_ip) 
    elsif p == '%timestamp%' # reserved parameter
      # TODO: move this to a timestamp function on blackstack-core
      ret.gsub!(p, Time.now.to_s.gsub(/\D/, '')) 
    else
#puts
#puts
#puts "p: #{p}"
      if node.parameters.has_key?(p.gsub(/%/, '').to_sym)
        ret.gsub!(p, node.parameters[p.gsub(/%/, '').to_sym].to_s)
      else
        raise "The parameter #{p} does not exist in the node descriptor #{node.parameters.to_s}"
      end
#puts
#puts
#puts "ret: #{ret}"
    end
  end
  # if the command is configured to run in background, then modify the ret to run in background.
  # note: even if you the flag show_ouput is on, you won't see any error message.
#puts
#puts "self.background: #{self.background.to_s}"
  if self.background #&& !BlackStack::Deployer.show_output
    lines = ret.strip.lines
    total = lines.size
    i = 0
    lines.each { |l| 
      i += 1
      if i == total
        l.gsub!(/;$/, ' > /dev/null 2>&1 &')
      else
        l.gsub!(/;$/, ' > /dev/null 2>&1;')
      end
    }
    ret = lines.join("\n")
  end
  # apply modifications due the sudo flag
  # return the code
  ret = node.code(ret, self.sudo)
#puts
#puts
#puts 'lalala'
#puts ret
=begin
  if self.sudo
    if node.using_password?
      ret = "echo '#{node.ssh_password.gsub(/'/, "\\\\'")}' | sudo -S su root -c '#{ret}'"
    elsif node.using_private_key_file?
      ret = "sudo -S su root -c '#{ret}'"
    end
  end  
=end
  # return
  ret
end

#initialize(h) ⇒ Object

def self.descriptor_error(h)



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/blackstack-deployer.rb', line 266

def initialize(h)
  errors = BlackStack::Deployer::CommandModule.descriptor_errors(h)
  raise "The node descriptor is not valid: #{errors.uniq.join(".\n")}" if errors.length > 0
  self.command = h[:command]
  self.sudo = h[:sudo].nil? ? true : h[:sudo]
  self.matches = []
  self.nomatches = []
  if h.has_key?(:matches)
    if h[:matches].is_a?(Regexp)
      self.matches << BlackStack::Deployer::Match.new(h[:matches])
    else
      h[:matches].each do |m|
        self.matches << BlackStack::Deployer::Match.new(m)
      end
    end
  end
  if h.has_key?(:nomatches)
    if h[:nomatches].is_a?(Regexp)
      self.nomatches << BlackStack::Deployer::NoMatch.new(h[:nomatches])
    else
      h[:nomatches].each do |m|
        self.nomatches << BlackStack::Deployer::NoMatch.new(m)
      end
    end
  end
#puts
#puts
#puts "command: #{self.command}"
#puts "h[:background] = #{h[:background]}"
  if h.has_key?(:background)
    self.background = h[:background]
  else
    self.background = false
  end
#puts "self.background = #{self.background}"
end

#run(node) ⇒ Object



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/blackstack-deployer.rb', line 386

def run(node)
  l = BlackStack::Deployer.logger
  errors = []
  output = nil

  # if self.command is a symbol
  if self.command.is_a?(Symbol)

    # if self.command is equel than :reboot
    if self.command == :reboot
      # call the node reboot method
      node.reboot
    else
      # look for a routine with this name
      r = BlackStack::Deployer.routines.select { |r| r.name == self.command.to_s }.first
      if !r.nil?
        r.run(node)
      else
        raise "The routine #{self.command.to_s} does not exist"
      end
    end

  # if self.command is a string
  elsif self.command.is_a?(String)
    s = self.code(node)
    # running the command
    l.logs "Show command output... " if BlackStack::Deployer.show_output
    l.log "\n\nCommand:\n--------\n\n#{s} " if BlackStack::Deployer.show_output
    output = node.ssh.exec!(s) 
    l.log "\n\nOutput:\n-------\n\n#{output}" if BlackStack::Deployer.show_output
    l.logf('done tracing.') if BlackStack::Deployer.show_output

    # validation: at least one of the matches should happen
    if self.matches.size > 0 && !self.background
      i = 0
      self.matches.each do |m|
        if m.validate(output).size == 0 # no errors 
          i += 1
        end
      end
      errors << "Command output doesn't match with any of the :matches" if i == 0
    end # if self.matches.size > 0 
    
    # validation: no one of the nomatches should happen
    self.nomatches.each do |m|
      errors += m.validate(output)
    end
  end # elsif code.is_a?(String)

  # return a hash descriptor of the command result 
  {
    :command => self.command,
    :code => s,
    :output => output,
    :errors => errors,
  }
end

#to_hashObject

def initialize(h)



303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/blackstack-deployer.rb', line 303

def to_hash
  h = {}
  h[:command] = self.command
  h[:sudo] = self.sudo
  h[:matches] = []
  h[:nomatches] = []
  self.matches.each do |m|
    h[:matches] << m.to_hash
  end
  self.nomatches.each do |m|
    h[:nomatches] << m.to_hash
  end
  h
end