Rails 3 - Script doesn't run after cap:deploy - deployment

I want to run few commands when i start application on server.
So, i wrote such script in config/deploy.rb:
desc "Start sphinx"
task :start_sphinx, :roles => :app do
run "cd #{current_path} && bundle install --without development test && bundle install --deployment && chmod 777 -R #{current_path}/tmp/ && rake thinking_sphinx:configure && rake thinking_sphinx:start"
end
But when i print
$ cap deploy
It doesn't work. So, please, tell me what is wrong!
Here is log from terminal:
$ cap deploy
* executing `deploy'
* executing `deploy:update'
** transaction: start
* executing `deploy:update_code'
updating the cached checkout on all servers
executing locally: "git ls-remote git://github.com/Loremaster/sample_app.git master"
command finished in 367ms
* executing "if [ -d /vol/www/apps/ror_tutorial/shared/cached-copy ]; then cd /vol/www/apps/ror_tutorial/shared/cached-copy && git fetch -q origin && git fetch --tags -q origin && git reset -q --hard b5073e9b9aefd98873489bb6e97249593ea1a978 && git clean -q -d -x -f; else git clone -q git://github.com/Loremaster/sample_app.git /vol/www/apps/ror_tutorial/shared/cached-copy && cd /vol/www/apps/ror_tutorial/shared/cached-copy && git checkout -q -b deploy b5073e9b9aefd98873489bb6e97249593ea1a978; fi"
servers: ["188.127.224.136"]
Password:
[188.127.224.136] executing command
command finished in 3070ms
copying the cached version to /vol/www/apps/ror_tutorial/releases/20120125165835
* executing "cp -RPp /vol/www/apps/ror_tutorial/shared/cached-copy /vol/www/apps/ror_tutorial/releases/20120125165835 && (echo b5073e9b9aefd98873489bb6e97249593ea1a978 > /vol/www/apps/ror_tutorial/releases/20120125165835/REVISION)"
servers: ["188.127.224.136"]
[188.127.224.136] executing command
command finished in 49970ms
* executing `deploy:finalize_update'
* executing "chmod -R g+w /vol/www/apps/ror_tutorial/releases/20120125165835"
servers: ["188.127.224.136"]
[188.127.224.136] executing command
command finished in 1023ms
* executing "rm -rf /vol/www/apps/ror_tutorial/releases/20120125165835/log /vol/www/apps/ror_tutorial/releases/20120125165835/public/system /vol/www/apps/ror_tutorial/releases/20120125165835/tmp/pids &&\\\n mkdir -p /vol/www/apps/ror_tutorial/releases/20120125165835/public &&\\\n mkdir -p /vol/www/apps/ror_tutorial/releases/20120125165835/tmp &&\\\n ln -s /vol/www/apps/ror_tutorial/shared/log /vol/www/apps/ror_tutorial/releases/20120125165835/log &&\\\n ln -s /vol/www/apps/ror_tutorial/shared/system /vol/www/apps/ror_tutorial/releases/20120125165835/public/system &&\\\n ln -s /vol/www/apps/ror_tutorial/shared/pids /vol/www/apps/ror_tutorial/releases/20120125165835/tmp/pids"
servers: ["188.127.224.136"]
[188.127.224.136] executing command
command finished in 150ms
* executing "find /vol/www/apps/ror_tutorial/releases/20120125165835/public/images /vol/www/apps/ror_tutorial/releases/20120125165835/public/stylesheets /vol/www/apps/ror_tutorial/releases/20120125165835/public/javascripts -exec touch -t 201201251659.26 {} ';'; true"
servers: ["188.127.224.136"]
[188.127.224.136] executing command
** [out :: 188.127.224.136] find: `/vol/www/apps/ror_tutorial/releases/20120125165835/public/images': No such file or directory
** [out :: 188.127.224.136] find: `/vol/www/apps/ror_tutorial/releases/20120125165835/public/stylesheets': No such file or directory
** [out :: 188.127.224.136] find: `/vol/www/apps/ror_tutorial/releases/20120125165835/public/javascripts': No such file or directory
command finished in 566ms
* executing `deploy:symlink'
* executing "rm -f /vol/www/apps/ror_tutorial/current && ln -s /vol/www/apps/ror_tutorial/releases/20120125165835 /vol/www/apps/ror_tutorial/current"
servers: ["188.127.224.136"]
[188.127.224.136] executing command
command finished in 56ms
** transaction: commit
* executing `deploy:restart'
* executing "sudo -p 'sudo password: ' touch /vol/www/apps/ror_tutorial/current/tmp/restart.txt"
servers: ["188.127.224.136"]
[188.127.224.136] executing command
command finished in 163ms

It looks like you are missing the callback. For example, if you wanted to run your script after the code has been updated, you can use an after callback like
after "deploy:update_code", "start_sphinx"
For more info on callbacks, check out the callbacks rdoc

Related

Jenkins build passes when ssh deploy fails

I'm using a here tag in a Jenkins build step to send my deploy commands over ssh, and unfortunately the build is passing even when the commands inside the here tag don't finish successfully:
ssh user#host <<EOF
cd /path/to/app
git pull
bower install
npm install
grunt build
cp -r /path/to/app/dist/* /path/to/dist/
forever restartall
exit
EOF
Is there a better way to approach this problem?
You are not catching any error codes inside your "here document".
Last command is exit and without an exit code, it will default to 0 which is success.
Since the last command of your ssh is a success, the whole command is treated as success, and build is a success.
Easiest way to fix that: chain all commands with && like so:
cd /path/to/app && git pull && bower install && npm install && grunt build && cp -r /path/to/app/dist/* /path/to/dist/ && forever restartall && exit
Best way to fix that: write a proper shell script, with error handling, and execute that. If you are too lazy to error handle every line, you can start the script with set -e which will fail the shell script on any individual error
Edit:
#!/bin/bash
appPath="/path/to/app"
distPath"/path/to/dist"
echo "My great deployment script"
echo "Deploying ${appPath} to ${distPath}
if [[ ! -w "${appPath}" ]]; then
echo "${appPath} is not writable, quitting"
exit 1
else
cd ${appPath} && git pull || { echo "Failed on 'git pull'"; exit 2; }
bower install || { echo "Failed on 'bower install'"; exit 3; }
npm install || { echo "Failed on 'npm install'"; exit 4; }
grunt build || { echo "Failed on 'grunt build'"; exit 5; }
if [[ -w "${distPath}" ]]; then
cp -r ${appPath}/dist/* ${distPath}/ || { echo "Failed on 'copy'"; exit 1 }
forever restartall || { echo "Failed on 'forever restartall'"; exit 6 }
echo "Deployment successful"
exit 0
fi
fi
You then execute it with: ssh user#host 'bash -s' < myfile.sh (if the file is local)
Or if you place the file remotely, then just: ssh user#host '/path/to/remote/myfile.sh'

Capistrano doesn't obey "within release_path"

I have this task:
namespace :custom do
desc "create a symlink to db config already on the server"
task :symlink_db_config do
on roles(:web) do
within release_path do
execute "pwd"
end
within release_path do
execute "ln -nfs /home/blog/config/database.yml ./database.yml"
end
end
end
end
For some infuriating reason, the pwd command is preceded by a cd to the release path, but the ln command is not. Why is that?
Here's the output, showing the above:
** Invoke custom:symlink_db_config (first_time)
** Execute custom:symlink_db_config
DEBUG[352cc4bb] Running /usr/bin/env if test ! -d /home/blog/staging/releases/20141010050707; then echo "Directory does not exist '/home/blog/staging/releases/20141010050707'" 1>&2; false; fi on 172.245.32.193
DEBUG[352cc4bb] Command: if test ! -d /home/blog/staging/releases/20141010050707; then echo "Directory does not exist '/home/blog/staging/releases/20141010050707'" 1>&2; false; fi
DEBUG[352cc4bb] Finished in 0.199 seconds with exit status 0 (successful).
// Here's the `pwd`; note the proper `cd` that occurs first:
INFO[67a83a04] Running /usr/bin/env pwd on 172.245.32.193
DEBUG[67a83a04] Command: cd /home/blog/staging/releases/20141010050707 && /usr/bin/env pwd
DEBUG[67a83a04] /home/blog/staging/releases/20141010050707
INFO[67a83a04] Finished in 0.268 seconds with exit status 0 (successful).
DEBUG[f46f64b3] Running /usr/bin/env if test ! -d /home/blog/staging/releases/20141010050707; then echo "Directory does not exist '/home/blog/staging/releases/20141010050707'" 1>&2; false; fi on 172.245.32.193
DEBUG[f46f64b3] Command: if test ! -d /home/blog/staging/releases/20141010050707; then echo "Directory does not exist '/home/blog/staging/releases/20141010050707'" 1>&2; false; fi
DEBUG[f46f64b3] Finished in 0.243 seconds with exit status 0 (successful).
//And now here's the `ln`... but where is the `cd`? I said `within release_path`, didn't I?
INFO[afdbd89c] Running /usr/bin/env ln -nfs /home/blog/config/database.yml ./database.yml on 172.245.32.193
DEBUG[afdbd89c] Command: ln -nfs /home/blog/config/database.yml ./database.yml
INFO[afdbd89c] Finished in 0.219 seconds with exit status 0 (successful).
So my ln fails because it's not in the right directory. Why didn't capistrano cd into the release directory like I told it to?
(Capistrano 3.2.1, by the way)
There's useful information in another answer here but briefly it seems the problem arises when there are spaces in the command you want to run.
I followed bricker's suggestion, e.g.
within release_path do
execute *%w[ ln -nfs /home/blog/config/database.yml ./database.yml ]
end

capifony no such file or directory on cap deploy

i've the following easy capifony script to copy the local project to my server:
set :application, "project"
set :domain, "88.88.88.88"
set :deploy_to, "/var/www/project"
set :app_path, "app"
set :user, "root"
set :scm, :git
set :repository, "file:///Users/berlin/vagrant-workspace/essen"
set :deploy_via, :capifony_copy_local
set :use_composer, true
set :use_composer_tmp, false
set :copy_vendors, true
role :web, domain
role :app, domain, :primary => true
set :use_sudo, false
set :keep_releases, 3
logger.level = Logger::MAX_LEVEL
This is my output for cap:deploy:setup:
* 2014-10-06 21:55:38 executing `deploy:setup'
* executing "mkdir -p /var/www/project /var/www/project/releases /var/www/project/shared"
servers: ["88.88.88.88"]
[88.88.88.88] executing command
command finished in 119ms
* executing "chmod g+w /var/www/project /var/www/project/releases /var/www/project/shared"
servers: ["88.88.88.88"]
[88.88.88.88] executing command
command finished in 144ms
And then i can't deploy because of a missing folder. It looks like that the script doesn't create the folder inside releases.
cap deploy
* 2014-10-06 21:55:59 executing `deploy'
* 2014-10-06 21:55:59 executing `deploy:update'
** transaction: start
* 2014-10-06 21:55:59 executing `deploy:update_code'
triggering before callbacks for `deploy:update_code'
--> Updating code base with capifony_copy_local strategy
--> Using Copy Local Strategy
executing locally: "git ls-remote file:///Users/berlin/vagrant-workspace/project HEAD"
command finished in 12ms
* getting (via checkout) revision a7ba07ac38fee4b5375c6d383886a906c1a551d5 to /var/folders/l6/ptd6l19s2vb98z6bbnb1k7b00000gq/T/20141006195559
executing locally: git clone -q file:///Users/berlin/vagrant-workspace/project /var/folders/l6/ptd6l19s2vb98z6bbnb1k7b00000gq/T/20141006195559 && cd /var/folders/l6/ptd6l19s2vb98z6bbnb1k7b00000gq/T/20141006195559 && git checkout -q -b deploy a7ba07ac38fee4b5375c6d383886a906c1a551d5
command finished in 72ms
* 2014-10-06 21:55:59 executing `symfony:composer:install'
triggering before callbacks for `symfony:composer:install'
* 2014-10-06 21:55:59 executing `symfony:composer:copy_vendors'
--> Copying vendors from previous release
* executing "vendorDir=/var/www/project/current/vendor; if [ -d $vendorDir ] || [ -h $vendorDir ]; then cp -a $vendorDir /var/www/project/releases/20141006195559; fi;"
servers: ["88.88.88.88"]
[88.88.88.88] executing command
command finished in 103ms
* 2014-10-06 21:56:00 executing `symfony:composer:get'
* executing "if [ -e /var/www/project/releases/20141006195559/composer.phar ]; then echo 'true'; fi"
servers: ["88.88.88.88"]
[88.88.88.88] executing command
command finished in 90ms
--> Downloading Composer
* executing "sh -c 'cd /var/www/project/releases/20141006195559 && curl -s http://getcomposer.org/installer | php'"
servers: ["88.88.88.88"]
[88.88.88.88] executing command
*** [err :: 88.88.88.88] sh: line 0: cd: /var/www/project/releases/20141006195559: No such file or directory
command finished in 106ms
*** [deploy:update_code] rolling back
* executing "rm -rf /var/www/project/releases/20141006195559; true"
servers: ["88.88.88.88"]
[88.88.88.88] executing command
command finished in 102ms
failed: "sh -c 'sh -c '\\''cd /var/www/project/releases/20141006195559 && curl -s http://getcomposer.org/installer | php'\\'''" on 88.88.88.88
I can't find any solution.
Does anybody have an idea for my problem? Thank you very much.
If someone will have the same problem. This is a working script for me:
set :application, "project"
set :domain, "88.88.88.88"
set :deploy_to, "/var/www/project"
set :app_path, "app"
set :user, "root"
set :scm, :git
set :repository, "file:///Users/berlin/vagrant-workspace/project"
set :deploy_via, :capifony_copy_local
set :use_composer, true
set :use_composer_tmp, true
role :web, domain
role :app, domain, :primary => true
set :use_sudo, false
set :keep_releases, 3
set :shared_files, ["app/config/parameters.yml"]
set :shared_children, [app_path + "/logs"]
#logger.level = Logger::MAX_LEVEL
after "deploy:finalize_update" do
capifony_pretty_print "--> Set permissions for writable directories"
run "chown -R www-data:www-data #{latest_release}/#{cache_path}"
run "chown -R www-data:www-data #{latest_release}/#{log_path}"
run "chmod -R 777 #{latest_release}/#{cache_path}"
puts "✔"
end
I don't know exactly the difference, but this snippet works fine.

