Capistrano 3, Rails 4, database configuration does not specify adapter - ruby-on-rails

When I start
cap production deploy
it fails like this:
DEBUG [4ee8fa7a] Command: cd /home/deploy/myapp/releases/releases/20131025212110 && (RVM_BIN_PATH=~/.rvm/bin RAILS_ENV= ~/.rvm/bin/myapp_rake assets:precompile )
DEBUG [4ee8fa7a] rake aborted!
DEBUG [4ee8fa7a] database configuration does not specify adapter
You can see that "RAILS_ENV=" is actually empty and I'm wondering why that might be happening? I assume that this is the reason for the latter error that I don't have a database configuration.
The deploy.rb file is below:
set :application, 'myapp'
set :repo_url, 'git#github.com:developer/myapp.git'
set :branch, :master
set :deploy_to, '/home/deploy/myapp/releases'
set :scm, :git
set :devpath, "/home/deploy/myapp_development"
set :user, "deploy"
set :use_sudo, false
set :default_env, { rvm_bin_path: '~/.rvm/bin' }
set :keep_releases, 5
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
# Your restart mechanism here, for example:
within release_path do
execute " bundle exec thin restart -O -C config/thin/production.yml"
end
end
end
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
within release_path do
end
end
end
after :finishing, 'deploy:cleanup'
end
Database.yml:
production:
adapter: mysql2
encoding: utf8
database: myapp_production
pool: 5
username: user
password: pass
host: localhost
development:
adapter: mysql2
encoding: utf8
database: myapp_development
pool: 5
username: user
password: pass
host: localhost
The issue is resolved if I add
set :rails_env, "production"
to my deploy.rb, but this looks like hardcoding to me and I'm sure there's a nicer solution.

Edit: Per this pull request, it's now fixed in version 1.1.0 of capistrano-rails.
Per this Github issue, another fix is to edit your Capfile. Comment out these two lines
#require 'capistrano/rails/assets'
#require 'capistrano/rails/migrations'
and put this line in
require 'capistrano/rails'
which will correctly set your RAILS_ENV variable.

Using Cap 3 and capistrano_rails on rails 4 I was getting the same error; in the environment file(s) being deployed, I set
set :stage, :production
set :rails_env, 'production' # even though doc says only need to do this if it's different
Doc here: https://github.com/capistrano/rails

Based on Marc's answer which definitely seems to be the right one,
you can workaround this until it is fixed upstream by adding this to your config/deploy.rb in the "namespace :deploy" block:
desc 'Provision env before assets:precompile'
task :fix_bug_env do
set :rails_env, (fetch(:rails_env) || fetch(:stage))
end
before "deploy:assets:precompile", "deploy:fix_bug_env"
This will force loading the env and provisionning RAILS_ENV before assets:precompile is called.

Seems to be a bug in capistrano-rails.
There is a task (rails.rake) that sets the environment either from rails_env or stage:
namespace :deploy do
before :starting, :set_rails_env do
set :rails_env, (fetch(:rails_env) || fetch(:stage))
end
end
But this task isn't called before i.e. assets:precompile. So this:
namespace :assets do
task :precompile do
on roles :web do
within release_path do
with rails_env: fetch(:rails_env) do
execute :rake, "assets:precompile"
end
end
end
end
end
fails because rails_env is nil if it isn't set explicitly.
If I have the time to dig a little deeper I'll file a bug report.

If you're using passenger, you need to add
rails_env production;
in the in web server's (eg: nginx) .conf where you've specified values for passenger_ruby and passenger_root.

What happens if you add a file:
deploy/production.rb
With this line:
set :stage, :production

Related

Rake db:Migrate with Nginx Capistrano

