#!/bin/bash # first time, this will be empty, to trigger enforce_rvm : ${_AO_RVM:=} export _AO_OPT="$@" function deploy_basename { local cmd="$(basename "$1")"; shift echo "${cmd#aoh-}" } # ensure script runs under rvm function enforce_rvm { if [[ -n "$_AO_RVM" ]]; then return fi export _AO_RVM=1 local pth_ruby_loader="${_AO_RUBY_LOADER:-}" : ${pth_ruby_loader:=$(ryaml $_AO_HOME/config/deploy.yml ruby_loader)} local ruby_deploy="$(ryaml $_AO_HOME/config/deploy.yml app_ruby)" if [[ -z $pth_ruby_loader || -z $ruby_deploy ]]; then return fi if [[ ! -x $pth_ruby_loader ]]; then return fi exec $pth_ruby_loader $ruby_deploy "$0" "$@" } enforce_rvm "$@" # define command line options: # var name, default, description, short option DEFINE_string "gateway" "" "deploy gateway host" "G" DEFINE_boolean "proxy" "$FLAGS_FALSE" "use a gateway to proxy deploys" "p" DEFINE_boolean "direct" "$FLAGS_FALSE" "override proxy" "d" DEFINE_string "chef" "" "location to chef repo with opsdb" "c" DEFINE_string "tag" "HEAD" "tag to deploy in production" "t" DEFINE_string "user" "" "app user override" "u" DEFINE_string "group" "" "app group override" "g" DEFINE_string "ssh_login" "" "ssh login override" "l" # entry point function main { export _AO_ARGS="$@" if [[ "$#" = 0 ]]; then logger_fatal "must specify a pod name or integration environment when deploying" exit 1 fi local nm_component="$1" # will shift later since this could be an integration environment if [[ "$#" = 1 ]]; then local deploy_base="$(ryaml $_AO_HOME/config/deploy.yml deploy_base)" if [[ -n "$deploy_base" ]]; then local pod_shortcut="$(ryaml $HOME/.getting-started/config/pods.yml pods ${deploy_base}${nm_component} pod)" if [[ -n "$pod_shortcut" ]]; then nm_component="${deploy_base}${nm_component}" set -- "$nm_component" fi unset pod_shortcut fi fi if [[ -r "$HOME/.getting-started/config/pods.yml" ]]; then local pod_shortcut="$(ryaml $HOME/.getting-started/config/pods.yml pods $nm_component pod)" if [[ -n "$pod_shortcut" ]]; then nm_component="$pod_shortcut" else shift # treat as a pod name fi local pod_alias="$(ryaml $HOME/.getting-started/config/pods.yml pods $nm_component alias)" if [[ -n "$pod_alias" ]]; then nm_component="$pod_alias" fi local is_local="$(ryaml $HOME/.getting-started/config/pods.yml pods $nm_component local)" local hst_gateway="$(ryaml $HOME/.getting-started/config/pods.yml pods $nm_component gateway)" if [[ -z "$is_local" ]]; then if [[ -z "$hst_gateway" ]]; then logger_fatal "$nm_component is not a pod name" exit 1 fi FLAGS_proxy="$FLAGS_TRUE" else if [[ -z "$FLAGS_chef" ]]; then FLAGS_chef="$HOME/.getting-started" fi export _AO_ENV="$(ryaml $HOME/.getting-started/config/pods.yml pods $nm_component ao_env)" export RAILS_ENV="$_AO_ENV" if [[ -z "$_AO_ENV" ]]; then _AO_ENV="$(ryaml $HOME/.getting-started/config/pods.yml pods $nm_component env)" fi export _AO_SSH="$HOME/.getting-started/ssh/config" fi if [[ -z "$FLAGS_gateway" ]]; then FLAGS_gateway="$hst_gateway" fi fi if [[ -n "$FLAGS_user" ]]; then export _AO_USER="$FLAGS_user" fi : ${_AO_DEPLOYER:=$LOGNAME} export _AO_DEPLOYER if [[ -n "$FLAGS_group" ]]; then export _AO_GROUP="$FLAGS_group" fi if [[ -n "$FLAGS_ssh_login" ]]; then export _AO_SSH_LOGIN="$FLAGS_ssh_login" fi if [[ -n "$FLAGS_chef" ]]; then export _AO_CHEF="$FLAGS_chef" fi if [[ -n "$FLAGS_gateway" ]]; then export GATEWAY="$FLAGS_gateway" fi case "$nm_component" in *) if [[ "$FLAGS_proxy" = "$FLAGS_FALSE" || "$FLAGS_direct" = "$FLAGS_TRUE" || -z "$FLAGS_gateway" ]]; then export _AO_DEPLOY=1 local _AO_THIS_HOST="$(ryaml $HOME/.getting-started/config/pods.yml pods $nm_component this_host)" if [[ -n "$_AO_THIS_HOST" ]]; then export _AO_THIS_HOST fi local _AO_THIS_POD="$(ryaml $HOME/.getting-started/config/pods.yml pods $nm_component this_pod)" if [[ -n "$_AO_THIS_POD" ]]; then export _AO_THIS_POD fi bundle check 2>&1 >/dev/null || { bundle install --quiet --local --path vendor/bundle || bundle check > /dev/null; } aomain "$nm_component" "$@" else if [[ "$#" > 0 ]]; then case "$1" in all|world) logger_fatal "cannot use all or world, these are dangerous" exit 1 ;; production) if [[ -z "$FLAGS_tag" || "$FLAGS_tag" = "HEAD" ]]; then case "$(deploy_basename $0)" in hosts|debug) true ;; *) logger_fatal "must specify a version using --tag" exit 1 ;; esac fi ;; esac fi local gateway="$FLAGS_gateway" if [[ -n "$FLAGS_ssh_login" ]]; then gateway="${FLAGS_ssh_login}@${gateway}" fi local remote_chef="" if [[ -n "$FLAGS_chef" ]]; then remote_chef="-c $FLAGS_chef " fi $shome/sbin/proxy "$gateway" "$FLAGS_tag" $(deploy_basename $0) ${remote_chef}"$nm_component" "$@" fi ;; esac }