Module: BlackStack::Deployer::RoutineModule

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

Overview

define attributes and methods of a deployer routine

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



98
99
100
# File 'lib/blackstack-deployer.rb', line 98

def commands
  @commands
end

#nameObject

Returns the value of attribute name.



98
99
100
# File 'lib/blackstack-deployer.rb', line 98

def name
  @name
end

Class Method Details

.descriptor_errors(h) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/blackstack-deployer.rb', line 100

def self.descriptor_errors(h)
  errors = []

  # validate: the parameter h is a hash
  errors << "The parameter h is not a hash" unless h.is_a?(Hash)
  
  # validate: the paramerer h has a key :name
  errors << "The parameter h does not have a key :name" unless h.has_key?(:name)
  
  # validate: the paramerer h has a key :command
  errors << "The parameter h does not have a key :commands" unless h.has_key?(:commands)
  
  # validate: the parameter h[:name] is a string or a symbol
  errors << "The parameter h[:name] is not a string" unless h[:name].is_a?(String)
  
  # validate: the parameter h[:name] is not 'reboot' because it is a reserved name
  errors << "The parameter h[:name] is a reserved name (#{h[:name].to_s})" if h[:name] == 'reboot'
  
  # validate: the parameter h[:commands] is required
  errors << "The parameter h[:commands] is required" if h[:commands].nil?

  # validate: the parametrer h[:commands] is an array
  errors << "The parameter h[:commands] is not an array" unless h[:commands].is_a?(Array)
  
  # validate: the parameter h[:commands] has at least one element
  errors << "The parameter h[:commands] does not have at least one element" unless h[:commands].size > 0
  
  # validate: each element of the array h[:commands] is a hash
  h[:commands].each do |c|
    errors += BlackStack::Deployer::CommandModule.descriptor_errors(c)
  end # h[:commands].each do |c|
    
  errors.uniq
end

Instance Method Details

#initialize(h) ⇒ Object

def self.descriptor_error(h)



135
136
137
138
139
140
141
142
143
# File 'lib/blackstack-deployer.rb', line 135

def initialize(h)
  errors = BlackStack::Deployer::RoutineModule.descriptor_errors(h)
  raise "The node descriptor is not valid: #{errors.uniq.join(".\n")}" if errors.length > 0
  self.name = h[:name]
  self.commands = []
  h[:commands].each do |c|
    self.commands << BlackStack::Deployer::Command.new(c)
  end
end

#run(node) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/blackstack-deployer.rb', line 155

def run(node)
  ret = []
  self.commands.each do |c|
    BlackStack::Deployer.logger.logs "Running command: #{c.command.to_s}... "
    h = c.run(node)
    ret << h

    #BlackStack::Deployer.logger.logs "Result: "
    #BlackStack::Deployer.logger.logf h.to_s

    if h[:errors].size == 0
      BlackStack::Deployer.logger.done
    else
      BlackStack::Deployer.logger.logf('error: ' + h.to_s)
      raise "Error running command: #{h.to_s}"
    end
  end
  ret
end

#to_hashObject



145
146
147
148
149
150
151
152
153
# File 'lib/blackstack-deployer.rb', line 145

def to_hash
  h = {}
  h[:name] = self.name
  h[:commands] = []
  self.commands.each do |c|
    h[:commands] << c.to_hash
  end
  h
end