forked from eBayClassifiedsGroup/PanteraS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provision.sh
executable file
·199 lines (163 loc) · 6.05 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
#!/bin/bash
paas="PanteraS"
REPODIR=`dirname $0`
IMAGE="panteras/paas-in-a-box:latest"
usage(){
cat <<_END_
Summary:
Provision $paas on either vagrant vm or a boot2docker vm.
Options
-m boot2docker|vagrant|vagrant-provision
-h help
-b rebuild $paas image
Examples
Provision $paas for use with boot2docker:
$0 -m boot2docker
Provision $paas for use with vagrant:
$0 -m vagrant
Provision $paas components inside the vagrant virtual machine
$0 -m vagrant-provision -b
_END_
}
MODE=""
BUILDIMAGES=false
while getopts ":m:bh" mode ; do
case $mode in
m)
case $OPTARG in
boot2docker|vagrant|vagrant-provision)
echo "installing with mode \"$OPTARG\""
MODE=$OPTARG
;;
*)
echo "invalid argument for \"$OPTARG\" for option -m" >&2
usage >&2
exit 1
;;
esac
;;
b)
BUILDIMAGES=true
;;
h)
usage >&2
exit 0
;;
:)
echo "Error: missing argument for -$OPTARG" >&2
usage >&2
exit 1
;;
esac
done
[ -z "$MODE" ] && {
echo "missing mode" >&2
usage 1>&2
exit 1
}
# tests
isMac() { [[ "$OSTYPE" == "darwin"* ]] && return 0 || return 1; }
isLinux() { [[ "$OSTYPE" == "linux"* ]] && return 0 || return 1; }
isUbuntu() { uname -a |grep -i ubuntu &>/dev/null && return 0 || return 1; }
hasVagrant() { $(hash vagrant 2>/dev/null) && return 0 || return 1; }
hasVirtualBox() { $(hash VBoxManage 2>/dev/null) && return 0 || return 1; }
hasDockerCompose() { $(hash docker-compose 2>/dev/null) && return 0 || return 1; }
hasBoot2docker() { $(hash boot2docker 2>/dev/null) && return 0 || return 1; }
hasDocker() { $(hash docker 2>/dev/null) && return 0 || return 1; }
case "$MODE" in
'vagrant')
hasVagrant || { echo "Vagrant not detected." >&2 && exit 1; }
hasVirtualBox || { echo "VirtualBox not detected." >&2 && exit 1; }
[ ! -f Vagrantfile ] && \
echo "Cannot find Vagrantfile. Are you running this from inside $paas git-repo top-level directory?" >&2 && \
exit 1
echo "starting vagrant vm with provisioning ..."
vagrant reload --provision
;;
'vagrant-provision')
echo "provisioning vagrant virtual machine with $paas components"
! isUbuntu && echo "error: $paas is only supported on an Ubuntu virtual machine host" && exit 1
[ ! -f /etc/apt/sources.list.d/docker.list ] && {
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv F76221572C52609D
echo 'deb https://apt.dockerproject.org/repo ubuntu-trusty main' > /etc/apt/sources.list.d/docker.list
sudo apt-get update
}
# ensure that "aufs" will be used within vagrant; see: https://forums.docker.com/t/docker-1-7-0-is-using-huge-amount-of-disk-space/2046
sudo apt-get -q -y install linux-image-extra-$(uname -r)
sudo apt-get -q -y install docker-engine=${DOCKER_VERSION} \
|| { wget http://apt.dockerproject.org/repo/pool/main/d/docker-engine/docker-engine_${DOCKER_VERSION}_amd64.deb \
&& dpkg -i docker-engine_${DOCKER_VERSION}_amd64.deb; }
! hasDockerCompose && {
sudo apt-get install -y python-pip
# Fix for pip - Ubuntu provide incompatible version
sudo pip install pip --upgrade
sudo pip install docker-compose --upgrade
}
# verify:
! hasDocker && echo "error: docker not detected." >&2 && exit 1
! hasDockerCompose && echo "error: docker-compose not detected" >&2 && exit 1
echo BUILDIMAGES
echo $BUILDIMAGES
[ "$BUILDIMAGES" == "true" ] && cd $REPODIR && sudo ./build-docker-images.sh \
|| docker pull ${IMAGE}
LOCALIP=${LOCALIP:-$(hostname --ip-address| awk '{ print $2}')}
DNS_CONFIG="DOCKER_OPTS=\"\${DOCKER_OPTS} --dns $LOCALIP\""
grep -q -- "$DNS_CONFIG" /etc/default/docker || echo $DNS_CONFIG >>/etc/default/docker && sudo service docker restart
# evil hack to set a proper /etc/hosts entry (non localhost) for our hostname.
# This is needed for marathon (and posible consul) checks to work properly. Since these
# service run within a docker container, and checks are performed on <MESOS_SLAVE_HOSTNAME>:<PORT>,
# this must resolve to a non localhost IP.
sudo sed -i "s/^127\.0\.1\.1\(.*\)/$LOCALIP\1/" /etc/hosts
cd $REPODIR
echo "stopping any running docker containers."
sudo docker-compose stop 2>/dev/null
sudo docker-compose kill 2>/dev/null
echo "deleting previously run docker containers."
sudo docker-compose rm --force 2>/dev/null
echo "(re)generating configuration file"
IP=$LOCALIP ./generate_yml.sh
echo "starting $paas components with docker-compose"
sudo docker-compose up -d
;;
"boot2docker")
! isMac && echo "boot2docker is only supported on OSX" >&2 && exit 1
! hasBoot2docker && echo "boot2docker not found. Please install the latest boot2docker version on your Mac" >&2 && exit 1
! hasDockerCompose && echo "DockerCompose not found. Please install docker-compose" >&2 && exit 1
[ "$(boot2docker status 2>/dev/null)" != "running" ] && {
echo "boot2docker not running. Attempting to start."
boot2docker up
} || {
echo "boot2docker is already running. not starting boot2docker."
}
$(boot2docker shellinit)
[ "$BUILDIMAGES" == "true" ] && cd $REPODIR && ./build-docker-images.sh \
|| docker pull ${IMAGE}
BOOT2DOCKERIP=$(boot2docker ip 2>/dev/null)
# dns config on boot2docker
DNS_CONFIG="EXTRA_ARGS=\\\"\\\${EXTRA_ARGS} --dns $BOOT2DOCKERIP\\\""
boot2docker ssh \
"sudo touch /var/lib/boot2docker/profile && \
grep -q -- \"$DNS_CONFIG\" /var/lib/boot2docker/profile || { \
echo $DNS_CONFIG | \
sudo tee -a /var/lib/boot2docker/profile && \
sudo /etc/init.d/docker restart && \
sleep 4
}"
echo "(re)generating docker-compose.yml configuration file"
cd $REPODIR
echo "generating docker-compose.yml configuration with boot2docker host ip $BOOT2DOCKERIP"
IP=$BOOT2DOCKERIP ./generate_yml.sh
echo "clean up previous container"
docker-compose stop 2>/dev/null
docker-compose kill 2>/dev/null
docker-compose rm --force 2>/dev/null
echo "starting $paas container"
docker-compose up -d
;;
*)
echo "wrong match" >&2
exit 1
;;
esac
# close boot2docker mode
exit 0