capistrano won't run bundle install - ruby-on-rails

I'm using chef and capistrano to create a server and later deploy my code. As I'm new to capistrano I took a tutorial from here and made some minor changes in order to get it going for me.
Whatever I do, I just cant get a bundler install to run on the remote server. Because of that there is no rake and the process stops at the assets:precompile. A github repo containing the code.
Am I trying something that I shouldn't do?

I fixed it with an additional task that explicitly runs bundle install. After that I still got an exception: stdout: Nothing written. I needed to add , raise_on_non_zero_exit: false
to be able to continue. Is anybody has a real test() to see if bundle install ran successfully I would be interested.
namespace :bundle do
desc "run bundle install and ensure all gem requirements are met"
task :install do
on roles(:app) do
execute "cd #{release_path} && RAILS_ENV=#{fetch(:stage)} bundle install --without=test", raise_on_non_zero_exit: false
end
end
end

Related

Run imported rake task with bundle

I'm trying to import and run a rake task, that I put in the Gemfile
I ran the bundle install and it find the installed gem containing my rake task there. But, when I run the bundle exec rake <namespace>:<task>, then then I get this output instead of success:
Don't know how to build task 'forum2discourse:import_punbb'
/usr/local/rvm/gems/ruby-2.0.0-p0-turbo/bin/ruby_noexec_wrapper:14:in `eval'
/usr/local/rvm/gems/ruby-2.0.0-p0-turbo/bin/ruby_noexec_wrapper:14:in `<main>'
(See full trace by running task with --trace)
It looks like the task weren't found. How should I instruct the bundle install so it finds the task?
According the rake task import manual (provided by #Daiku). In the Rakefile, you can enumerate all the gems required in you project, and then try import all the rake task exported in those gems, if any, like this:
Gem::Specification.all.each do |spec|
Dir.glob('**/*.rake').each {|file| load file }
end
The try:
$ rake -T
the Gem I was trying to install isn't published yet, so adding
gem 'forum2discourse'
to the Gemfile isn't enough. I wast trying to fix this by checking out the repo, then installing the Gem globally? with sudo bundle install <gem>. The bundle install then stopped complaining about not having the needed gem, but it wouldn't expose the tasks in the gem. However, having the link to the github repo in the gem definition fixes the issue:
gem 'forum2discourse', github: 'initforthe/forum2discourse'
Ruby is magick :-)

cron job fails with "is not checked out. Please run `bundle install` (Bundler::GitError)"

I seem to have a similar problem as this post, where Bundler complains of a gem not being checked out. However, mine shows an error when running a rake task using a cron job. (Otherwise, the site seems to deploy fine in production using Phusion Passenger, and the rake task runs when called in the command line )
The error is:
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.18/lib/bundler/source.rb:571:in load_spec_files': git://github.com/moneill/Google-Maps-for-Rails.git (at modified_markers) is not checked out. Please runbundle install` (Bundler::GitError)
I tried using bundle install --deployment ; bundle pack followed by bundle install --path vendor/cache.
One thing I haven't tried is to locally compile the git project and install the gem in the vendor/bundle folder. I am not using RVM for this particular server.
The cron job command is cd /home/[dir]/[rails_app_folder]/ && RAILS_ENV=production /usr/local/bin/bundle exec rake [task] --trace
Thank you!

How to deploy a custom Gem using capistrano?

I'm new to RoR. My gem does some background processing and loads a thin server so it can be checked from time to time. I need to put this on my web server using Capistrano.
Do people normally deploy gems to their servers or should the app be written in some other way?
Also is Capistrano the correct way to deploy gems?
Have you checked out Bundler before? That works very well with capistrano deployments and you can use to package your gems with your app on deployments.
Or you can use the gem Jeweler : https://github.com/technicalpickles/jeweler
I found out deploying a custom Gem is no different to any other project type. I just needed to add a new Capistrano task to my deploy.rb file so that the gem could get installed after the files get downloaded onto the server by Capsitrano. This is all I had to do.
desc "Install this gem"
task :setup_install, roles: :app do
run "cd #{release_path} && gem build zoe.gemspec"
run "cd #{release_path} && gem install YOUR-GEM-NAME.gem --quiet"
end
after "deploy:finalize_update", "deploy:setup_install"
desc "Uninstall this gem"
task :setup_uninstall, roles: :app do
run "gem uninstall -x YOUR-GEM-NAME"
end
before "deploy:setup_install", "deploy:setup_uninstall"

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.

How do I manage bundled gems in a capistrano deployment?

Currently, I'm just running the following:
after 'deploy:update_code', 'deploy:bundle'
namespace :deploy do
task :bundle do
run "cd #{release_path} && bundle install --deployment --without development test staging"
end
end
Now this works fine, but the bundler ends up installing all the gems to vendor/gems every time. While I understand the merit in isolating the gems to each release (as opposed to using the shared folder, which (?) might result in errors on rollback and such), surely there is a better way to do this that doesn't take as much time to deploy and saves on disk space.
Things have changed after the release of bundler 1.0
You don't need to create a custom task from deploy.
just use require 'bundler/capistrano'
take a look at this http://blog.josephholsten.com/2010/09/deploying-with-bundler-and-capistrano/ for more details

Resources