Fix syntax error in claiming script. (#8452)
* Fix syntax error in claiming script. * Synchronized error messages between claiming script and C code * Fix exit code check
Markos Fountoulakis committed
Mar 21, 2020 at 23:01 UTC
b862a14064433c8b16cfc6a1e37d8dc21240b77a
2 files changed
+34
-29
claim/claim.c
+11
-8
@@ -13,13 +13,16 @@ static char *claiming_errors[] = {
13
"Missing dependencies", // 3
14
"Failure to connect to endpoint", // 4
15
"Unknown HTTP error message", // 5
16
- "invalid agent id", // 6
17
- "invalid public key", // 7
18
- "token has expired", // 8
19
- "invalid token", // 9
20
- "duplicate agent id", // 10
21
- "claimed in another workspace", // 11
22
- "internal server error" // 12
16
+ "invalid node id", // 6
17
+ "invalid node name", // 7
18
+ "invalid room id", // 8
19
+ "invalid public key", // 9
20
+ "token expired/token not found/invalid token", // 10
21
+ "already claimed", // 11
22
+ "processing claiming", // 12
23
+ "Internal Server Error", // 13
24
+ "Gateway Timeout", // 14
25
+ "Service Unavailable" // 15
26
};
27
28
@@ -95,7 +98,7 @@ void claim_agent(char *claiming_arguments)
98
return;
99
}
100
errno = 0;
98
- unsigned maximum_known_exit_code = sizeof(claiming_errors) / sizeof(claiming_errors[0]);
101
+ unsigned maximum_known_exit_code = sizeof(claiming_errors) / sizeof(claiming_errors[0]) - 1;
102
103
if ((unsigned)exit_code > maximum_known_exit_code) {
104
error("Agent failed to be claimed with an unknown error.");
claim/netdata-claim.sh.in
+23
-21
@@ -203,7 +203,7 @@ fi
203
204
205
if [ "${URLTOOL}" = "curl" ] ; then
206
- URLCOMMAND="curl --connect-timeout 5 --retry 3 -s -i -X PUT -d \"@${CLAIMING_DIR}/tmpin.txt\""
206
+ URLCOMMAND="curl --connect-timeout 5 --retry 0 -s -i -X PUT -d \"@${CLAIMING_DIR}/tmpin.txt\""
207
if [ "${NOPROXY}" = "yes" ] ; then
208
URLCOMMAND="${URLCOMMAND} -x \"\""
209
elif [ -n "${PROXY}" ] ; then
@@ -258,27 +258,29 @@ if [ "${VERBOSE}" == 1 ] ; then
258
fi
259
260
HTTP_STATUS_CODE=$(grep "HTTP" "${CLAIMING_DIR}/tmpout.txt" | awk -F " " '{print $2}')
261
-if [ "${HTTP_STATUS_CODE}" -ne 204 ] ; then
262
- ERROR_MESSAGE=$(grep "\"errorMsgKey\":" "${CLAIMING_DIR}/tmpout.txt" | awk -F "errorMsgKey\":\"" '{print $2}' | awk -F "\"" '{print $1}')
263
- case ${ERROR_MESSAGE} in
264
- "ErrInvalidNodeID") EXIT_CODE=6 ;;
265
- "ErrInvalidNodeName") EXIT_CODE=7 ;;
266
- "ErrInvalidRoomID") EXIT_CODE=8 ;;
267
- "ErrInvalidPublicKey") EXIT_CODE=9 ;;
268
- "ErrForbidden") EXIT_CODE=10 ;;
269
- "ErrAlreadyClaimed") EXIT_CODE=11 ;;
270
- "ErrProcessingClaim") EXIT_CODE=12 ;;
271
- "ErrInternalServerError") EXIT_CODE=13 ;;
272
- "ErrGatewayTimeout") EXIT_CODE=14 ;;
273
- "ErrServiceUnavailable") EXIT_CODE=15 ;;
274
- *) EXIT_CODE=5 ;;
275
- esac
276
- echo >&2 "Failed to claim node."
261
+
262
+if [ "${HTTP_STATUS_CODE}" = "204" ] ; then
263
rm -f "${CLAIMING_DIR}/tmpout.txt"
278
- exit $EXIT_CODE
264
+ echo -n "${ID}" >"${CLAIMING_DIR}/claimed_id"
265
+ rm -f "${CLAIMING_DIR}/token"
266
+ echo >&2 "Node was successfully claimed."
267
+ exit 0
268
fi
269
270
+ERROR_MESSAGE=$(grep "\"errorMsgKey\":" "${CLAIMING_DIR}/tmpout.txt" | awk -F "errorMsgKey\":\"" '{print $2}' | awk -F "\"" '{print $1}')
271
+case ${ERROR_MESSAGE} in
272
+ "ErrInvalidNodeID") EXIT_CODE=6 ;;
273
+ "ErrInvalidNodeName") EXIT_CODE=7 ;;
274
+ "ErrInvalidRoomID") EXIT_CODE=8 ;;
275
+ "ErrInvalidPublicKey") EXIT_CODE=9 ;;
276
+ "ErrForbidden") EXIT_CODE=10 ;;
277
+ "ErrAlreadyClaimed") EXIT_CODE=11 ;;
278
+ "ErrProcessingClaim") EXIT_CODE=12 ;;
279
+ "ErrInternalServerError") EXIT_CODE=13 ;;
280
+ "ErrGatewayTimeout") EXIT_CODE=14 ;;
281
+ "ErrServiceUnavailable") EXIT_CODE=15 ;;
282
+ *) EXIT_CODE=5 ;;
283
+esac
284
+echo >&2 "Failed to claim node."
285
rm -f "${CLAIMING_DIR}/tmpout.txt"
282
-echo -n "${ID}" >"${CLAIMING_DIR}/claimed_id"
283
-rm -f "${CLAIMING_DIR}/token"
284
-echo >&2 "Node was successfully claimed."
286
+exit $EXIT_CODE