Why capistrano check non existence of rbenv directory? - testing

I get following message when I run cap production deploy in capistrano 3.
DEBUG [5021b4ce] Running /usr/bin/env [ ! -d /usr/local/rbenv/versions/2.0.0-p451 ] on example.com
DEBUG [5021b4ce] Command: [ ! -d /usr/local/rbenv/versions/2.0.0-p451 ]
DEBUG [5021b4ce] Finished in 0.853 seconds with exit status 1 (failed).
This is rbenv part in config/deploy.rb.
set :rbenv_type, :system # or :user, depends on your rbenv setup
set :rbenv_ruby, '2.0.0-p451'
set :rbenv_path, '/usr/local/rbenv'
set :rbenv_prefix, "RBENV_ROOT=#{fetch(:rbenv_path)} RBENV_VERSION=#{fetch(:rbenv_ruby)} #{fetch(:rbenv_path)}/bin/rbenv exec"
set :rbenv_map_bins, %w{rake gem bundle ruby rails}
set :rbenv_roles, :all # default value
I thought Capistrano should check existence of rbenv directory.
Why the command is [ ! -d #{rbenv_dir}], instead of [ -d #{rbenv_dir}]?

Related

Capistrano 3 runs every command twice (new install) - Configuration issue

I just completed my capistrano installation for the first time. Most of everything is left to default settings, I configured my server, its authentification, and the remote folder, as well as the access to my git repository.
I use capistrano to deploy php code to my server.
cap staging deploy and cap production deploy function, but they run every command twice. It sometimes causes problems when those tasks are executed too quickly on the server, returning error codes, which stops the deploying process.
an example of my output when running cap staging deploy
DEBUG[47ecea59] Running /usr/bin/env if test ! -d ~/www/test_server/repo; then echo "Directory does not exist '~/www/test_server/repo'" 1>&2; false; fi on ftp.cluster013.ovh.net
DEBUG[47ecea59] Command: if test ! -d ~/www/test_server/repo; then echo "Directory does not exist '~/www/test_server/repo'" 1>&2; false; fi
DEBUG[c450e730] Running /usr/bin/env if test ! -d ~/www/test_server/repo; then echo "Directory does not exist '~/www/test_server/repo'" 1>&2; false; fi on ftp.cluster013.ovh.net
DEBUG[c450e730] Command: if test ! -d ~/www/test_server/repo; then echo "Directory does not exist '~/www/test_server/repo'" 1>&2; false; fi
It does the same with every single task, except the one I defined myself (in my deploy.rb, I defined a :set_distant_server task that moves around files with server info)
I am pretty sure I missed something during the initial configuration.
Here is my capfile, still to default settings :
# Load DSL and Setup Up Stages
require 'capistrano/setup'
# Includes default deployment tasks
require 'capistrano/deploy'
# Includes tasks from other gems included in your Gemfile
# 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/*.rake').each { |r| import r }
Followed by my deploy.rb file:
# config valid only for Capistrano 3.1
lock '3.2.1'
set :scm, :git
set :application, 'Application name'
# I use token authentification
set :repo_url, 'https://XXXXXXXXXXX:#XXXXXXX.git'
set :role, 'web'
# Default value for :log_level is :debug
set :log_level, :debug
set :tmp_dir, 'www/test_server/tmp'
set :keep_releases, 8
role :deploy_server, "XXXuser_name#XXXX_server"
task :set_distant do
on roles(:deploy_server) do
execute 'echo ------------******* STAGING *******------------'
execute 'cp ~/www/test_server/current/access_distant.php ~/www/test_server/current/access.php'
execute 'cp ~/www/test_server/current/session_distant.php ~/www/test_server/current/session.php'
end
end
after "deploy:finished", :set_distant
Here is my staging.rb, much shorter:
server 'XXX_server', user: 'XXXuser_name', roles: %w{web}, port: 22, password: 'XXXpassword'
set :deploy_to, '~/www/test_server'
set :branch, 'staging'
And my production.rb, very similar:
server 'XXX_server', user: 'XXXuser_name', roles: %w{web}, port: 22, password: 'XXXpassword'
set :deploy_to, '~/www/beta/'
I'm pretty sure I missed a step in all the prerequisites to make it run nicely. I am new to ruby, to gems, and didn't use shell for a very long time.
Does anyone see why those commands are run twice, and how I could fix it?
In advance, many many thanks.
Additional info:
Ruby version: ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]
Capistrano version: cap -V
Capistrano Version: 3.2.1 (Rake Version: 10.1.0)
I did not create a Gemfile or set it up, I understood it was not needed in Capistrano 3. Anyway, I would not know how to do it.
I was having this same issue and realized I didn't need both
role :web
and
server '<server>'
I got rid of role :web and that got rid of the 2nd execution.

Permissions issues with bundler install

I am working on a cap/deploy script for my rails application. On my server, I am using RVM to manage my ruby install.
The issue that I am seeing is even though I have set the runner/user/group to the correct user I am getting permissions errors when I try to install gems.
This is the error that I am seeing:
** loading stage recipe 'deploy'
** loading stage recipe 'init_config'
** loading stage recipe 'nginx'
executing locally: "git ls-remote ############################### ec2"
command finished in 1113ms
* executing `deploy:setup'
triggering before callbacks for `deploy:setup'
* executing `init_config'
* executing "whoami"
servers: ["ec2-23-23-24-42.compute-1.amazonaws.com"]
[ec2-23-23-24-42.compute-1.amazonaws.com] executing command
** [out :: ec2-23-23-24-42.compute-1.amazonaws.com] ubuntu
command finished in 529ms
* executing "gem install bundler"
servers: ["ec2-23-23-24-42.compute-1.amazonaws.com"]
[ec2-23-23-24-42.compute-1.amazonaws.com] executing command
** [out :: ec2-23-23-24-42.compute-1.amazonaws.com] ERROR: While executing gem ... (Gem::FilePermissionError)
** [out :: ec2-23-23-24-42.compute-1.amazonaws.com] You don't have write permissions into the /home/ubuntu/.rvm/gems/ruby-1.9.2-p290 directory.
command finished in 1026ms
*** failed: "bash --login -c 'gem install bundler'" on ec2-23-23-24-42.compute-1.amazonaws.com
This is my export cap file:
load 'deploy'
# ================================================================
# ROLES
# ================================================================
role :app, "ec2-23-23-24-42.compute-1.amazonaws.com"
# ================================================================
# VARIABLES
# ================================================================
# Webistrano defaults
set :webistrano_project, "sweet_high"
set :webistrano_stage, "staging"
set :application, ""
set :branch, "ec2"
set :bundle_disable_shared_gems, "\"1\""
set :bundle_path, "vendor/bundler_gems"
set :default_shell, "bash --login"
set :deploy_to, "/data/\#{application}"
set :deploy_via, :remote_cache
set :domain, ""
set :environment, "staging"
set :group, "ubuntu"
set :nginx_conf_path, "/etc/nginx/sites-enabled"
set :password, "deployment_user(SSH user) password"
set :rails_env, "staging"
set :repository, "t"
set :runner, "ubuntu"
set :rvm_bin_path, "/home/#{user}/.rvm/bin/rvm"
set :rvm_type, :user
set :scm, :git
set :scm_password, "your_SVN_password"
set :scm_username, "your_SVN_user"
set :ssh_options, {:forward_agent => true,:paranoid => false,:keys => ["/home/#{user}/.ssh/rails_app_server.pem"]}
set :use_sudo, true
set :user, "ubuntu"
# ================================================================
# TEMPLATE TASKS
# ================================================================
# allocate a pty by default as some systems have problems without
default_run_options[:pty] = true
# set Net::SSH ssh options through normal variables
# at the moment only one SSH key is supported as arrays are not
# parsed correctly by Webistrano::Deployer.type_cast (they end up as strings)
[:ssh_port, :ssh_keys].each do |ssh_opt|
if exists? ssh_opt
logger.important("SSH options: setting #{ssh_opt} to: #{fetch(ssh_opt)}")
ssh_options[ssh_opt.to_s.gsub(/ssh_/, '').to_sym] = fetch(ssh_opt)
end
end
# ================================================================
# CUSTOM RECIPES
# ================================================================
before "deploy:setup", :init_config
after "deploy:setup", :setup_cleanup
namespace :init_config do
desc <<-DESC
Created directory structure, changes ownership, etc...
DESC
task :default do
run "gem install bundler"
run "sudo mkdir -p #{deploy_to}"
run "sudo chown -R #{user}:#{group} #{deploy_to}"
run "mkdir -p #{shared_path}/config"
run "mkdir -p #{shared_path}/config"
File.open("/home/#{user}/.ssh/id_rsa", "rb").each_line{ |line|
run "echo #{line} >> ~/.ssh/id_rsa"
}
run "chown #{user}:#{group} ~/.ssh/id_rsa"
run "chmod 600 ~/.ssh/id_rsa"
end
end
your task init_config:default needs small addition:
run "sudo chown -R #{user}:#{group} /home/#{user}/.rvm"
run "gem install bundler"

restart sunspot solr when make a deploy with capistrano in rails 3.1

I have a project rails 3.1 in production environment.
This is my deploy.rb now:
$:.unshift(File.expand_path('./lib', ENV['rvm_path'])) # Add RVM's lib directory to the load pathe
require "rvm/capistrano" # Load RVM's capistrano plugin.
require "bundler/capistrano"
set :rvm_ruby_string, 'ruby-1.9.2-p318#global'
set :rvm_type, :user
set :application, "domain.com"
set :user, 'user'
#set :repository, "#{user}#ip.ip.ip.ip:~/app"
set :repository, "ssh://git#bitbucket.org/user/app.git"
set :keep_releases, 3
set :scm, :git
set :use_sudo, false
set :deploy_to, "~/#{application}"
#set :deploy_via, :copy
set :unicorn_conf, "#{deploy_to}/current/config/unicorn.rb"
set :unicorn_pid, "#{deploy_to}/shared/pids/unicorn.pid"
set :deploy_via, :remote_cache
ssh_options[:forward_agent] = true
default_run_options[:pty] = true
role :web, "ip.ip.ip.ip" # Your HTTP server, Apache/etc
role :app, "ip.ip.ip.ip" # This may be the same as your `Web` server
role :db, "ip.ip.ip.ip", :primary => true # This is where Rails migrations will run
namespace :deploy do
task :restart do
run "if [ -f #{unicorn_pid} ] && [ -e /proc/$(cat #{unicorn_pid}) ]; then kill -USR2 `cat #{unicorn_pid}`; else cd #{deploy_to}/current && bundle exec unicorn -c #{unicorn_conf} -E #{rails_env} -D; fi"
end
task :start do
run "bundle exec unicorn -c #{unicorn_conf} -E #{rails_env} -D"
end
task :stop do
run "if [ -f #{unicorn_pid} ] && [ -e /proc/$(cat #{unicorn_pid}) ]; then kill -QUIT ` cat #{unicorn_pid}`; fi"
end
end
load 'deploy/assets'
after "deploy:restart", "deploy:cleanup"
I want make these tasks in capistrano. Now I perform these tasks manually:
1º I kill sunspot solr pid with:
a) Find the pid with ps aux | grep 'solr'
b) Kill pid with kill pid_number
2º Remove index solr in production environment if exist with:
a) rm -r solr/data/production/index
3º turn on sunspot solr with:
a) RAILS_ENV=production rake sunspot:solr:start
4º Reindex models with:
a) RAILS_ENV=production rake sunspot:mongo:reindex
My question is:
How can I add these tasks to my deploy.rb?
Thank you!
This might be a good starting point:
namespace :solr do
task :reindex do
run "cd #{current_path} && #{rake} RAILS_ENV=#{rails_env} sunspot:solr:reindex"
end
end
You can just call the rake sunspot:solr:stop instead of kill? I'm not sure you need to remove the index if you are going to do a reindex...
task :stop_solr do
begin
run("cd #{deploy_to}/current && /usr/bin/env rake sunspot:solr:stop RAILS_ENV=#{rails_env}")
rescue Exception => error
puts "***Unable to stop Solr with error: #{error}"
puts "***Solr may have not been started. Continuing anyway.***"
end
end
#restart and reindex any newly added full search fields:
task :restart_solr do
begin
run("cd #{release_path} && /usr/bin/env rake sunspot:solr:start RAILS_ENV=#{rails_env}")
rescue Exception => error
puts "***Unable to start Solr with error: #{error}."
puts "***Continuing anyway.***"
end
end
task :reindex_solr do
begin
run("cd #{release_path} && /usr/bin/env rake sunspot:reindex RAILS_ENV=#{rails_env}")
rescue Exception => error
puts "***Unable to reindex Solr with error: #{error}"
puts "***Continuing anyway.***"
end
end
Also, as I mentioned in my comment to Kris's answer, you'll have issues if you clean up old Capistrano directories, unless you kill the SOLR process and force it to point to new index files. One way to avoid this scenario is to set up SOLR in a shared directory and reset the symlink during deployment.

