Class: EY::Config

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

Defined Under Namespace

Classes: ConfigurationError, EnvironmentConfig

Constant Summary collapse

CONFIG_FILES =
["config/ey.yml", "ey.yml"].map {|path| Pathname.new(path)}.freeze
EY_YML_HINTS =
"# ey.yml supports many deploy configuration options when committed in an\n# application's repository.\n#\n# Valid locations: REPO_ROOT/ey.yml or REPO_ROOT/config/ey.yml.\n#\n# Examples options (defaults apply to all environments for this application):\n#\n# defaults:\n#   migrate: true                           # Default --migrate choice for ey deploy\n#   migration_command: 'rake migrate'       # Default migrate command to run when migrations are enabled\n#   branch: default_deploy_branch           # Branch/ref to be deployed by default during ey deploy\n#   bundle_without: development test        # The string to pass to bundle install --without ''\n#   maintenance_on_migrate: true            # Enable maintenance page during migrate action (use with caution) (default: true)\n#   maintenance_on_restart: false           # Enable maintanence page during every deploy (default: false for unicorn & passenger)\n#   ignore_database_adapter_warning: false  # Hide the warning shown when the Gemfile does not contain a recognized database adapter (mongodb for example)\n#   your_own_custom_key: 'any attribute you put in ey.yml is available in deploy hooks'\n# environments:\n#   YOUR_ENVIRONMENT_NAME: # All options pertain only to the named environment\n#     any_option: 'override any of the options above with specific options for certain environments'\n#     migrate: false\n#\n# Further information available here:\n# https://support.cloud.engineyard.com/entries/20996661-customize-your-deployment-on-engine-yard-cloud\n#\n# NOTE: Please commit this file into your git repository.\n#\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = nil) ⇒ Config



12
13
14
15
16
# File 'lib/engineyard/config.rb', line 12

def initialize(file = nil)
  @path = file ? Pathname.new(file) : CONFIG_FILES.find{|pathname| pathname.exist? }
  @config = (@path ? YAML.load_file(@path.to_s) : {}) || {} # load_file returns `false' when the file is empty
  @config["environments"] ||= {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/engineyard/config.rb', line 18

def method_missing(meth, *args, &blk)
  key = meth.to_s.downcase
  if @config.key?(key)
    @config[key]
  else
    super
  end
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Instance Method Details

#[](key) ⇒ Object



40
41
42
# File 'lib/engineyard/config.rb', line 40

def [](key)
  @config[key.to_s.downcase]
end

#default_endpointObject



52
53
54
# File 'lib/engineyard/config.rb', line 52

def default_endpoint
  "https://cloud.engineyard.com/"
end

#default_endpoint?Boolean



56
57
58
# File 'lib/engineyard/config.rb', line 56

def default_endpoint?
  default_endpoint == endpoint
end

#default_environmentObject



60
61
62
63
64
65
# File 'lib/engineyard/config.rb', line 60

def default_environment
  d = environments.find do |name, env|
    env["default"]
  end
  d && d.first
end

#defaultsObject



67
68
69
# File 'lib/engineyard/config.rb', line 67

def defaults
  @config['defaults'] ||= {}
end

#endpointObject



44
45
46
# File 'lib/engineyard/config.rb', line 44

def endpoint
  env_var_endpoint || default_endpoint
end

#ensure_pathObject



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/engineyard/config.rb', line 133

def ensure_path
  return if @path && @path.exist?
  unless EY::Repo.exist?
    raise "Not in application directory. Unable to save configuration."
  end
  if Pathname.new('config').exist?
    @path = Pathname.new('config/ey.yml')
  else
    @path = Pathname.new('ey.yml')
  end
  @path
end

#env_var_endpointObject



48
49
50
# File 'lib/engineyard/config.rb', line 48

def env_var_endpoint
  ENV["CLOUD_URL"]
end

#environment_config(environment_name) ⇒ Object



71
72
73
74
# File 'lib/engineyard/config.rb', line 71

def environment_config(environment_name)
  environments[environment_name] ||= {}
  EnvironmentConfig.new(environments[environment_name], environment_name, self)
end

#ey_yml_commentsObject



125
126
127
128
129
130
131
# File 'lib/engineyard/config.rb', line 125

def ey_yml_comments
  if @path.exist?
    existing = @path.readlines.grep(/^#/).map {|line| line.strip }.join("\n")
  else
    EY_YML_HINTS
  end
end

#fetch(key, default = nil, &block) ⇒ Object



32
33
34
# File 'lib/engineyard/config.rb', line 32

def fetch(key, default = nil, &block)
  block ? @config.fetch(key.to_s, &block) : @config.fetch(key.to_s, default)
end

#fetch_from_defaults(key, default = nil, &block) ⇒ Object



36
37
38
# File 'lib/engineyard/config.rb', line 36

def fetch_from_defaults(key, default=nil, &block)
  block ? defaults.fetch(key.to_s, &block) : defaults.fetch(key.to_s, default)
end

#respond_to?(meth) ⇒ Boolean



27
28
29
30
# File 'lib/engineyard/config.rb', line 27

def respond_to?(meth)
  key = meth.to_s.downcase
  @config.key?(key) || super
end

#set_default_option(key, value) ⇒ Object



91
92
93
94
# File 'lib/engineyard/config.rb', line 91

def set_default_option(key, value)
  defaults[key] = value
  write_ey_yaml
end

#set_environment_option(environment_name, key, value) ⇒ Object



76
77
78
79
80
# File 'lib/engineyard/config.rb', line 76

def set_environment_option(environment_name, key, value)
  environments[environment_name] ||= {}
  environments[environment_name][key] = value
  write_ey_yaml
end

#write_ey_yamlObject



82
83
84
85
86
87
88
89
# File 'lib/engineyard/config.rb', line 82

def write_ey_yaml
  ensure_path
  comments = ey_yml_comments
  @path.open('w') do |f|
    f.puts comments
    f.puts YAML.dump(@config)
  end
end