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.



166
167
168
# File 'lib/my-ruby-deployer.rb', line 166

def command
  @command
end

Class Method Details

.descriptor_errors(c) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/my-ruby-deployer.rb', line 168

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)



198
199
200
201
202
# File 'lib/my-ruby-deployer.rb', line 198

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



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

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)



204
205
206
207
208
# File 'lib/my-ruby-deployer.rb', line 204

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