LoginSignup
14
14

More than 5 years have passed since last update.

xbuild を使って AWS Elastic Beanstalk で任意のバージョンの Ruby を利用する方法

Last updated at Posted at 2014-03-06

カスタムAMIを使わずに .ruby-version を見て、バージョンを切り替える秘伝のタレ的な、.ebextensions を発明した。以下の様な設定ファイルを .ebextensions 配下に作って、デプロイするだけで良い。
とりあえず、2014/3/6 現在はこれで動く。ついでに、Rubyのビルドの高速化とgitからbundle installした場合にPassengerがgemを認識しない問題の修正も入っている。

xbuild を使っているので、perl、python, php, node, ruby の全てに応用できるはず。t1.micro インスタンスだと起動に30分くらいかかるけど、そこは初回だけなんで我慢しよう。

option_settings:
  - namespace: aws:elasticbeanstalk:application:environment
    option_name: NOKOGIRI_USE_SYSTEM_LIBRARIES
    value: yes
  - namespace: aws:elasticbeanstalk:application:environment
    option_name: DEFAULT_RUBY_VERSION
    value: 2.1.0

packages:
  yum:
    git: []

files:
  /opt/elasticbeanstalk/hooks/appdeploy/pre/05_install_ruby.sh:
    mode: "000755"
    user: root
    group: root
    encoding: plain
    content: |
      #!/usr/bin/env bash
      ORG_PATH=$PATH
      . /opt/elasticbeanstalk/support/envvars
      cd $EB_CONFIG_APP_ONDECK
      export RUBY_VERSION=$DEFAULT_RUBY_VERSION
      if [ -f .ruby-version ]; then
        export RUBY_VERSION=`cat .ruby-version`
      fi
      echo "use ruby ${RUBY_VERSION}"
      export RUBY_PATH=/usr/local/bin/ruby-${RUBY_VERSION}
      if [ ! -e $RUBY_PATH/bin/ruby ]; then
        echo "install ruby-${RUBY_VERSION} start"
        CONFIGURE_OPTS='--disable-install-rdoc' /usr/local/bin/xbuild/ruby-install ${RUBY_VERSION} ${RUBY_PATH}
        if [ $? != 0 ]; then
          echo "ERROR: install ruby ${RUBY_VERSION} failed!"
          exit 1
        else
          echo "install ruby ${RUBY_VERSION} succeeded"
        fi
      fi
      ${RUBY_PATH}/bin/gem install bundler --no-ri --no-rdoc
      ${RUBY_PATH}/bin/gem install passenger --no-ri --no-rdoc
      export PATH=${RUBY_PATH}/bin:$ORG_PATH
      echo "export PATH=${PATH}" > /opt/elasticbeanstalk/support/envvars.d/appenv
      true

  /opt/elasticbeanstalk/hooks/appdeploy/pre/10_bundle_install.sh:
    #!/usr/bin/env bash
    . /opt/elasticbeanstalk/support/envvars
    cd $EB_CONFIG_APP_ONDECK
    if [ -f Gemfile ]; then
      echo "running 'bundle install' with Gemfile:"
      cat Gemfile
      bundle install --deployment
      if [ $? != 0 ]; then
        echo "ERROR: bundle install failed!"
        exit 1
      else
        echo "bundle install succeeded"
      fi
    else
      echo "no Gemfile found! Skipping bundle install stage!"
    fi
    if [ -f Gemfile.lock ]; then
      echo "encountered a Gemfile.lock, setting proper permissions"
      chown $EB_CONFIG_APP_USER:$EB_CONFIG_APP_USER Gemfile.lock
    else
      echo "no Gemfile.lock file found, so no permissions to set on it"
    fi
    true

commands:
  01_clone_xbuild:
    command: git clone https://github.com/tagomoris/xbuild.git
    cwd: /usr/local/bin
    ignoreErrors: true

container_commands:
  01_force_restart_passenger:
    command: /etc/init.d/passenger restart
14
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
14