Capistrano deployment changing directory for migrations - ruby-on-rails

I have a rails app that is not in the root directory of the repository. When it is deployed, some other static files are deployed with it in a parent directory. The structure is something like this:
root
-- otherstuff
-- railsapp
When I do a deployment with cap deploy:migrations, the Capistrano command that gets executed looks like this, which of course doesn't work:
cd /u/apps/minicart/releases/20100717215044; rake RAILS_ENV=staging db:migrate
How do I change this so that it will be:
cd /u/apps/minicart/releases/20100717215044/railsapp; rake RAILS_ENV=staging db:migrate
I made it work by adding a task that executes this command after deploy:finalize_update, but I would prefer to use the built in method, plus my hacked version is executed with every deployment.
Any advice would be appreciated.
Tim

This turned out to be very simple.
I added a deploy namepace to my deploy.rb file and then redefined the migrate method. Now my method runs on cap deploy:migrations.
namespace :deploy do
desc "Migrating the database"
task :migrate, :roles => :app do
run <<-CMD
cd #{release_path}/minicart; RAILS_ENV=#{stage} rake db:migrate
CMD
end
end

Related

Capistrano: trying to run rake db:seed on remote server

I'm trying to run my seed file on a remote server using capistrano. My deploy is OK, so there is no issue there. Here is the code for running the seed file in config/deploy.rb
namespace :seed do
desc "Run a task on a remote server."
# run like: cap staging rake:invoke task=a_certain_task
task :default do
run("cd #{deploy_to}/current; /usr/bin/env bundle exec rake #{ENV['db:seed']} RAILS_ENV=#{rails_env}")
end
end
I am evoking this task by running 'cap seed'.
Whats weird is it looks like tests are running when I run this..HERE is a snippet.
Maybe the problem is with #{ENV['db:seed']} part. Isn't it should be just db:seed. The eniviroment variable db:seed doesn't exist so You are calling a pure rake command.
Try this:
run("cd #{deploy_to}/current; /usr/bin/env bundle exec rake db:seed RAILS_ENV=#{rails_env}")

Rails deployment - how do you do rake db:reset with capistrano?

I am using Linode with Ubuntu 10.04 and Capistrano, Unicorn, & Nginx to deploy.
How do I do the equivalent of heroku run rake db:reset with this setup? Is it as simple as cap deploy:cold again to run the migrations?
I've already deployed and want to drop all databases and rerun all the migrations but am not sure which commands to run with this setup to do so.
I wrote a tiny little file you can copy to run arbitrary rake tasks via capistrano: http://jessewolgamott.com/blog/2012/09/10/the-one-where-you-run-rake-commands-with-capistrano/
once setup, you can:
cap sake:invoke task="db:reset"
For Capistrano 3 without actual dropping the database. Use bundle exec cap db:reset
namespace :db do
desc 'Resets DB without create/drop'
task :reset do
on primary :db do
within release_path do
with rails_env: fetch(:stage) do
execute :rake, 'db:schema:load'
execute :rake, 'db:seed'
end
end
end
end
end
You could add the following to your deploy.rb file
namespace :custom do
task :task do
run "cd #{current_path} && bundle exec rake db:reset RAILS_ENV=#{rails_env}"
end
end
Then run cap custom:task to clear the database.
If you are using Capistrano 3, consider using the capistrano-rails-collection.
You can also use copy the code directly from db.rake file from the repository.
Or, if you want a full-fledged solution to run all your rake tasks on a remote server, check out the Cape gem.

Automatically Starting ElasticSearch with Rails

