Skip to content

Commit aa7d6b1

Browse files
committed
fixes
1. decouple util files 2. fetch workspace_id from user_facing_id
1 parent 758981e commit aa7d6b1

File tree

5 files changed

+97
-55
lines changed

5 files changed

+97
-55
lines changed

startupscript/butane/004-git-clone-devcontainer.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ api_url="${api_url%.git}"
4646
private_status=$(curl --retry 5 -s "${api_url}" | jq -r ".status")
4747
if [[ "${PRIVATE_DEVCONTAINER_ENABLED}" == "TRUE" && "${private_status}" == 404 ]]; then
4848
# Get ECM service URL
49-
if ! ECM_SERVICE_URL="$(get_service_url "ecm")"; then
49+
SERVER="$(get_metadata_value "terra-cli-server" "prod")"
50+
if ! ECM_SERVICE_URL="$(get_service_url "ecm" "${SERVER}")"; then
5051
exit 1
5152
fi
5253

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,74 @@
11
#!/bin/bash
22
# monitoring-utils.sh defines helper functions for notifying WSM of VM startup states.
33

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
910

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"
1517

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}" \
3021
-H "Authorization: Bearer $(/home/core/wb.sh auth print-access-token)" \
3122
-H "Content-Type: application/json" \
32-
-d "${payload}" \
23+
-d "$payload" \
3324
-w "\n%{http_code}")
25+
26+
local http_code
3427
http_code=$(echo "$response" | tail -n1)
28+
local response_body
3529
response_body=$(echo "$response" | head -n -1)
36-
if [[ $http_code -ne 200 ]]; then
30+
31+
if [[ "$http_code" != "200" ]]; then
3732
echo "Failed to record VM state. HTTP ${http_code}: ${response_body}" >&2
3833
return 1
3934
fi
35+
4036
echo "VM state recorded successfully: ${response_body}"
4137
}
4238

39+
# Record devcontainer start event
4340
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
4447
payload=$(cat <<EOF
4548
{
46-
"state": "DEVCONTAINER_START",
47-
"success": 1
49+
"event": "DEVCONTAINER_START",
50+
"isSuccess": true
4851
}
4952
EOF
5053
)
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
6061
return 1
6162
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"
6374
}

startupscript/butane/pre-devcontainer.sh

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,34 @@
33
# pre-devcontainer.sh creates a file used by the devcontainer service for monitoring
44
# and to keep track of the number of service failures.
55

6+
set -o errexit
7+
set -o nounset
8+
set -o pipefail
9+
set -o xtrace
10+
11+
# Keep track of the number of service failures
612
touch /tmp/devcontainer-failure-count
713

14+
# Fetch and cache workspace_id
15+
source /home/core/metadata-utils.sh
16+
source /home/core/service-utils.sh
817
MONITORING_UTILS_FILE="/home/core/monitoring-utils.sh"
18+
WORKSPACE_ID_CACHE_FILE="/tmp/workspace_id_cache"
919
FIRST_BOOT_START_FILE="/home/core/first-boot-start"
1020
if [[ -f "${MONITORING_UTILS_FILE}" && ! -f "${FIRST_BOOT_START_FILE}" ]]; then
1121
# First boot file does not exist
12-
# Record startup begin for monitoring
22+
## Cache workspace id to be used by probe-proxy-readiness.sh
23+
WORKSPACE_USER_FACING_ID="$(get_metadata_value "terra-workspace-id" "")"
24+
SERVER="$(get_metadata_value "terra-cli-server" "prod")"
25+
WSM_SERVICE_URL="$(get_service_url "wsm" "${SERVER}")"
26+
RESPONSE=$(curl -s -X GET "${WSM_SERVICE_URL}/api/workspaces/v1/workspaceByUserFacingId/${WORKSPACE_USER_FACING_ID}" \
27+
-H "Authorization: Bearer $(/home/core/wb.sh auth print-access-token)")
28+
WORKSPACE_ID=$(echo "${RESPONSE}" | jq -r '.id');
29+
echo "${WORKSPACE_ID}" > "${WORKSPACE_ID_CACHE_FILE}"
30+
31+
## Record devcontainer begin for monitoring
1332
source "${MONITORING_UTILS_FILE}"
14-
record_devcontainer_start "${CLOUD_PLATFORM}"
33+
RESOURCE_ID="$(get_metadata_value "wb-resource-id" "")"
34+
record_devcontainer_start "${WSM_SERVICE_URL}" "${WORKSPACE_ID}" "${RESOURCE_ID}"
1535
fi
16-
touch "${FIRST_BOOT_START_FILE}"
36+
touch "${FIRST_BOOT_START_FILE}"

