-
Notifications
You must be signed in to change notification settings - Fork 1.5k
bootstrap: record progress of services #4742
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
openshift-merge-robot
merged 3 commits into
openshift:master
from
staebler:bootstrap_service_markers
Mar 19, 2021
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
data/data/bootstrap/files/usr/local/bin/bootstrap-service-record.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| #!/usr/bin/env bash | ||
| # This library provides a helper functions for recording when a service | ||
| # and its stages start and end. | ||
|
|
||
| # SERVICE_RECORDS_DIR is the directory under which service records will be stored. | ||
| SERVICE_RECORDS_DIR="${SERVICE_RECORDS_DIR:-/var/log/openshift/}" | ||
| # SYSTEMD_UNIT_NAME is the name of the systemd unit for the service | ||
| SYSTEMD_UNIT_NAME="$(ps -o unit= $$)" | ||
| # SERVICE_NAME is the name of the service | ||
| SERVICE_NAME="${SERVICE_NAME:-${SYSTEMD_UNIT_NAME%.service}}" | ||
| # STAGE_NAME is the name of the current stage in the service | ||
| STAGE_NAME="" | ||
|
|
||
|
|
||
| # add_service_record_entry adds a record entry to the service records file. | ||
| # PHASE - phase being recorded; one of "start", "end", "stage start", or "stage end" | ||
| # RESULT - result of the action | ||
| # MESSAGE - message giving more detail about the result of the action | ||
| add_service_record_entry() { | ||
| local FILENAME="${SERVICE_RECORDS_DIR}/${SERVICE_NAME}.json" | ||
| mkdir --parents $(dirname "${FILENAME}") | ||
| # Append the new entry to the existing array in the file. | ||
| # If the file does not already exist, start with an empty array. | ||
| # The new entry contains only the fields that have non-empty values, since the | ||
| # stage, result, error line, and error message are all optional. | ||
| ([ -f "${FILENAME}" ] && cat "${FILENAME}" || echo '[]') | \ | ||
| jq \ | ||
| --arg timestamp "$(date +"%Y-%m-%dT%H:%M:%SZ")" \ | ||
| --arg preCommand "${PRE_COMMAND-}" \ | ||
| --arg postCommand "${POST_COMMAND-}" \ | ||
| --arg stage "${STAGE_NAME-}" \ | ||
| --arg phase "${PHASE}" \ | ||
| --arg result "${RESULT-}" \ | ||
| --arg errorLine "${ERROR_LINE-}" \ | ||
| --arg errorMessage "${ERROR_MESSAGE-}" \ | ||
| '. += [ | ||
| {$timestamp,$preCommand,$postCommand,$stage,$phase,$result,$errorLine,$errorMessage} | | ||
| reduce keys[] as $k (.; if .[$k] == "" then del(.[$k]) else . end) | ||
| ]' \ | ||
| > "${FILENAME}.tmp" && \ | ||
| mv "${FILENAME}.tmp" "${FILENAME}" | ||
| } | ||
|
|
||
| # record_service_start() records the start of a service. | ||
| record_service_start() { | ||
| local PHASE="start" | ||
|
|
||
| add_service_record_entry | ||
| } | ||
|
|
||
| # record_service_end(result) records the end of a service. | ||
| # ERROR_LINE - line where the error occurred, if there was an error | ||
| # ERROR_MESSAGE - error message, if there was an error | ||
| record_service_end() { | ||
| local PHASE="end" | ||
| local RESULT=${1:?Must specify a result} | ||
|
|
||
| add_service_record_entry | ||
| } | ||
|
|
||
| # record_service_stage_start(stage_name) records the start of a stage of a service. | ||
| record_service_stage_start() { | ||
| if [ -n "${STAGE_NAME}" ] | ||
| then | ||
| echo "attempt to record the start of a stage without ending the previous one" | ||
| exit 1 | ||
| fi | ||
|
|
||
| local PHASE="stage start" | ||
| STAGE_NAME=${1:?Must specify a stage name} | ||
|
|
||
| add_service_record_entry | ||
| } | ||
|
|
||
| # record_service_stage_end(result) records the end of a stage of a service. | ||
| # ERROR_LINE - line where the error occurred, if there was an error | ||
| # ERROR_MESSAGE - error message, if there was an error | ||
| record_service_stage_end() { | ||
| if [ -z "${STAGE_NAME}" ] | ||
| then | ||
| echo "attempt to record the end of a stage without starting one" | ||
| exit 1 | ||
| fi | ||
|
|
||
| local PHASE="stage end" | ||
| local RESULT=${1:?Must specify a result} | ||
|
|
||
| add_service_record_entry | ||
|
|
||
| STAGE_NAME="" | ||
| } | ||
|
|
||
| # record_service_stage_success records the successful end of a stage of a service. | ||
| record_service_stage_success() { | ||
| record_service_stage_end "success" | ||
| } | ||
|
|
||
| record_service_stage_failure() { | ||
| local ERROR_LINE | ||
| local ERROR_MESSAGE | ||
| get_error_info ERROR_LINE ERROR_MESSAGE | ||
| record_service_stage_end "failure" | ||
| } | ||
|
|
||
| record_service_exit() { | ||
| if [ "$1" -eq 0 ] | ||
| then | ||
| local RESULT="success" | ||
| else | ||
| local RESULT="failure" | ||
| local ERROR_LINE | ||
| local ERROR_MESSAGE | ||
| get_error_info ERROR_LINE ERROR_MESSAGE | ||
| fi | ||
|
|
||
| if [ -n "${STAGE_NAME}" ] | ||
| then | ||
| record_service_stage_end "${RESULT}" | ||
| fi | ||
|
|
||
| record_service_end "${RESULT}" | ||
| } | ||
|
|
||
| get_error_info() { | ||
| local -n error_line=$1 | ||
| local -n error_message=$2 | ||
| error_line="$(caller 1)" | ||
| error_message="$(journalctl --unit="${SYSTEMD_UNIT_NAME}" --lines=3 --output=cat)" | ||
| } | ||
|
|
||
| record_service_start | ||
|
|
||
| trap 'record_service_exit $?' EXIT | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider
start->service startend->service endThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds reasonable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll change the phase for pre- and post-commands to
pre-command start,pre-command end,post-command start, andpost-command end, too.