-
Notifications
You must be signed in to change notification settings - Fork 19
Add monitoring to devcontainer startup #268
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| #!/bin/bash | ||
| # monitoring-utils.sh defines helper functions for notifying WSM of VM startup states. | ||
|
|
||
| # Log an event to WSM | ||
| function log_event() { | ||
| if [[ $# -lt 4 ]]; then | ||
| echo "usage: log_event <wsm_url> <workspace_id> <resource_id> <payload>" >&2 | ||
| return 1 | ||
| fi | ||
|
|
||
| # Input params | ||
| local wsm_url="$1" | ||
| local workspace_id="$2" | ||
| local resource_id="$3" | ||
| local payload="$4" | ||
| local log_url="${wsm_url}/api/workspaces/v1/${workspace_id}/resources/${resource_id}/instance-state" | ||
|
|
||
| # Log VM event | ||
| local response | ||
| response=$(curl -s -X POST "${log_url}" \ | ||
| -H "Authorization: Bearer $(/home/core/wb.sh auth print-access-token)" \ | ||
| -H "Content-Type: application/json" \ | ||
| -d "$payload" \ | ||
| -w "\n%{http_code}") | ||
|
|
||
| local http_code | ||
| http_code=$(echo "$response" | tail -n1) | ||
| local response_body | ||
| response_body=$(echo "$response" | head -n -1) | ||
|
|
||
| if [[ "$http_code" != "200" ]]; then | ||
| echo "Failed to record VM state. HTTP ${http_code}: ${response_body}" >&2 | ||
| return 1 | ||
| fi | ||
|
|
||
| echo "VM state recorded successfully: ${response_body}" | ||
| } | ||
|
|
||
| # Record devcontainer start event | ||
| function record_devcontainer_start() { | ||
| if [[ $# -lt 3 ]]; then | ||
| echo "usage: record_devcontainer_start <wsm_url> <workspace_id> <resource_id>" >&2 | ||
| return 1 | ||
| fi | ||
|
|
||
| local payload | ||
| payload=$(cat <<EOF | ||
| { | ||
| "event": "DEVCONTAINER_START", | ||
| "isSuccess": true | ||
| } | ||
| EOF | ||
| ) | ||
| log_event "$1" "$2" "$3" "$payload" | ||
| } | ||
|
|
||
| # Record devcontainer end event | ||
| function record_devcontainer_end() { | ||
| if [[ $# -lt 4 || ("$4" != "true" && "$4" != "false") ]]; then | ||
| echo "usage: record_devcontainer_end <wsm_url> <workspace_id> <resource_id> <isSuccess - true/false>" >&2 | ||
| return 1 | ||
| fi | ||
|
|
||
| local success="$4" | ||
| local payload | ||
| payload=$(cat <<EOF | ||
| { | ||
| "event": "DEVCONTAINER_END", | ||
| "isSuccess": ${success} | ||
| } | ||
| EOF | ||
| ) | ||
| log_event "$1" "$2" "$3" "$payload" | ||
| } | ||
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 |
|---|---|---|
| @@ -1,6 +1,36 @@ | ||
| #!/bin/bash | ||
|
|
||
| # pre-devcontainer.sh creates a file used by the devcontainer service to keep | ||
| # track of the number of service failures. | ||
| # pre-devcontainer.sh creates a file used by the devcontainer service for monitoring | ||
| # and to keep track of the number of service failures. | ||
|
|
||
| touch /tmp/devcontainer-failure-count | ||
| set -o errexit | ||
| set -o nounset | ||
| set -o pipefail | ||
| set -o xtrace | ||
|
|
||
| # Keep track of the number of service failures | ||
| touch /tmp/devcontainer-failure-count | ||
|
|
||
| # Fetch and cache workspace_id | ||
| source /home/core/metadata-utils.sh | ||
| source /home/core/service-utils.sh | ||
| MONITORING_UTILS_FILE="/home/core/monitoring-utils.sh" | ||
| WORKSPACE_ID_CACHE_FILE="/tmp/workspace_id_cache" | ||
| FIRST_BOOT_START_FILE="/home/core/first-boot-start" | ||
| if [[ -f "${MONITORING_UTILS_FILE}" && ! -f "${FIRST_BOOT_START_FILE}" ]]; then | ||
june-hua marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # First boot file does not exist | ||
| ## Cache workspace id to be used by probe-proxy-readiness.sh | ||
| WORKSPACE_USER_FACING_ID="$(get_metadata_value "terra-workspace-id" "")" | ||
| SERVER="$(get_metadata_value "terra-cli-server" "prod")" | ||
| WSM_SERVICE_URL="$(get_service_url "wsm" "${SERVER}")" | ||
| RESPONSE=$(curl -s -X GET "${WSM_SERVICE_URL}/api/workspaces/v1/workspaceByUserFacingId/${WORKSPACE_USER_FACING_ID}" \ | ||
| -H "Authorization: Bearer $(/home/core/wb.sh auth print-access-token)") | ||
| WORKSPACE_ID=$(echo "${RESPONSE}" | jq -r '.id'); | ||
| echo "${WORKSPACE_ID}" > "${WORKSPACE_ID_CACHE_FILE}" | ||
|
|
||
| ## Record devcontainer begin for monitoring | ||
| source "${MONITORING_UTILS_FILE}" | ||
| RESOURCE_ID="$(get_metadata_value "wb-resource-id" "")" | ||
| record_devcontainer_start "${WSM_SERVICE_URL}" "${WORKSPACE_ID}" "${RESOURCE_ID}" | ||
| fi | ||
| touch "${FIRST_BOOT_START_FILE}" | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| #!/bin/bash | ||
| # service-utils defines helper functions for determining service path. | ||
|
|
||
| # Map the server to appropriate service path | ||
| function get_service_url() { | ||
| if [[ $# -lt 2 ]]; then | ||
| echo "usage: get_service_url <service> <server>" >&2 | ||
| return 1 | ||
| fi | ||
|
|
||
| local SERVICE="$1" | ||
| local SERVER="$2" | ||
|
|
||
| case "${SERVER}" in | ||
| "dev-stable") echo "https://workbench-dev.verily.com/api/${SERVICE}" ;; | ||
| "dev-unstable") echo "https://workbench-dev-unstable.verily.com/api/${SERVICE}" ;; | ||
| "test") echo "https://workbench-test.verily.com/api/${SERVICE}" ;; | ||
| "prod") echo "https://workbench.verily.com/api/${SERVICE}" ;; | ||
| *) return 1 ;; | ||
| esac | ||
| } | ||
| readonly -f get_service_url |
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.
Uh oh!
There was an error while loading. Please reload this page.