startupscript/butane/probe-proxy-readiness.sh

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,28 @@ if docker ps -q --filter "name=proxy-agent" | grep -q . \
1515
&& docker ps -q --filter "name=application-server" | grep -q .; then
1616
echo "Proxy is ready."
1717
status="$(get_guest_attribute "startup_script/status" "")"
18-
success=0
18+
isSuccess="false"
1919
if [[ "${status}" != "ERROR" ]]; then
2020
set_metadata "startup_script/status" "COMPLETE"
21-
success=1
21+
isSuccess="true"
2222
fi
2323

2424
FIRST_BOOT_END_FILE="/home/core/first-boot-end"
2525
MONITORING_UTILS_FILE="/home/core/monitoring-utils.sh"
2626
if [[ -f "${MONITORING_UTILS_FILE}" && ! -f "${FIRST_BOOT_END_FILE}" ]]; then
2727
# first boot file does not exist
28-
# record startup end for monitoring
28+
# record devcontainer end for monitoring
29+
source /home/core/service-utils.sh
2930
source "${MONITORING_UTILS_FILE}"
30-
record_devcontainer_end "${success}"
31+
32+
# Fetch required values
33+
WORKSPACE_ID_CACHE_FILE="/tmp/workspace_id_cache"
34+
WORKSPACE_ID=$(cat "${WORKSPACE_ID_CACHE_FILE}")
35+
RESOURCE_ID="$(get_metadata_value "wb-resource-id" "")"
36+
SERVER="$(get_metadata_value "terra-cli-server" "prod")"
37+
WSM_SERVICE_URL="$(get_service_url "wsm" "${SERVER}")"
38+
39+
record_devcontainer_end "${WSM_SERVICE_URL}" "${WORKSPACE_ID}" "${RESOURCE_ID}" "${isSuccess}"
3140
fi
3241
touch "${FIRST_BOOT_END_FILE}"
3342
else

startupscript/butane/service-utils.sh

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33

44
# Map the server to appropriate service path
55
function get_service_url() {
6-
SERVER="$(get_metadata_value "terra-cli-server" "")"
7-
if [[ -z "${SERVER}" ]]; then
8-
SERVER="dev-stable"
6+
if [[ $# -lt 2 ]]; then
7+
echo "usage: get_service_url <service> <server>" >&2
8+
return 1
99
fi
10-
readonly SERVER
1110

11+
local SERVICE="$1"
12+
local SERVER="$2"
1213

1314
case "${SERVER}" in
14-
"dev-stable") echo "https://workbench-dev.verily.com/api/${SERVER}" ;;
15-
"dev-unstable") echo "https://workbench-dev-unstable.verily.com/api/${SERVER}" ;;
16-
"test") echo "https://workbench-test.verily.com/api/${SERVER}" ;;
17-
"prod") echo "https://workbench.verily.com/api/${SERVER}" ;;
15+
"dev-stable") echo "https://workbench-dev.verily.com/api/${SERVICE}" ;;
16+
"dev-unstable") echo "https://workbench-dev-unstable.verily.com/api/${SERVICE}" ;;
17+
"test") echo "https://workbench-test.verily.com/api/${SERVICE}" ;;
18+
"prod") echo "https://workbench.verily.com/api/${SERVICE}" ;;
1819
*) return 1 ;;
1920
esac
2021
}
21-
readonly -f get_service_url
22+
readonly -f get_service_url

0 commit comments

Comments
 (0)