forked from MicroPyramid/docker-box
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
349 lines (310 loc) · 14.6 KB
/
install.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/bin/sh
#TODO replace if condition with test to handle errors using status codes.
env_dir="$HOME/docker_box/db_env" &&
project_dir="$HOME/docker_box" &&
docker_api_port=2375 &&
command_exists(){
command -v "$@" > /dev/null 2>&1
}
dist_info(){
lsb_dist='' &&
dist_version='' &&
if command_exists lsb_release; then
lsb_dist="$(lsb_release -si)"
fi &&
if [ -z "$lsb_dist" ] && [ -r /etc/lsb-release ]; then
lsb_dist="$(. /etc/lsb-release && echo "$DISTRIB_ID")"
fi &&
if [ -z "$lsb_dist" ] && [ -r /etc/debian_version ]; then
lsb_dist='debian'
fi &&
if [ -z "$lsb_dist" ] && [ -r /etc/fedora-release ]; then
lsb_dist='fedora'
fi &&
if [ -z "$lsb_dist" ] && [ -r /etc/oracle-release ]; then
lsb_dist='oracleserver'
fi &&
if [ -z "$lsb_dist" ] && [ -r /etc/centos-release ]; then
lsb_dist='centos'
fi &&
if [ -z "$lsb_dist" ] && [ -r /etc/redhat-release ]; then
lsb_dist='redhat'
fi &&
if [ -z "$lsb_dist" ] && [ -r /etc/os-release ]; then
lsb_dist="$(. /etc/os-release && echo "$ID")"
fi &&
lsb_dist="$(echo "$lsb_dist" | tr '[:upper:]' '[:lower:]')" &&
if [ "${lsb_dist}" = "redhatenterpriseserver" ]; then
lsb_dist='redhat'
fi &&
case "$lsb_dist" in
ubuntu)
if command_exists lsb_release; then
dist_version="$(lsb_release --codename | cut -f2)"
fi &&
if [ -z "$dist_version" ] && [ -r /etc/lsb-release ]; then
dist_version="$(. /etc/lsb-release && echo "$DISTRIB_CODENAME")"
fi
;;
debian|raspbian)
dist_version="$(cat /etc/debian_version | sed 's/\/.*//' | sed 's/\..*//')" &&
case "$dist_version" in
8)
dist_version="jessie"
;;
7)
dist_version="wheezy"
;;
esac
;;
oracleserver)
lsb_dist="oraclelinux" &&
dist_version="$(rpm -q --whatprovides redhat-release --queryformat "%{VERSION}\n" | sed 's/\/.*//' | sed 's/\..*//' | sed 's/Server*//')"
;;
fedora|centos|redhat)
dist_version="$(rpm -q --whatprovides ${lsb_dist}-release --queryformat "%{VERSION}\n" | sed 's/\/.*//' | sed 's/\..*//' | sed 's/Server*//' | sort | tail -1)"
;;
*)
if command_exists lsb_release; then
dist_version="$(lsb_release --codename | cut -f2)"
fi &&
if [ -z "$dist_version" ] && [ -r /etc/os-release ]; then
dist_version="$(. /etc/os-release && echo "$VERSION_ID")"
fi
;;
esac
} &&
create_macvlan_network(){
echo ' ' &&
echo '########### Creating MACVLAN network(individual IP adresses)(https://github.com/MicroPyramid/docker-box/wiki/Network) ###########' &&
printf "Enter Subnet(eg: 88.99.102.64/26): " &&
read subnet &&
printf "Enter gateway(eg: 88.99.102.65): " &&
read gateway &&
printf "Enter ethernet interface(eg: eth0): " &&
read ethernet_interface &&
sudo docker network create -d macvlan -o macvlan_mode=bridge --subnet="$subnet" --gateway="$gateway" -o parent="$ethernet_interface" dbox_macvlan
} &&
create_bridge_network(){
echo ' ' &&
echo "########### Creating bridge network(IP's from external subnet)(https://github.com/MicroPyramid/docker-box/wiki/Network) ###########" &&
printf "Enter Subnet(eg: 88.99.114.16/28): " &&
read subnet2 &&
printf "Enter gateway(eg: 88.99.114.17): " &&
read gateway2 &&
sudo docker network create --driver=bridge --subnet "$subnet2" --gateway="$gateway2" dbox_bridge &&
sudo iptables -I DOCKER -d "$subnet2" -j ACCEPT
} &&
#TODO replace 2375 by variable
docker_tcp="[Unit]
Description=Docker HTTP Socket for the API
[Socket]
ListenStream=2375
BindIPv6Only=both
Service=docker.service
[Install]
WantedBy=sockets.target" &&
restrict_api_access(){
iptables -A INPUT -i lo -p tcp --dport 2375 -j ACCEPT &&
iptables -A INPUT -p tcp --dport 2375 -j DROP
} &&
install(){
dist_info &&
case "$lsb_dist" in
"Ubuntu"|"ubuntu") sudo apt update -y &&
sudo apt install -y curl &&
if ! command_exists docker
then
sudo update-grub &&
curl -sSL https://get.docker.com/ | sh &&
sudo usermod -aG docker $(whoami)
fi &&
sudo apt install -y python3-dev python-pip git python-virtualenv &&
mkdir "$project_dir" &&
virtualenv -p python3 "$env_dir" &&
cd "$project_dir" &&
. db_env/bin/activate &&
mkdir docker_box && cd docker_box &&
git init && git remote add origin 'https://github.com/MicroPyramid/docker-box' &&
git pull origin master &&
#sed replacement pattern considers ampersand, forward slash and backslash(if seperator) as special chars.
secret_key=`python -c 'import random; print("".join([random.SystemRandom().choice("abcdefghijklmnopqrstuvwxyz0123456789!@#$%^*(-_=+)") for i in range(50)]))'`
sed -i "s/\(SECRET_KEY\s=\s\).*/\1'$secret_key'/g" $project_dir/docker_box/docker_box/settings.py &&
yes | pip install -r requirements.txt &&
python manage.py migrate &&
python manage.py createsuperuser &&
if [ "$dist_version" = "xenial" ]
then
da_systemd_xenial(){
restrict_api_access &&
echo "$docker_tcp" > /tmp/docker_tcp.socket &&
sudo mv /tmp/docker_tcp.socket /etc/systemd/system/docker_tcp.socket &&
sudo systemctl stop docker && sudo systemctl start docker_tcp.socket &&
sudo systemctl start docker
} &&
curl -Is -X GET http://localhost:"$docker_api_port"/images/json | grep -i 'HTTP/1.1 200 OK' ||
da_systemd_xenial
elif [ "$dist_version" = "trusty" ]
then
da_upstart(){
restrict_api_access &&
sudo sed -i 's/DOCKER_OPTS=$/DOCKER_OPTS="-H tcp:\/\/0.0.0.0:'"$docker_api_port"' -H unix:\/\/\/var\/run\/docker.sock"/g' /etc/init/docker.conf &&
sudo service docker restart
} &&
curl -Is -X GET http://localhost:"$docker_api_port"/images/json | grep -i 'HTTP/1.1 200 OK' ||
da_upstart
else
echo "Can't install for this distribution version" &&
exit 0
fi &&
echo ' ' &&
printf '############## Network Setup(https://github.com/MicroPyramid/docker-box/wiki/Network) #############' &&
echo ' ' &&
printf "Are there extra IP's"' with MAC addresses(y/n): ' &&
read has_mac &&
if [ "$has_mac" = "y" ]
then
create_macvlan_network
fi &&
printf "Are there extra IP's"' without MAC addresses(y/n): ' &&
read has_no_mac &&
if [ "$has_no_mac" = "y" ]
then
create_bridge_network
fi &&
echo ' ' &&
printf "Enter Host IP Address: " &&
read ip_addr &&
sed -i "s/\(HOST_IP_ADDR\s=\s\).*/\1'$ip_addr'/g" $project_dir/docker_box/docker_box/settings.py &&
sed -i "s/\(ALLOWED_HOSTS\s=\s\).*/\1['$ip_addr']/g" $project_dir/docker_box/docker_box/settings.py &&
printf "Enter a port for docker_box: " &&
read port &&
echo "Starting docker_box on port "$port"" &&
sudo su - $(whoami) -c "cd ${project_dir}/docker_box && . ${env_dir}/bin/activate && python manage.py runserver 0.0.0.0:"$port""
;;
"Debian"|"debian")
sudo apt-get update -y &&
sudo apt-get install -y curl &&
if ! command_exists docker
then
curl -sSL https://get.docker.com/ | sh &&
sudo usermod -aG docker $(whoami)
fi &&
sudo apt-get install -y python3-dev python-pip git python-virtualenv &&
mkdir "$project_dir" &&
virtualenv -p python3 "$env_dir" &&
cd "$project_dir" &&
. db_env/bin/activate &&
mkdir docker_box && cd docker_box &&
git init && git remote add origin 'https://github.com/MicroPyramid/docker-box' &&
git pull origin master &&
#sed replacement pattern considers ampersand, forward slash and backslash(if seperator) as special chars.
secret_key=`python -c 'import random; print("".join([random.SystemRandom().choice("abcdefghijklmnopqrstuvwxyz0123456789!@#$%^*(-_=+)") for i in range(50)]))'`
sed -i "s/\(SECRET_KEY\s=\s\).*/\1'$secret_key'/g" $project_dir/docker_box/docker_box/settings.py &&
yes | pip install -r requirements.txt &&
python manage.py migrate &&
python manage.py createsuperuser &&
if [ "$dist_version" = "jessie" ]
then
da_systemd_jessie(){
restrict_api_access &&
echo "$docker_tcp" > /tmp/docker_tcp.socket &&
sudo mv /tmp/docker_tcp.socket /etc/systemd/system/docker_tcp.socket &&
sudo systemctl stop docker && sudo systemctl start docker_tcp.socket &&
sudo systemctl start docker
} &&
curl -Is -X GET http://localhost:"$docker_api_port"/images/json | grep -i 'HTTP/1.1 200 OK' ||
da_systemd_jessie
elif [ "$dist_version" = "wheezy" ]
then
docker_api_sysV(){
restrict_api_access &&
sudo sed -i 's/DOCKER_OPTS=$/DOCKER_OPTS="-H tcp:\/\/0.0.0.0:'"$docker_api_port"' -H unix:\/\/\/var\/run\/docker.sock"/g' /etc/init/docker.conf &&
sudo service docker restart
} &&
curl -Is -X GET http://localhost:"$docker_api_port"/images/json | grep -i 'HTTP/1.1 200 OK' ||
docker_api_sysV
else
echo "Can't install for this distribution version" &&
exit 0
fi &&
create_macvlan_network &&
create_bridge_network &&
printf "Enter a port for docker_box: " &&
read port &&
echo "Starting docker_box on port "$port"" &&
sudo su - $(whoami) -c "cd ${project_dir}/docker_box && . ${env_dir}/bin/activate && python manage.py runserver 0.0.0.0:"$port""
;;
"CentOS"|"centos")
sudo yum update -y &&
sudo yum -y install curl &&
install_python35(){
sudo yum -y groupinstall "Development tools" &&
sudo yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel&&
curl https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz -o Python-3.5.2.tgz &&
tar xvfz Python-3.5.2.tgz &&
cd Python-3.5.2 && ./configure --prefix=/usr/local --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib" &&
make && sudo make install
} &&
python3 -c 'import sys; assert sys.version_info >= (3,5)' 2> /dev/null ||
install_python35 &&
sudo $(which pip3) install virtualenv &&
if ! command_exists docker
then
curl -sSL https://get.docker.com/ | sh &&
sudo usermod -aG docker $(whoami)
fi &&
mkdir "$project_dir" &&
virtualenv -p python3 "$env_dir" &&
cd "$project_dir" &&
. db_env/bin/activate &&
mkdir docker_box && cd docker_box &&
#if [ "$dist_version" = "6" ]
#then
# sudo yum -y install http://opensource.wandisco.com/centos/6/git/x86_64/wandisco-git-release-6-1.noarch.rpm &&
# sudo yum -y install git
#fi &&
#TODO update git for centos6
git init && git remote add origin 'https://github.com/MicroPyramid/docker-box' &&
git pull origin master &&
#sed replacement pattern considers ampersand, forward slash and backslash(if seperator) as special chars.
secret_key=`python -c 'import random; print("".join([random.SystemRandom().choice("abcdefghijklmnopqrstuvwxyz0123456789!@#$%^*(-_=+)") for i in range(50)]))'`
sed -i "s/\(SECRET_KEY\s=\s\).*/\1'$secret_key'/g" $project_dir/docker_box/docker_box/settings.py &&
yes | pip install -r requirements.txt &&
python manage.py migrate &&
python manage.py createsuperuser &&
if [ "$dist_version" = "7" ]
then
da_systemd_centos(){
restrict_api_access &&
ls /etc/systemd/system/docker.service.d/ || sudo mkdir /etc/systemd/system/docker.service.d/ &&
da_centos_config="[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H tcp://127.0.0.1:2375 -H unix://var/run/docker.sock" &&
echo "$da_centos_config" > /tmp/docker.conf &&
sudo mv /tmp/docker.conf /etc/systemd/system/docker.service.d/ &&
sudo systemctl daemon-reload &&
sudo systemctl restart docker
} &&
curl -Is -X GET http://localhost:"$docker_api_port"/images/json | grep -i 'HTTP/1.1 200 OK' ||
da_systemd_centos
elif [ "$dist_version" = "6" ]
then
da_sysV(){
restrict_api_access &&
sudo sed -i 's/DOCKER_OPTS=$/DOCKER_OPTS="-H tcp:\/\/0.0.0.0:'"$docker_api_port"' -H unix:\/\/\/var\/run\/docker.sock"/g' /etc/init/docker.conf &&
sudo service docker restart
} &&
curl -Is -X GET http://localhost:"$docker_api_port"/images/json | grep -i 'HTTP/1.1 200 OK' ||
da_sysV
fi &&
create_macvlan_network &&
create_bridge_network &&
printf "Enter a port for docker_box: " &&
read port &&
echo "Starting docker_box on port "$port"" &&
sudo su - $(whoami) -c "cd ${project_dir}/docker_box && . ${env_dir}/bin/activate && python manage.py runserver 0.0.0.0:"$port""
;;
esac
}
install