Capistrano tmp:dir error:cannot run git-ssh.sh: No such file or directory

on my server I´m not allowed to create an tmp folder that executes any content.
So with capistrano 3 I have the option to set a path for a kind of custom tmp folder.
Is there any syntax I have to consider, cause my settings execute an error with cap staging deploy:
INFO[f99c486e] Running /usr/bin/env mkdir -p custom_tmp/myproject/ on xxxx.de
DEBUG[f99c486e] Command: /usr/bin/env mkdir -p custom_tmp/myproject/
INFO[f99c486e] Finished in 1.140 seconds with exit status 0 (successful).
DEBUGUploading custom_tmp/myproject/git-ssh.sh 0.0%
INFOUploading custom_tmp/myproject/git-ssh.sh 100.0%
INFO[91b7d9b8] Running /usr/bin/env chmod +x custom_tmp/myproject/git-ssh.sh on xxxx.de
DEBUG[91b7d9b8] Command: /usr/bin/env chmod +x custom_tmp/myproject/git-ssh.sh
INFO[91b7d9b8] Finished in 0.080 seconds with exit status 0 (successful).
DEBUG[f4db290c] Running /usr/bin/env git ls-remote -h git#bitbucket.org:hallo/www.myproject.de.git on xxxx.de
DEBUG[f4db290c] Command: ( GIT_ASKPASS=/bin/echo GIT_SSH=custom_tmp/myproject/git-ssh.sh /usr/bin/env git ls-remote -h git#bitbucket.org:hallo/www.myproject.de.git )
DEBUG[f4db290c] 66791b22a61cd1af57d117a817129e491e83d88c refs/heads/master
DEBUG[f4db290c] Finished in 3.235 seconds with exit status 0 (successful).
INFO[74118c8e] Running /usr/bin/env mkdir -pv development/myproject/shared development/myproject/releases on xxxx.de
DEBUG[74118c8e] Command: /usr/bin/env mkdir -pv development/myproject/shared development/myproject/releases
INFO[74118c8e] Finished in 0.079 seconds with exit status 0 (successful).
INFO[10e40636] Running /usr/bin/env mkdir -pv development/myproject/shared/media on xxxx.de
DEBUG[10e40636] Command: /usr/bin/env mkdir -pv development/myproject/shared/media
INFO[10e40636] Finished in 0.086 seconds with exit status 0 (successful).
DEBUG[38889a64] Running /usr/bin/env [ -f development/myproject/current/REVISION ] on xxxx.de
DEBUG[38889a64] Command: [ -f development/myproject/current/REVISION ]
DEBUG[38889a64] Finished in 0.079 seconds with exit status 1 (failed).
DEBUG[5dfc387f] Running /usr/bin/env [ -f development/myproject/repo/HEAD ] on xxxx.de
DEBUG[5dfc387f] Command: [ -f development/myproject/repo/HEAD ]
DEBUG[5dfc387f] Finished in 0.095 seconds with exit status 1 (failed).
DEBUG[44d0214c] Running /usr/bin/env if test ! -d development/myproject/; then echo "Directory does not exist 'development/myproject/'" 1>&2; false; fi on xxxx.de
DEBUG[44d0214c] Command: if test ! -d development/myproject/; then echo "Directory does not exist 'development/myproject/'" 1>&2; false; fi
DEBUG[44d0214c] Finished in 0.079 seconds with exit status 0 (successful).
INFO[dac1f8fd] Running /usr/bin/env git clone --mirror git#bitbucket.org:hallo/www.myproject.de.git development/myproject/repo on xxxx.de
DEBUG[dac1f8fd] Command: cd development/myproject/ && ( GIT_ASKPASS=/bin/echo GIT_SSH=custom_tmp/myproject/git-ssh.sh /usr/bin/env git clone --mirror git#bitbucket.org:hallo/www.myproject.de.git development/myproject/repo )
DEBUG[dac1f8fd] Cloning into bare repository development/myproject/repo...
DEBUG[dac1f8fd] error: cannot run custom_tmp/myproject/git-ssh.sh: No such file or directory
DEBUG[dac1f8fd] fatal: unable to fork
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing on host xxxx.de: git exit status: 128
git stdout: Nothing written
git stderr: Nothing written
On my server the folder "custom_tmp/myproject/" ist still there with there with the git-ssh.sh. So I wonder about the error.
Here my deploy.rb
#config valid only for Capistrano 3.1
lock '3.2.1'
set :application, 'myproject'
set :repo_url, 'git#bitbucket.org:hallo/www.myproject.de.git'
# Default deploy_to directory is /var/www/my_app
set :deploy_to, 'development/myproject/'
#Default value for :scm is :git
#set :scm, :git
#Default value for :format is :pretty
#set :format, :pretty
# Default value for :log_level is :debug
set :log_level, :debug
#Default value for :pty is false
set :pty, true
# Default value for :linked_files is []
# set :linked_files, %w{config/database.yml}
# Default value for linked_dirs is []
set :linked_dirs, %w{media}
#tmp dir
set :tmp_dir,"custom_tmp"
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app) do
# Your restart mechanism here, for example:
# execute :touch, release_path.join('tmp/restart.txt')
end
end
end
Okay, I´m one step closer.
The path to my tmp dir wasn´t correct.
I did an pwd on my server and put the shown path in front of my "custom_tmp" like:
set :tmp_dir,"/cutomer/homeages/13/233232/custom_tmp"
But the cap staging deploy interrupt by an new error depending on the repo folder:
fatal: destination path 'development/www.myproject.com/repo' already exists and is not an empty directory.
okay,
I got it.
Have to do the same with the:
set :deploy_to

rails 4 + mina deployment failure

i am deploying a rails 4 application using mina deployment. my deployment script is as
require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
require 'mina/rvm' # for rvm support. (http://rvm.io)
set :domain, 'someplace.com'
set :deploy_to, '/home/deploy/projects/website'
set :repository, 'git#github.com:someone/repo.git'
set :branch, 'master'
set :identity_file, "#{ENV['HOME']}/.ssh/id_rsa"
set :user, 'deploy' # Username in the server to SSH to.
set :shared_paths, ['config/database.yml', 'config/credentials.yml', 'log', 'tmp']
task :environment do
invoke :'rvm:use[ruby-2.1.0#default]'
end
task :setup => :environment do
queue! %[mkdir -p "#{deploy_to}/shared/log"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/log"]
queue! %[mkdir -p "#{deploy_to}/shared/config"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/config"]
queue! %[touch "#{deploy_to}/shared/config/database.yml"]
queue %[echo "-----> Be sure to edit 'shared/config/database.yml'."]
queue! %[touch "#{deploy_to}/shared/config/credentials.yml"]
queue %[echo "-----> Be sure to edit 'shared/config/credentials.yml'."]
end
desc "Deploys the current version to the server."
task :deploy => :environment do
deploy do
invoke :'git:clone'
invoke :'deploy:link_shared_paths'
invoke :'bundle:install'
invoke :'rails:assets_precompile'
to :launch do
queue "touch #{deploy_to}/tmp/restart.txt"
end
end
end
when i deploy as 'mina deploy', i get error as
...
Symlinking shared paths
$ mkdir -p "./config"
$ mkdir -p "."
$ rm -rf "./config/database.yml"
$ ln -s "/home/deploy/projects/website/shared/config/database.yml" "./config/database.yml"
$ rm -rf "./config/credentials.yml"
$ ln -s "/home/deploy/projects/website/shared/config/credentials.yml" "./config/credentials.yml"
$ rm -rf "./log"
$ ln -s "/home/deploy/projects/website/shared/log" "./log"
$ rm -rf "./tmp"
$ ln -s "/home/deploy/projects/website/shared/tmp" "./tmp"
-----> Installing gem dependencies using Bundler
$ mkdir -p "/home/deploy/projects/website/shared/bundle"
$ mkdir -p "./vendor"
$ ln -s "/home/deploy/projects/website/shared/bundle" "./vendor/bundle"
$ bundle install --without development:test --path "./vendor/bundle" --binstubs bin/ --deployment
...
Your bundle is complete!
Gems in the groups development and test were not installed.
It was installed into ./vendor/bundle
-----> Precompiling asset files
$ RAILS_ENV="production" bundle exec rake assets:precompile RAILS_GROUPS=assets
rake aborted!
File exists # dir_s_mkdir - /home/deploy/projects/website/tmp/build-138935597031149/tmp
/home/deploy/projects/website/tmp/build-138935597031149/vendor/bundle/ruby/2.1.0/gems/sprockets-2.10.1/lib/sprockets/cache/file_store.rb:25:in `[]='
/home/deploy/projects/website/tmp/build-138935597031149/vendor/bundle/ruby/2.1.0/gems/sprockets-2.10.1/lib/sprockets/caching.rb:34:in `cache_set'
Make sure that shared/tmp directory is created if not ssh into the server and
$ mkdir /home/deploy/projects/website/shared/tmp
make sure you have the right permissions too.
drwxr-xr-x
If you are symlinking your tmp directory ensure that it exists. If it doesn't the broken symlink will cause the same issue.