-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.sh
executable file
·104 lines (84 loc) · 3.13 KB
/
build.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
#!/usr/bin/env bash
function build_base_image(){
echo "[INFO] Building base image for cluster"
(cd base && ./build.sh) || (echo "[ERROR] build base image failed" && exit 1)
echo "[INFO] Built base image for cluster"
}
function build_service_images(){
echo "[INFO] Building service images for cluster"
(docker-compose build &&\
echo "[INFO] Built service images for cluster"
) || ( echo "[ERROR] build service images failed" && exit 1)
}
function before_build_scripts(){
(
for D in `find ./ -type d`
do
(cd $D && test -x before_build.sh && (sh before_build.sh))
done )
}
function build_images(){
before_build_scripts
build_base_image
build_service_images
}
function create_network_config(){
echo "[INFO] Creating docker network $DOCKER_MACHINE_NAME-local"
(docker network create -d bridge \
--subnet=172.20.0.0/16 --gateway 172.20.0.1 --ip-range=172.20.0.0/16 \
$DOCKER_MACHINE_NAME-local)
echo "[INFO] Create /etc/resolver setup for dns"
export DOCKER_HOST_IP=$(docker-machine ip $DOCKER_MACHINE_NAME)
sudo rm -rf /etc/resolver/$DOCKER_MACHINE_NAME-local
sudo mkdir /etc/resolver
echo "nameserver $DOCKER_HOST_IP" | sudo tee /etc/resolver/$DOCKER_MACHINE_NAME-local
sudo route -n add -net 172.20.0.0 $DOCKER_HOST_IP
}
function remove_network_config(){
echo "[INFO] Removing old network config"
# remove old networking settings if exists
sudo rm /etc/resolver/$DOCKER_MACHINE_NAME-local
sudo route -n delete 172.20.0.0
}
function create_docker_machine(){
(docker-machine create --driver=virtualbox --virtualbox-memory $VIRTUALBOX_MEMORY $DOCKER_MACHINE_NAME &&\
echo "[INFO] Docker machine created")
}
function start_services(){
(docker-compose up) &
(docker-compose scale clusternode=$CLUSTER_NODES)
}
function clean_up(){
echo "[INFO] Remove old orphan volumes"
(docker-compose down --remove-orphans && docker volume rm $(docker volume ls -f dangling=true -q))
}
function start_docker_machine(){
echo "[INFO] Start docker machine $DOCKER_MACHINE_NAME"
docker-machine start $DOCKER_MACHINE_NAME
eval $(docker-machine env $DOCKER_MACHINE_NAME)
}
########################## Main Script ########################################
# start main script
eval "$(docker-machine env -u)" # reset docker machine env
DOCKER_MACHINE_NAME=cdh5
CLUSTER_NODES=$1
VIRTUALBOX_MEMORY=8000
[ -z "$CLUSTER_NODES" ] && CLUSTER_NODES=1
echo "[INFO] Make all scripts executable"
# make all scripts executable
find ./ -name "*.sh" -exec chmod +x {} \;
echo "[INFO] DOCKER_MACHINE_NAME=$DOCKER_MACHINE_NAME CLUSTER_NODES=$CLUSTER_NODES VIRTUALBOX_MEMORY=$VIRTUALBOX_MEMORY"
echo "[INFO] Starting cluster with nodes: $CLUSTER_NODES"
docker_machine_name=$(docker-machine ls | grep "^$DOCKER_MACHINE_NAME$" )
if [ ${#docker_machine_name} -ge 1 ]; then
echo "[INFO] Docker machine already exists booting up"
else
echo "[INFO] Docker machine does not exists creating it first"
create_docker_machine
fi
start_docker_machine
clean_up
remove_network_config
create_network_config
build_images
start_services $1