I've been doing Ruby on Rails development with ElasticSearch between two machines and its starting to get a little annoying. My usual workflow is:
git pull
bundle install
rake db:migrate (or rake db:setup depending)
rails server
elasticsearch -f -D myconfig.xml
rake environment tire:import CLASS=MyObject FORCE=true
Is there anyway I can add all of these commands to some type of start up script in Rails to bring them all into one place? It would make bringing up a dev environment a lot easier on me everytime I switch machines.
The best way I've found is to use the Foreman gem to kickstart your project and associated processes.
It looks like you should do this in your deployment using Capistrano. Here is an example config/deploy.rb file:
[basic parts omitted]
after "deploy", "bundler:bundle_install"
after "bundler:bundle_install", "db:db_migrate"
after "deploy:db_migrate", "deploy:elastic_search_indexing"
namespace :bundler do
desc 'call bundle install'
task :bundle_install do
run "cd #{deploy_to}/current && bundle install"
end
end
namespace :db do
desc 'fire the db migrations'
task :db_migrate do
run "cd #{deploy_to}/current && bundle exec rake db:migrate RAILS_ENV=\"production\""
end
end
namespace :elasticsearch do
desc 'run elasticsearch indexing via tire'
task :index_classes do
run "cd #{deploy_to}/current && bundle exec rake environment tire:import CLASS=YourObject FORCE=true "
end
end
[rest omitted]
Make sure you have a config file on the target machine (Linux) in /etc/elasticsearch/elasticsearch.yml with contents like:
cluster:
name: elasticsearch_server
network:
host: 66.98.23.12
And the last point to mention is that you should create an initializer config/initializers/tire.rb:
if Rails.env == 'production'
Tire.configure do
url "http://66.98.23.12:9200"
end
end
As you can see, this is the exact same IP address, but only used for the production environment. I assume that you access elasticsearch locally (in development mode) via localhost. elasticsearch is connection per default to
http://0.0.0.0:9200
A good starting point and also in depth help is provided by awesome Ryan Bates and his Railscasts http://railscasts.com/episodes?utf8=%E2%9C%93&search=capistrano
Whats keeping you from putting it in a bash script? And put the script inside your RAILS_APP_HOME/scripts folder?
#!/bin/sh
git pull
bundle install
rake db:migrate
rails server
elasticsearch -f -D myconfig.xml
rake environment tire:import CLASS=MyObject FORCE=true

rake db:migrate RAILS_ENV=development

Why we get an error on the command rake db:migrate
Rails Error: Unable to access log file. Please ensure that /home/mahaloo/mahaloo/releases/20120329200051/log/development.log exists and is chmod 0666. The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.
rake aborted!
unable to open database file
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Whats wrong there. I tryed to deploy via capistrano, i used this tutorial to setup capistrano http://teachmetocode.com/screencasts/basic-deployment-with-capistrano/
You're either missing the log directory or file. Have you run cap deploy:setup ?
Otherwise manually create the log file first.
This is likely because you're following the practice of not checking your database.yml into source control. If that is the case, you can make a copy of your database.yml in your deploy shared/config folder, and create a Capistrano task to symlink that back into your release folder. Something like this (in namespace deploy)
task :create_symlinks do
run "ln -nfs #{shared_path}/db/production.sqlite3 #{release_path}/db/production.sqlite3"
run " -nfs #{shared_path}/config/ldap.yml #{release_path}/config/ldap.yml"
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end
Then call this in a hook
after "deploy:finalize_update", "deploy:create_symlinks"
after "deploy:finalize_update", "deploy:migrate"
I think that would work, that's how we're step up on our project. This is similar to these questions:
database.yml deployment best practice
Capistrano - can't deploy my database.yml
How to manage Rails database.yml
Have you try with sudo if your enviroment its on linux, for example, i got that error trying to run the migration, rake db:migrate, so i used sudo rake db:migrate and that's work, maybe because the rake when its trying to consult development.log doesn't have the right permissions or something like that.

Prevent whenenver gem from running --clear-crontab before gems installed

I am using capistrano, and the whenever gem, on a fresh deploy to a server without the whenever gem installed, capistrano attempts to run
whenever --clear-crontab
BEFORE the rake gems:install command has been run, its clear (from this) that this command runs after deploy_code but so does my command that installs the gems (below)..
after "deploy:update_code", "deploy:symlink_config"
deploy.task :symlink_config, :roles => :app do
# create a symlink to the database.yml file located in the shared_path
run "ln -nsf #{shared_path}/config/database.yml #{current_release}/config"
# install any missing gems
run "cd #{current_release} && sudo rake gems:install --trace RAILS_ENV=#{rails_env}"
# migrate the database
run "cd #{current_release} && rake db:migrate --trace RAILS_ENV=#{rails_env}"
end
Is there a way to order these tasks, because on a cold deploy I always get whenever: not found and have to manually install the whenever gem on the remote server
What I ended up doing is removing the require "whenever/capistrano" from the config\deploy.rb to avoid the "automatic" deploy. Instead I have added a task that executes the --clear-crontab and --update-crontab. This works as it will execute in the sequence I set it to.
I have based it off this post, which deals with a slightly different problem but has the same solution - not to use the "automatic" integration with capistrano.

Resources