Capistrano部署期间出错

时间:2014-08-19 18:14:38

标签: capistrano sh unicorn capistrano3

当我运行cap production deploy:start时,Capistrano在启动Unicorn服务器时抛出错误,但是如果我通过SSH连接到服务器并运行./home/deployer/apps/<APPNAME>/current/config/unicorn_init.sh start,服务器将启动它应该启动(但是,没有输出说服务器已经在终端中启动了。)

我得到的错误如下:

INFO[fc27be26] Running /usr/bin/env /home/deployer/apps/explorelocal/current/config/unicorn_init.sh start on explore.jackhughesweb.com
DEBUG[fc27be26] Command: /home/deployer/apps/explorelocal/current/config/unicorn_init.sh start
DEBUG[fc27be26]     /home/deployer/apps/explorelocal/current/config/unicorn_init.sh: 1: eval:
DEBUG[fc27be26]     bundle: not found
DEBUG[fc27be26]
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing on host <SERVER HOST>: /home/deployer/apps/<APPNAME>/current/config/unicorn_init.sh start exit status: 127
/home/deployer/apps/<APPNAME>/current/config/unicorn_init.sh start stdout: Nothing written
/home/deployer/apps/<APPNAME>/current/config/unicorn_init.sh start stderr: Nothing written
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/command.rb:97:in `exit_status='
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:148:in `block (5 levels) in _execute'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/channel.rb:551:in `call'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/channel.rb:551:in `do_request'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:561:in `channel_request'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:465:in `dispatch_incoming_packets'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:221:in `preprocess'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:205:in `process'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:169:in `block in loop'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/channel.rb:269:in `wait'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:170:in `block (3 levels) in _execute'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/channel.rb:514:in `call'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/channel.rb:514:in `do_open_confirmation'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:545:in `channel_open_confirmation'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:465:in `dispatch_incoming_packets'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:221:in `preprocess'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:205:in `process'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:169:in `block in loop'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:172:in `block (2 levels) in _execute'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:186:in `with_ssh'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:131:in `block in _execute'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:128:in `tap'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:128:in `_execute'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:66:in `execute'
config/deploy.rb:35:in `block (3 levels) in <top (required)>'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:54:in `instance_exec'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:54:in `run'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/runners/sequential.rb:10:in `block in execute'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/runners/sequential.rb:8:in `each'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/runners/sequential.rb:8:in `execute'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/coordinator.rb:21:in `each'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/dsl.rb:6:in `on'
config/deploy.rb:32:in `block (2 levels) in <top (required)>'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/capistrano-3.2.1/lib/capistrano/application.rb:15:in `run'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/capistrano-3.2.1/bin/cap:3:in `<top (required)>'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/bin/cap:23:in `load'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/bin/cap:23:in `<main>'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/bin/ruby_executable_hooks:15:in `eval'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/bin/ruby_executable_hooks:15:in `<main>'
SSHKit::Command::Failed: /home/deployer/apps/<APPNAME>/current/config/unicorn_init.sh start exit status: 127
/home/deployer/apps/<APPNAME>/current/config/unicorn_init.sh start stdout: Nothing written
/home/deployer/apps/<APPNAME>/current/config/unicorn_init.sh start stderr: Nothing written
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/command.rb:97:in `exit_status='
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:148:in `block (5 levels) in _execute'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/channel.rb:551:in `call'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/channel.rb:551:in `do_request'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:561:in `channel_request'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:465:in `dispatch_incoming_packets'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:221:in `preprocess'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:205:in `process'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:169:in `block in loop'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/channel.rb:269:in `wait'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:170:in `block (3 levels) in _execute'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/channel.rb:514:in `call'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/channel.rb:514:in `do_open_confirmation'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:545:in `channel_open_confirmation'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:465:in `dispatch_incoming_packets'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:221:in `preprocess'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:205:in `process'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:169:in `block in loop'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:172:in `block (2 levels) in _execute'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:186:in `with_ssh'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:131:in `block in _execute'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:128:in `tap'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:128:in `_execute'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:66:in `execute'
config/deploy.rb:35:in `block (3 levels) in <top (required)>'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:54:in `instance_exec'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:54:in `run'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/runners/sequential.rb:10:in `block in execute'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/runners/sequential.rb:8:in `each'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/runners/sequential.rb:8:in `execute'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/coordinator.rb:21:in `each'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/sshkit-1.5.1/lib/sshkit/dsl.rb:6:in `on'
config/deploy.rb:32:in `block (2 levels) in <top (required)>'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/capistrano-3.2.1/lib/capistrano/application.rb:15:in `run'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/gems/capistrano-3.2.1/bin/cap:3:in `<top (required)>'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/bin/cap:23:in `load'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/bin/cap:23:in `<main>'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/bin/ruby_executable_hooks:15:in `eval'
/Users/Jack/.rvm/gems/ruby-2.0.0-p481/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => deploy:start
(See full trace by running task with --trace)

我的unicorn_init.sh文件包含以下内容:

#!/bin/sh
set -e

# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/home/deployer/apps/<APPNAME>/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

2 个答案:

答案 0 :(得分:1)

我通过将以下内容添加到.bashrc的顶部来解决我的问题:

export PATH="$PATH:$HOME/.rvm/bin"

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"

答案 1 :(得分:0)

Capistrano运行非登录shell,其环境与用户通过ssh登录时的环境完全不同。您可以阅读更多相关信息on this link from capistranorb.com

当capistrano运行bundle something时 - 它会为每个命令添加完整路径和所有必要的环境变量。 这是示例命令(它使用RBENV,但它与rvm相同):

cd /var/www/webresume_qa/current &&
  ( RBENV_ROOT=~/.rbenv RBENV_VERSION=1.9.3-p429
  ~/.rbenv/bin/rbenv exec bundle exec
  unicorn -D -c /var/www/webresume_qa/shared/config/unicorn.rb -E qa )

不幸的是,由于您的unicorn_init.sh脚本在bundle exec unicorn失败之前没有预先设置所有这些环境。

我制作了一个试图解决此问题的插件capistrano-unicorn-nginx。如果您无法运行整个插件,则可能只想检查unicorn_init.sh的具体解决方案:

我知道这很复杂,但我希望它有所帮助。