|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +#--------------------------------------------------------------------- |
| 4 | +usage () |
| 5 | +{ |
| 6 | + cat <<EOT |
| 7 | +
|
| 8 | +${0##*/} |
| 9 | + A provisioning script for initializing a bare-metal server. This runs |
| 10 | + from the the new machine, expects to have shell and sudo access, and |
| 11 | + handles initial setup before handing off to \`bootstrap.sh\`. Installs |
| 12 | + git, clones the specified repo and starts provisioning using the |
| 13 | + specified APP_ENV value. |
| 14 | +
|
| 15 | +Usage: |
| 16 | + From the machine to be provisioned: |
| 17 | +
|
| 18 | + curl https://raw.githubusercontent.com/loadsys/CakePHP-Shell-Scripts/master/${0##*/} | bash -s -- <REPO_CLONE_URL> <GIT_BRANCH> <APP_ENV_VALUE> |
| 19 | +
|
| 20 | + Should be run as the user the app will run as on the target server. |
| 21 | +
|
| 22 | +EOT |
| 23 | + |
| 24 | + exit ${1:-0} # Exit with code 0 unless an arg is passed to the method. |
| 25 | +} |
| 26 | +if [ "$1" = '-h' ]; then |
| 27 | + usage |
| 28 | +fi |
| 29 | + |
| 30 | + |
| 31 | +# Set up working vars. |
| 32 | +REPO_CLONE_URL=${1} |
| 33 | +GIT_BRANCH=${2} |
| 34 | +APP_ENV=${3} |
| 35 | +INSTALL_DIR="/var/www" |
| 36 | +BOOTSTRAP_SCRIPT="./bootstrap.sh" |
| 37 | + |
| 38 | + |
| 39 | +# Validate input and environment. |
| 40 | +if [ -z "$REPO_CLONE_URL" ] ; then |
| 41 | + echo "!! Must provide git repo URL as first arg (https recommended over git/ssh)." |
| 42 | + exit 1 |
| 43 | +fi |
| 44 | + |
| 45 | +if [ -z "$GIT_BRANCH" ] ; then |
| 46 | + echo "!! Must provide desired git branch as second arg." |
| 47 | + exit 2 |
| 48 | +fi |
| 49 | + |
| 50 | +if [ -z "$APP_ENV" ] ; then |
| 51 | + echo "!! Must provide desired APP_ENV as third arg." |
| 52 | + exit 3 |
| 53 | +fi |
| 54 | + |
| 55 | +if [ -d "${INSTALL_DIR}" ] && [ -n $(find "${INSTALL_DIR}" -maxdepth 0 -type d -empty 2>/dev/null) ]; then |
| 56 | + echo "!! Installation directory is not empty. Aborting." |
| 57 | + exit 4 |
| 58 | +fi |
| 59 | + |
| 60 | + |
| 61 | +# main() |
| 62 | + |
| 63 | +if [ -z "$(which git)" ]; then |
| 64 | + echo "## Installing git." |
| 65 | + sudo apt-get -y update |
| 66 | + sudo apt-get -y install git-core build-essential |
| 67 | +elif git --version 2>&1 >/dev/null | grep -q 'xcode-select'; then |
| 68 | + echo "## Triggering Xcode Command Line Tools installation. Either complete" |
| 69 | + echo "## that process or install git into your PATH yourself, then press" |
| 70 | + echo "## [ENTER] to continue." |
| 71 | + read WAIT_FOR_INSTALL_TO_FINISH |
| 72 | + if [ ! -x /Library/Developer/CommandLineTools/usr/bin/git ]; then |
| 73 | + echo "!! Git installation appears to have failed." |
| 74 | + echo "!! Please manually install and retry. Aborting." |
| 75 | + exit 5 |
| 76 | + fi |
| 77 | +else |
| 78 | + echo "## Git appears to already be installed." |
| 79 | +fi |
| 80 | + |
| 81 | +# Alternative fully automated approach: |
| 82 | +# Ref: https://apple.stackexchange.com/a/121044/17403 |
| 83 | +# |
| 84 | +# xcode-select --install |
| 85 | +# sleep 1 |
| 86 | +# osascript <<EOD |
| 87 | +# tell application "System Events" |
| 88 | +# tell process "Install Command Line Developer Tools" |
| 89 | +# keystroke return |
| 90 | +# click button "Agree" of window "License Agreement" |
| 91 | +# end tell |
| 92 | +# end tell |
| 93 | +# EOD |
| 94 | + |
| 95 | + |
| 96 | +echo "## Cloning repo." |
| 97 | +git clone -b $GIT_BRANCH $REPO_CLONE_URL repo |
| 98 | + |
| 99 | +echo "## Moving repo into place." |
| 100 | +sudo mv repo "${INSTALL_DIR}" |
| 101 | + |
| 102 | +cd "${INSTALL_DIR}" |
| 103 | + |
| 104 | +if [ ! -x "${BOOTSTRAP_SCRIPT}" ] ; then |
| 105 | + echo "!! Bootstrap script is not present. Exiting." |
| 106 | + exit 6 |
| 107 | +fi |
| 108 | + |
| 109 | +echo "## Executing bootstrap script." |
| 110 | +"${BOOTSTRAP_SCRIPT}" $APP_ENV |
0 commit comments