I'm running a server with Nginx, Capistrano, Rails
I made some db:migrations on my local machine and then want to push them to these changes to my server. However I can't figure out how to migrate my database on the server. How do I do this?
I've Tried
1)
cap production deploy
cap production deploy:migrate
2)
[On server - in current]
rake db:migrate
but none of these seem to work. How do I make this migration?
Capistrano File
lock '3.4.0'
require 'capistrano/sidekiq'
set :whenever_command, "bundle exec whenever"
require "whenever/capistrano"
set :application, 'myApp'
set :repo_url, 'git...'
set :keep_releases, 5
set :scm, :git
set :repository, "git..."
set :scm_passphrase, "..."
set :user, "..."
set :use_sudo, false
set :deploy_to, "/.../.../apps/appName"
namespace :deploy do
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
end
end
end
1) If you are using capistrano-rails you should set
set :migration_role, 'migrator' # Defaults to 'db'
2) On server you are making migration in development environment, try
RAILS_ENV=production bundle exec rails db:migrate
To run migrations with capistrano, on production.rb file which stays inside /config/deploy you have add "db" role
e.g.
roles: %w{web app db}
also capistrano migrate tasks works if there is a difference between current_path and release_path migrations. what you can do is remove migrations from inside current_path and then deploy and then migrate.

Running Migration after capistrano deployment

I am trying to learn deployment using capistrano. I want to deploy the code on a separate folder in my local machine and run migrations after deployment.
The capistrano gems used in the project are as follows
capistrano (3.4.0)
capistrano-bundler (1.1.4)
capistrano-ext (1.2.1)
capistrano-rails (1.1.3)
The application is using ruby 2.1 and rails 4.1
The deploy file is as follows
require 'capistrano/rails/migrations'
lock '3.4.0'
set :application, 'capistrano_study'
set :repo_url, 'https://github.com/xxxxxx/capistrano_study.git'
# config valid only for current version of Capistrano
set :stages, ["staging", "production"]
set :default_stage, "staging"
set :user, "prajeesh"
after "deploy:updated", "deploy:migrate"
namespace :deploy do
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
# Here we can do anything such as:
# within release_path do
# execute :rake, 'cache:clear'
# end
end
end
end
staging.rb file as follows.
server 'xx.x.x.xxx', user: 'prajeesh', roles: %w{app db web}, my_property: :my_value
set :deploy_to, "/home/prajeesh/Desktop/projects/capistrano_staging"
Database.yml
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: cap_test_staging
pool: 5
username: root
password: xxxxx
# socket: /var/run/mysqld/mysqld.sock
staging:
adapter: mysql2
encoding: utf8
reconnect: false
database: cap_test_staging
pool: 5
username: root
password: xxxxx
When i run the command cap staging:deploy, the deployment is working fine. The issue is that the migrations are not running after deployment.
Does anyone know how to fix this?
Edit:
This is the error that i am getting.
INFO [175f4b0b] Running /usr/bin/env rake db:migrate as
prajeesh#xx.x.x.xxx
DEBUG [175f4b0b] Command: cd /home/prajeesh/Desktop/projects/capistrano_staging/current && ( RAILS_ENV=development /usr/bin/env rake db:migrate )
DEBUG [175f4b0b] rake aborted!
DEBUG [175f4b0b]
cannot load such file -- bundler/setup
If i run the command RAILS_ENV=development /usr/bin/env rake db:migrate directly from the project path, the migration is running but through capistrano it is not working.
Any help would be appreciated.
Hey you should run the below command to get it run command:
cap deploy:migrate
To get it run, you can see the documentation here
Updated for automating migration:
after "deploy:update_code", "deploy:migrate"
Into the file config/deploy.rb.
You should require capistrano/rails/migrations in your Capfile as mentioned in here. It'll do the trick.
If you're on Capistrano 3, I did
set :migration_role, :app in addition to adding require 'capistrano/rails/migrations' to my deploy.rb file.
(source: capistrano-rails docs)

chef rails 4 ruby 2.1 rbenv capistrano shared bin passenger file missing

