-
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| #!/bin/bash | ||
| # monitoring-utils.sh defines helper functions for notifying WSM of VM startup states. | ||
|
|
||
| source /home/core/service-utils.sh | ||
|
|
||
| WORKSPACE_ID="$(get_metadata_value "terra-workspace-id" "")" | ||
| RESOURCE_ID="$(get_metadata_value "terra-resource-id" "")" | ||
| readonly WORKSPACE_ID RESOURCE_ID | ||
|
|
||
| # Get WSM endpoint URL | ||
| if ! WSM_SERVICE_URL="$(get_service_url "wsm")"; then | ||
| exit 1 | ||
| fi | ||
| LOG_URL="${WSM_SERVICE_URL}/api/workspaces/${WORKSPACE_ID}/resource/${RESOURCE_ID}/instance-state" | ||
|
|
||
| function record_devcontainer_end() { | ||
| if [[ $# -lt 2 || ("$2" != "0" && "$2" != "1") ]]; then | ||
| echo "usage: record_devcontainer_end <success/fail - 1/0>" | ||
| exit 1 | ||
| fi | ||
| SUCCESS="$1" | ||
| payload=$(cat <<EOF | ||
| { | ||
| "state": "DEVCONTAINER_END", | ||
| "success": ${SUCCESS} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it too late to change this to status too? |
||
| } | ||
| EOF | ||
| ) | ||
| 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}") | ||
| http_code=$(echo "$response" | tail -n1) | ||
| response_body=$(echo "$response" | head -n -1) | ||
| if [[ $http_code -ne 200 ]]; then | ||
| echo "Failed to record VM state. HTTP ${http_code}: ${response_body}" >&2 | ||
| return 1 | ||
| fi | ||
| echo "VM state recorded successfully: ${response_body}" | ||
| } | ||
|
|
||
| function record_devcontainer_start() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we refactor the two method to calls function log_event() and pass in payload? |
||
| payload=$(cat <<EOF | ||
| { | ||
| "state": "DEVCONTAINER_START", | ||
| "success": 1 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the openapi in your add endpoint PR this is a boolean, will it map correctly?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Haven't been able to test this locally yet, will be checking that this works properly! |
||
| } | ||
| EOF | ||
| ) | ||
| 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}") | ||
| http_code=$(echo "$response" | tail -n1) | ||
| response_body=$(echo "$response" | head -n -1) | ||
| if [[ $http_code -ne 200 ]]; then | ||
| echo "Failed to record VM state. HTTP ${http_code}: ${response_body}" >&2 | ||
| return 1 | ||
| fi | ||
| echo "VM state recorded successfully: ${response_body}" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,16 @@ | ||
| #!/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 | ||
| touch /tmp/devcontainer-failure-count | ||
|
|
||
| MONITORING_UTILS_FILE="/home/core/monitoring-utils.sh" | ||
| FIRST_BOOT_START_FILE="/home/core/first-boot-start" | ||
| if [[ -f "${MONITORING_UTILS_FILE}" && ! -f "${FIRST_BOOT_START_FILE}" ]]; then | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious, do we expect cases where the monitoring-utils file doesn't exist? |
||
| # First boot file does not exist | ||
| # Record startup begin for monitoring | ||
| source "${MONITORING_UTILS_FILE}" | ||
| record_devcontainer_start "${CLOUD_PLATFORM}" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does it take in CLOUD_PLATFORM? |
||
| fi | ||
| touch "${FIRST_BOOT_START_FILE}" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,9 +15,21 @@ if docker ps -q --filter "name=proxy-agent" | grep -q . \ | |
| && docker ps -q --filter "name=application-server" | grep -q .; then | ||
| echo "Proxy is ready." | ||
| status="$(get_guest_attribute "startup_script/status" "")" | ||
| success=0 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: status_code since it's not just success
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so 0 is success and 1 is failure? i'm little confused because devcontainer start is passing in success:1 |
||
| if [[ "${status}" != "ERROR" ]]; then | ||
| set_metadata "startup_script/status" "COMPLETE" | ||
| success=1 | ||
| fi | ||
|
|
||
| FIRST_BOOT_END_FILE="/home/core/first-boot-end" | ||
| MONITORING_UTILS_FILE="/home/core/monitoring-utils.sh" | ||
| if [[ -f "${MONITORING_UTILS_FILE}" && ! -f "${FIRST_BOOT_END_FILE}" ]]; then | ||
| # first boot file does not exist | ||
| # record startup end for monitoring | ||
| source "${MONITORING_UTILS_FILE}" | ||
| record_devcontainer_end "${success}" | ||
| fi | ||
| touch "${FIRST_BOOT_END_FILE}" | ||
yuhuyoyo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| else | ||
| echo "proxy-agent or application-server is not started" | ||
| exit 1 | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,21 @@ | ||||||
| #!/bin/bash | ||||||
| # service-utils defines helper functions for determining service path. | ||||||
|
|
||||||
| # Map the server to appropriate service path | ||||||
| function get_service_url() { | ||||||
| SERVER="$(get_metadata_value "terra-cli-server" "")" | ||||||
| if [[ -z "${SERVER}" ]]; then | ||||||
| SERVER="dev-stable" | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be prod? |
||||||
| fi | ||||||
| readonly SERVER | ||||||
|
|
||||||
|
|
||||||
| case "${SERVER}" in | ||||||
| "dev-stable") echo "https://workbench-dev.verily.com/api/${SERVER}" ;; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should these be
Suggested change
|
||||||
| "dev-unstable") echo "https://workbench-dev-unstable.verily.com/api/${SERVER}" ;; | ||||||
| "test") echo "https://workbench-test.verily.com/api/${SERVER}" ;; | ||||||
| "prod") echo "https://workbench.verily.com/api/${SERVER}" ;; | ||||||
| *) return 1 ;; | ||||||
| esac | ||||||
| } | ||||||
| readonly -f get_service_url | ||||||
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.
source metadata-util