Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions baremetal-bootstrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#!/usr/bin/env bash

#---------------------------------------------------------------------
usage ()
{
cat <<EOT

${0##*/}
A provisioning script for initializing a bare-metal server. This runs
from the the new machine, expects to have shell and sudo access, and
handles initial setup before handing off to \`bootstrap.sh\`. Installs
git, clones the specified repo and starts provisioning using the
specified APP_ENV value.

Usage:
From the machine to be provisioned:

curl https://raw.githubusercontent.com/loadsys/CakePHP-Shell-Scripts/master/${0##*/} | bash -s -- <REPO_CLONE_URL> <GIT_BRANCH> <APP_ENV_VALUE>

Should be run as the user the app will run as on the target server.

EOT

exit ${1:-0} # Exit with code 0 unless an arg is passed to the method.
}
if [ "$1" = '-h' ]; then
usage
fi


# Set up working vars.
REPO_CLONE_URL=${1}
GIT_BRANCH=${2}
APP_ENV=${3}
INSTALL_DIR="/var/www"
BOOTSTRAP_SCRIPT="./bootstrap.sh"


# Validate input and environment.
if [ -z "$REPO_CLONE_URL" ] ; then
echo "!! Must provide git repo URL as first arg (https recommended over git/ssh)."
exit 1
fi

if [ -z "$GIT_BRANCH" ] ; then
echo "!! Must provide desired git branch as second arg."
exit 2
fi

if [ -z "$APP_ENV" ] ; then
echo "!! Must provide desired APP_ENV as third arg."
exit 3
fi

if [ -d "${INSTALL_DIR}" ] && [ -n $(find "${INSTALL_DIR}" -maxdepth 0 -type d -empty 2>/dev/null) ]; then
echo "!! Installation directory is not empty. Aborting."
exit 4
fi


# main()

if [ -z "$(which git)" ]; then
echo "## Installing git."
sudo apt-get -y update
sudo apt-get -y install git-core build-essential
elif git --version 2>&1 >/dev/null | grep -q 'xcode-select'; then
echo "## Triggering Xcode Command Line Tools installation. Either complete"
echo "## that process or install git into your PATH yourself, then press"
echo "## [ENTER] to continue."
read WAIT_FOR_INSTALL_TO_FINISH
if [ ! -x /Library/Developer/CommandLineTools/usr/bin/git ]; then
echo "!! Git installation appears to have failed."
echo "!! Please manually install and retry. Aborting."
exit 5
fi
else
echo "## Git appears to already be installed."
fi

# Alternative fully automated approach:
# Ref: https://apple.stackexchange.com/a/121044/17403
#
# xcode-select --install
# sleep 1
# osascript <<EOD
# tell application "System Events"
# tell process "Install Command Line Developer Tools"
# keystroke return
# click button "Agree" of window "License Agreement"
# end tell
# end tell
# EOD


echo "## Cloning repo."
git clone -b $GIT_BRANCH $REPO_CLONE_URL repo

echo "## Moving repo into place."
sudo mv repo "${INSTALL_DIR}"

cd "${INSTALL_DIR}"

if [ ! -x "${BOOTSTRAP_SCRIPT}" ] ; then
echo "!! Bootstrap script is not present. Exiting."
exit 6
fi

echo "## Executing bootstrap script."
"${BOOTSTRAP_SCRIPT}" $APP_ENV