Module: BlackStack::Deployer::CommandModule

Included in:
Command
Defined in:
lib/my-ruby-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

#commandObject

Returns the value of attribute command.



221
222
223
# File 'lib/my-ruby-deployer.rb', line 221

def command
  @command
end

Class Method Details

.descriptor_errors(c) ⇒ Object



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
# File 'lib/my-ruby-deployer.rb', line 223

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)

  # 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
    c[:command].strip.split("\n").each { |l| 
      l.strip!
    }
  end

  # 
  errors.uniq
end

Instance Method Details

#initialize(h) ⇒ Object

def self.descriptor_error(h)



253
254
255
256
257
# File 'lib/my-ruby-deployer.rb', line 253

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

#run(n, l = nil, params = {}) ⇒ Object

running pre-defined commands: :root calling to other routines: :‘change-name’ calling



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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/my-ruby-deployer.rb', line 268

def run(n, l=nil, params={})
  l = BlackStack::DummyLogger.new(nil) if l.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 n reboot method
      n.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(n, l, params)
      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.command.dup

    #l.logs "Replacing merge-tags... "
    s.scan(/%[a-zA-Z0-9\_]+%/).uniq.each do |p|
      #l.logs "Replacing #{p.blue}... "
      if p == '%timestamp%' # reserved parameter
        # TODO: move this to a timestamp function on blackstack-core
        s.gsub!(p, Time.now.to_s.gsub(/\D/, '')) 
      else
        # replace node parameters
        if n.parameters.has_key?(p.gsub(/%/, '').to_sym)
          s.gsub!(p, n.parameters[p.gsub(/%/, '').to_sym].to_s)
        # replace routine-call parameters
        elsif params.has_key?(p.gsub(/%/, '').to_sym)
          s.gsub!(p, params[p.gsub(/%/, '').to_sym].to_s)
        else
          raise "The parameter #{p} does not exist in the node descriptor #{n.parameters.to_s}"
        end
      end
      #l.logf 'done'.green
    end      
    #l.logf 'done'.green

    l.logs "Running command... "
    n.ssh.exec!(s)
    l.logf 'done'.green
  end # elsif code.is_a?(String)
end

#to_hashObject

def initialize(h)



259
260
261
262
263
# File 'lib/my-ruby-deployer.rb', line 259

def to_hash
  h = {}
  h[:command] = self.command
  h
end