如何告诉rails应用程序中的unicorn shell脚本使用特定的ruby和gemset

时间:2014-12-31 05:16:03

标签: ruby-on-rails ruby shell ruby-on-rails-4 unicorn

如何设置unicorn shell文件以使用ruby-2.1.2@my_gemset,以便它可以作为sudo service unicorn start运行。我的Rails应用程序配置文件夹包含unicorn.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-90}
APP_ROOT=/home/user/Documents/workspace/MyApp
PID=$APP_ROOT/tmp/pids/unicorn.pid
CMD="cd $APP_ROOT;bundle exec bin/unicorn -c $APP_ROOT/config/unicorn.rb -E development"
AS_USER="www-data"
action="$1"
set -u

OLD_BIN="$PID.oldbin"

sig () {
  test -s "$PID" && kill -$1 `cat $PID`
}

oldsig () {
  test -s $OLD_BIN && kill -$1 `cat $OLD_BIN`
}

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 3
  then
    n=$TIMEOUT
    while test -s $OLD_BIN && test $n -ge 0
    do
      printf '.' && sleep 1 && n=$(( $n - 1 ))
    done
    echo

    if test $n -lt 0 && test -s $OLD_BIN
    then
      echo >&2 "$OLD_BIN 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

但是当我运行sudo service unicorn start时,我收到了此错误

/var/lib/gems/1.8/gems/bundler-1.5.3/lib/bundler/runtime.rb:220: warning: Insecure world writable dir /usr/local/bin in PATH, mode 040777
Could not find rake-10.4.2 in any of the sources
Run `bundle install` to install missing gems.

但实际上我正在使用ruby-2.1.2@my_gemset。如何使用rvm告诉shell脚本使用此gemset。

我的bin/unicorn文件包含此内容

#!/usr/bin/env ruby
#
# This file was generated by Bundler.
#
# The application 'unicorn' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'pathname'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
  Pathname.new(__FILE__).realpath)

require 'rubygems'
require 'bundler/setup'

load Gem.bin_path('unicorn', 'unicorn')

我知道我必须将/ usr / bin / env ruby​​的路径更改为我的ruby,但不知道如何执行此操作。另外请告诉我如何设置unicorn shell脚本以使用特定gemy的特定gemset.Thanks

2 个答案:

答案 0 :(得分:0)

我在远程服务器中使用rvm来管理我的宝石。我已经提交了 .ruby-gemset .ruby-version 以便它是统一的。在我的部署脚本中,我添加了下面的代码段,以确保我在正确的文件夹中使用正确的宝石集运行命令。所以执行:

source ~/.rvm/scripts/rvm; cd #{current_path}; /etc/init.d/unicorn #{command}"

我很确定这可以进一步重构,但我还没有更新我的脚本一段时间。但到目前为止,它一直在努力。我正在使用Capistrano 3。

答案 1 :(得分:0)

我以前没有做过这件事,但我认为这会奏效 -

添加此行 -

CMD_TO_USE_GEMSET="cd $APP_ROOT; rvm use ruby-2.1.2@my_gemset"

CMD="cd $APP_ROOT;bundle exec bin/unicorn -c $APP_ROOT/config/unicorn.rb -E development"

unicorn.sh

"start" -

之后,在你的unicorn.sh的"restart|reload"run "$CMD"部分写下来
run "$CMD_TO_USE_GEMSET"

现在运行命令重启unicorn -

sudo service unicorn start