|
1 | 1 | #!/bin/bash |
2 | 2 | # monitoring-utils.sh defines helper functions for notifying WSM of VM startup states. |
3 | 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 |
| 4 | +# Log an event to WSM |
| 5 | +function log_event() { |
| 6 | + if [[ $# -lt 4 ]]; then |
| 7 | + echo "usage: log_event <wsm_url> <workspace_id> <resource_id> <payload>" >&2 |
| 8 | + return 1 |
| 9 | + fi |
9 | 10 |
|
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" |
| 11 | + # Input params |
| 12 | + local wsm_url="$1" |
| 13 | + local workspace_id="$2" |
| 14 | + local resource_id="$3" |
| 15 | + local payload="$4" |
| 16 | + local log_url="${wsm_url}/api/workspaces/v1/${workspace_id}/resources/${resource_id}/instance-state" |
15 | 17 |
|
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}" \ |
| 18 | + # Log VM event |
| 19 | + local response |
| 20 | + response=$(curl -s -X POST "${log_url}" \ |
30 | 21 | -H "Authorization: Bearer $(/home/core/wb.sh auth print-access-token)" \ |
31 | 22 | -H "Content-Type: application/json" \ |
32 | | - -d "${payload}" \ |
| 23 | + -d "$payload" \ |
33 | 24 | -w "\n%{http_code}") |
| 25 | + |
| 26 | + local http_code |
34 | 27 | http_code=$(echo "$response" | tail -n1) |
| 28 | + local response_body |
35 | 29 | response_body=$(echo "$response" | head -n -1) |
36 | | - if [[ $http_code -ne 200 ]]; then |
| 30 | + |
| 31 | + if [[ "$http_code" != "200" ]]; then |
37 | 32 | echo "Failed to record VM state. HTTP ${http_code}: ${response_body}" >&2 |
38 | 33 | return 1 |
39 | 34 | fi |
| 35 | + |
40 | 36 | echo "VM state recorded successfully: ${response_body}" |
41 | 37 | } |
42 | 38 |
|
| 39 | +# Record devcontainer start event |
43 | 40 | function record_devcontainer_start() { |
| 41 | + if [[ $# -lt 3 ]]; then |
| 42 | + echo "usage: record_devcontainer_start <wsm_url> <workspace_id> <resource_id>" >&2 |
| 43 | + return 1 |
| 44 | + fi |
| 45 | + |
| 46 | + local payload |
44 | 47 | payload=$(cat <<EOF |
45 | 48 | { |
46 | | - "state": "DEVCONTAINER_START", |
47 | | - "success": 1 |
| 49 | + "event": "DEVCONTAINER_START", |
| 50 | + "isSuccess": true |
48 | 51 | } |
49 | 52 | EOF |
50 | 53 | ) |
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 |
| 54 | + log_event "$1" "$2" "$3" "$payload" |
| 55 | +} |
| 56 | + |
| 57 | +# Record devcontainer end event |
| 58 | +function record_devcontainer_end() { |
| 59 | + if [[ $# -lt 4 || ("$4" != "true" && "$4" != "false") ]]; then |
| 60 | + echo "usage: record_devcontainer_end <wsm_url> <workspace_id> <resource_id> <isSuccess - true/false>" >&2 |
60 | 61 | return 1 |
61 | 62 | fi |
62 | | - echo "VM state recorded successfully: ${response_body}" |
| 63 | + |
| 64 | + local success="$4" |
| 65 | + local payload |
| 66 | + payload=$(cat <<EOF |
| 67 | +{ |
| 68 | + "event": "DEVCONTAINER_END", |
| 69 | + "isSuccess": ${success} |
| 70 | +} |
| 71 | +EOF |
| 72 | +) |
| 73 | + log_event "$1" "$2" "$3" "$payload" |
63 | 74 | } |
0 commit comments