Class: AWSCarb::Config

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/aws-carb/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/aws-carb/config.rb', line 7

def config
  @config
end

Instance Method Details

#[](key) ⇒ Object



96
97
98
# File 'lib/aws-carb/config.rb', line 96

def [](key)
  @config[key]
end

#check_route53_settingsObject



20
21
22
23
# File 'lib/aws-carb/config.rb', line 20

def check_route53_settings
  die 'route53: no zone id specified!' if @config[:route53][:zone].nil?
  die 'route53: no ttl specified!'     if @config[:route53][:zone].nil?
end

#create(cli_arguments) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/aws-carb/config.rb', line 9

def create(cli_arguments)

  @config = load_file(cli_arguments.global.config_file)

  merge_cli_arguments_with_config(cli_arguments)

  establish_hostname_and_domain

  check_route53_settings
end

#displayObject



160
161
162
163
164
# File 'lib/aws-carb/config.rb', line 160

def display
  puts "# config:"
  ap @config
  puts
end

#establish_hostname_and_domainObject

try and work out the hostname, presidence is:

  • config file

  • user_data_template_variables cli args

note: raw user_data is not checked (not to be confused with user_data_template or user_data_template_variables..)



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
# File 'lib/aws-carb/config.rb', line 107

def establish_hostname_and_domain
  ShellSpinner "# checking whether hostname and domain have been set", false do

    hostname, domain = nil

    @config[:route53][:hostname] = find_with_context(:hostname, :user_data_template_variables) if find_with_context(:hostname, :user_data_template_variables)
    @config[:route53][:domain]   = find_with_context(:domain,   :user_data_template_variables) if find_with_context(:domain,   :user_data_template_variables)
    @config[:route53][:hostname] = find_with_context(:hostname, :route53)                      if find_with_context(:hostname, :route53)
    @config[:route53][:domain]   = find_with_context(:domain, :route53)                        if find_with_context(:domain, :route53)

    help = <<-HEREDOC.strip_heredoc
      #       
      #         checked:
      #          'common', 'user_data_template_variables',
      #          and 'route53' sections of config
      #          --common-variables, --route53-variables,
      #          and --user-data-template-variables
      #
      #          route53 dynamic DNS will not be updated!
    HEREDOC

    domain   = @config[:route53][:domain]
    hostname = @config[:route53][:hostname]

    if domain.nil? and hostname.nil?
      debug "# WARNING: hostname and domain not found"
      debug help
      debug
    elsif domain and hostname.nil?
      debug "# WARNING: hostname not found"
      debug help
      debug
    elsif domain.nil? and hostname
      debug "# WARNING: domain not found"
      debug help
      debug
    else
      debug "# found hostname and domain:"
      debug "hostname: #{hostname}"
      debug "domain:   #{domain}"
      debug

      @config[:route53][:new_dns_records] = {
        :public  => { :alias => "#{hostname}.#{domain}.",         :target => nil },
        :private => { :alias => "#{hostname}-private.#{domain}.", :target => nil }
      }
    end
  end

  puts
end

#find_with_context(key, context) ⇒ Object

when looking for a key, check ‘common’ section first, then override if a value in the supplied context is found..



90
91
92
93
94
# File 'lib/aws-carb/config.rb', line 90

def find_with_context(key, context) 
  return @config[context][key] if @config[context][key]
  return @config[:common][key] if @config[:common][key]
  return nil
end

#load_file(cli_argument_config_file) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/aws-carb/config.rb', line 72

def load_file(cli_argument_config_file)

  # allow forcing of no config file..
  return if cli_argument_config_file.empty?

  config_file = cli_argument_config_file

  begin
    # make keys symbols so we can more easily merge with cli arg structs..
    @config = YAML.load_file(config_file).deep_symbolize_keys
  rescue => e
    puts "# failed to load config file: '#{config_file}'"
    die e
  end
end

#merge_cli_arguments_with_config(cli_arguments) ⇒ Object



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
# File 'lib/aws-carb/config.rb', line 25

def merge_cli_arguments_with_config(cli_arguments)
  begin

    config_sections = [:common, :general, :ec2, :route53, :user_data_template_variables, :user_data_template]

    # special condition: common command line arguments are shared between all instances first..
    if cli_arguments.subcommand.config_overrides.common_variables
      @config[:common] ||= {}
      @config[:common].merge! cli_arguments.subcommand.config_overrides.common_variables
    end

    # all sections share 'common' variables..
    config_sections.each do |section|
      @config[section] ||= {}
      @config[section].merge! @config[:common]
    end

    # merge the config overrides hashes into config
    if cli_arguments.subcommand.config_overrides
      cli_arguments.subcommand.config_overrides.marshal_dump.each do |key, value|

        next if key == :common

        # key differs from command line argument - we lose the _variables suffix
        config_key = key.to_s.gsub('_variables', '').to_sym

        @config[config_key] ||= {}
        @config[config_key].merge! cli_arguments.subcommand.config_overrides.send(key)
      end
    end

    # merge the convenience arguments..
    config_sections.each do |section|
      if cli_arguments.subcommand.send(section.to_s)
        @config[section].merge! cli_arguments.subcommand.send(section.to_s).marshal_dump
      end
    end

    # merge the convenience argument parameters with config
    @config.deep_symbolize_keys!

  rescue => e
    puts "# failed to merge cli arguments with config"
    die e
  end
end