-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathokapi-hooks.sh
More file actions
executable file
·136 lines (124 loc) · 2.92 KB
/
okapi-hooks.sh
File metadata and controls
executable file
·136 lines (124 loc) · 2.92 KB
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
#!/bin/bash
set -e # exit on error
set -x # echo commands
set -f # no file file glob expansion
call_curl() {
if test -n "${CURL_TOK}"; then
curl -w '\n' --show-error --silent ${CURL_TOK} $*
else
curl -w '\n' --show-error --silent $*
fi
}
post() {
call_curl -XPOST -HContent-Type:application/json $*
}
delete() {
call_curl -XDELETE $*
}
login() {
if test "${OKAPI_TOKEN}" = none; then
return
fi
if test -z "${OKAPI_TOKEN}"; then
if test -z "$OKAPI_USER" -o -z "$OKAPI_PASS"; then
echo "No OKAPI_TOKEN; OKAPI_USER and OKAPI_PASS required"
exit 1
fi
tmp=`mktemp`
post --fail-with-body -D$tmp -d"{\"username\":\"${OKAPI_USER}\",\"password\":\"${OKAPI_PASS}\"}" $U/authn/login
OKAPI_TOKEN=`awk '/x-okapi-token/ {print $2}' < $tmp|tr -d '[:space:]'`
fi
CURL_TOK="-HX-Okapi-Token:${OKAPI_TOKEN}"
}
# TODO: handle that previous instance is a different version
hook_pre_delete() {
if test -n "${OKAPI_TENANTS}"; then
for T in $OKAPI_TENANTS; do
echo "[{\"id\":\"${SVCID}\",\"action\":\"disable\"}]" | post -d @- $U/_/proxy/tenants/$T/install
done
fi
delete "$U/_/discovery/modules/${SVCID}/${INSTID}"
delete "$U/_/proxy/modules/${SVCID}"
}
hook_post_install() {
echo $OKAPI_MD | post --fail-with-body -d @- $U/_/proxy/modules
echo "{\"srvcId\":\"$SVCID\",\"instId\":\"${INSTID}\",\"url\":\"${MODULE_URL}\"}" | post --fail-with-body -d @- $U/_/discovery/modules
if test -n "${OKAPI_TENANTS}"; then
for T in $OKAPI_TENANTS; do
echo "[{ \"id\":\"${SVCID}\",\"action\":\"enable\"}]" | post --fail-with-body -d @- $U/_/proxy/tenants/$T/install
done
fi
}
tenants_lookup() {
tmp=`mktemp`
call_curl --fail-with-body -o $tmp $U/_/proxy/tenants
m=""
for t in `jq '.[].id' -r < $tmp `; do
match=false
for pattern in ${OKAPI_ADMIN_TENANT}; do
case $t in
${pattern})
match=true
;;
esac
done
$match && continue
for pattern in ${OKAPI_TENANTS}; do
case $t in
${pattern})
match=true
;;
esac
done
$match && m="$m $t"
done
if test -z "$m"; then
echo "No tenants matched"
exit 1
fi
OKAPI_TENANTS=$m
}
prepare() {
fail=false
if test -z "$OKAPI_TENANTS"; then
echo "OKAPI_TENANTS not set, will deploy only (no enablement)"
fi
if test -z "$OKAPI_URL"; then
echo "OKAPI_URL not set"
fail=true
fi
U=$OKAPI_URL
if test -z "$OKAPI_MD"; then
echo "OKAPI_MD not set"
fail=true
fi
if test -z "$MODULE_URL"; then
echo "MODULE_URL not set"
fail=true
fi
if ! which curl > /dev/null; then
echo "curl not found"
fail=true
fi
if ! which jq > /dev/null; then
echo "jq not found"
fail=true
fi
if $fail; then
echo "Exiting"
exit 1
fi
OKAPI_ADMIN_TENANT=${OKAPI_ADMIN_TENANT:-supertenant}
SVCID=`echo $OKAPI_MD | jq -r '.id'`
INSTID=inst-${SVCID}
if test -n "${OKAPI_TENANTS}"; then
OKAPI_TENANTS=$(echo "$OKAPI_TENANTS" | tr ',' ' ')
fi
}
prepare
login
if test -n "${OKAPI_TENANTS}"; then
tenants_lookup
fi
hook_pre_delete
hook_post_install