Rake db:Migrate with Nginx Capistrano - ruby-on-rails

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.

Related

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.

Multistaging capistrano: db:migrate working correctly on production but not on staging

I have set up capistrano to deploy to staging and production. Honestly, I'm not very familiar with capistrano. I did this via just using standard capistrano (not multi-host). I pass a variable in such as:
cap production deploy
cap staging deploy
But my db:migrate isn't working correcty.
with cat staging deploy:
I get shis:
* executing "cd /data/sites/staging.domain.com/apps/d-rails/releases/20121212203353 && bundle exec rake RAILS_ENV=production db:migrate"
and would like (just sub production -> staging):
* executing "cd /data/sites/staging.domain.com/apps/d-rails/releases/20121212203353 && bundle exec rake RAILS_ENV=staging db:migrate"
How would I set this up? Or what should I look at first to fix?
In my deploy.rb, I have:
task :production do
set :deploy_to, "/data/sites/domain.com/apps/#{application}"
end
task :staging do
set :deploy_to, "/data/sites/staging.domain.com/apps/#{application}"
after 'deploy:update_code' do
run "cd #{release_path}; RAILS_ENV=staging bundle exec rake assets:precompile --trace"
end
end
thx in advance
I think it would be easier to use capistrano's multistaging feature. Here is my setup for production and staging deployment:
config/deploy.rb
require 'capistrano/ext/multistage'
require 'bundler/capistrano'
set :application, "yourappname"
set :repository, "git#yourhost.com:yourrepo.git"
set :stages, %w(production staging)
set :default_stage, "staging" # running "cap deploy" deploys to staging, "cap production deploy" deploys to production
set :user, "deploy" # the ssh user which does the deployment on the server
set :use_sudo, false
set :scm, :git
set :default_environment, {
'PATH' => "/usr/local/rbenv/shims:/usr/local/rbenv/bin:/usr/local/rbenv/versions/1.9.3-p327/bin:$PATH"
}
after "deploy:update_code", "deploy:migrate"
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
The set :default_environment is needed if you have to include some additional pathes for your deployment (because, the normal .bashrc or .bash_profile isn't included when capistrano logs into the server)
config/deploy/production.rb
set :rails_env, "production"
set :deploy_to, "/var/www/your_production_folder"
role :web, "example.com" # Your HTTP server, Apache/etc
role :app, "example.com" # This may be the same as your `Web` server
role :db, "example.com", :primary => true # This is where Rails migrations will run
config/deploy/staging.rb
set :rails_env, "staging"
set :deploy_to, "/var/www/your_staging_folder"
role :web, "example.com" # Your HTTP server, Apache/etc
role :app, "example.com" # This may be the same as your `Web` server
role :db, "example.com", :primary => true # This is where Rails migrations will run
Be sure to include the RailsEnv variable in your VirtualHost config. If you are using Apache, this would look like this:
<VirtualHost *:80>
ServerName staging.example.com
ServerAlias www.staging.example.com
# !!! Be sure to point DocumentRoot to 'public'!
DocumentRoot /var/www/your_staging_folder/current/public
<Directory /var/www/your_staging_folder/current/public>
# This relaxes Apache security settings.
AllowOverride all
# MultiViews must be turned off.
Options -MultiViews
#AuthName "Staging Server"
#AuthType Basic
#AuthUserFile /var/staging.htpasswd
#require valid-user
</Directory>
RailsEnv staging
</VirtualHost>
The uncommented AuthName, AuthType is used if you want to password protect your staging environment. When you are finished configuring this stuff, test your deployment with cap deploy:setup, this sets up the folder structure. A cap deploy:cold will copy all the files of your application to the directory. A cap deploy:migrate migrates your db. But you can also only just do a cap deploy.
Another thing is, that you have to set up a staging env in the rails app. For this, copy the config/environments/production.rb (or development.rb, what you prefer) to staging.rb and adjust the configs for your needs.
I hope I haven't forgotten anything ;) Let me know if you have any further problems

rails / capistrano - error compiling assets

When I deploy with capistrano I get an error (and rollback) when capistrano attempts to run assets:precompile.
I'm using rails 3.2.1, bundler 1.0.22, capistrano 2.11.2
If I run rake assets:precompile from /webapps/myapp/current it runs successfully.
error:
failed: "sh -c 'cd /webapps/myapp/releases/20120304160347 && bundle exec rake RAILS_ENV=production RAILS_GROUPS=assets assets:precompile'"
deploy.rb
require "bundler/capistrano"
load 'deploy/assets'
set :application, "myapp"
set :domain, '24.17.71.95'
set :repository, "."
set :deploy_via, :copy
set :local_repository, '/home/me/myapp/.git'
set :deploy_to, '/webapps/myapp/'
set :scm, :none #:git
set :user, 'me'
set :password, 'me$pw'
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
default_run_options[:pty] = true
role :web, domain # Your HTTP server, Apache/etc
role :app, domain # This may be the same as your `Web` server
role :db, domain, :primary => true # This is where Rails migrations will run
set :branch, 'master'
# if you're still using the script/reaper helper you will need
# these http://github.com/rails/irs_process_scripts
before "deploy:assets:precompile", "bundle:install"
# If you are using Passenger mod_rails uncomment this:
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
As far as I know, the asset precompiling task should be one of the last (or even the last one) tasks that are beeing executed.
So please try to move
load 'deploy/assets'
out of deploy.rb into Capfile (root folder of your Rails app) and paste it as the last line of the file.
See my answer to this Stackoverflow question.
If that fails, try the advice in http://www.simonecarletti.com/blog/2012/02/heroku-and-rails-3-2-assetprecompile-error/

Sudo doesn't have access to shell commands when deploying with Capistrano

I'm deploying my Rails 3 app using capistrano.
I have on user (deploy) who's been added to sudoers. This is the user I'm deploying with.
When I log on to the server as deploy I have access to all of the gem commands I need .ie: bundle, whenever etc.
Capistrano seems to be running as sudo though, and when I try:
sudo whenever
I get
sudo: whenever: command not found
This means each time I try to deploy, it fails and rolls back.
I've tried setting :use_sudo to false in my deploy.rb file but still no luck
set :user, "deploy"
set :runner, user
set :use_sudo, false
Any suggestions?
Here's my complete deploy script in case there's anything in there I've missed:
require 'config/boot'
require 'hoptoad_notifier/capistrano'
require 'capistrano/ext/multistage'
require "whenever/capistrano"
#
set :whenever_command, "bundle exec whenever"
set :application, "MYAPP"
set :repository, "git#github.com:myAccount/myRepos.git"
# only keep 3 previous releases after cleanup
set :keep_releases, 3
set :scm, "git"
set :scm_user, "me"
set :branch, lambda {rails_env}
set :deploy_to, lambda {"/var/www/#{application}/#{rails_env}"}
default_run_options[:pty] = true
role :web, "xxx.xxx.xxx.xxx" # Your HTTP server, Apache/etc
role :app, "xxx.xxx.xxx.xxx" # This may be the same as your `Web` server
role :db, "xxx.xxx.xxx.xxx", :primary => true # This is where Rails migrations will run
set :user, "deploy"
set :runner, user
set :use_sudo, false
ssh_options[:paranoid] = false
ssh_options[:port] = 22
namespace :deploy do
task :start do ; end
task :stop do ; end
task :restart, :roles => :app do
run " touch #{File.join(current_path,'tmp','restart.txt')}"
end
end
namespace :bundle do
desc "run bundle install"
task :install do
run "cd #{current_release} && bundle install"
end
end
namespace :tail do
desc "Tail the current environment's log file"
task :log, :roles => :app do
stream "tail -f #{shared_path}/log/#{rails_env}.log"
end
desc "Tail the new relic log file"
task :new_relic, :roles => :app do
stream "tail -f #{shared_path}/log/new_relic.log"
end
end
before "deploy:restart", "bundle:install"
after "deploy:restart", "deploy:cleanup"
after "deploy:restart", "whenever:update_crontab"
on server run which whenever or whereis whenever you should get full path to the command put it into script:
set :whenever_command, "path_to-whenever"
It's not clean solution but might work.
Another solution might be sudo reconfiguration, go to /etc/sudoers and have a look on env_keep adding PATH might have been important, to keep all the stuff important for application you could use rvm, capistrano-rvm integration and put all the displayed variables from rvm info to env_keep, theoreticaly it should work, just be careful to not mess anything

Resources