I'm new to deploying with Capistrano, and I'm trying the following:
deploy.rb:
set :application, "example.co.uk"
# If you aren't deploying to /u/apps/#{application} on the target
# servers (which is the default), you can specify the actual location
# via the :deploy_to variable:
set :deploy_to, "/home/example/#{application}"
# SCM Options
default_run_options[:pty] = true # Must be set for the password prompt from git to work
ssh_options[:forward_agent] = true # Agent forwarding keys
set :repository, "git#github.com:mongeese/example.git" # Your clone URL
set :scm, "git"
set :branch, "master"
set :deploy_via, :remote_cache
set :user, "james" # The server's user for deploys
role :app, "example.co.uk"
role :web, "example.co.uk"
role :db, "example.co.uk", :primary => true
set :use_sudo, false
I get the following output:
* executing `deploy:restart'
* executing "/home/example/example.co.uk/current/script/process/reaper"
servers: ["example.co.uk"]
[example.co.uk] executing command
** [out :: example.co.uk] sh: /home/example/example.co.uk/current/script/process/reaper: not found
command finished
The "james" user can sudo. If I take out :use_sudo, I get the following error:
* executing "sudo -p 'sudo password: ' -u app /home/example/example.co.uk/current/script/process/reaper"
servers: ["example.co.uk"]
[example.co.uk] executing command
** [out :: example.co.uk] sudo: unknown user: app
command finished
I'm obviously missing something completely, as Google only seems to turn up old results about this.
There must have been a problem with the recipes, the following override works fine:
set :application, "example.co.uk"
# If you aren't deploying to /u/apps/#{application} on the target
# servers (which is the default), you can specify the actual location
# via the :deploy_to variable:
set :deploy_to, "/home/example/#{application}"
# SCM Options
default_run_options[:pty] = true # Must be set for the password prompt from git to work
ssh_options[:forward_agent] = true # Agent forwarding keys
set :repository, "git#github.com:example/MyRepo.git" # Your clone URL
set :scm, "git"
set :branch, "master"
set :deploy_via, :remote_cache
set :user, "james" # The server's user for deploys
role :app, "example.co.uk"
role :web, "example.co.uk"
role :db, "example.co.uk", :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
For people who encounter the same issue, have a glance to :
http://capitate.rubyforge.org/recipes/deploy.html#deploy:restart
When calling the "cap deploy" command, "update" + "restart" in the "deploy" namespace are called.
Default behaviour for "restart" is to call the "script/process/reaper" script under the current path. In James's answer, "restart" is overridden with the below command :
run "touch #{current_path}/tmp/restart.txt"
For example, people using unicorn should process like :
#launch unicorn
task :start, roles: :app, except: { no_release: true } do
run "cd #{current_path} && bundle exec unicorn_rails -c config/unicorn.rb -E #{rails_env} -D"
end
#stop unicorn
task :stop, roles: :app, except: { no_release: true } do
run "kill -KILL -s QUIT `cat #{shared_path}/pids/unicorn.pid`"
end
#when calling "cap deploy", files will be updated with #update# task (default behaviour),
#then "restart" task will be called (overridden below)
task :restart, roles: :app, except: { no_release: true } do
stop
start
end
Hope my contribution will be helpful to someone...
Related
I'm using the gem Capistrano ~2.15.5 and deploying my application to my remote server. Everything works fine until the end of the cap prod deploy command. I'm receiving this error:
** [52.74.34.196 :: out] /bin/bash: ebs/apps/CloudTrendz/releases/20150318203140/REVISION: No such file or directory
** [52.74.34.196 :: out]
command finished in 4923ms
*** [deploy:update_code] rolling back
* executing "rm -rf ebs/apps/CloudTrendz/releases/20150318203140; true"
servers: ["52.74.34.196"]
[52.74.34.196] executing command
command finished in 411ms
failed: "/bin/bash --login -c 'git clone -b master git#github.com:prashant4224/CloudTrendz.git ebs/apps/CloudTrendz/releases/20150318203140 && cd ebs/apps/CloudTrendz/releases/20150318203140 && git checkout -b deploy 16cc51ebe1acd97cb489f10313d85f7ae9a69f6b && rm -Rf ebs/apps/CloudTrendz/releases/20150318203140/.git && (echo 16cc51ebe1acd97cb489f10313d85f7ae9a69f6b > ebs/apps/CloudTrendz/releases/20150318203140/REVISION)'" on 52.74.34.196
Here deploy.rb
require "bundler/capistrano"
set :application, "CloudTrendz"
set :repository, "git#github.com:prashant4224/CloudTrendz.git"
set :deploy_to, "ebs/apps/#{application}"
set :applicationdir, "ebs/apps/#{application}"
set :scm, :git # You can set :scm explicitly or Capistrano will make an intelligent guess based on known version control directory names
set :branch, "master"
set :deploy_to, applicationdir
set :deploy_via, :export
set :scm_verbose, true
set :user, "ubuntu"
set :use_sudo, false
set :rails_env, "production"
set :keep_releases, 2
set :precompile_only_if_changed, true
default_run_options[:pty] = true
default_run_options[:shell] = '/bin/bash --login'
ssh_options[:keys] = %w(~/.ssh/prashantec2.pem)
after "deploy:update_code", "deploy:copy_configs"
task :prod do
set :domain, "52.74.34.196"
set :repository, "git#github.com:prashant4224/CloudTrendz.git"
set :local_repository, "git#github.com:prashant4224/CloudTrendz.git"
set :branch, "master"
set :scm_verbose, true
server "52.74.34.196", :app, :web, :db, :primary => true
set :deploy_env, "prod"
"deploy"
end
namespace :deploy do
task :copy_configs, :roles => :app do
run "cp #{release_path}/../../shared/database.yml #{release_path}/config/database.yml"
end
task :migrate, :roles => :app do
run "cd #{release_path} && bundle exec rake db:migrate"
end
task :restart, :roles => :app, :except => { :no_release => true } do
run "#{try_sudo} touch #{File.join(current_path, 'tmp', 'restart.txt')}"
if deploy_env == 'prod'
tag_name = Time.now.strftime("deploy_%Y_%m_%d_%H_%M")
system "git tag -a -m 'Deployment on prod' #{tag_name}"
system "git push origin #{tag_name}"
if $? != 0
raise "Pushing tag to origin failed"
end
end
end
namespace :assets do
task :precompile, :roles => :web, :except => { :no_release => true } do
logger.info "Skipping asset pre-compilation because there were no asset changes"
end
end
end
after "deploy:update", "deploy:migrate", "deploy:cleanup"
What is wrong with this code?
Thank you in advance
It looks to me like it is failing when cloning the git repository from Github. Make sure that you've added the remote server's SSH key to the Github repository.
You can verify if this is the problem by SSH'ing into the remote server and attempting to run:
git clone git#github.com:prashant4224/CloudTrendz.git
If this fails, copy the contents of ~/.ssh/id_rsa.pub and then go to https://github.com/prashant4224/CloudTrendz/settings/keys, click "Add deploy key" and paste it in.
If ~/.ssh/id_rsa.pub does not exist on the remote server, run ssh-keygen to create it.
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
i cant get capistrano to fully deploy my rails app to my dreamhost VPS..
as far as i can tell its halting at creating the release directory..
$ cap deploy
i get
failed: "sh -c 'cd /home/gasloggr/gasloggr.com/releases/20120824064241 && bundle install --gemfile /home/gasloggr/gasloggr.com/releases/20120824064241/Gemfile --path /home/gasloggr/gasloggr.com/shared/bundle --deployment --quiet --without development test'" on gasloggr.com
for troubleshooting purposes i ran what was in quotes on the server itself and i received..
bash: cd: /home/gasloggr/gasloggr.com/releases/20120824064241: No such file or directory
a quick ls -alh of the releases dir, and guess what... its empty.
My deploy.rb file
require 'bundler/capistrano'
default_run_options[:pty] = false
ssh_options[:forward_agent] = true
set :use_sudo, false
set :user, "gasloggr"
set :application, "gasloggr.com"
set :repository, "git#github.com:gorelative/GasLoggr.git"
set :scm, :git
set :branch, 'master'
set :git_shallow_clone, 1
set :deploy_via, :copy
set :copy_compression, :bz2
set :rails_env, 'production'
set :deploy_to, "/home/gasloggr/#{application}"
role :web, "#{application}" # Your HTTP server, Apache/etc
role :app, "#{application}" # This may be the same as your `Web` server
role :db, "#{application}", :primary => true # This is where Rails migrations will run
# role :db, "your slave db-server here"
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
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 have tried removing :deploy_via as well as all of the below:
set :deploy_via, :remote_cache
set :deploy_via, :copy
Please try again following the methods outlined here: http://wiki.dreamhost.com/Capistrano
This specific part may be of the most help, mentioning cap deploy:setup : http://wiki.dreamhost.com/Capistrano#Deployment_with_Capistrano
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.
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