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
-
#command ⇒ Object
Returns the value of attribute command.
-
#matches ⇒ Object
Returns the value of attribute matches.
-
#nomatches ⇒ Object
Returns the value of attribute nomatches.
-
#sudo ⇒ Object
Returns the value of attribute sudo.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(h) ⇒ Object
def self.descriptor_error(h).
-
#run(node) ⇒ Object
def to_hash.
-
#to_hash ⇒ Object
def initialize(h).
Instance Attribute Details
#command ⇒ Object
Returns the value of attribute command.
178 179 180 |
# File 'lib/blackstack-deployer.rb', line 178 def command @command end |
#matches ⇒ Object
Returns the value of attribute matches.
178 179 180 |
# File 'lib/blackstack-deployer.rb', line 178 def matches @matches end |
#nomatches ⇒ Object
Returns the value of attribute nomatches.
178 179 180 |
# File 'lib/blackstack-deployer.rb', line 178 def nomatches @nomatches end |
#sudo ⇒ Object
Returns the value of attribute sudo.
178 179 180 |
# File 'lib/blackstack-deployer.rb', line 178 def sudo @sudo end |
Class Method Details
.descriptor_errors(c) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 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 |
# File 'lib/blackstack-deployer.rb', line 180 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 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 # errors.uniq end |
Instance Method Details
#initialize(h) ⇒ Object
def self.descriptor_error(h)
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 |
# File 'lib/blackstack-deployer.rb', line 236 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 end |
#run(node) ⇒ Object
def to_hash
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 318 319 320 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 |
# File 'lib/blackstack-deployer.rb', line 278 def run(node) errors = [] code = self.command output = nil # if code is a symbol if code.is_a?(Symbol) # if code is equel than :reboot if code == :reboot # call the node reboot method node.reboot else # look for a routine with this name r = BlackStack::Deployer.routines.select { |r| r.name == code.to_s }.first if !r.nil? r.run(node) else raise "The routine #{code.to_s} does not exist" end end # if code is a string elsif code.is_a?(String) # replacing parameters code.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 code.gsub!(p, node.eth0_ip) elsif p == '%timestamp%' # reserved parameter # TODO: move this to a timestamp function on blackstack-core code.gsub!(p, Time.now.to_s.gsub(/\D/, '')) else if node.parameters.has_key?(p.gsub(/%/, '').to_sym) code.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 end end # running the command output = node.exec(code, self.sudo) # validation: at least one of the matches should happen if self.matches.size > 0 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 => code, :output => output, :errors => errors, } end |
#to_hash ⇒ Object
def initialize(h)
263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/blackstack-deployer.rb', line 263 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 |