Class: StackBuilder::Stack::Stack
- Inherits:
-
Object
- Object
- StackBuilder::Stack::Stack
- Defined in:
- lib/stackbuilder/stack/stack.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
Instance Method Summary collapse
- #destroy ⇒ Object
-
#initialize(provider, stack_file, id = nil, overrides = nil) ⇒ Stack
constructor
A new instance of Stack.
- #orchestrate(events = nil, name = nil, scale = nil) ⇒ Object
- #scale(name, scale) ⇒ Object
Constructor Details
#initialize(provider, stack_file, id = nil, overrides = nil) ⇒ Stack
Returns a new instance of Stack.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/stackbuilder/stack/stack.rb', line 13 def initialize(provider, stack_file, id = nil, overrides = nil) StackBuilder::Common::Config.set_silent @logger = StackBuilder::Common::Config.logger raise InvalidArgs, "Node provider is not derived from StackBuilder::Stack::NodeProvider." unless provider.is_a?(NodeProvider) @provider = provider env_vars = provider.get_env_vars stack = StackBuilder::Common.load_yaml(stack_file, env_vars) merge_maps( stack, overrides.end_with?('.json') ? JSON.load(File.new(overrides, 'r')) : JSON.load(overrides) ) \ unless overrides.nil? @logger.debug("Initializing stack definition:\n #{stack.to_yaml}") if id.nil? @id = SecureRandom.uuid.gsub(/-/, '') @provider.set_stack(stack, @id) else @id = id @provider.set_stack(stack, @id) end @name = stack["name"] @nodes = { } if stack.has_key?("stack") && stack["stack"].is_a?(Array) stack["stack"].each do |n| raise ArgumentError, "Node does not have a 'node' attribute " + "that identifies it: #{n}" unless n.has_key?("node") node_id = n["node"] raise ArgumentError, "Node identified by \"#{node_id}\" " + "already exists." if @nodes.has_key? (node_id) n["attributes"] = { } if n["attributes"].nil? merge_maps(n["attributes"], stack["attributes"]) unless stack["attributes"].nil? node_manager = @provider.get_node_manager(n) raise InvalidArgs, "Node task is of an invalid type. It is not derived " + "from StackBuilder::Stack::Node." unless node_manager.is_a?(NodeManager) @nodes[node_id] = NodeTask.new(node_manager, @nodes, n, id) end # Associate dependencies stack["stack"].each do |n| node_task = @nodes[n["node"]] if n.has_key?("depends_on") && n["depends_on"].is_a?(Array) n["depends_on"].each do |d| raise ArgumentError, "Dependency node \"#{d}\" " + "is not defined." unless @nodes.has_key?(d) node_task.add_dependency(d) end end if n.has_key?("targets") && n["targets"].is_a?(Array) n["targets"].each do |d| raise ArgumentError, "Target node \"#{d}\" " + "is not defined." unless @nodes.has_key?(d) node_task.add_dependency(d, true) end end node_task.process_attribute_dependencies end else raise ArgumentError, "System needs to have at least one node defined." end end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
9 10 11 |
# File 'lib/stackbuilder/stack/stack.rb', line 9 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/stackbuilder/stack/stack.rb', line 10 def name @name end |
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
11 12 13 |
# File 'lib/stackbuilder/stack/stack.rb', line 11 def nodes @nodes end |
Instance Method Details
#destroy ⇒ Object
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 |
# File 'lib/stackbuilder/stack/stack.rb', line 201 def destroy @nodes.values.each { |n| n.deleted = true } begin destroy_events = Set.new([ "stop", "uninstall" ]) self.orchestrate(destroy_events) rescue Exception => msg @logger.warn("An error was encountered attempting to do an orderly tear down of the system: #{msg}") @logger.info("All remaining nodes will be destroyed forcefully.") end threads = [ ] @nodes.values.each do |n| (n.manager.get_scale - 1).downto(0) do |i| threads << Thread.new { @logger.debug("Deleted #{n} #{i}.") $stdout.printf("Deleting node \"%s\" #%d.\n", n.name, i) unless @logger.debug? n.manager.delete(i) } end end threads.each { |t| t.join } end |
#orchestrate(events = nil, name = nil, scale = nil) ⇒ Object
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 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 |
# File 'lib/stackbuilder/stack/stack.rb', line 98 def orchestrate(events = nil, name = nil, scale = nil) events = Set.new([ "configure" ]) if events.nil? unless name.nil? node = @nodes[name] raise StackBuilder::Common::StackBuilderError, "Invalid node name \"#{name}'\"." if node.nil? unless scale.nil? raise ArgumentError, \ "The scale for node \"#{node.name}\" must be greater than 0." \ if scale < 1 raise ArgumentError, \ "The scale for node \"#{node.name}\" cannot be greater than #{node.max_scale}." \ if scale > node.max_scale node.scale = scale end end prep_threads = [ ] execution_list = [ ] if name.nil? @nodes.each_value do |n| execution_list << n if n.init_dependency_count == 0 prep_threads += n.prepare end task_count = @nodes.size else # Only process nodes that depend on 'name' and their dependencies def add_parent_task(node, prep_threads, nodes_visited) prep_threads += node.prepare nodes_visited << node.name node.init_dependency_count(1) node.parent_nodes.each do |n| add_parent_task(n, prep_threads, nodes_visited) end end node = @nodes[name] nodes_visited = Set.new([ node.name ]) execution_list << node prep_threads += node.prepare node.parent_nodes.each do |n| add_parent_task(n, prep_threads, nodes_visited) end task_count = nodes_visited.size end execution_count = 0 terminate = false while !terminate && !execution_list.empty? do mutex = Mutex.new new_execution_list = [ ] exec_threads = [ ] execution_list.each do |n| exec_threads << Thread.new { begin executable_parents = n.orchestrate(events) mutex.synchronize { new_execution_list |= executable_parents execution_count += 1 } rescue Exception => msg @logger.error("Processing node '#{n}' terminated with an exception: #{msg}") @logger.info(msg.backtrace.join("\n\t")) terminate = true end } end exec_threads.each { |t| t.join } execution_list = new_execution_list end prep_threads.each { |t| t.join } @nodes.each_value do |n| n.prev_scale = n.scale end raise StackBuilder::Common::StackBuilderError, "Processing of stack nodes " + "did not complete because of errors." if execution_count < task_count end |
#scale(name, scale) ⇒ Object
197 198 199 |
# File 'lib/stackbuilder/stack/stack.rb', line 197 def scale(name, scale) self.orchestrate(nil, name, scale) end |