Reputation: 2554
I'm trying to deploy my rails application to Amazon Ec2.
I'm using Capistrano + Unicorn + Nginx.
The deploy:setup works fine, but when i try to do deploy:cold, there is an error:
servers: ["ec2-xx-xxx-xx-xx.sa-east-1.compute.amazonaws.com"]
[ec2-xx-xxx-xx-xx.sa-east-1.compute.amazonaws.com] executing command
[err :: ec2-xx-xxx-xx-xx.sa-east-1.compute.amazonaws.com] su: must be run from a terminal
failed: "rvm_path=$HOME/.rvm/ $HOME/.rvm/bin/rvm-shell '2.1.0' -c '/etc/init.d/unicorn_app start'" on ec2-xx-xxx-xx-xx.sa-east-1.compute.amazonaws.com
I searched in StackOverFlow and i saw some topics to help me, but without succeed.
My unicorn.rb:
root = "/home/deployer/apps/myapp/current"
working_directory root
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"
listen "/tmp/unicorn.myapp.sock"
worker_processes 2
timeout 30
My unicorn_init.sh:
#!/bin/sh
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Manage unicorn server
# Description: Start, stop, restart unicorn server for a specific application.
### END INIT INFO
set -e
# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/home/deployer/apps/myapp/current
PID=$APP_ROOT/tmp/pids/unicorn.pid
CMD="cd $APP_ROOT; bundle exec unicorn -D -c $APP_ROOT/config/unicorn.rb -E production"
AS_USER=deployer
set -u
OLD_PIN="$PID.oldbin"
sig () {
test -s "$PID" && kill -$1 `cat $PID`
}
oldsig () {
test -s $OLD_PIN && kill -$1 `cat $OLD_PIN`
}
run () {
if [ "$(id -un)" = "$AS_USER" ]; then
eval $1
else
su -c "$1" - $AS_USER
fi
}
case "$1" in
start)
sig 0 && echo >&2 "Already running" && exit 0
run "$CMD"
;;
stop)
sig QUIT && exit 0
echo >&2 "Not running"
;;
force-stop)
sig TERM && exit 0
echo >&2 "Not running"
;;
restart|reload)
sig HUP && echo reloaded OK && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
run "$CMD"
;;
upgrade)
if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
then
n=$TIMEOUT
while test -s $OLD_PIN && test $n -ge 0
do
printf '.' && sleep 1 && n=$(( $n - 1 ))
done
echo
if test $n -lt 0 && test -s $OLD_PIN
then
echo >&2 "$OLD_PIN still exists after $TIMEOUT seconds"
exit 1
fi
exit 0
fi
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
run "$CMD"
;;
reopen-logs)
sig USR1
;;
*)
echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
exit 1
;;
esac
My deploy.rb:
require 'bundler/capistrano'
require "rvm/capistrano"
load 'deploy'
load 'deploy/assets'
server "ec2-xx-xxx-xx-xx.sa-east-1.compute.amazonaws.com", :web, :app, :db, primary: true
# set :rvm_type, :system
set :rvm_ruby_string, '2.1.0'
set :default_env, { rvm_bin_path: '~/.rvm/bin' }
set :application, "myapp"
set :user, "deployer"
set :deploy_to, "/home/#{user}/apps/#{application}"
set :deploy_via, :remote_cache
set :use_sudo, false
set :scm, "git"
set :repository, "[email protected]:myapp/#{application}.git"
set :branch, "master"
set :normalize_asset_timestamps, false
set :ssh_options, { :forward_agent => true }
after "deploy", "deploy:migrate"
after "deploy", "deploy:cleanup"
namespace :deploy do
%w[start stop restart].each do |command|
desc "#{command} unicorn server"
task command, roles: :app, except: {no_release: true} do
run "/etc/init.d/unicorn_#{application} #{command}"
end
end
task :setup_config, roles: :app do
sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"
sudo "ln -nfs #{current_path}/config/unicorn_init.sh /etc/init.d/unicorn_#{application}"
run "mkdir -p #{shared_path}/config"
put File.read("config/database.example.yml"), "#{shared_path}/config/database.yml"
puts "Now edit the config files in #{shared_path}."
end
after "deploy:setup", "deploy:setup_config"
task :symlink_config, roles: :app do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end
after "deploy:finalize_update", "deploy:symlink_config"
desc "Make sure local git is in sync with remote."
task :check_revision, roles: :web do
unless `git rev-parse HEAD` == `git rev-parse origin/master`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
before "deploy", "deploy:check_revision"
end
UPDATE --------------------------------------------------------------------------
Finally I solved my problem. This link helped me to solve!
http://ferryzhou.wordpress.com/2013/11/22/rails-digitalocean-nginx-unicorn-capistrano-bitbucket/
I just change the path of unicorn_init.sh. Thanks everyone.
Upvotes: 1
Views: 730
Reputation: 584
The erro su: must be run from a terminal
is from your unicorn_init.sh.
In run() function, it requires to be called in 'deployer' user session.
Please focus on that, and it seems the code is different with what you are getting error.
e.g.
You mentioned
failed: "rvm_path=$HOME/.rvm/ $HOME/.rvm/bin/rvm-shell '2.1.0' -c '/etc/init.d/unicorn_app start'" on ec2-xx-xxx-xx-xx.sa-east-1.compute.amazonaws.com
But in your capistrano config, it says that application is 'myapp'.
set :application, "myapp"
# ...
%w[start stop restart].each do |command|
desc "#{command} unicorn server"
task command, roles: :app, except: {no_release: true} do
# this will make command '/etc/init.d/unicorn_myapp start|stop|restart'
run "/etc/init.d/unicorn_#{application} #{command}"
end
end
Log in with deployer (same user in cap config, set :user, "deployer"
) user via ssh
Make sure you can run bellow command to start unicorn.
rvm_path=$HOME/.rvm/ $HOME/.rvm/bin/rvm-shell '2.1.0' -c '/etc/init.d/unicorn_myapp start'
Upvotes: 1