Skip to content

Commit b8f6be0

Browse files
Updating the script with a new finish line and modifying the status logic
1 parent 409fce0 commit b8f6be0

1 file changed

Lines changed: 25 additions & 12 deletions

File tree

community/modules/scripts/wait-for-startup/scripts/wait-for-startup-status.sh

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@ fetch_cmd="gcloud compute instances get-serial-port-output ${INSTANCE_NAME} --po
5858
# finishes on the new guest agent
5959
FINISH_LINE="startup-script exit status"
6060
# Match string for failures on the new guest agent
61-
FINISH_LINE_ERR="Script.*failed with error:"
61+
FINISH_LINE_ERR="Script \"startup-script\" failed with error:"
6262

6363
# NEW: Accept also these finish lines as success.
6464
STARTUP_SCRIPT_SUCCEEDED_LINE="google-startup-scripts.service: Succeeded."
6565
STARTUP_SCRIPT_FINISHED_LINE="Finished Google Compute Engine Startup Scripts."
66+
STARTUP_SCRIPT_SERVICE_FINISHED_LINE="Finished google-startup-scripts.service - Google Compute Engine Startup Scripts."
6667

6768
NON_FATAL_ERRORS=(
6869
"Internal error"
@@ -72,7 +73,7 @@ until [[ now -gt deadline ]]; do
7273
ser_log=$(
7374
set -o pipefail
7475
${fetch_cmd} 2>"${error_file}" |
75-
c1grep "${FINISH_LINE}\|${FINISH_LINE_ERR}\|${STARTUP_SCRIPT_SUCCEEDED_LINE}\|${STARTUP_SCRIPT_FINISHED_LINE}"
76+
c1grep "${FINISH_LINE}\|${FINISH_LINE_ERR}\|${STARTUP_SCRIPT_SUCCEEDED_LINE}\|${STARTUP_SCRIPT_FINISHED_LINE}\|${STARTUP_SCRIPT_SERVICE_FINISHED_LINE}"
7677
) || {
7778
err=$(cat "${error_file}")
7879
echo "$err"
@@ -89,37 +90,49 @@ until [[ now -gt deadline ]]; do
8990
fi
9091
}
9192
if [[ -n "${ser_log}" ]]; then break; fi
92-
echo "Could not detect end of startup script. Sleeping."
9393
sleep 5
9494
now=$(date +%s)
9595
done
9696

9797
# This line checks for an exit code - the assumption is that there is a number
98-
# at the end of the line and it is an exit code
99-
STATUS=$(sed -r 's/.*([0-9]+)\s*$/\1/' <<<"${ser_log}" | uniq)
98+
# at the end of the line and it is an exit code.
99+
# Modified to correctly extract the last numeric exit status from the relevant log line.
100+
LAST_EXIT_STATUS=$(echo "${ser_log}" | grep -oP "(?<=Script \"startup-script\" failed with error: exit status )[0-9]+" | tail -n 1)
101+
if [[ -z "${LAST_EXIT_STATUS}" ]]; then
102+
LAST_EXIT_STATUS=$(echo "${ser_log}" | grep -oP "(?<=startup-script exit status )[0-9]+" | tail -n 1)
103+
fi
104+
100105
# This specific text is monitored for in tests, do not change.
101106
INSPECT_OUTPUT_TEXT="To inspect the startup script output, please run:"
102-
if [[ "${STATUS}" == 0 ]]; then
103-
echo "startup-script finished successfully"
104-
elif [[ "${STATUS}" == 1 ]]; then
107+
108+
# --- Prioritize explicit failure from the script itself ---
109+
if [[ "${LAST_EXIT_STATUS}" == 1 ]]; then
105110
echo "startup-script finished with errors, ${INSPECT_OUTPUT_TEXT}"
106111
echo "${fetch_cmd}"
112+
exit 1
113+
# --- Then explicit success from the script itself ---
114+
elif [[ "${LAST_EXIT_STATUS}" == 0 ]]; then
115+
echo "startup-script finished successfully"
116+
exit 0
107117
elif echo "${ser_log}" | grep -qE "${STARTUP_SCRIPT_SUCCEEDED_LINE}"; then
108118
echo "startup-script finished successfully (startup script succeeded line detected)"
109119
exit 0
110120
elif echo "${ser_log}" | grep -qE "${STARTUP_SCRIPT_FINISHED_LINE}"; then
111121
echo "startup-script finished successfully (startup script finished line detected)"
112122
exit 0
123+
elif echo "${ser_log}" | grep -qE "${STARTUP_SCRIPT_SERVICE_FINISHED_LINE}"; then
124+
echo "startup-script finished successfully (startup script service finished line detected)"
125+
exit 0
126+
# --- If we reached deadline, it's a timeout ---
113127
elif [[ now -ge deadline ]]; then
114128
echo "startup-script timed out after ${TIMEOUT} seconds"
115129
echo "${INSPECT_OUTPUT_TEXT}"
116130
echo "${fetch_cmd}"
117131
exit 1
132+
# --- All other cases are considered failure or invalid state ---
118133
else
119-
echo "Invalid return status: '${STATUS}'"
134+
echo "Invalid or undetermined startup script status. Last detected exit status: '${LAST_EXIT_STATUS}'"
120135
echo "${INSPECT_OUTPUT_TEXT}"
121136
echo "${fetch_cmd}"
122-
exit 1
137+
exit "${LAST_EXIT_STATUS}"
123138
fi
124-
125-
exit "${STATUS}"

0 commit comments

Comments
 (0)