-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathinstall-as-docker.sh
executable file
·97 lines (79 loc) · 2.71 KB
/
install-as-docker.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
#!/bin/bash
SCRIPT_DEBUG=true
DOCKER_IMAGE="${DOCKER_IMAGE:-quay.io/cafebazaar/blacksmith}"
DOCKER_NAME=blacksmith_docker
DOCKER_EXEC="docker"
display_usage() {
echo -e "Usage:"
echo "$0 <workspace-dir> <etcd-endpoints> <interface> [other-args]"
echo
echo "Example:"
echo "$0 ~/blacksmith-workspace http://example1.com:2379,http://example2.com:2379 etc0 -debug"
echo
}
die() {
echo "$@" 1>&2;
echo
exit 1
}
print_logs() {
echo "Logs of the created container:"
echo
$DOCKER_EXEC logs $DOCKER_NAME
echo
}
if [ $# -lt 3 ]; then
display_usage
exit 1
fi
WORKSPACE_DIR=$(realpath $1)
[ -d "$WORKSPACE_DIR" ] || die "Workspace does not exists: $WORKSPACE_DIR"
ETCD_ENDPOINTS=$2
INTERFACE=$3
OTHER_ARGS="${@:4}"
IP=$(ip addr | awk '/inet/ && /'$INTERFACE'/{sub(/\/.*$/,"",$2); print $2}')
[ "$IP" ] || die "Failed getting ip of the interface $INTERFACE"
# TODO: calculate instead of assuming the simple case
IFS=. read -r i1 i2 i3 i4 <<< "$IP"
[ "$i4" -lt 21 ] || echo "Warning: Your IP will be among those the dhcp is going to assign to the nodes! (Detected IP: $IP)" 1>&2
LEASE_START="$i1.$i2.$i3.31"
LEASE_RANGE=244
LEASE_SUBNET="255.255.255.0"
ROUTER="$i1.$i2.$i3.1"
DNS=$(grep nameserver /etc/resolv.conf | head -1 | awk '{print $2}')
[ "$DNS" ] || die "Failed getting DNS server from /etc/resolv.conf"
if [ SCRIPT_DEBUG ]; then
echo
echo "DOCKER_IMAGE: $DOCKER_IMAGE"
echo "WORKSPACE_DIR: $WORKSPACE_DIR"
echo "ETCD_ENDPOINTS: $ETCD_ENDPOINTS"
echo "INTERFACE: $INTERFACE"
echo "OTHER_ARGS: $OTHER_ARGS"
echo
echo "IP: $IP"
echo "ROUTER: $ROUTER"
echo "DNS: $DNS"
echo "LEASE_START: $LEASE_START"
echo "LEASE_RANGE: $LEASE_RANGE"
echo "LEASE_SUBNET: $LEASE_SUBNET"
echo
fi
VOLUME_ARGS="-v ${WORKSPACE_DIR}:/workspace:Z"
ARGS="-etcd $ETCD_ENDPOINTS -if $INTERFACE -lease-start $LEASE_START -lease-range $LEASE_RANGE -dns $DNS -http-listen 0.0.0.0:8000 $OTHER_ARGS"
$DOCKER_EXEC kill -s HUP $DOCKER_NAME; $DOCKER_EXEC kill $DOCKER_NAME || echo "NOT FATAL"
$DOCKER_EXEC rm $DOCKER_NAME || echo "NOT FATAL"
$DOCKER_EXEC run --name $DOCKER_NAME --restart=always --net=host -d $VOLUME_ARGS $DOCKER_IMAGE $ARGS || die "Failed"
echo "Installed as $DOCKER_NAME, waiting for a few seconds..."
sleep 3
if [ "$(docker inspect $DOCKER_NAME | grep "\"Restarting\": false")" ]; then
print_logs
echo Seems OK.
curl -X PUT http://127.0.0.1:8000/api/variables/net-conf -d "value={\"netmask\":\"$LEASE_SUBNET\", \"router\": \"$ROUTER\"}"
echo
else
echo "Something went wrong for docker container $DOCKER_NAME"
echo "Calculated args: $ARGS"
print_logs
echo "Killing the container..."
$DOCKER_EXEC kill $DOCKER_NAME && echo "Killed"
fi