在亚马逊上部署Capistrano和Unicorn

时间:2014-02-11 12:48:35

标签: ruby-on-rails deployment amazon-ec2 capistrano unicorn

我正在尝试将我的rails应用程序部署到Amazon Ec2。

我正在使用Capistrano + Unicorn + Nginx。

deploy:setup工作正常,但是当我尝试部署:cold时,出现错误:

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

我在StackOverFlow中搜索过,我看到一些主题可以帮助我,但没有成功。

我的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

我的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

我的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, "git@bitbucket.org: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

更新---------------------------------------------- ----------------------------

最后我解决了我的问题。这个链接帮我解决了!

http://ferryzhou.wordpress.com/2013/11/22/rails-digitalocean-nginx-unicorn-capistrano-bitbucket/

我只是改变了unicorn_init.sh的路径。谢谢大家。

1 个答案:

答案 0 :(得分:1)

erro su: must be run from a terminal来自你的unicorn_init.sh。

在run()函数中,需要在'deployer'用户会话中调用它。

请专注于此,似乎代码与您收到错误的内容不同。

e.g。

你提到了

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

但是在你的capistrano配置中,它说应用程序是'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
  1. 通过ssh

  2. 使用部署者(cap config中的同一用户,set :user, "deployer")用户登录
  3. 确保您可以运行bellow命令启动独角兽。

  4. rvm_path=$HOME/.rvm/ $HOME/.rvm/bin/rvm-shell '2.1.0' -c '/etc/init.d/unicorn_myapp start'