We are relatively new to using chef to deploy our applications. Currently, an odd issue we are experiencing and have yet to find a solution for relates to our bin/passenger configuration file. For some reason when the server is constructed with chef it does not exist or chef is not creating it. Maybe capistrano is not creating it... We are a bit dumbfounded by this one.
As you can see from the attached image, we know the file is not there. All of our current scripts match 4 other servers that are running successfully but for some reason this new build will not create the file. Or TBH, we are completely missing some steps. It has been some very long nights trying to get this going.
We used chef to build the server and we are using capistrano to deploy to the box
Anyone have any thoughts? Need more information? Pointers?
Our current config/deploy.rb file:
set :application, 'digest'
set :scm, :git
set :repo_url, '{omitted private repo}'
set :branch, 'experiment/cap'
set :deploy_to, '/home/apps/api'
set :deploy_via, :remote_cache
set :user, 'deploy'
set :use_sudo, false
set :rbenv_type, :system
set :rbenv_ruby, '2.1.0'
set :rbenv_path, '/opt/rbenv'
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
execute "mkdir -p #{release_path}/tmp ; touch #{release_path}/tmp/restart.txt"
end
end
desc 'Create application symlinks'
task :symlinks do
on roles(:app), in: :sequence, wait: 5 do
execute "rm #{release_path}/config/database.yml"
execute "ln -s #{shared_path}/config/database.yml #{release_path}/config/database.yml"
execute "ln -s #{shared_path}/config/secrets.yml #{release_path}/config/secrets.yml"
execute "ln -s #{shared_path}/bin/passenger #{release_path}/bin/passenger"
end
end
after :finishing, 'deploy:cleanup'
after 'deploy:updated', 'deploy:symlinks'
end
namespace :setup do
desc 'Copy the secrets.yml and database.yml files'
task config: [ 'config/secrets.yml', 'config/database.yml' ] do |t|
on roles(:all) do
execute "mkdir -p #{shared_path}/config"
t.prerequisites.each do |file|
upload! file, "#{shared_path}/config"
end
end
end
end
In our config/deploy/staging.rb file:
set :stage, :staging
# Simple Role Syntax
# ==================
# Supports bulk-adding hosts to roles, the primary
# server in each group is considered to be the first
# unless any hosts have the primary property set.
role :app, %w{deploy#208.94.36.146}
role :web, %w{deploy#208.94.36.146}
set :rails_env, "staging"
Our staging server bin folder:
You can see the application is making it to the box with the current releases setup:
Our current application on the server:
Our current application config directory:
I'm assuming passenger isn't in the Gemfile which is would cause the binstub to not get created. Is that the issue?

Capistrano 3 is not running rails migrations when deployed

I want to deploy to production an app to my local server. i'm using capistrano 3.
this is my capfile
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/rvm'
require 'capistrano/bundler'
require 'capistrano/rails'
#require 'capistrano/rails/migrations'
#require 'capistrano/rails/assets'
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }
this is my deploy.rb
# config valid only for Capistrano 3.1
lock '3.1.0'
set :application, 'ImpresaZiliani'
set :repo_url, 'francesco#10.0.1.8:repos/impresaziliani.git'
set :branch, 'master'
# Default branch is :master
# ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }
# Default deploy_to directory is /var/www/my_app
set :deploy_to, '/home/francesco/impresaziliani'
# Default value for :scm is :git
set :scm, :git
set :deploy_user, "francesco"
set :rails_env, "production"
set :keep_releases, 5
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
# Your restart mechanism here, for example:
# execute :touch, release_path.join('tmp/restart.txt')
end
end
after :publishing, :restart
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
# Here we can do anything such as:
# within release_path do
# execute :rake, 'cache:clear'
# end
end
end
end
set :rvm_ruby_version, '2.1.1'
set :default_env, { rvm_bin_path: '~/.rvm/bin' }
SSHKit.config.command_map[:rake] = "#{fetch(:default_env)[:rvm_bin_path]}/rvm ruby-# {fetch(:rvm_ruby_version)} do bundle exec rake"
my database.yml is ok since if i run manually the migrations on the server it works, i have tried with uncommenting the line of capistrano/rails/migrations and assets but nothing changes: when i deploy it runs fine till the bundler install, then without any warning or error, skip to the asset precompiler and doesn't run migrations.
how can i fix this?
thank you
You also need to make the user deploying has the role of db, such as:
server 'you_ip_address', user: 'user_name', roles: %w{web app db}
rake db:migrate is automatic per deploy in capistrano 3
you just need to uncomment #require 'capistrano/rails/migrations' in your Capfile
Both Jude Calimbas and hiveer's answers are more accurate than the accepted answer - the migration task is run automatically as part of the deploy task.
However, their answers do not explain the problem observed. The only thing that occurs to me is that the database.yml file is not explicitly linked in the deploy.rb file. So a line like
set :linked_files, %w{config/database.yml}
would have fixed it.
I know that this is an old question but it would be interesting to know more details from the OP regarding the problem and the fix.

Why does :restore_manifest in cap staging deploy fail?

Having setup capistrano on my rails app I was deploying okay I then went and changed some css values on my local site. This was fine but then when I went to deploy my site with cap staging deploy it started to do its normal routine tasks just fine but when it came to capistrano's precompile task for assets it failed. I have managed to source where it is going wrong on the capistrano area and thats here:
task :backup_manifest do
on roles(fetch(:assets_roles)) do
within release_path do
execute :cp,
release_path.join('public', fetch(:assets_prefix), 'manifest*'),
release_path.join('assets_manifest_backup')
end
end
end
task :restore_manifest do
on roles(fetch(:assets_roles)) do
within release_path do
source = release_path.join('assets_manifest_backup')
target = capture(:ls, release_path.join('public', fetch(:assets_prefix),
'manifest*')).strip
if test "[[ -f #{source} && -f #{target} ]]"
execute :cp, source, target
else
msg = 'Rails assets manifest file (or backup file) not found.'
warn msg
fail Capistrano::FileNotFound, msg
end
end
end
end
It fails here where you have within release_path do as thats in the stack trace but I do not know why it does that as i have not changed any tasks at all just ccs tweaks.
Here are my deployment settings for capistrano:
deploy.rb
lock '3.1.0'
server "188.226.182.102"
set :application, "ForgeAndCo"
set :scm, "git"
set :repo_url, "git#made-by-mark.beanstalkapp.com:/made-by-mark/forge.git"
# set :scm_passphrase, ""
set :user, "deploy"
set :use_sudo, false
set :ssh_options, {
forward_agent: true,
port: 14439
}
set :assets_prefix, 'prepackaged-assets'
# files we want symlinking to specific entries in shared.
set :linked_files, %w{config/database.yml}
# dirs we want symlinking to shared
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
SSHKit.config.command_map[:rake] = "bundle exec rake" #8
SSHKit.config.command_map[:rails] = "bundle exec rails"
set :branch, ENV["REVISION"] || ENV["BRANCH_NAME"] || "master"
set :keep_releases, 20
namespace :deploy do
desc 'Restart passenger without service interruption (keep requests in a queue while restarting)'
task :restart do
on roles(:app) do
execute :touch, release_path.join('tmp/restart.txt')
unless execute :curl, '-s -k --location localhost | grep "Forge" > /dev/null'
exit 1
end
end
end
end
after 'deploy:publishing', 'deploy:restart'
deploy/staging.rb
role :app, %w{deploy#188.226.182.102}
role :web, %w{deploy#188.226.182.102}
role :db, %w{deploy#188.226.182.102}
# Extended Server Syntax
# ======================
# This can be used to drop a more detailed server
# definition into the server list. The second argument
# something that quacks like a hash can be used to set
# extended properties on the server.
# server 'example.com', user: 'deploy', roles: %w{web app}, my_property: :my_value
set :stage, :staging
server "188.226.182.102", user: "deploy", roles: %w{web app db}
set :deploy_to, "/home/deploy/forge_staging"
set :rails_env, 'staging' # If the environment differs from the stage name
set :migration_role, 'migrator' # Defaults to 'db'
set :assets_roles, [:web, :app] # Defaults to [:web]
set :assets_prefix, 'prepackaged-assets' # Defaults to 'assets' this should match config.assets.prefix in your rails config/application.rb
set :branch, ENV["REVISION"] || ENV["BRANCH_NAME"] || "master"
Does anyone know why it would fail at this task at all?

Resources