Skip to content

Commit e451025

Browse files
authored
Merge pull request #25 from StackGuardian/enhan/update-runner-group-on-reg-err
[SG-295] Enhan/update runner group on reg err
2 parents e5a7986 + 1aab2ee commit e451025

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

main.sh

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ log_date() { #{{{
126126

127127
err() { #{{{
128128
printf "%s ${C_RED_BOLD}ERROR: ${C_RESET}%s${C_BOLD} %s${C_RESET} %s\n" "$(log_date)" "${1}" "${2}" "${@:3}" >&2
129+
update_runner_group "${1} ${2} ${@:3}"
129130
}
130131
#}}}: err
131132

@@ -135,6 +136,7 @@ log_err() { #{{{
135136
msg="$(tail -n1 "$LOG_FILE" | cut -d":" -f2-)"
136137
err="$(tail -n1 "$LOG_FILE" | cut -d":" -f1)"
137138
printf "%s ${C_RED_BOLD}ERROR: ${C_RESET}%s${C_BOLD} %s${C_RESET}\n" "$(log_date)" "$err" "$msg" >&2
139+
update_runner_group "${err} ${msg}"
138140
}
139141
#}}}: log_err
140142

@@ -491,18 +493,34 @@ cgroupsv2() { #{{{
491493
}
492494
#}}}: cgroupsv2
493495

496+
update_runner_group(){
497+
url="${SG_BASE_API}/orgs/${ORGANIZATION_ID}/runnergroups/${RUNNER_GROUP_ID}/"
498+
499+
err_msg=$(echo -n "$1" | tr -cd "[:print:]")
500+
501+
debug "Error message ${err_msg}"
502+
503+
payload="{ \"RunnerRegistrationErrors\": { \"$(ip route | grep default | cut -d" " -f9)\" : { \"RunnerId\": \"${RUNNER_ID}\" , \"error\": \"${err_msg}\", \"timestamp\": \"$( date -u -Iseconds )\", \"command\": \"${0} ${@}\" } } }"
504+
505+
if api_call "PATCH" "$payload"; then
506+
debug "updated runner group with error msg"
507+
else
508+
debug "failed to update runner group with error msg"
509+
fi
510+
}
511+
494512
api_call() { #{{{
495513
# TODO: Support draining of instance
496-
if [[ -n "$1" ]]; then
514+
if [[ -n "$2" ]]; then
497515
response=$(curl --max-time 10 -i -s \
498-
-X POST \
516+
-X "$1" \
499517
-H "Authorization: apikey ${SG_NODE_TOKEN}" \
500518
-H "Content-Type: application/json" \
501-
-d "$1" \
519+
-d "$2" \
502520
"${url}")
503521
else
504522
response=$(curl --max-time 10 -i -s \
505-
-X POST \
523+
-X "$1" \
506524
-H "Authorization: apikey ${SG_NODE_TOKEN}" \
507525
-H "Content-Type: application/json" \
508526
"${url}")
@@ -694,6 +712,8 @@ clean_local_setup() { #{{{
694712

695713
clean_cron
696714

715+
[[ -e "/tmp/env_variables.sh" ]] && source /tmp/env_variables.sh || :
716+
697717
# Wait for AWS SSM Managed Instance to deregister on AWS side
698718
sleep 10s
699719

@@ -998,7 +1018,7 @@ fetch_organization_info() { #{{{
9981018

9991019
debug "Calling URL:" "${url}"
10001020

1001-
if api_call; then
1021+
if api_call "POST"; then
10021022
spinner_msg "Trying to fetch registration data" 0
10031023
spinner_wait "Preparing environment.."
10041024
metadata="$(echo "${response}" | jq -r '.data.RegistrationMetadata[0]')"
@@ -1310,7 +1330,7 @@ deregister_instance() { #{{{
13101330

13111331
spinner_wait "Trying to deregister instance.."
13121332
if [[ "$LOG_DEBUG" =~ true|True ]]; then printf "\n"; fi
1313-
if api_call "$payload"; then
1333+
if api_call "POST" "$payload"; then
13141334
spinner_msg "Trying to deregister instance" 0
13151335
# if deregister_ssm_instance_response is [], remove /var/lib/amazon/ssm dir as the MI does not exist anymore, the below logic fails if the ssm deregister api does not return a response but the instance is still alive
13161336
# echo $data

0 commit comments

Comments
 (0)