My problem is really simple but I just can't find an answer on google.
I would like to insert a yml configuration into a remote file on my server through a Capistrano task.
While it was really easy with Capistrano 2 and the "put" command, I just can't find the right way to do it with Capistrano 3.
For instance, here is my yml configuration:
set(:database_username, "db_user")
ask(:database_password, "Database Password: ")
db_config = <<-EOF
base: &base
adapter: postgresql
encoding: unicode
reconnect: false
pool: 10
username: #{fetch(:database_username)}
password: #{fetch(:database_password)}
staging:
database: #{fetch(:application)}_staging
<<: *base
production:
database: #{fetch(:application)}_production
<<: *base
EOF
I'd like to insert my "db_config" variable in the database.yml file. Using Capistrano 2 I would do so like this:
put db_config, "#{shared_path}/config/database.yml"
But with Capistrano 3 it no longer works.
I tried something like this:
execute "echo '#{db_config}' > #{shared_path}/config/database.yml"
But end of lines are not kept, they are replaced by ";" because of the 'echo' command.
Does anyone have ever performed something like this with Capistrano 3?
Otherwise, I will just go along with a "database.yml.example" and modify it directly after deployment.
Thanks!
[UPDATE] here is my whole Capistrano 3 task code:
namespace :db do
desc "Create database yaml in shared path"
task :configure do
on roles(:db) do
if capture("cat #{shared_path}/config/database.yml").length > 0
puts "### INFO: Database.yml already exists"
else
set(:database_username, "db_user")
ask(:database_password, "Database Password: ")
db_config = <<-EOF
base: &base
adapter: postgresql
encoding: unicode
reconnect: false
pool: 10
username: #{fetch(:database_username)}
password: #{fetch(:database_password)}
staging:
database: #{fetch(:application)}_staging
<<: *base
production:
database: #{fetch(:application)}_production
<<: *base
EOF
execute "mkdir -p #{shared_path}/config"
execute "touch #{shared_path}/config/database.yml"
# Everything works so far. This is the buggy part:
#execute "echo '#{db_config}' > #{shared_path}/config/database.yml"
execute "cat '#{db_config}' > #{shared_path}/config/database.yml"
end
end
end
end