Class: Mccloud::Config::Provider

Inherits:
Object
  • Object
show all
Defined in:
lib/mccloud/config/provider.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Provider

Returns a new instance of Provider.



11
12
13
14
# File 'lib/mccloud/config/provider.rb', line 11

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

Instance Attribute Details

#componentsObject

Returns the value of attribute components.



8
9
10
# File 'lib/mccloud/config/provider.rb', line 8

def components
  @components
end

#envObject (readonly)

Returns the value of attribute env.



9
10
11
# File 'lib/mccloud/config/provider.rb', line 9

def env
  @env
end

Instance Method Details

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

Yields:

  • (provider_stub)


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
# File 'lib/mccloud/config/provider.rb', line 16

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
  provider_stub=OpenStruct.new
  provider_stub.provider=OpenStruct.new

  env.logger.debug("config provider"){ "Start stubbing provider"}

  # Now we can 'execute' the config file using our stub component
  # For guessing the provider type
  yield provider_stub

  env.logger.debug("config provider"){ "End stubbing provider"}

  # After processing we extract the provider type and options again
  provider_type=provider_stub.provider.flavor
  env.logger.debug("config provider"){ "Found provider of type #{provider_type}"}
  provider_options=provider_stub.provider.options

  begin
    # Now that we know the actual provider, we can check if the provider has this type of component
    require_path='mccloud/provider/'+provider_type.to_s.downcase+"/provider"
      require require_path

    # Now we can create the real provider
    real_provider=Object.const_get("Mccloud").const_get("Provider").const_get(provider_type.to_s.capitalize).const_get("Provider").new(name,provider_options,env)
    provider_stub.provider=real_provider
    yield provider_stub

    env.logger.debug("config provider"){ "Instantiating provider #{name.to_s}"}
    components[name.to_s]=provider_stub.provider
  rescue ::Mccloud::Error => ex
    raise ::Mccloud::Error, "Error loading provider with #{name}\n#{ex}"
  end
end