Module: BlackStack::Deployer::RoutineModule

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



93
94
95
# File 'lib/my-ruby-deployer.rb', line 93

def commands
  @commands
end

#nameObject

Returns the value of attribute name.



93
94
95
# File 'lib/my-ruby-deployer.rb', line 93

def name
  @name
end

Class Method Details

.descriptor_errors(h) ⇒ Object



95
96
97
98
99
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
# File 'lib/my-ruby-deployer.rb', line 95

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)



130
131
132
133
134
135
136
137
138
# File 'lib/my-ruby-deployer.rb', line 130

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, l = nil, params = {}) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/my-ruby-deployer.rb', line 150

def run(node, l=nil, params={})
  l = BlackStack::DummyLogger.new(nil) if l.nil?
  l.logs "Running routine #{self.name.blue} on node #{node.name.blue}... "
  i = 0
  self.commands.each do |c|
    i += 1
    l.logs "Command #{i.to_s.blue}... "
    c.run(node, l, params)
    l.logf 'done'.green
  end
  l.logf 'done'.green
end

#to_hashObject



140
141
142
143
144
145
146
147
148
# File 'lib/my-ruby-deployer.rb', line 140

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