-
Notifications
You must be signed in to change notification settings - Fork 1
/
baremetal-bootstrap
110 lines (86 loc) · 2.78 KB
/
baremetal-bootstrap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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