forked from appcelerator-archive/amp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvertise_service
executable file
·90 lines (87 loc) · 3.03 KB
/
advertise_service
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
#!/bin/bash
# usage:
# advertise_service <service_name>: adds the service in /etc/hosts
# advertise_service -r <service_name>: removes the service from /etc/hosts
# advertise_service -i <local_ip> [-r] <service_name>: adds or remove the
# service from value of local_ip (instead of 127.0.0.1)
# resolves the hostname of a Docker service and adds it in local host resolution
_advertise_service(){
local _host_file="/etc/hosts"
local _container_id
local _hostname
local _loop=0
local _retries=12
local _remove_only=0
if [[ "x$1" = "xremove" ]]; then _remove_only=1; _retries=1; shift ; fi
local _line_pattern=$1
local _service=$2
if [[ -z "$_service" ]]; then echo "ERROR: $(basename $0) should be called with the name of a service"; return 1 ; fi
if [[ ! -w "$_host_file" ]]; then echo "WARN - $_host_file is not writable, won't change it for $_service"; return 0; fi
while [[ -z $_container_id ]]; do
_container_id=$(docker ps --filter "name=${_service}\." --format '{{ .ID }}' 2>/dev/null | tail -1)
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
echo "WARN - caught a Docker error while advertising $_service"
return 0
fi
((_loop++))
if [[ $_loop -gt $_retries ]]; then
if [[ $_remove_only -eq 0 ]]; then echo "WARN - cannot find service $_service to advertise"; fi
return 0
fi
sleep 1
done
if [[ $(echo "$_container_id" | wc -w) -ne 1 ]]; then
if [[ $_remove_only -eq 0 ]]; then
echo "ERROR - unable to advertise service $_service"
return 1
else
return 0
fi
fi
cat "$_host_file" | grep "$_line_pattern" | grep -q "$_container_id"
if [[ $? -eq 0 ]]; then
sed -i.bak "s/ $_container_id//" "$_host_file"
echo "$_container_id has been removed from $_host_file"
fi
# check for any other hostname that looks like a Docker host
_hostname=$(grep "$_line_pattern" "$_host_file" | sed 's/^.*\([a-h0-9]\{12\}\).*$/\1/')
#if no match, _hostname will be the full line
echo "$_hostname" | grep -q "$_line_pattern"
if [[ $? -ne 0 && -n "$_hostname" ]]; then
sed -i.bak "s/^\($_line_pattern.*\)$_hostname\(.*\)$/\1\2/" "$_host_file"
echo "WARN - $_hostname looks like a Docker hostname and has been removed from $_host_file"
fi
egrep -q "^$_line_pattern[ ]*$" $_host_file
if [[ $? -eq 0 ]]; then
sed -i.bak "s/^$_line_pattern[ ]*$//" "$_host_file"
echo "$_line_pattern removed from $_host_file because empty"
fi
if [[ $_remove_only -eq 1 ]]; then return 0; fi
grep -q "$_line_pattern" "$_host_file"
if [[ $? -ne 0 ]]; then
echo "$_line_pattern $_container_id" >> "$_host_file"
else
sed -i.bak "s/^\(${_line_pattern}.*\)$/\1 $_container_id/" $_host_file
fi
echo "$_container_id has been added to $_host_file"
}
ip=127.0.0.1
services=""
remove=""
while getopts ":i:r" opt; do
case $opt in
r)
remove=remove
shift
;;
i)
ip=$OPTARG
shift 2
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
services=$@
_advertise_service $remove "$ip" $services