Skip to content

Instantly share code, notes, and snippets.

@roktas
Created May 19, 2014 21:22
Show Gist options
  • Save roktas/39ca548d4f279a1bcedf to your computer and use it in GitHub Desktop.
Save roktas/39ca548d4f279a1bcedf to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
# encoding: utf-8
require 'active_support/core_ext/hash'
require 'securerandom'
require 'yaml'
port ||= ENV['APP_PORT'] || '8080'
envs = %w(production development test)
secret = Hash[
envs.map do |env|
[env, SecureRandom.hex(64)]
end
]
[
{
target: 'config/database.yml',
patch: Hash[
envs.map do |env|
[
env, {
'adapter' => 'mysql2',
'socket' => '/var/run/mysqld/mysqld.sock',
'encoding' => 'utf8',
'database' => env,
'username' => env,
'password' => env,
}
]
end
]
},
{
target: 'config/secrets.yml',
patch: Hash[
envs.map do |env|
[
env, {
'secret_key_base' => secret[env],
}
]
end
]
},
{
target: 'config/config.yml',
patch: Hash[
envs.map do |env|
[
env, {
'url' => "http://localhost:#{port}",
'ip' => '0.0.0.0',
'session_store' => {
'servers' => %w(redis:///tmp/redis.sock)
},
'secret_key_base' => secret[env],
}
]
end
]
},
].each do |hunk|
variants = %w(sample example).map do |ext|
hunk[:target] + '.' + ext
end
src = variants.find do |f|
File.exists? f
end
if src
yaml = YAML.load_file(src).deep_merge! hunk[:patch]
File.open(hunk[:target], 'w') do |f|
f.write(yaml.to_yaml.sub("---\n", ''))
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment