|
| 1 | +#!/bin/bash |
| 2 | +# monitoring-utils.sh defines helper functions for notifying WSM of VM startup states. |
| 3 | + |
| 4 | +source /home/core/service-utils.sh |
| 5 | + |
| 6 | +WORKSPACE_ID="$(get_metadata_value "terra-workspace-id" "")" |
| 7 | +RESOURCE_ID="$(get_metadata_value "terra-resource-id" "")" |
| 8 | +readonly WORKSPACE_ID RESOURCE_ID |
| 9 | + |
| 10 | +# Get WSM endpoint URL |
| 11 | +if ! WSM_SERVICE_URL="$(get_service_url "wsm")"; then |
| 12 | + exit 1 |
| 13 | +fi |
| 14 | +LOG_URL="${WSM_SERVICE_URL}/api/workspaces/${WORKSPACE_ID}/resource/${RESOURCE_ID}/instance-state" |
| 15 | + |
| 16 | +function record_devcontainer_end() { |
| 17 | + if [[ $# -lt 2 || ("$2" != "0" && "$2" != "1") ]]; then |
| 18 | + echo "usage: record_devcontainer_end <success/fail - 1/0>" |
| 19 | + exit 1 |
| 20 | + fi |
| 21 | + SUCCESS="$1" |
| 22 | + payload=$(cat <<EOF |
| 23 | +{ |
| 24 | + "state": "DEVCONTAINER_END", |
| 25 | + "success": ${SUCCESS} |
| 26 | +} |
| 27 | +EOF |
| 28 | +) |
| 29 | + response=$(curl -s -X POST "${LOG_URL}" \ |
| 30 | + -H "Authorization: Bearer $(/home/core/wb.sh auth print-access-token)" \ |
| 31 | + -H "Content-Type: application/json" \ |
| 32 | + -d "${payload}" \ |
| 33 | + -w "\n%{http_code}") |
| 34 | + http_code=$(echo "$response" | tail -n1) |
| 35 | + response_body=$(echo "$response" | head -n -1) |
| 36 | + if [[ $http_code -ne 200 ]]; then |
| 37 | + echo "Failed to record VM state. HTTP ${http_code}: ${response_body}" >&2 |
| 38 | + return 1 |
| 39 | + fi |
| 40 | + echo "VM state recorded successfully: ${response_body}" |
| 41 | +} |
| 42 | + |
| 43 | +function record_devcontainer_start() { |
| 44 | + payload=$(cat <<EOF |
| 45 | +{ |
| 46 | + "state": "DEVCONTAINER_START", |
| 47 | + "success": 1 |
| 48 | +} |
| 49 | +EOF |
| 50 | +) |
| 51 | + response=$(curl -s -X POST "${LOG_URL}" \ |
| 52 | + -H "Authorization: Bearer $(/home/core/wb.sh auth print-access-token)" \ |
| 53 | + -H "Content-Type: application/json" \ |
| 54 | + -d "${payload}" \ |
| 55 | + -w "\n%{http_code}") |
| 56 | + http_code=$(echo "$response" | tail -n1) |
| 57 | + response_body=$(echo "$response" | head -n -1) |
| 58 | + if [[ $http_code -ne 200 ]]; then |
| 59 | + echo "Failed to record VM state. HTTP ${http_code}: ${response_body}" >&2 |
| 60 | + return 1 |
| 61 | + fi |
| 62 | + echo "VM state recorded successfully: ${response_body}" |
| 63 | +} |
0 commit comments