Capistrano deployment errors with database.yml

I’m trying to deploy using capistrano however I keep getting this database.yml not found error. My database.yml file is not commited to my repository and I’ve tried to set up my deploy.rb file to create a database.yml on the fly but it doesnt seem to be working. Here is my deploy.rb file:

require "bundler/capistrano"

set :application, "myapp.com"
set :app_name, "myapp"
set :repository,  "git@bitbucket.org:myrepo/fltctr.git"
set :scm, :git

server "100.100.100.100", :web, :app, :db, primary: true

  set :user, "user" # As defined on your server
  set :password, "pwwd"
  set :deploy_to, "/var/www/html/myapp.com" # Directory in which the deployment will take place
  set :mysql_user, 'root_user'
  set :mysql_password, 'root_pwwd'

set :deploy_via, :remote_cache
set :use_sudo, false
ssh_options[:forward_agent] = true

namespace :deploy do
  task :start do ; end
  task :stop do ; end
  task :restart, :roles => :app, :except => { :no_release => true } do
    run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
  end
end

namespace :deploy do  
  desc "Recreate symlink"
  task :resymlink, :roles => :app do
    run "#{try_sudo} rm -f #{current_path} && ln -s #{release_path} #{current_path}"
  end

end


require 'erb'

before "deploy:setup", :db
after "deploy:update_code", "db:symlink"

namespace :db do
  desc "Create database yaml in shared path"
  task :default do
    db_config = ERB.new <<-EOF
    base: &base
      adapter: mysql
      socket: /var/run/mysqld/mysql.sock
      username: #{mysql_user}
      password: #{mysql_password}

    development:
      database: #{app_name}_dev
      <<: *base

    test:
      database: #{app_name}_test
      <<: *base

    production:
      database: #{app_name}_prod
      <<: *base
    EOF

    run "#{try_sudo} mkdir -p #{shared_path}/config"
    put db_config.result, "#{shared_path}/config/database.yml"
  end

  desc "Make symlink for database yaml"
  task :symlink do
    run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
  end
end


after "deploy:create_symlink", "deploy:resymlink", "deploy:update_crontab"

Everytime I run cap deploy I get the following error after which deployment stops and rolls back.

  • require file from gem
  • What port to use sending email with SMTP (mailgun) in rails app on production server (DigitalOcean)?
  • How to install capistrano v2.13.5 with ruby 1.8.7?
  • rails active admin deployment : couldn't find file 'jquery-ui'
  • Capistrano 3 SSHKit::Runner::ExecuteError: Exception while executing on host agent could not sign data with requested identity
  • How to build task 'gems:install'
  • *** [err :: ] rake aborted!
    *** [err :: ] No such file or directory - /var/www/html/myapp.com/releases/20130224112516/config/database.yml
    *** [err :: ] /var/www/html/myapp.com/shared/bundle/ruby/1.9.1/gems/railties-3.2.3/lib/rails/application/configuration.rb:115:in `read'
    

    Please help – I really need to get this done.

  • Excluding files from being deployed with Capistrano while still under version control with Git
  • Starting background tasks with Capistrano
  • Installing capistrano on Ubuntu
  • How can I deploy 'private' gems that my apps can access but no one else can get?
  • Rails Error: NoMethodError: undefined method `instance' for Capistrano::Configuration:Class Did you mean? instance_of?
  • How to deploy an RubyGem-based Server
  • One Solution collect form web for “Capistrano deployment errors with database.yml”

    You need to write to a file not just puts to stdout:

    Change this line:

    put db_config.result, “#{shared_path}/config/database.yml”

    to

    File.open(“#{shared_path}/config/database.yml”, “w”) { |f| f.puts db_config.result }

    Ruby is the best programming language in the world - Ruby on Rails.