I'm trying to set up Capistrano deployment for the first time and I wanted to test it on my development directory before attempting it on production. Normally I wouldn't even bother with Capistrano in the dev environment, but I'm having issues when deploying. It seems that Capistrano wants to:
A) run the bundle command: bundle --without development test
and
B) run rake assets:precompile in the development environment. I don't want that. Why would I? Maybe in the 'staging' environment if I ever wanted to do that, but certainly not in development mode.
The biggest hurdle at the moment is the fact that it bundles thinking it's in production mode and thus skips over gems which are required when it DOES correctly use the development environment when precompiling assets.
EDIT: Here's a sample of two scripts which are run - the first scripts run bundler as if we're in the production environment, and the last runs it in development environment (RAILS_ENV=development). We of course get the error because BetterErrors is a gem that's loaded only in the development environment and so it can't find BetterErrors because the call to bundle was in the production environment.
INFO [9797fc64] Running ~/.rvm/bin/rvm default do bundle install --binstubs /home/vps_user/rails_deployments/dev.www/shared/bin --path /home/vps_user/rails_deployments/dev.www/shared/bundle --without development test --deployment --quiet on localhost
DEBUG [9797fc64] Command: cd /home/vps_user/rails_deployments/dev.www/releases/20140217224858 && ~/.rvm/bin/rvm default do bundle install --binstubs /home/vps_user/rails_deployments/dev.www/shared/bin --path /home/vps_user/rails_deployments/dev.www/shared/bundle --without development test --deployment --quiet
INFO [9797fc64] Finished in 1.883 seconds with exit status 0 (successful).
DEBUG [da905ff7] Running /usr/bin/env if test ! -d /home/vps_user/rails_deployments/dev.www/releases/20140217224858; then echo "Directory does not exist '/home/vps_user/rails_deployments/dev.www/releases/20140217224858'" 1>&2; false; fi on localhost
DEBUG [da905ff7] Command: if test ! -d /home/sprvps_userucewo/rails_deployments/dev.www/releases/20140217224858; then echo "Directory does not exist '/home/vps_user/rails_deployments/dev.www/releases/20140217224858'" 1>&2; false; fi
DEBUG [da905ff7] Finished in 0.044 seconds with exit status 0 (successful).
INFO [0562438c] Running ~/.rvm/bin/rvm default do bundle exec rake assets:precompile on localhost
DEBUG [0562438c] Command: cd /home/vps_user/rails_deployments/dev.www/releases/20140217224858 && ( RAILS_ENV=development ~/.rvm/bin/rvm default do bundle exec rake assets:precompile )
DEBUG [0562438c] rake aborted!
DEBUG [0562438c] uninitialized constant BetterErrors
Is there something wrong with my setup? I'm using Capistrano v3+, with Ruby v2.1.0 inside rvm.
Gemfile:
if RUBY_PLATFORM !~ /mingw/
gem 'capistrano-rails'
gem 'capistrano-rvm'
gem 'capistrano-bundler'
end
Capfile:
# Load DSL and Setup Up Stages
require 'capistrano/setup'
# Includes default deployment tasks
require 'capistrano/deploy'
require 'capistrano/rails'
require 'capistrano/rvm'
# require 'capistrano/rbenv'
# require 'capistrano/chruby'
require 'capistrano/bundler'
# require 'capistrano/rails/assets'
# require 'capistrano/rails/migrations'
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }
deploy.rb:
# config valid only for Capistrano 3.1
lock '3.1.0'
set :application, 'website'
set :repo_url, 'git#bitbucket.org:MyUserName/website.git'
set :user, 'vps_user'
set :tmp_dir, '/home/vps_user/tmp'
# Default value for keep_releases is 5
set :keep_releases, 3
SSHKit.config.command_map[:rake] = "bundle exec rake"
SSHKit.config.command_map[:rails] = "bundle exec rails"
# Common directories (usually assets)
set :linked_dirs, %w{ public/assets/emails public/assets/events public/assets/photographs public/assets/updates public/assets/video public/assets/wines }
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
deploy/development.rb
set :branch, :develop
set :stage, :development
set :rails_env, 'development'
set :deploy_to, '/home/vps_user/rails_deployments/dev.www'
server 'localhost', user: 'vps_user', roles: %w{web app}
deploy/production.rb
set :branch, :master
set :stage, :production
set :rails_env, 'production'
set :deploy_to, '/home/vps_user/rails_deployments/www'
server 'localhost', user: 'vps_user', roles: %w{web app}
And I run the deploy command as such: bundle exec cap development deploy
OK so looking at Capistrano::Bundler under 'Usage' I saw an option in there called :bundle_without and it looked promising. So I put set :bundle_without, 'production' inside of my development.rb deploy script and it worked!
This doesn't solve the issue that Capistrano keeps trying to generate precompiled assets but I'm sure there's a solution out there like overriding the rake task or something.
Related
Okay,
I'm running into some weird issue with our deployment no longer working anymore. I cannot track everything that changed, but I'm hoping someone can help me out with debugging this issue and find out what is going wrong.
deploy.rb
# config valid only for current version of Capistrano
lock '3.4.0'
set :application, 'app_name'
set :repo_url, 'github_url'
set :deploy_to, '/home/user/path'
# Source Control Configuration
set :scm, :git
# Produces: Please enter the name of the branch. (develop):
set(:branch, ask('the name of the branch.', 'develop'))
# Ask whether we want to perform a backup.
# Produces: Please enter whether to perform a backup of the database? (Y):
set(:backup, ask('whether to perform a backup of the database?', 'Y'))
puts("setting branch to: #{fetch(:branch)}")
set :user, 'my_user'
set :assets_roles, [:web, :app, :vm]
set :keep_assets, 2
set :linked_files, fetch(:linked_files, []).push('.chamber.pem', 'Procfile', '.env')
set :linked_dirs, fetch(:linked_dirs, []).push('data', 'tmp', 'log', 'uploads')
set :keep_releases, 10
set :bundle_path, nil
set :bundle_binstubs, nil
set :bundle_flags, '--system'
set :rvm1_map_bins, fetch(:rvm1_map_bins, []).push('honeybadger')
# Slack integration options
set :slack_via_slackbot, Settings.slack.via_slackbot
set :slack_team, Settings.slack.team
set :slack_token, Settings.slack.token
set :slack_icon_emoji, -> { Settings.slack.icon_emoji }
set :slack_channel, -> { Settings.slack.channel }
set :slack_username, -> { Settings.slack.username }
set :slack_run_starting, -> { Settings.slack.run_starting }
set :slack_run_finished, -> { Settings.slack.run_finished }
set :slack_run_failed, -> { Settings.slack.run_failed }
set :slack_msg_finished, -> { "#channel #{ENV['USER'] || ENV['USERNAME']} has deployed branch *#{fetch :branch}* of #{fetch :application} to *#{fetch :rails_env}*." }
set :slack_msg_failed, -> { "#{ENV['USER'] || ENV['USERNAME']} failed to deploy branch #{fetch :branch} of #{fetch :application} to #{fetch :rails_env, 'production'}." }
# Rake config
SSHKit.config.command_map[:rake] = 'bundle exec rake'
before 'deploy:started', 'deploy:rm:stop'
after 'deploy:rm:stop', 'deploy:rm:backup'
after 'deploy:finished', 'deploy:rm:start'
after 'deploy:migrate', 'deploy:rm:seed
Capfile
# Load DSL and set up stages
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
require 'rvm1/capistrano3' # Do not use capistrano/bundler
require 'whenever/capistrano'
require 'capistrano/honeybadger'
require 'slackistrano/capistrano'
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
require 'net/ssh/proxy/command'
def vm(tunnel:, tunnel_user: fetch(:user), vm_user: fetch(:user))
server "#{fetch(:stage)}-vm01", {
roles: [:vm],
# The user on the VM
user: vm_user,
ssh_options: {
# The user on the machine we're tunneling through
proxy: Net::SSH::Proxy::Command.new("ssh #{tunnel_user}##{tunnel} -W %h:%p"),
}
}
end
So I can run the cap command locally, and it does part of the work. Had some issues first with the new sprockets, but now it's complaining about the following when it needs to precompile the assets:
INFO [21eca199] Running bundle exec rake assets:precompile as riskmethods#playground.riskmethods.net DEBUG [21eca199] Command: cd /home/riskmethods/riskmethods/releases/20160212152030 && ( export RAILS_ENV="playground" ; bundle exec rake assets:precompile )
DEBUG [2eac7a5b] Finished in 0.078 seconds with exit status 0 (successful).
INFO [4b29f0c4] Running bundle exec rake assets:precompile as riskmethods#playground-vm01
DEBUG [4b29f0c4] Command: cd /home/riskmethods/riskmethods/releases/20160212152030 && ( export RAILS_ENV="playground" ; bundle exec rake assets:precompile )
DEBUG [21eca199] bash: bundle: command not found
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as riskmethods#playground.riskmethods.net: rake exit status: 127
rake stdout: Nothing written
rake stderr: bash: bundle: command not found
SSHKit::Command::Failed:
rake exit status: 127
rake stdout: Nothing written
rake stderr: bash: bundle: command not found
Tasks: TOP => deploy:assets:precompile
(See full trace by running task with --trace)
The deploy has failed with an error:
Exception while executing as riskmethods#playground.riskmethods.net:
rake exit status: 127
rake stdout: Nothing written
rake stderr: bash: bundle: command not found
DEBUG [4b29f0c4] bash: bundle: command not found
As your error message saying bash: bundle: command not found
It seems you dont have the gem bundler installed on the server
install bundler gem on server by running the following command that should do the trick
gem install bundler
I think I have set up Capistrano3 correctly on my machine and that it should be connecting to my server. When I run cap production deploy (on my machine) I receive the following error messenger:
DEBUG [2959f403] Running /usr/bin/env [ -d ~/.rbenv/versions/2.1.2 ] as [user#myIP]
DEBUG [2959f403] Command: [ -d ~/.rbenv/versions/2.1.2 ]
DEBUG [2959f403] Finished in 0.420 seconds with exit status 1 (failed).
ERROR rbenv: 2.1.2 is not installed or not found in ~/.rbenv/versions/2.1.2
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as [user#myIP]: exit
SystemExit: exit
Tasks: TOP => rbenv:validate
(See full trace by running task with --trace)
On my machine (iMac):
$ ruby -v
ruby 2.1.2p95
$ rbenv -v
rbenv 0.4.0
$ which rebind
/usr/local/bin/rbenv
On my server (ubuntu)
$ ruby -v
ruby 2.1.2p95
$ rbenv -v
rbenv 0.4.0-151-g83ac0fb
$ which rebind
/home/[user]/.rbenv/bin/rbenv
Any ideas how to fix this?
** More Info From Comments **
deploy.rb
# config valid only for current version of Capistrano
lock '3.4.0'
set :application, [app name]
set :repo_url, [repo location]
set :repository, [repo location]
set :scm, :git
set :deploy_user, [user]
set :user, [user]
set :use_sudo, false
set :rails_env, "production"
set :deploy_via, :copy
# setup rvm.
# how many old releases do we want to keep
set :keep_releases, 5
# files we want symlinking to specific entries in shared.
set :linked_files, %w{config/database.yml config/application.yml}
# dirs we want symlinking to shared
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
# what specs should be run before deployment is allowed to
# continue, see lib/capistrano/tasks/run_tests.cap
set :tests, []
# which config files should be copied by deploy:setup_config
# see documentation in lib/capistrano/tasks/setup_config.cap
# for details of operations
set(:config_files, %w(
nginx.conf
database.example.yml
log_rotation
monit
unicorn.rb
unicorn_init.sh
))
# which config files should be made executable after copying
# by deploy:setup_config
set(:executable_config_files, %w(
unicorn_init.sh
))
# files which need to be symlinked to other parts of the
# filesystem. For example nginx virtualhosts, log rotation
# init scripts etc.
set(:symlinks, [
{
source: "nginx.conf",
link: "/etc/nginx/sites-enabled/#{fetch(:full_app_name)}"
},
{
source: "unicorn_init.sh",
link: "/etc/init.d/unicorn_#{fetch(:full_app_name)}"
},
{
source: "log_rotation",
link: "/etc/logrotate.d/#{fetch(:full_app_name)}"
},
{
source: "monit",
link: "/etc/monit/conf.d/#{fetch(:full_app_name)}.conf"
}
])
namespace :deploy do
# make sure we're deploying what we think we're deploying
before :deploy, "deploy:check_revision"
# only allow a deploy with passing tests to deployed
before :deploy, "deploy:run_tests"
# compile assets locally then rsync
after :finishing, 'deploy:cleanup'
after 'deploy:publishing', 'deploy:restart'
end
Server
$ ls /home/[user]/.rbenv/versions/
[blank... ]
$
If /home/[user]/.rbenv/versions is an empty directory on the server, then you have no versions of ruby installed.
Try running rbenv install 2.1.2 on the server. You may need to install the ruby-build plugin first.
I'm trying to deploy with Capistrano and RVM and even though when running cap bundler:install_bundler which is supposed to install bundler, and succeeds. After, when running cap deploy it fails with an error that bundler is not installed.
** [out :: victorstan.com] ERROR: Gem bundler is not installed, run `gem install bundler` first.
Any ideas why it installs bundler, but then can't find it, or installs it in the wrong place (i guess?). Thanks!
here is part of my deploy.rb script:
set :application, "dinner"
set :domain, "victorstan.com"
set :repository, "ssh://#{domain}/~/#{application}.git"
set :use_sudo, false
set :user, "victorstan" # The server's user for deploys
set :scm, :git
set :scm_username, "passenger"
set :deploy_to, "/srv/www/#{application}"
set :keep_releases, 2
set :branch, "master"
set :deploy_via, :copy # won't make cap prompt for password when deploying
set :scm_verbose, true
set :rvm_ruby_string, 'ruby-2.0.0-p195#dinner'
set :rvm_type, :user
set :rvm_install_type, :stable
set :whenever_command, "bundle exec whenever"
require "bundler/capistrano"
require "rvm/capistrano"
require "whenever/capistrano"
If I try to use cap to install bundler, it works:
cap bundler:install_bundler
* 2013-06-08 15:33:54 executing `bundler:install_bundler'
* executing "gem install bundler"
servers: ["victorstan.com"]
[victorstan.com] executing command
** [out :: victorstan.com] Successfully installed bundler-1.3.5
** [out :: victorstan.com] 1 gem installed
command finished in 1373ms
And here is the output when running cap deploy:
Switched to a new branch 'deploy'
command finished in 984ms
* Compressing /var/folders/w4/_6g_9lqd491_6cmhzb0kcmzh0000gn/T/20130608192600 to /var/folders/w4/_6g_9lqd491_6cmhzb0kcmzh0000gn/T/20130608192600.tar.gz
executing locally: tar czf 20130608192600.tar.gz 20130608192600
command finished in 53ms
servers: ["victorstan.com"]
** sftp upload /var/folders/w4/_6g_9lqd491_6cmhzb0kcmzh0000gn/T/20130608192600.tar.gz -> /tmp/20130608192600.tar.gz
[victorstan.com] /tmp/20130608192600.tar.gz
[victorstan.com] done
* sftp upload complete
* executing "cd /srv/www/dinner/releases && tar xzf /tmp/20130608192600.tar.gz && rm /tmp/20130608192600.tar.gz"
servers: ["victorstan.com"]
[victorstan.com] executing command
command finished in 467ms
* 2013-06-08 15:26:05 executing `deploy:finalize_update'
triggering before callbacks for `deploy:finalize_update'
* 2013-06-08 15:26:05 executing `bundle:install'
* executing "cd /srv/www/dinner/releases/20130608192600 && bundle install --gemfile /srv/www/dinner/releases/20130608192600/Gemfile --path /srv/www/dinner/shared/bundle --deployment --quiet --without development test"
servers: ["victorstan.com"]
[victorstan.com] executing command
** [out :: victorstan.com] ERROR: Gem bundler is not installed, run `gem install bundler` first.
command finished in 598ms
*** [deploy:update_code] rolling back
* executing "rm -rf /srv/www/dinner/releases/20130608192600; true"
servers: ["victorstan.com"]
[victorstan.com] executing command
command finished in 513ms
failed: "rvm_path=$HOME/.rvm/ $HOME/.rvm/bin/rvm-shell 'ruby-2.0.0-p195#dinner' -c 'cd /srv/www/dinner/releases/20130608192600 && bundle install --gemfile /srv/www/dinner/releases/20130608192600/Gemfile --path /srv/www/dinner/shared/bundle --deployment --quiet --without development test'" on victorstan.com
is ruby-2.0.0-p195#dinner installed / created on the server?
you can ensure it is with adding:
before 'deploy:setup', 'rvm:install_ruby'
more details on using rvm-capistrano can be found here: https://github.com/wayneeseguin/rvm-capistrano#readme
I have read some more examples on setting up deploy script and i've re-arranged the order of elements of my deploy script, as well as ena
set :application, "dinner"
set :domain, "victorstan.com"
set :repository, "ssh://#{domain}/~/#{application}.git"
set :use_sudo, false
set :user, "victorstan" # The server's user for deploys
set :scm, :git
set :scm_username, "passenger"
set :scm_passphrase, "*********" # The deploy user's password
set :deploy_to, "/srv/www/#{application}"
set :keep_releases, 2
set :branch, "master"
set :deploy_via, :copy # won't make cap prompt for password when deploying
set :scm_verbose, true
set :rvm_ruby_string, :local
set :rvm_type, :user
set :rvm_install_type, :stable
set :default_shell, :bash
set :whenever_command, "bundle exec whenever"
default_run_options[:pty] = true
# before 'deploy', 'rvm:install_ruby'
before 'deploy:setup', 'rvm:install_ruby'
require "rvm/capistrano"
require "bundler/capistrano"
require "whenever/capistrano"
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
Some notable elements that have changed:
set :rvm_ruby_string set to :local instead of explicitly naming it: 'ruby-2.0.0-p195#dinner', not sure if this has any implication on the issue though, seems unlikely.
set :default_shell, :bash was another parameter I added.
And, as I mentioned, the ordering or the different DSL types might make a difference: set comes first, then before hooks, then require and then role after which comes the rest of the deploy script. Perhaps this can help someone in a similar situation.
Note, when I first tested this script I had before 'deploy', 'rvm:install_ruby' enabled, but I have since disabled it and the script continues to work...
Now I must admit that I'm stumbling around in the dark a little bit as far as this deployment lark is concerned. I'll try and explain the situation best I can; I have set up a test deployment server and am trying to deploy my app to it with capistrano, however, I am encountering some difficulties surrounding my gems and their dependencies, as the error below shoes.
[mike-test] executing command
[mike-test] rvm_path=$HOME/.rvm/ $HOME/.rvm/bin/rvm-shell '1.9.3' -c 'cd /home/deploy/myapp/releases/20120910081544 && bundle install --gemfile /home/deploy/myapp/releases/20120910081544/Gemfile --path /home/depoy/myapp/shared/bundle --deployment --quiet --without development test'
** [out :: mike-test] Some gems seem to be missing from your vendor/cache directory.
** [out :: mike-test] Could not find log4r-1.1.10 in any of the sources
command finished in 9134ms
*** [deploy:update_code] rolling back
log4r isn't in my gemfile so I can only assume it's a (perhaps production only?) dependency of another gem. I don't know why bundler isn't downloading the gem and installing it if it can't find it? I put the gem in my gemfile, ran bundle install locally, and then committed and deployed again and got the same error but with a different gem this time (spreadsheet), so that appeared to solve the error in that case only, but doesn't identify the problem.
Something else to muddy the water, I'm trying to use RVM on the production server and despite reading a lot about it I'm still not 100% sure I get how it works, so that might be a factor.
My deploy.rb:
require "bundler/capistrano"
require "rvm/capistrano"
# SCM Settings
set :rvm_ruby_string, '1.9.3'
set :use_sudo, false
ssh_options[:forward_agent] = true
default_run_options[:pty] = true
set :branch, :mikedev
set :deploy_via, :remote_cache
set :copy_cache, true
set :git_enable_submodules, 0
set :repository, "our_git_repo.git"
set :scm, :git
set :user, :deploy
set :keep_releases, 1
set :application, "myapp"
set :deploy_to, "/home/deploy/myapp"
set :branch, "mikedev"
role :web, "mike-test"
role :app, "mike-test"
role :db, "mike-test", :primary => true
namespace :deploy do
desc "Restarting mod_rails with restart.txt"
task :restart, :roles => :app, :except => { :no_release => true } do
run "touch #{current_path}/tmp/restart.txt"
end
[:start, :stop].each do |t|
desc "#{t} task is a no-op with mod_rails"
task t, :roles => :app do ; end
end
end
Any guidance would be much appreciated.
bundle install --deployment ... will not download any gems if vendor/cache is present. It'll look for gems just there. There are two options:
remove vendor/cache directory (even if it's already empty) from your VCS
or run bundle package and add all new files under vendor/cache to your VCS
The latter seems to be better option. In this way you protect your deployment from outages of rubygems servers.
I deployed my rails 3 app onto the production server using Capistrano. The deployment is correct, the application server is correctly restarted but when I log onto the production server and go into "my_path/current", I cannot run "rails c":
The program 'rails' is currently not installed. You can install it by typing:
sudo apt-get install rails
I checked the gem list and everything seems to be correctly installed though.
My config/deploy.rb file is:
require "bundler/capistrano"
# Add RVM's lib directory to the load path.
$:.unshift(File.expand_path('./lib', ENV['rvm_path']))
# Load RVM's capistrano plugin.
require "rvm/capistrano"
set :rvm_ruby_string, '1.9.3'
set :rvm_type, :user # Don't use system-wide RVM
set :application, "MyApp"
set :repository, "git#github.com:user/myapp.git"
set :scm, :git
set :branch, "master"
set :scm_verbose, true
set :scm_username, "user" # GITHUB user name
set :user, "user" # VPS user name
set :scm_passphrase, "pass" # VPS password
set :deploy_to, "/home/luc/be"
set :use_sudo, false
server "my_server", :web, :app, :db, :primary => true
# start / stop application server
namespace :deploy do
task :start do
invoke_command "cd /home/luc/be/current; bundle exec thin start -C config/thin.config"
end
task :stop do
invoke_command "cd /home/luc/be/current; bundle exec thin stop -C config/thin.config"
end
task :restart do
invoke_command "cd /home/luc/be/current; bundle exec thin restart -C config/thin.config"
end
end
Try bundle exec rails c. Also check what error, if any, is thrown when you visit the page in your browser