-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.sh
executable file
·156 lines (128 loc) · 4.1 KB
/
bootstrap.sh
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/bash
# The output from this script is found in the instance
# system log available through the AWS API, or the
# console.
#Settings needed to bootstrap and load settings
GIT_BRANCH=${GIT_BRANCH:-master}
#PIP_ARGS=""
# Can read multiple settings URLS by separating them with a space
#SETTINGS_URL=""
#DEBUG=true
# Can be set here, or loaded from s3 settings
# Takes a space separated list of scripts to run
NEXT_SCRIPT=${NEXT_SCRIPT:-""}
### PREREQs
# python virtualenv and pip
# curl
set -e
cd /root
# Directory where we store intermediate steps/arguments/things we want to share
# between scripts
if [ ! -d /root/.swarmy ]; then
mkdir /root/.swarmy/
fi
export SWARMYDIR=/root/.swarmy/
if [ -z "$DEBUG" ]; then
# If we aren't debugging, we just want to have stdout/stderr be redirect to
# files instead so that we can check after the fact that things ran
# successfully
# Close STDOUT file descriptor
exec 1<&-
# Close STDERR FD
exec 2<&-
# Open STDOUT as $LOG_FILE file for read and write.
exec 1<>$SWARMYDIR/log.stdout
# Redirect STDERR to STDOUT
exec 2<>$SWARMYDIR/log.stderr
else
echo "Debug run of swarmy bootstrap.sh"
set -x
#printenv
fi
echo -n "Starting swarmy bootstrap: "
date
#Create a virtualenv, don't download new pip/setuptools
virtualenv VENV
. VENV/bin/activate
# This allows pinning pip/setuptools from your own PyPI repo
pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org $PIP_ARGS -U pip
pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org $PIP_ARGS -U setuptools
pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org boto
pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org awscli
pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org docopt
if [ ! -d swarmy ]
then
mkdir -p swarmy
# Do this in multiple steps so that a failure to download doesn't cause tar
# to uncompress partially
curl -sL -o swarmy.tar.gz \
"https://api.github.com/repos/Crunch-io/swarmy/tarball/${GIT_BRANCH}"
if [ $? -gt 0 ]; then
echo "Failed to download Swarmy. Refusing to continue."
rm -f swarmy.tar.gz
exit 1
fi
tar -xzf swarmy.tar.gz --strip-components=1 -C swarmy
rm -f swarmy.tar.gz
fi
pip install $PIP_ARGS -e swarmy
function download_next
{
# Usage: FILE=$(download_next URL [tmpfile_suffix])
# FILE will be set to the local path of the file
# Calling method is responsible for deleting the file
OUT=$(mktemp --suffix="$2" bootstrap.XXXXXXXXXX) || { echo "Failed to create temp file"; exit 1; }
case "$1" in
s3://*)
#Download the script, set perms, and then execute
# AWSCLI is a req of swarmy and is installed during setup.py develop above
aws s3 cp --quiet $1 $OUT > /dev/null 2>&1
;;
http*://*)
#Download the script using curl, then execute
curl -sL $1 > $OUT
;;
*)
cat $1 > $OUT
;;
esac
echo -n $OUT
}
# Load environment settings from URL
if [ -n "$SETTINGS_URL" ]; then
URLLIST=($SETTINGS_URL)
for settings in ${URLLIST[@]}; do
if [ -n "$settings" ]; then
SSS=$(download_next $settings .profile)
set +e
source $SSS
#clean up
if [ -z "$DEBUG" ]; then
rm -f $SSS
fi
set -e
fi
done
else
if [ -n "$DEBUG" ]; then
echo "No settings url provided."
fi
fi
# Call the next script
# Make this an array
if [ -n "$NEXT_SCRIPT" ]; then
read -a URLLIST <<<$NEXT_SCRIPT
for script in ${URLLIST[@]}; do
if [ -n "$script" ]; then
SCRIPT=$(download_next $script .stage2.sh)
source $SCRIPT
if [ -z "$DEBUG" ]; then
rm -f $SCRIPT
fi
fi
done
else
echo "No stage 2 provided."
fi
echo -n "Swarmy bootstrap script finished: "
date