繁体   English   中英

Rails +独角兽+ Nginx + Capistrano 3 + Linode VPS:无法识别暂存环境

[英]Rails + Unicorn + Nginx + Capistrano 3 + Linode VPS: Not Recognizing Staging Environment

我已经绞尽脑汁了几天,并且在这个问题上已经用尽了我的研究。

一些背景知识:我有一个Rails应用程序,在生产中工作得很好。 我在不同目录下的同一服务器上添加了暂存环境。 我可以去登台现场。

我注意到的是:

  • 我的代码更改(在暂存中)正确显示在暂存站点上。
  • 我的登台数据库已成功创建。

问题:

看来我的登台站点认为这是生产站点。 我觉得我没有在某个地方正确设置过渡环境。 一些奇怪的事情正在发生:

  • 写入production.log
  • 它正在使用生产数据库

代码:(我已经替换了实际的域名/ IP)

config / deploy.rb

# config valid only for current version of Capistrano
lock '3.3.5'

set :stages, %w(production staging)
set :default_stage, 'staging'

set :repo_url, 'git@github.com:test/test.git'
set :user, 'deploy'
set :linked_dirs, %w{log tmp/pids tmp/cache tmp/sockets public/system/members}

namespace :deploy do

  %w[start stop restart].each do |command|
    desc 'Manage Unicorn'
    task command do
      on roles(:app), in: :sequence, wait: 1 do
        execute "/etc/init.d/unicorn_#{fetch(:application)} #{command}"
      end      
    end
  end

  after :publishing, :restart

end

config / deploy / staging.rb

set :rails_env, 'staging'
set :application, 'test_staging'
set :deploy_to, '/var/www/staging.test.co'
set :branch, 'staging'

role :app, %w{deploy@IP_HERE}
role :web, %w{deploy@IP_HERE}
role :db,  %w{deploy@IP_HERE}

config / unicorn.rb

if ENV["RAILS_ENV"] == "production"
    root = "/var/www/test.co/current"
else
    root = "/var/www/staging.test.co/current"
end
working_directory root
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"

if ENV["RAILS_ENV"] == "production"
    listen "/tmp/unicorn.test.sock"
else
    listen "/tmp/unicorn.test_staging.sock"
end
worker_processes 1
timeout 30

[在服务器上] /etc/init.d/unicorn_test_staging

#!/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=/var/www/staging.test.co/current
PID=$APP_ROOT/tmp/pids/unicorn.pid
CMD="cd $APP_ROOT; bundle exec unicorn -D -c      $APP_ROOT/config/unicorn.rb -E staging"
AS_USER=deploy
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 "
  exit 1
  ;;
esac

config / database.yml

development:
 adapter: postgresql
 encoding: unicode
 database: test_dev
 host: localhost
 pool: 5
 username: test
 password: password

staging:
 adapter: postgresql
 encoding: unicode
 database: test_staging

production:
 adapter: postgresql
 encoding: unicode
 database: test_production

如果您需要任何其他代码来帮助我在这里找到问题,请告诉我。 感谢任何人对此的帮助。

谢谢!

您的登台服务器很可能正在调用生产服务器的路由,因为它们位于同一主机上。

您需要确保使用以下命令设置子文件夹:config.relative_url_root或RAILS_RELATIVE_URL_ROOT环境变量。

配置Rails应用程序中查找relative_url_root

终于明白了。 我在屏幕上编写了Rails.env,它在我的登台站点上返回了“生产”。 这就是为什么它使用我的生产数据库和日志。

最初创建暂存站点时,最初忘记将/etc/init.d/unicorn_myapp中的unicorn初始化脚本更改为-E暂存。 我将其设置为几天前的分期,但并没有解决问题。

所以今天出于绝望,我进入服务器并尝试了一次完整的独角兽停止并启动

sudo service unicorn_myapp stop
sudo service unicorn_myapp start

这样就解决了问题! 我的Rails.env开始返回“登台”,并且现在一切正常。

TLDR:如果您更改独角兽的初始化脚本,请务必确保停止并启动独角兽应用程序。 一个简单的重启对我不起作用。

暂无
暂无

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

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