Class: Veewee::Config::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/veewee/config/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, config) ⇒ Collection

Returns a new instance of Collection.



14
15
16
17
18
19
20
# File 'lib/veewee/config/collection.rb', line 14

def initialize(type,config)
  @type=type
  @components=Hash.new
  @providers=config.providers
  @config=config
  @env=config.env
end

Instance Attribute Details

#componentsObject

Returns the value of attribute components.



8
9
10
# File 'lib/veewee/config/collection.rb', line 8

def components
  @components
end

#configObject

Returns the value of attribute config.



10
11
12
# File 'lib/veewee/config/collection.rb', line 10

def config
  @config
end

#envObject (readonly)

Returns the value of attribute env.



12
13
14
# File 'lib/veewee/config/collection.rb', line 12

def env
  @env
end

#typeObject

Returns the value of attribute type.



9
10
11
# File 'lib/veewee/config/collection.rb', line 9

def type
  @type
end

Instance Method Details

#define(name) {|component_stub| ... } ⇒ Object

Yields:

  • (component_stub)


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
# File 'lib/veewee/config/collection.rb', line 22

def define(name)
  # We do this for vagrant syntax
  # Depending on type, we create a variable of that type
  # f.i. component_stub.vm or component_stub.lb
  component_stub=OpenStruct.new
  component_stub.send("#{@type}=",::Veewee::Config::Component.new(config))

  env.logger.info("config collection"){ "First pass of reading the config"}
  # Now we can 'execute' the config file using our stub component
  # For guessing the provider type
  yield component_stub

  env.logger.debug("config collection"){ "Stubs collection type #{@type} is loaded"}

  # After processing we extract the component again
  component=component_stub.send("#{@type}")
  provider=@providers[component.provider.to_s]

  env.logger.debug("config collection"){ "We get here"}

  abort "Provider #{component.provider.to_s} does not (yet) exist" if provider.nil?
  real_component=provider.get_component(@type.capitalize,env)
  begin

    # And repeat the same process with a real component
    env.logger.debug("config collection"){ "Component of #{@type}"}

    component_stub.send("#{@type}=",real_component)

    yield component_stub
    # After processing we extract the component again
    component=component_stub.send("#{@type}")
    # And we set the name
    component.name=name

    # We set the provider for this component
    component.provider=provider

    # And register this component with the provider
    # if it is a vm, we add to the hash vms
    # if it is an ip, we add it to the hash ips
    provider_collection=provider.instance_variable_get("@#{@type}s")
    provider_collection[name]=component

    # And we also add it to the global config element
    # So we can have all components of a similar type in one place
    config_collection=@config.instance_variable_get("@#{@type}s")
    config_collection[name]=component

    # Now we can ask the component to validate itself
    #component_stub.validate
    components[name.to_s]=component

  rescue Error => e
    env.ui.error "Error loading component with #{name} of type #{@type} for provider #{component.provider.type}"
  end
end