Module: Capsize

Included in:
CapsizeEC2, CapsizeSQS
Defined in:
lib/capsize/version.rb,
lib/capsize/capsize.rb,
lib/capsize/ec2_plugin.rb,
lib/capsize/sqs_plugin.rb

Overview

:nodoc:

Defined Under Namespace

Modules: CapsizeEC2, CapsizeSQS, VERSION

Instance Method Summary collapse

Instance Method Details

#get(symbol = nil) ⇒ Object

capsize.get(:symbol_name) checks for variables in several places, with this precedence (from low to high):

  • default capistrano or capsize set variables (available with fetch())

  • Set in :capsize_config_dir/:capsize_config_file_name (overwrites previous)

  • Set in :capsize_config_dir/:capsize_secure_config_file_name (overwrites previous)

  • Passed in as part of the command line params and available as ENV (overwrites previous)

Raises:

  • (Exception)


9
10
11
12
13
14
15
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/capsize/capsize.rb', line 9

def get(symbol=nil)

  raise Exception if symbol.nil? || symbol.class != Symbol # TODO : Jesse: fixup exceptions in capsize

  # populate the OpenStructs with contents of config files so we can query them.
  @capsize_config ||= load_config(:config_file => "#{fetch(:capsize_config_dir)}/#{fetch(:capsize_config_file_name)}")
  @secure_config ||= load_config(:config_file => "#{fetch(:capsize_secure_config_dir)}/#{fetch(:capsize_secure_config_file_name)}")

  # fetch var from default capsize or default capistrano config vars,
  # and if it doesn't exist set it to nil
  set symbol, fetch(symbol, nil)

  # if symbol exists as a var in the secure config, then set it to that
  # overriding default cap or capsize config vars
  if @secure_config.respond_to?(symbol)
    set symbol, @secure_config.send(symbol)
  end

  # if symbol exists as a var in the standard capsize config, then set it to that
  # overriding secure config vars
  if @capsize_config.respond_to?(symbol)
    set symbol, @capsize_config.send(symbol)
  end

  # if ENV["SYMBOL_NAME"] isn't nil set it to ENV["SYMBOL_NAME"]
  # ENV vars passed on the command line override any previously defined vars
  unless ENV[symbol.to_s.upcase].nil?
    set symbol, ENV[symbol.to_s.upcase]
  end

  # Some config values should fall back to reasonable defaults.
  # If we didn't find a value set in any config anywhere, then we should
  # set a reasonable default value for these special cases.  It is better to
  # do this in this one place instead of specifying fallbacks all over the place
  # in the app.  Helps keep it DRY.
  case symbol
  when :group_name
    # the :group_name should fall back to the application name if not provided.
    if fetch(:group_name, nil).nil?
      set symbol, fetch(:application)
    end
  when :key_name
    # the :key_name should fall back to the application name if not provided.
    if fetch(:key_name, nil).nil?
      set symbol, fetch(:application)
    end
  when :group_description
    # the :group_description should fall back to the application name if not provided.
    if fetch(:group_description, nil).nil?
      set symbol, fetch(:application)
    end
  end


  # If we have a good set variable then return that variable, else send back a nil
  # if that's what we get and let the calling method either raise an exception
  # or determine how to gracefully handle it.  We don't want to raise an exception every
  # time a get fails.  nil might be just fine as an answer for some questions.
  return fetch(symbol)

end

#load_config(options = {}) ⇒ Object

load specified “:config_file => ‘foo.yaml’” into a OpenStruct object and return it.

Raises:

  • (Exception)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/capsize/capsize.rb', line 73

def load_config(options = {})
  options = {:config_file => ""}.merge(options)

  raise Exception, "Config file location required" if options[:config_file].nil? || options[:config_file].empty?

  if File.exist?(options[:config_file])

    # try to load the yaml config file
    begin
      config = OpenStruct.new(YAML.load_file(options[:config_file]))
      env_config =  OpenStruct.new(config.send(deploy_env))
    rescue Exception => e
      env_config = nil
    end

    # Send back an empty OpenStruct if we can't load the config file.
    # config files are not required!  Want to avoid method calls on nil
    # if there are no config files to load.
    if env_config.nil?
      return OpenStruct.new
    else
      return env_config
    end

  end
end