Mixed Mode RVM giving error when used in init script

I've installed RVM in Mixed Mode and have Phusion Passenger running in stand alone mode.
I've found this init script to start my Phusion Passenger standalone server on startup: http://memcloud.com/note/show/167
Modifying only the prescribed values, it was giving me the following error, but would still run
-su: /home/myuser/.rvm/bin/rvm: No such file or directory
I ran which rvm in myuser and found out that RVM is in /usr/local/rvm/bin/rvm. So I updated the RVM variable to reflect that, and changed RVM="$USER_HOME/.rvm/bin/rvm" to RVM="/usr/local/rvm/bin/rv". Now it's giving me the following message, but it still runs.
RVM is not a function, selecting rubies with 'rvm use ...' will not work.
Not really sure if it's a problem if the system is running, but I'd just like to be sure.
I would say this script is wrong, you should use something more like this:
#!/usr/bin/env bash
### BEGIN INIT INFO
# Provides: my-app passenger in standalone
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/stop my-app web site
### END INIT INFO
# BEGIN MINIMAL CHANGES
USER=www-data
USER_HOME=/var/www
APP_PATH=/var/www/my-app/current
GEM_SET=ruby-1.8.7-p330#my-app
ADDRESS=127.0.0.1
PORT=3000
ENVIRONMENT=production
# END MINIMAL CHANGES
RVM="/usr/local/rvm/bin/rvm"
PASSENGER="$USER_HOME/.rvm/gems/$GEM_SET/bin/passenger"
PASSENGER="cd $APP_PATH; $RVM $GEM_SET do $PASSENGER"
CMD_START="$PASSENGER start -a $ADDRESS -p $PORT -e $ENVIRONMENT -d"
CMD_STOP="$PASSENGER stop -p $PORT"
. /lib/lsb/init-functions
case "$1" in
start)
echo "Starting myapp passenger"
echo $CMD_START
su - $USER -c "$CMD_START"
;;
stop)
echo "Stopping myapp passenger"
echo $CMD_STOP
su - $USER -c "$CMD_STOP"
;;
*)
echo "Usage: $0 start|stop" >&2
exit 3
;;
esac
you could also replace GEM_SET=. to make rvm use ruby stored in .rvmrc but this requires that $USER trusted that .rvmrc ... which could be also done in this script with:
su - $USER -c "rvm rvmrc trust $APP_PATH"
called as first line in start)

Deploying Ruby on rails on Deamhost fails

I'm trying to deploy a simple Ruby on Rails app to dreamhost but I'm getting an error related to the bundle command. Here is the error:
servers: ["bullseye.dreamhost.com"]
[bullseye.dreamhost.com] executing command
command finished
* executing "find /home/USER-NAME/MY-DOMAIN/releases/20110123014150/public/images /home/USER-NAME/MY-DOMAIN/releases/20110123014150/public/stylesheets /home/USER-NAME/MY-DOMAIN/releases/20110123014150/public/javascripts -exec touch -t 201101230141.52 {} ';'; true"
servers: ["bullseye.dreamhost.com"]
[bullseye.dreamhost.com] executing command
command finished
triggering after callbacks for `deploy:update_code'
* executing `bundle:install'
* executing "ls -xt /home/USER-NAME/MY-DOMAIN/releases"
servers: ["bullseye.dreamhost.com"]
[bullseye.dreamhost.com] executing command
command finished
* executing "bundle install --gemfile /home/USER-NAME/MY-DOMAIN/releases/20110123014150/Gemfile --path /home/USER-NAME/MY-DOMAIN/shared/bundle --deployment --quiet --without development test"
servers: ["bullseye.dreamhost.com"]
[bullseye.dreamhost.com] executing command
*** [err :: bullseye.dreamhost.com] sh: bundle: command not found
command finished
*** [deploy:update_code] rolling back
* executing "rm -rf /home/USER-NAME/MY-DOMAIN/releases/20110123014150; true"
servers: ["bullseye.dreamhost.com"]
[bullseye.dreamhost.com] executing command
command finished
failed: "sh -c 'bundle install --gemfile /home/USER-NAME/MY-DOMAIN/releases/20110123014150/Gemfile --path /home/USER-NAME/MY-DOMAIN/shared/bundle --deployment --quiet --without development test'" on bullseye.dreamhost.com
Here is my deploy.rb file.
require 'bundler/capistrano'
set :user, "MY-USERNAME"
set :password, "MY-PASSWORD"
set :domain, 'bullseye.dreamhost.com' # Dreamhost servername where your account is located
set :project, 'blog' # Your application as its called in the repository
set :application, 'MY-DOMAIN' # Your app's location (domain or sub-domain name as setup in panel)
set :applicationdir, "/home/#{user}/#{application}" # The standard Dreamhost setup
# version control config
set :scm_username, 'MY-SVN-USERNAME'
set :scm_password, 'MY-SVN-PWD'
set :repository, "http://MY-SVN-URL/01/blog/"
# roles (servers)
role :web, domain
role :app, domain
role :db, domain, :primary => true
# deploy config
set :deploy_to, applicationdir
set :deploy_via, :export
# additional settings
default_run_options[:pty] = false # Forgo errors when deploying from windows
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
I found a similar question at Bundle install failing when deploying a Rails 3 app to Dreamhost with Capistrano but the solution didn't work. Anybody have any ideas as to what might be happening in my case?
I got this figured out eventually. What I did was
ssh into the dreamhost server and execute which bundle command
from ssh session execute echo $PATH command
edit config/deploy.rb and combine both strings with a : between and place inside default_environment PATH value, see below
set :default_environment, {
'PATH' => "'/usr/lib/ruby/gems/1.8/bin//bundle:/home/sayed3/.gems/bin:/usr/lib/ruby/gems/1.8/bin/:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games'"
}
Once I did this it was able to execute the bundle command successfully, but then I started running into some other issues. I eventually decided to use a VPS that I have hosted elsewhere, but I'm sure if I spent a bit more time I could have figured it out.
Besides adding the path to bundle, as specified above, I also had to add the following line to my config/deploy.rb in order to force capistrano to use bash, instead of the default shell, which, on dreamhost, is dash:
set :shell, '/bin/bash'