#!/bin/bash set -e shome="$(cd -P -- "$(dirname -- "$BASH_SOURCE")/.." && pwd -P)" function ryaml { ruby -ryaml -e ' def ps x unless x.nil? puts (x.class == String || x.class == Fixnum) ? x : x.to_yaml end end ps ARGV[1..-1].inject(YAML.load(File.read(ARGV[0]))) {|acc, key| acc[acc.class == Array ? key.to_i : key] } ' "$@" 2>&- } function verify_ruby { local ruby_runner="$*"; # verify ruby version matches deploy descriptor local ruby_version_platform="$($ruby_runner ruby -e 'puts "#{RUBY_VERSION} #{RUBY_PLATFORM}"' 2>&-)" local version_ruby="${ruby_version_platform%% *}" local platform_ruby="${ruby_version_platform##* }" if [[ -n "$app_ruby_version" ]]; then if [[ "$version_ruby" != "$app_ruby_version" ]]; then return 1 fi fi if [[ -n "$app_ruby_platform" ]]; then if [[ "$platform_ruby" != "$app_ruby_platform" ]]; then return 1 fi fi if [[ -n "$not_app_ruby_platform" ]]; then if [[ "$platform_ruby" = "$not_app_ruby_platform" ]]; then return 1 fi fi } function fatal_ruby { local ruby_loader="$*" : ${ruby_loader:=ruby} echo "Expecting $ruby_loader to have:" 1>&2 if [[ -n "$app_ruby_version" ]]; then echo " version $app_ruby_version" 1>&2 fi if [[ -n "$app_ruby_platform" ]]; then echo " platform $app_ruby_platform" 1>&2 fi if [[ -n "$not_app_ruby_platform" ]]; then echo " not platform $not_app_ruby_platform" 1>&2 fi exit 1 } function main { # load ruby configuration from deploy descriptor local app_ruby="$(ryaml $shome/config/deploy.yml app_ruby)" local app_ruby_version="$(ryaml $shome/config/deploy.yml app_ruby_version)" local app_ruby_platform="$(ryaml $shome/config/deploy.yml app_ruby_platform)" local not_app_ruby_platform="$(ryaml $shome/config/deploy.yml not_app_ruby_platform)" local ruby_loader="$(ryaml $shome/config/deploy.yml ruby_loader)" local ruby_runner="$ruby_loader $app_ruby" local chef_path="$(ryaml $shome/config/deploy.yml chef_path)" # if ruby loader is rvm-exec, try to find it if [[ "$ruby_loader" = "rvm-exec" ]]; then PATH="$PATH:/usr/local/rvm/bin:$HOME/.rvm/bin" local pth_rvm="$(type -f $ruby_loader 2>&- | awk '{print $3}')" if [[ ! -x "$pth_rvm" ]]; then # if rvm-exec not found, don't both with ruby version management ruby_runner="" fi fi if [[ -d "$chef_path" ]]; then # in operations environments, must use ruby loader if !verify_ruby "$ruby_runner"; then # ruby loader is not loading the right ruby fatal_ruby $ruby_runner fi else # proxied deploy, so relax rvm rquirements, just check current ruby if verify_ruby; then # current ruby is OK ruby_runner="" true elif [[ -n "$ruby_runner" ]] && verify_ruby "$ruby_runner"; then # trying again with ruby_runner works true else fatal_ruby # since rvm requirements are relaxed, report on current ruby errors fi fi if ! $ruby_runner bundle check 2>&- > /dev/null; then $ruby_runner bundle install --local --quiet --path vendor/bundle fi # pass deploy command and arguments to ao script in alpha_omega gem $ruby_runner bundle exec ao "${BASH_SOURCE##*/}" "$@" } main "$@"