質問

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
役に立ちましたか?

解決

I had trouble with this solution, because capistrano 3 replaces newlines with semicolons. So here is my code:

namespace :setup do
  task :setup_database do
    ask(:db_user, 'db_user')
    ask(:db_pass, 'db_pass')
    ask(:db_name, 'db_name')
    db_config = <<-EOF
production:
  adapter: mysql2
  database: #{fetch(:db_name)}
  username: #{fetch(:db_user)}
  password: #{fetch(:db_pass)}
    EOF

    on roles(:app) do
      execute "mkdir -p #{shared_path}/config"
      upload! StringIO.new(db_config), "#{shared_path}/config/database.yml"
    end
  end
end

他のヒント

this should work.

execute "cat '#{db_config}' > #{shared_path}/config/database.yml"
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top