Class: Topo::Provision::TopologyGenerator

Inherits:
Object
  • Object
show all
Includes:
TSort
Defined in:
lib/topo/provision/topology_generator.rb

Instance Method Summary collapse

Constructor Details

#initializeTopologyGenerator

Returns a new instance of TopologyGenerator.



29
30
31
# File 'lib/topo/provision/topology_generator.rb', line 29

def initialize
  @dep = {}
end

Instance Method Details

#add(resource, depends_on = [], generators = {}) ⇒ Object

resource - the resource hash depends_on - an array of resource names generators - a hash of lambdas that are generator code to run for that resource

keys are: :deploy :undeploy
will be called as block.call resource


38
39
40
# File 'lib/topo/provision/topology_generator.rb', line 38

def add(resource, depends_on=[], generators={})
 @dep[resource['name']] = [ resource, depends_on, generators ]
end

#generate(action = :deploy) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/topo/provision/topology_generator.rb', line 51

def generate(action=:deploy)
  each_strongly_connected_component { |nodes|
    STDERR.puts "WARN: Topology contains a cyclic dependency: #{nodes.inspect}" if nodes.length > 1
    nodes.each { |node|
      resource, depends_on, gen_data = @dep.fetch node
      resource_generator = gen_data[:resource_generator]
      if resource_generator && resource_generator.respond_to?(action)
        resource_generator.send(action) 
      else
        resource_generator.send("default_action", action) 
     end
    }
  }
end

#reverse_generate(action = :undeploy) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/topo/provision/topology_generator.rb', line 66

def reverse_generate(action=:undeploy)
  strongly_connected_components.reverse_each { |nodes|
    STDERR.puts "WARN: Topology contains a cyclic dependency: #{nodes.inspect}" if nodes.length > 1
    nodes.each { |node|
      resource, depends_on, gen_data = @dep.fetch node
      resource_generator = gen_data[:resource_generator]
      if resource_generator && resource_generator.respond_to?(action)
        resource_generator.send(action) 
      else
        resource_generator.send("default_action", action) 
     end
    }
  }
end

#tsort_each_child(node, &block) ⇒ Object



46
47
48
49
# File 'lib/topo/provision/topology_generator.rb', line 46

def tsort_each_child(node, &block)
  resource, depends_on, gen = @dep.fetch node
  depends_on.each(&block)
end

#tsort_each_node(&block) ⇒ Object



42
43
44
# File 'lib/topo/provision/topology_generator.rb', line 42

def tsort_each_node(&block)
  @dep.each_key(&block)
end