-
Notifications
You must be signed in to change notification settings - Fork 2
/
register.sh
executable file
·167 lines (137 loc) · 5.33 KB
/
register.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
#!/bin/bash
#
# Mit diesem Skript registriert sich eine App beim
# ScaleiT ETCD Service.
#
# (C) 2019, Ondics GmbH
echo
echo "ScaleIT Registration Sidecar"
echo
echo "pid: $$"
#
# Eine Auflistung der aktuellen Werte, die mitgegeben werden sollen, stehen im Readme.
#
echo "host-ip========= $HOST_IP"
# Rancher-Metadaten holen
RANCHER_HOST_IP=$(curl -s "http://rancher-metadata/2015-12-19/hosts/0/agent_ip")
if [ "x$HOST_IP" != "x" ]; then
RANCHER_HOST_IP=$HOST_IP
fi
SSO_DOMAIN="[hostname-fehlt]"
SSO_DOMAIN_META=$(curl -s "http://rancher-metadata/latest/services/de-ondics-sso-protect-active/labels/platform_url")
if [ "$SSO_DOMAIN_META" != "" ]; then
SSO_DOMAIN=$SSO_DOMAIN_META
fi
# Default-ETCD, wenn nicht gesetzt
ETCD_HOSTNAME=etcd
ETCD_PORT=49501
if [ -z "$ETCD_URI" ]; then
if [ -z "$RANCHER_HOST_IP" ]; then
ETCD_URI=$ETCD_HOSTNAME:$ETCD_PORT
else
ETCD_URI=$RANCHER_HOST_IP:$ETCD_PORT
fi
fi
echo ETCD_URI: $ETCD_URI
#check if etcd is up and running
echo "Check if etcd is up and running ..."
#STR='"health": "false"'
#STR=$(curl -sb -H "Accept: application/json" "http://$ETCD_URI/health")
#while [[ "$STR" != *'"health":"true"'* ]]
STATUS="unhealthy"
while [ "$STATUS" != 'healthy' ]; do
echo "Waiting for etcd ($ETCD_URI) ..."
HEALTH_JSON=$(curl -sb -H "Accept: application/json" "http://$ETCD_URI/health")
echo "HEALTH_JSON=[$HEALTH_JSON]"
STATUS=$(echo $HEALTH_JSON | sed 's/^.*"health":[[:space:]]*"true".*$/healthy/g')
echo "Status=[$STATUS]"
sleep 1
done
echo "etcd is up"
#überprüfen, ob stack-name gesetzt, sonst id verwenden
if [ -z ${STACK_NAME+x} ]; then
STACK_NAME=$APP_ID
fi
APP_REGISTRY_URL="http://$ETCD_URI/v2/keys/apps/$STACK_NAME"
echo APP_REGISTRY_URL: $APP_REGISTRY_URL
function etcd_add() {
echo "ETCD: $1 => $2"
curl --silent -L -X PUT "$APP_REGISTRY_URL/$1" -d value="$2" >> /dev/null
if [ "$?" != 0 ]; then
echo "Fehler: ETCD-Schreiben geht nicht"
exit 1
fi
}
# Url zusammensetzen für Port und Namen-Adressierung
# $1 = Pfad
# $2 = Port
# $3 = Servicename
function build_url() {
if [ "$SSO_ACTIVATED" == "true" ]; then
if [ "$3" == "" ]; then
echo "https://$SSO_APP_PREFIX.$SSO_DOMAIN_META$1"
else
echo "https://$3-$SSO_APP_PREFIX.$SSO_DOMAIN_META$1"
fi
else
echo "http://$RANCHER_HOST_IP:$2$1"
fi
}
# Jetzt die Werte nach ETCD schreiben
echo "ScaleIT ETCD aktualisieren:"
etcd_add id "$APP_ID"
etcd_add name "$APP_NAME"
etcd_add title "$APP_TITLE"
etcd_add shortDescription "$APP_SHORT_DESCRIPTION"
etcd_add description "$APP_DESCRIPTION"
etcd_add category "$APP_CATEGORY"
APP_DOMAIN_URL=$(build_url "$APP_DOMAIN_PATH" "$APP_DOMAIN_PORT" "$APP_DOMAIN_SERVICENAME")
etcd_add domainUrl "$APP_DOMAIN_URL"
APP_ADMIN_URL=$(build_url "$APP_ADMIN_PATH" "$APP_ADMIN_PORT" "$APP_ADMIN_SERVICENAME")
etcd_add adminUrl "$APP_ADMIN_URL"
# rückwärtskompatibilität
if [ -z ${APP_ICON_SERVICENAME+x} ]; then
if ! [ -z ${APP_WEBCONTENT_SERVICENAME+x} ]; then
echo "DEPRECATED:"
echo "environment APP_WEBCONTENT_SERVICENAME is unset, use APP_ICON_SERVICENAME";
APP_ICON_SERVICENAME="$APP_WEBCONTENT_SERVICENAME"
fi
fi
APP_ICON_URL=$(build_url "$APP_ICON_PATH" "$APP_ICON_PORT" "$APP_ICON_SERVICENAME")
etcd_add iconUrl "$APP_ICON_URL"
# rückwärtskompatibilität
if [ -z ${APP_API_SERVICENAME+x} ]; then
if ! [ -z ${APP_API_SERVICE+x} ]; then
echo "DEPRECATED:"
echo "environment APP_API_SERVICENAME is unset, use APP_API_SERVICE";
APP_API_SERVICENAME="$APP_API_SERVICE"
fi
fi
APP_API_URL=$(build_url "$APP_API_PATH" "$APP_API_PORT" "$APP_API_SERVICENAME")
etcd_add apiUrl "$APP_API_URL"
# ...und Zeitstempel!
etcd_add updatedAt "`date '+%Y-%m-%d %H:%M:%S'`"
echo "Registration completed."
# SIGTERM-handler
# Beim Abbruch (z.B. ctrl-c) App wieder deregistrieren
term_handler() {
echo "*** Registration Sidecar shut down - START"
# Eintrag löschen
echo "*** App deregistrieren"
curl --silent -L -X PUT "$APP_REGISTRY_URL?recursive=true" \
-XDELETE >> /dev/null
# alternativ: Status auf offline setzen
#echo "*** App Status auf offline setzen"
#curl -L -X PUT "$APP_REGISTRY_URL/status" -d value="Offline"
echo "*** Registration Sidecar shut down - ENDE"
exit 143; # 128 + 15 -- SIGTERM
}
# setup handlers
# on callback, kill the last background process,
# which is `tail -f /dev/null` and execute the specified handler
trap 'kill ${!}; term_handler' SIGTERM SIGINT
# wait forever
while true
do
tail -f /dev/null & wait ${!}
done