Skip to content

Instantly share code, notes, and snippets.

@carlcraig
Last active January 18, 2016 09:52
Show Gist options
  • Save carlcraig/377ed30bfa2015ea5049 to your computer and use it in GitHub Desktop.
Save carlcraig/377ed30bfa2015ea5049 to your computer and use it in GitHub Desktop.
Capistrano Symfony Parameters
desc "create empty parameters"
task :parameters_empty do
on roles(:all) do
if not test("[ -f #{shared_path}/app/config/parameters.yml ]")
upload! StringIO.new('---'), "#{shared_path}/app/config/parameters.yml"
end
end
end
desc "configure parameters with dist values if not defined"
task :parameters do
on roles(:all) do
distParameters = YAML::load(capture("cat #{release_path}/app/config/parameters.yml.dist"))
if test("[ -f #{shared_path}/app/config/parameters.yml ]")
parameters = YAML::load(capture("cat #{shared_path}/app/config/parameters.yml"))
if parameters
distParameters['parameters'].each do |k,v|
if parameters['parameters'][k].nil? && parameters['parameters'].fetch(k, false) == false
set :distParameter, ask(k, v)
parameters['parameters'][k] = fetch(:distParameter)
end
end
else
parameters = YAML::load(capture("cat #{release_path}/app/config/parameters.yml.dist"))
distParameters['parameters'].each do |k,v|
set :distParameter, ask(k, v)
parameters['parameters'][k] = fetch(:distParameter)
end
end
else
parameters = YAML::load(capture("cat #{release_path}/app/config/parameters.yml.dist"))
distParameters['parameters'].each do |k,v|
set :distParameter, ask(k, v)
parameters['parameters'][k] = fetch(:distParameter)
end
end
upload! StringIO.new(parameters.to_yaml), "#{shared_path}/app/config/parameters.yml"
end
end
desc "configure all parameters"
task :parameters_configure do
on roles(:all) do
distParameters = YAML::load(capture("cat #{release_path}/app/config/parameters.yml.dist"))
if test("[ -f #{shared_path}/app/config/parameters.yml ]")
parameters = YAML::load(capture("cat #{shared_path}/app/config/parameters.yml"))
if parameters
distParameters['parameters'].each do |k,v|
if parameters['parameters'][k].nil? && parameters['parameters'].fetch(k, false) == false
set :distParameter, ask(k, v)
parameters['parameters'][k] = fetch(:distParameter)
else
set :distParameter, ask(k, parameters['parameters'][k])
parameters['parameters'][k] = fetch(:distParameter)
end
end
else
parameters = YAML::load(capture("cat #{release_path}/app/config/parameters.yml.dist"))
distParameters['parameters'].each do |k,v|
set :distParameter, ask(k, v)
parameters['parameters'][k] = fetch(:distParameter)
end
end
else
parameters = YAML::load(capture("cat #{release_path}/app/config/parameters.yml.dist"))
distParameters['parameters'].each do |k,v|
set :distParameter, ask(k, v)
parameters['parameters'][k] = fetch(:distParameter)
end
end
upload! StringIO.new(parameters.to_yaml), "#{shared_path}/app/config/parameters.yml"
end
end
@carlcraig
Copy link
Author

This will sync parameters.yml with parameters.yml.dist, providing two tasks parameters which will prompt for missing parameters and parameters_configure which will allow you to change all the parameters.

To add to your scripts you will want to add:

before 'deploy:check:linked_files', 'parameters_empty'
before 'deploy:symlink:shared', 'parameters'

@Jekis
Copy link

Jekis commented Jan 15, 2016

Thanks, @carlcraig! That's exactly what I was looking for.

Maybe parameters.yml should not be copied to a shared folder? Each release could have different set of parameters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment