forked from cloudfoundry-community/terraform-aws-cf-install
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprovision.sh
288 lines (241 loc) · 8.51 KB
/
provision.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/bin/bash
# fail immediately on error
set -e -x
# echo "$0 $*" > ~/provision.log
fail() {
echo "$*" >&2
exit 1
}
# Variables passed in from terraform, see aws-vpc.tf, the "remote-exec" provisioner
AWS_KEY_ID=${1}
AWS_ACCESS_KEY=${2}
REGION=${3}
VPC=${4}
BOSH_SUBNET=${5}
IPMASK=${6}
CF_IP=${7}
CF_SUBNET1=${8}
CF_SUBNET1_AZ=${9}
CF_SUBNET2=${10}
CF_SUBNET2_AZ=${11}
BASTION_AZ=${12}
BASTION_ID=${13}
LB_SUBNET1=${14}
CF_SG=${15}
CF_ADMIN_PASS=${16}
CF_DOMAIN=${17}
CF_BOSHWORKSPACE_VERSION=${18}
CF_SIZE=${19}
DOCKER_SUBNET=${20}
INSTALL_DOCKER=${21}
boshDirectorHost="${IPMASK}.1.4"
cfReleaseVersion="207"
cd $HOME
(("$?" == "0")) ||
fail "Could not find HOME folder, terminating install."
# Generate the key that will be used to ssh between the bastion and the
# microbosh machine
if [[ ! -f ~/.ssh/id_rsa ]]; then
ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa
fi
# Prepare the jumpbox to be able to install ruby and git-based bosh and cf repos
release=$(cat /etc/*release | tr -d '\n')
case "${release}" in
(*Ubuntu*|*Debian*)
sudo apt-get update -yq
sudo apt-get install -yq aptitude
sudo aptitude -yq install build-essential vim-nox git unzip tree \
libxslt-dev libxslt1.1 libxslt1-dev libxml2 libxml2-dev \
libpq-dev libmysqlclient-dev libsqlite3-dev \
g++ gcc make libc6-dev libreadline6-dev zlib1g-dev libssl-dev libyaml-dev \
libsqlite3-dev sqlite3 autoconf libgdbm-dev libncurses5-dev automake \
libtool bison pkg-config libffi-dev cmake
;;
(*Centos*|*RedHat*|*Amazon*)
sudo yum update -y
sudo yum install -y epel-release
sudo yum install -y git unzip xz tree rsync openssl openssl-devel \
zlib zlib-devel libevent libevent-devel readline readline-devel cmake ntp \
htop wget tmux gcc g++ autoconf pcre pcre-devel vim-enhanced gcc mysql-devel \
postgresql-devel postgresql-libs sqlite-devel libxslt-devel libxml2-devel \
yajl-ruby cmake
;;
esac
# Install RVM
if [[ ! -d "$HOME/rvm" ]]; then
git clone https://github.com/rvm/rvm
fi
if [[ ! -d "$HOME/.rvm" ]]; then
cd rvm
./install
fi
cd $HOME
if [[ ! "$(ls -A $HOME/.rvm/environments)" ]]; then
~/.rvm/bin/rvm install ruby-2.1
fi
if [[ ! -d "$HOME/.rvm/environments/default" ]]; then
~/.rvm/bin/rvm alias create default 2.1
fi
source ~/.rvm/environments/default
source ~/.rvm/scripts/rvm
# Install BOSH CLI, bosh-bootstrap, spiff and other helpful plugins/tools
gem install fog-aws -v 0.1.1 --no-ri --no-rdoc --quiet
gem install bundler bosh-bootstrap --no-ri --no-rdoc --quiet
# We use fog below, and bosh-bootstrap uses it as well
cat <<EOF > ~/.fog
:default:
:aws_access_key_id: $AWS_KEY_ID
:aws_secret_access_key: $AWS_ACCESS_KEY
:region: $REGION
EOF
# This volume is created using terraform in aws-bosh.tf
if [[ ! -d "$HOME/workspace" ]]; then
sudo /sbin/mkfs.ext4 /dev/xvdc
sudo /sbin/e2label /dev/xvdc workspace
echo 'LABEL=workspace /home/ubuntu/workspace ext4 defaults,discard 0 0' | sudo tee -a /etc/fstab
mkdir -p /home/ubuntu/workspace
sudo mount -a
sudo chown -R ubuntu:ubuntu /home/ubuntu/workspace
fi
# As long as we have a large volume to work with, we'll move /tmp over there
# You can always use a bigger /tmp
if [[ ! -d "$HOME/workspace/tmp" ]]; then
sudo rsync -avq /tmp/ /home/ubuntu/workspace/tmp/
fi
if ! [[ -L "/tmp" && -d "/tmp" ]]; then
sudo rm -fR /tmp
sudo ln -s /home/ubuntu/workspace/tmp /tmp
fi
# bosh-bootstrap handles provisioning the microbosh machine and installing bosh
# on it. This is very nice of bosh-bootstrap. Everyone make sure to thank bosh-bootstrap
mkdir -p {bin,workspace/deployments/microbosh,workspace/tools}
pushd workspace/deployments
pushd microbosh
cat <<EOF > settings.yml
---
bosh:
name: bosh-${VPC}
provider:
name: aws
credentials:
provider: AWS
aws_access_key_id: ${AWS_KEY_ID}
aws_secret_access_key: ${AWS_ACCESS_KEY}
region: ${REGION}
address:
vpc_id: ${VPC}
subnet_id: ${BOSH_SUBNET}
ip: ${boshDirectorHost}
EOF
if [[ ! -d "$HOME/workspace/deployments/microbosh/deployments" ]]; then
bosh bootstrap deploy
fi
# We've hardcoded the IP of the microbosh machine, because convenience
bosh -n target https://${boshDirectorHost}:25555
bosh login admin admin
if [[ ! "$?" == 0 ]]; then
#wipe the ~/workspace/deployments/microbosh folder contents and try again
echo "Retry deploying the micro bosh..."
fi
popd
# There is a specific branch of cf-boshworkspace that we use for terraform. This
# may change in the future if we come up with a better way to handle maintaining
# configs in a git repo
if [[ ! -d "$HOME/workspace/deployments/cf-boshworkspace" ]]; then
git clone --branch ${CF_BOSHWORKSPACE_VERSION} http://github.com/cloudfoundry-community/cf-boshworkspace
fi
pushd cf-boshworkspace
mkdir -p ssh
gem install bundler
bundle install
# Pull out the UUID of the director - bosh_cli needs it in the deployment to
# know it's hitting the right microbosh instance
DIRECTOR_UUID=$(bosh status --uuid)
# If CF_DOMAIN is set to XIP, then use XIP.IO. Otherwise, use the variable
if [[ $CF_DOMAIN == "XIP" ]]; then
CF_DOMAIN="${CF_IP}.xip.io"
fi
if [[ ! -f "/usr/local/bin/spiff" ]]; then
curl -sOL https://github.com/cloudfoundry-incubator/spiff/releases/download/v1.0.3/spiff_linux_amd64.zip
unzip spiff_linux_amd64.zip
sudo mv ./spiff /usr/local/bin/spiff
rm spiff_linux_amd64.zip
fi
# This is some hackwork to get the configs right. Could be changed in the future
/bin/sed -i \
-e "s/CF_SUBNET1_AZ/${CF_SUBNET1_AZ}/g" \
-e "s/CF_SUBNET2_AZ/${CF_SUBNET2_AZ}/g" \
-e "s/LB_SUBNET1_AZ/${CF_SUBNET1_AZ}/g" \
-e "s/CF_ELASTIC_IP/${CF_IP}/g" \
-e "s/CF_SUBNET1/${CF_SUBNET1}/g" \
-e "s/CF_SUBNET2/${CF_SUBNET2}/g" \
-e "s/LB_SUBNET1/${LB_SUBNET1}/g" \
-e "s/DIRECTOR_UUID/${DIRECTOR_UUID}/g" \
-e "s/CF_DOMAIN/${CF_DOMAIN}/g" \
-e "s/CF_ADMIN_PASS/${CF_ADMIN_PASS}/g" \
-e "s/IPMASK/${IPMASK}/g" \
-e "s/CF_SG/${CF_SG}/g" \
-e "s/LB_SUBNET1_AZ/${CF_SUBNET1_AZ}/g" \
deployments/cf-aws-${CF_SIZE}.yml
# Upload the bosh release, set the deployment, and execute
deployedVersion=$(bosh releases | grep " ${cfReleaseVersion}" | awk '{print $4}')
deployedVersion="${deployedVersion//[^[:alnum:]]/}"
if [[ ! "$deployedVersion" == "${cfReleaseVersion}" ]]; then
bosh upload release https://bosh.io/d/github.com/cloudfoundry/cf-release?v=${cfReleaseVersion}
bosh deployment cf-aws-${CF_SIZE}
bosh prepare deployment || bosh prepare deployment #Seems to always fail on the first run...
else
bosh deployment cf-aws-${CF_SIZE}
fi
# Work around until bosh-workspace can handle submodules
if [[ "cf-aws-${CF_SIZE}" == "cf-aws-large" ]]; then
pushd .releases/cf
./update
popd
fi
# We locally commit the changes to the repo, so that errant git checkouts don't
# cause havok
currentGitUser="$(git config user.name || /bin/true )"
currentGitEmail="$(git config user.email || /bin/true )"
if [[ "${currentGitUser}" == "" || "${currentGitEmail}" == "" ]]; then
git config --global user.email "${USER}@${HOSTNAME}"
git config --global user.name "${USER}"
echo "blarg"
fi
gitDiff="$(git diff)"
if [[ ! "${gitDiff}" == "" ]]; then
git commit -am 'commit of the local deployment configs'
fi
# Keep trying until there is a successful BOSH deploy.
for i in {0..2}
do bosh -n deploy
done
echo "Install Traveling CF"
if [[ "$(cat $HOME/.bashrc | grep 'export PATH=$PATH:$HOME/bin/traveling-cf-admin')" == "" ]]; then
curl -s https://raw.githubusercontent.com/cloudfoundry-community/traveling-cf-admin/master/scripts/installer | bash
echo 'export PATH=$PATH:$HOME/bin/traveling-cf-admin' >> $HOME/.bashrc
source $HOME/.bashrc
fi
# Now deploy docker services if requested
if [[ $INSTALL_DOCKER == "true" ]]; then
cd ~/workspace/deployments
if [[ ! -d "$HOME/workspace/deployments/docker-services-boshworkspace" ]]; then
git clone https://github.com/cloudfoundry-community/docker-services-boshworkspace.git
fi
echo "Update the docker-aws-vpc.yml with cf-boshworkspace parameters"
/home/ubuntu/workspace/deployments/docker-services-boshworkspace/shell/populate-docker-aws-vpc ${CF_SIZE}
dockerDeploymentManifest="/home/ubuntu/workspace/deployments/docker-services-boshworkspace/deployments/docker-aws-vpc.yml"
/bin/sed -i "s/SUBNET_ID/${DOCKER_SUBNET}/g" "${dockerDeploymentManifest}"
cd ~/workspace/deployments/docker-services-boshworkspace
bundle install
bosh deployment docker-aws-vpc
bosh prepare deployment
# Keep trying until there is a successful BOSH deploy.
for i in {0..2}
do bosh -n deploy
done
fi
echo "Provision script completed..."
exit 0
# FIXME: enable this again when smoke_tests work
# bosh run errand smoke_tests