-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-universe.sh
executable file
·85 lines (78 loc) · 2.1 KB
/
docker-universe.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
#!/bin/bash
function print_usage {
echo "Usage: docker-universe.sh [OPTIONS] COMMAND"
echo "Options:"
echo " -b BIND_ADDR - Bind address. Required"
echo " -g GSUP_IMAGE - URL to group supervisor image in Docker registry"
echo " -h - Prints this help"
echo "Commands:"
echo " start - Starts new universe from current node"
echo " join HOST - Joins current node to universe though HOST"
}
gsup_image="localhost:5000/group-supervisor"
while getopts "b:g:h" option; do
case "$option" in
g) gsup_image=$OPTARG;;
b) bind_addr=$OPTARG;;
h) print_usage; exit;;
esac
done
shift $((OPTIND - 1))
function create_group {
join=$1
docker swarm init --advertise-addr $bind_addr
docker pull $gsup_image
docker network create --driver overlay groupnet
docker service create --network groupnet --name group_storage redis
join_env=$([ "$join" != "" ] && echo "--env JOIN=$join" || echo "")
docker service create --network groupnet \
--publish 10000:10000 \
--mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock \
--env STORAGE_URL="redis://group_storage:6379/0" \
--env HOSTNAME=$bind_addr \
$join_env \
--name group_supervisor $gsup_image
}
function start {
create_group
}
function join {
host=$1
group=$(curl -s "http://$host:10000/nodes/group" | jq -r ".group")
echo "GROUP: $group"
if [ "$group" != "null" ]; then
host=$(echo "$group" | jq -r ".supervisor")
echo "NEW HOST: $host"
token=$(curl -s "http://$host:10000/nodes/swarm_token" | jq -r ".token")
echo "TOKEN: $token"
docker swarm join --token $token "$host:2377"
else
echo "CREATE GROUP"
create_group "$host"
fi
}
function leave {
docker swarm leave -f
}
case "$1" in
start)
if [ -z "$bind_addr" ]; then
echo "-b options requied"
exit 1
fi
start
;;
join)
if [ -z "$bind_addr" ]; then
echo "-b options requied"
exit 1
fi
join $2
;;
leave)
leave
;;
*)
print_usage
;;
esac