Sudo doesn't have access to shell commands when deploying with Capistrano - ruby-on-rails

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

Related

Amazon EC2 still uses the old code, not the newly deployed one

I am deploying code with Capistrano, this is the content of deploy.rb:
require 'bundler/capistrano'
set :application, "project_name"
set :use_sudo, false
set :scm, :git
set :repository, "git#bitbucket.org:my_name/fileito.git"
set :branch, "master"
set :deploy_via, :remote_cache
#set :deploy_via, :copy
set :user, "deployer"
set :password, "password"
set :deploy_to, "/home/deployer/project_name"
#set :app_site, "ec2-xx-xxx-xx-xxx.compute-1.amazonaws.com"
set :app_site, "xx.xxx.xxx.xxx"
role :web, app_site # Your HTTP server, Apache/etc
role :app, app_site # This may be the same as your `Web` server
role :db, app_site, :primary => true # This is where Rails migrations will run
require 'capistrano-unicorn'
after 'deploy:restart', 'unicorn:reload' # app IS NOT preloaded
after 'deploy:restart', 'unicorn:restart' # app preloaded
set :whenever_command, "bundle exec whenever"
require "whenever/capistrano/recipes"
# Added:
after "deploy:stop", "deploy:start"
namespace :deploy do
task :start do
run "/etc/init.d/apache2 start"
end
task :stop do
run "/etc/init.d/apache2 stop"
end
task :restart, :roles => :app, :except => { :no_release => true } do
run "touch #{File.join(current_path,'tmp','restart.txt')}"
end
end
But when I deploy the new code and check the app in the browser, I see there still the old code. What's wrong? When I take a look into the current directory, there is the new code - I manually checked the files through the cat command.
How is possible that in browser is still the old version -> the old code?
Thanks
There are a variety of reasons that this can happen. Here are a few links you should check:
Unicorn continues to use old code following deploy + restart
Unicorn restarting in old release directory
Restarting Unicorn with USR2 doesn't seem to reload production.rb settings

How can I get Capistrano to deploy without prompting for a password?

When I run cap deploy at a certain point towards the end of the process, I get a password prompt...
** keeping 5 of 6 deployed releases
* executing "sudo -p 'sudo password: ' rm -rf /opt/deployed_rails_apps/myapp/releases/20120922200242"
servers: ["myhost.com"]
[username#myhost.com] executing command
Password:
Is there a way I can get it to complete without prompting me to enter a password?
Here's my deploy.rb...
require "bundler/capistrano"
require "rvm/capistrano"
set :rvm_ruby_string, 'ruby-1.9.3-p194#run_passenger'
set :rvm_type, :user
set :application, 'myapp'
set :repository, 'git#github.com:fakename/myrepository.git'
set :deploy_to, "/opt/deployed_rails_apps/#{application}"
set :scm, "git"
set :branch, "master"
set :deploy_via, :remote_cache
set :keep_releases, 5
after "deploy:update", "deploy:cleanup"
load 'deploy/assets'
role :app, 'username#myhost.com'
role :web, 'username#myhost.com'
namespace :deploy do
task :start, :roles => :app do
run "touch #{current_release}/tmp/restart.txt"
end
task :stop, :roles => :app do
# Do nothing.
end
desc "Restart Application"
task :restart, :roles => :app do
run "touch #{current_release}/tmp/restart.txt"
end
end
Assuming your deployment user has appropriate permissions and you don’t need to use sudo, you can set :use_sudo to false and Capistrano won’t use it.
Just add
set :use_sudo, false
to your deploy.rb.
Set up your SSH public key on the machine you're deploying to. This isn't a Capistrano thing. Here's one guide. There are many others that you can find with a quick search.
If you really need root permission in order to delete files under /opt/deployed_rails_apps/myapp/releases/ then you can edit the /etc/sudoers file to allow your deploy user to use sudo without the password.
But I suspect the answer by #matt using set :use_sudo, false should take care of your problem.

Capistrano cap deploy:setup does not create any files

I don't get any error message while I execute cap deploy:setup. It attempts to create folder /myapp as mkdir -p /home/user/apps/myapp, and also other folders (releases, shared). There is no permission issue as I can execute mkdir -p /home/user/apps/myapp without any error in terminal. Furthermore, cap deploy:check gives You appear to have all necessary dependencies installed message. Am I supposed to have those folders created with cap deploy:setup? If so, why am I not getting any error message and also the folders created?
Here's my config/deploy.rb file:
set :application, "myapp"
set :location, "myserver"
set :domain, "apps.mydomain.com"
set :user, "app_admin"
set :repository, "svn+ssh://app_admin#192.168.XXX.XXX/home/svn/myapp"
set :svn_username, "svn_admin"
set :svn_password, "password"
set :use_sudo, false
set :scm, :subversion
default_run_options[:pty] = true
role :web, location
role :app, location
role :db, location, :primary => true
set :deploy_to, "/home/app_admin/#{application}"
set :deploy_via, :checkout
ssh_options[:forward_agent] = true
namespace :deploy do
task :start, :roles => :app do
run "touch #{current_path}/tmp/restart.txt"
end
task :stop, :roles => :app do
#do nothing
end
desc "Restart Application"
task :restart, :roles => :app do
run "touch #{current_path}/tmp/restart.txt"
end
end
What capistrano version? What OS?
I have problems with Capistrano in Ubuntu - try Windows! I've usually found that Windows is the best server platform. Except for the infrequent crashes, windows server is very powerful.

Capistrano Deploy Error

I've recently had to make some updates to an app that I haven't touched in about a year. When attempting to deploy with Capistrano (via Github), I get this error:
[deploy:update_code] exception while rolling back: IOError, closed stream
Full logged error here: https://gist.github.com/2829751
I reinstalled my server's SSH key after the Github SSH security scare. Nothing should have changed on the remote server, and deploying worked fine previously. The only significant change on my local system is moving to RVM.
Any ideas what's causing the error?
Here's my deploy.rb file, if that helps:
default_run_options[:pty] = true
set :domain, 'xxx.xxx.xxx'
set :repository, "XXX MY REPO XXX"
set :branch, 'master'
set :password, 'XXXXXXX'
set :deploy_to, "/var/www/#{domain}"
set :scm, :git
set :repository_cache, "git_cache"
set :deploy_via, :remote_cache
ssh_options[:paranoid] = false
set :user, "XXX"
set :runner, user
set :use_sudo, true
set :rails_env, 'production'
role :app, domain # multiple domains can be added here for parallel deployment (i.e. test_app)
role :web, domain
role :db, domain, :primary => true
namespace :deploy do
task :start, :roles => :app do
run "touch #{release_path}/tmp/restart.txt"
end
task :stop, :roles => :app do
# Do nothing.
end
desc "Restart Application"
task :restart, :roles => :app do
run "touch #{release_path}/tmp/restart.txt"
end
end
deploy.task :cold do
deploy.update
deploy.create_db
deploy.migrate
deploy.restart # Change to start if we're using mongrels
end
after "deploy:update_code", :update_config
after "deploy:restart", "delayed_job:restart"
after "deploy", "deploy:cleanup"
#links config files from shared to the release path and mongrel path after deployment
desc "Create all symlinks and files needed for app activation ofter deployment"
task :update_config, :roles => :web do
run "ln -s -f /var/www/#{domain}/shared/database.yml #{release_path}/config/database.yml"
run "ln -s -f /var/www/#{domain}/shared/app.yml #{release_path}/config/app.yml"
run "ln -s -f /var/www/#{domain}/shared/cache #{release_path}/public/cache"
run "ln -s -f /var/www/#{domain}/shared/survey_cache #{release_path}/public/surveys"
run "ln -s -f /var/www/#{domain}/shared/surveys #{release_path}/surveys"
end
desc "changes ownership to cbdsm:git"
task :update_permissions, :roles => :web do
sudo "chown -R #{user}:git /var/www/#{domain}"
end
namespace :delayed_job do
desc "Start the delayed_job process"
task :start, :roles => :app do
run "cd #{current_path} && RAILS_ENV=#{rails_env} script/delayed_job -n 3 start"
end
desc "Stop the delayed_job process"
task :stop, :roles => :app do
run "cd #{current_path} && RAILS_ENV=#{rails_env} script/delayed_job stop"
end
desc "Restart the delayed_job process"
task :restart, :roles => :app do
delayed_job.stop
delayed_job.start
end
end
UPDATE: This seems to be a problem set :use_sudo, true. Removing that line and any commands which require sudo seemed to fix the problem. It's still not entirely clear to me what changed--that made that line problematic. It worked fine before.
Additionally, I removed the default_run_options[:pty] = true line.
As posted above:
This seems to be a problem set :use_sudo, true. Removing that line and any commands which require sudo seemed to fix the problem. It's still not entirely clear to me what changed--that made that line problematic. It worked fine before.
Additionally, I removed the default_run_options[:pty] = true line.
In these cases post the /config/deploy.rb would help...
BTW, how do you set the :deploy_via setting? Try to change this to :remote_cache (from :copy):
set :deploy_via, :remote_cache
and see what happens.

How to deploy Rails 2.3.2 app with Capistrano from the top (for FastCGI)?

Disclaimer: i am a noob to Rails and Capistrano (but an intermediate .NET C# developer) trying to learn more.
i have been have the toughest time getting a Capistrano development process set-up. here's what i have so far for my deploy.rb:
set :application, "MyAppName"
set :domain, "domainname"
set :user, "userid"
set :repository, "svn+ssh://#{user}##{domain}/home/#{user}/svn/#{application}/trunk"
set :rails_env, :production
set :chmod755, %w(app config db lib public vendor script tmp public/dispatch.cgi public/dispatch.fcgi public/dispatch.rb)
set :deploy_to, "/home/#{user}/apps/#{application}"
set :use_sudo, false
set :deploy_via, :checkout
set :group_writable, false
default_run_options[:pty] = true
role :app, domain
role :web, domain
role :db, domain, :primary => true
here's my Capfile:
load 'deploy' if respond_to?(:namespace) # cap2 differentiator
Dir['vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
load 'config/deploy'
namespace :deploy do
task :restart, :roles => :app do
run "#{current_path}/script/process/reaper --dispatcher=dispatch.fcgi"
end
desc "Set the proper permissions for directories and files on HostingRails accounts"
task :after_deploy do
run(chmod755.collect do |item|
"chmod 755 #{current_path}/#{item}"
end.join(" && "))
end
end
Rails 2.3.2 doesn't have the script/process directory so the restart task doesn't work at all. How do I fix this?
Is there a way to set the RAILS_ENV variable in environment.rb to "production" when the application is deployed ("set :rails_env, :production" does not do it)?
Also, am I missing anything else from these 2 files?
Updated my answer, see #3...
I don't use FastCGI for deployment, but a few things that come to mind:
The process scripts have been moved out of Rails core and into a plugin, which you can grab from GitHub.
Looking at my deploy.rb file, you'll likely need to pass the RAILS_ENV environment variable into the script through the command line, similar to:
run #{current_path}/script/process/reaper RAILS_ENV=#{rails_env} --dispatcher=dispatch.fcgi
EDIT: Actually, it is slightly odd to have your tasks in the Capfile instead of deploy.rb - although looking at this thread on the Capistrano Google Group, it's not completely frowned upon. Up to you/your team, I suppose.
Hope this points you in the right direction.
Here's my deploy.rb, which hasn't really been updated in a long time. I run two sed commands to set the environment inside my :after_update_code task. I'm certain there's an easier way to do it, but this is my current method.
require 'erb'
before "deploy:setup", :db
after "deploy:update_code", "db:symlink"
after "deploy", "deploy:cleanup"
def prompt_with_default(var, default)
set(var) do
Capistrano::CLI.ui.ask "Enter #{var} [#{default}] : "
end
set var, default if eval("#{var.to_s}.empty?")
end
default_run_options[:pty] = true
set :rake, "/usr/local/bin/rake"
set :user, "your username"
set :application, "application name"
set :repository, "svn repository path"
set :host, "domain name"
set :db_host, "DB host name"
set :db_user, "DB user name"
set :db_prefix, "any DB name prefix your host might require"
set :rails_env, "production"
set :deploy_to, "/home/username/rails/#{host}" # your deployment directory
role :app, host
role :web, host
role :db, host, :primary => true
set :use_sudo, false
set :checkout, "export"
#
# TASKS
#
desc "Tasks to execute after code update"
task :after_update_code, :roles => [:app, :db, :web] do
# Make sure we're running in production
run "sed -i -e '/ENV.*RAILS_ENV/s/# //' #{release_path}/config/environment.rb"
run "sed -i -e '/ENV.*RAILS_ENV/s/production/#{rails_env}/' #{release_path}/config/environment.rb"
# Add banned IPs
deny_lines = []
File.readlines("#{release_path}/config/banned_ips").each {|ip|
deny_lines << "deny from #{ip}"
}
ip_ban_block = <<EOBAN
# Bannination
order allow,deny
#{deny_lines.join}
allow from all
EOBAN
run "sed -i -e 's/# BANNED IPS/#{ip_ban_block}/' #{release_path}/public/.htaccess"
run "chmod +x #{release_path}/script/runner"
run "chmod +x #{release_path}/script/process/reaper"
run "chmod +x #{release_path}/script/process/spawner"
run "chmod 755 #{release_path}/public/dispatch.*"
end
desc "Restarting after deployment"
task :after_deploy, :roles => [:app, :db, :web] do
run "cd #{current_path} && rake RAILS_ENV=production db:sessions:clear tmp:clear"
# fix permissions
run "dos2unix #{release_path}/public/dispatch.*"
run "dos2unix #{release_path}/public/.htaccess"
run "chmod -R 755 #{release_path}"
run "chmod -R 775 #{release_path}/log #{release_path}/tmp #{release_path}/script"
run "find #{release_path}/ | xargs touch"
run "touch #{deploy_to}/current/public/dispatch.fcgi"
end
desc "Restarting after rollback"
task :after_rollback, :roles => [:app, :db, :web] do
run "touch #{deploy_to}/current/public/dispatch.fcgi"
end
namespace :db do
desc "Create database yaml in shared path"
task :default do
prompt_with_default(:db_password, "")
db_config = ERB.new <<-EOF
base: &base
adapter: mysql
port: 3306
host: #{db_host}
username: #{db_user}
password: #{db_password}
development:
database: #{db_prefix}#{application}development
<<: *base
test:
database: #{db_prefix}#{application}test
<<: *base
production:
database: #{db_prefix}#{application}production
<<: *base
EOF
run "mkdir -p #{shared_path}/config"
put db_config.result, "#{shared_path}/config/database.yml"
end
desc "Make symlink for database yaml"
task :symlink do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end
end
Once I set up my variables, I have a bash script that runs:
svn export $svn_path . --force
cap deploy
That'll ask me for my password twice. Once for exporting from SVN, and once as a prompt to automatically generate my database.yml in the shared path.

Resources