【问题标题】:Deployment with Capistrano and Unicorn in Amazon在 Amazon 中使用 Capistrano 和 Unicorn 进行部署
【发布时间】:2014-03-09 06:22:59
【问题描述】:

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

我正在使用 Capistrano + Unicorn + Nginx。

deploy:setup 工作正常,但是当我尝试执行 deploy: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 中搜索,我看到了一些对我有帮助的主题,但没有成功。

我的独角兽.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 的路径。谢谢大家。

【问题讨论】:

  • 你能手动启动你的独角兽服务吗?
  • 我在服务器上试过: bundle exec unicorn -E production -c config/unicorn.rb 没有任何反应。我认为这是 Capistrano 的工作,我不能简单地手动启动独角兽,因为在每次部署中我都必须这样做。

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


【解决方案1】:

错误 su: must be run from a terminal 来自您的 unicorn_init.sh。

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

请注意这一点,似乎代码与您遇到的错误有所不同。

例如

你提到了

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. 使用部署者(cap config 中的同一用户,set :user, "deployer")用户通过 ssh 登录

  2. 确保您可以运行以下命令来启动独角兽。

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

【讨论】:

  • 嗨 xmpolaris,我写错了,它是 myapp。当我尝试执行启动 unicorn 的行时,出现此错误:bash: /etc/init.d/unicorn_myapp: Permission denied
  • 确保 /etc/init.d/unicorn_myapp 可由部署者用户运行。使用 chown 和 chmod。
猜你喜欢
  • 2012-12-20
  • 1970-01-01
  • 2016-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-27
相关资源
最近更新 更多