简体   繁体   English

在亚马逊上使用Capistrano和Unicorn进行部署

[英]Deployment with Capistrano and Unicorn in Amazon

I'm trying to deploy my rails application to Amazon Ec2. 我正在尝试将Rails应用程序部署到Amazon Ec2。

I'm using Capistrano + Unicorn + Nginx. 我正在使用Capistrano + Unicorn + Nginx。

The deploy:setup works fine, but when i try to do deploy:cold, there is an error: 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

I searched in StackOverFlow and i saw some topics to help me, but without succeed. 我在StackOverFlow中进行了搜索,但看到了一些可以帮助我的话题,但没有成功。

My unicorn.rb: 我的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: 我的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: 我的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

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/ http://ferryzhou.wordpress.com/2013/11/22/rails-digitalocean-nginx-unicorn-capistrano-bitbucket/

I just change the path of unicorn_init.sh. 我只是更改unicorn_init.sh的路径。 Thanks everyone. 感谢大家。

The erro su: must be run from a terminal is from your unicorn_init.sh. su: must be run from a terminal是来自unicorn_init.sh。

In run() function, it requires to be called in 'deployer' user session. 在run()函数中,需要在“部署者”用户会话中调用它。

Please focus on that, and it seems the code is different with what you are getting error. 请专注于此,似乎代码与您得到的错误有所不同。

eg 例如

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'. 但是在您的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. Log in with deployer (same user in cap config, set :user, "deployer" ) user via ssh 通过ssh登录到deployer(cap配置中为同一用户, set :user, "deployer" )用户

  2. 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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM