Class: Stemcell::MetadataSource

Inherits:
Object
  • Object
show all
Defined in:
lib/stemcell/metadata_source.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  'chef_environment' => 'production',
  'git_branch'       => 'production',
  'count'            => 1,
  'instance_hostname' => '',
  'instance_domain_name' => '',
}
METADATA_ATTRIBUTES =

Search for instance metadata in the following role attributes, with priority given to the keys at the head.

[
  :instance_metadata,
  :stemcell
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chef_root) ⇒ MetadataSource

Returns a new instance of MetadataSource.

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
# File 'lib/stemcell/metadata_source.rb', line 24

def initialize(chef_root)
  @chef_root = chef_root

  raise ArgumentError, "You must specify a chef root" unless chef_root

  template_options = read_template
  @default_options = DEFAULT_OPTIONS.merge(template_options['defaults'])

  @all_backing_store_options = template_options['backing_store']
  @all_azs_by_region = template_options['availability_zones']
end

Instance Attribute Details

#chef_root ⇒ Object (readonly)

Returns the value of attribute chef_root.



6
7
8
# File 'lib/stemcell/metadata_source.rb', line 6

def chef_root
  @chef_root
end

#default_options ⇒ Object (readonly)

Returns the value of attribute default_options.



7
8
9
# File 'lib/stemcell/metadata_source.rb', line 7

def default_options
  @default_options
end

Instance Method Details

#expand_role(role, environment, override_options = {}, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


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
# File 'lib/stemcell/metadata_source.rb', line 36

def expand_role(role, environment, override_options={}, options={})
  raise ArgumentError, "Missing chef role" unless role
  raise ArgumentError, "Missing chef environment" unless environment
  allow_empty_roles = options.fetch(:allow_empty_roles, false)

  role_options = expand_role_options(role, environment)
  role_empty   = role_options.nil? || role_options.empty?

  raise EmptyRoleError if !allow_empty_roles && role_empty

  backing_store_options =
    expand_backing_store_options(
        default_options,
        role_options,
        override_options
      )

  # Merge all the options together in priority order
  merged_options = default_options.dup
  merged_options.deep_merge!(backing_store_options)
  merged_options.deep_merge!(role_options) if role_options
  merged_options.deep_merge!(override_options)

  # Add the AZ if not specified
  if (region = merged_options['region'])
    merged_options['availability_zone'] ||= random_az_in_region(region)
  end

  # The chef environment and role used to expand the runlist takes
  # priority over all other options.
  merged_options['chef_environment'] = environment
  merged_options['chef_role']        = role

  merged_options
end