Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 2 additions & 19 deletions startupscript/butane/004-git-clone-devcontainer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,7 @@ if [[ $# -lt 1 ]]; then
usage
fi


# Map the server to appropriate service path
function get_service_url() {
case "$1" in
"dev-stable") echo "https://workbench-dev.verily.com/api/$2" ;;
"dev-unstable") echo "https://workbench-dev-unstable.verily.com/api/$2" ;;
"test") echo "https://workbench-test.verily.com/api/$2" ;;
"prod") echo "https://workbench.verily.com/api/$2" ;;
*) return 1 ;;
esac
}
readonly -f get_service_url

source /home/core/service-utils.sh
source /home/core/metadata-utils.sh

# To accommodate the use of SSH URLs for public Git repositories, set the following Git configuration:
Expand Down Expand Up @@ -58,12 +46,7 @@ api_url="${api_url%.git}"
private_status=$(curl --retry 5 -s "${api_url}" | jq -r ".status")
if [[ "${PRIVATE_DEVCONTAINER_ENABLED}" == "TRUE" && "${private_status}" == 404 ]]; then
# Get ECM service URL
SERVER="$(get_metadata_value "terra-cli-server" "")"
readonly SERVER
if [[ -z "${SERVER}" ]]; then
SERVER="prod"
fi
if ! ECM_SERVICE_URL="$(get_service_url "${SERVER}" "ecm")"; then
if ! ECM_SERVICE_URL="$(get_service_url "ecm")"; then
exit 1
fi

Expand Down
63 changes: 63 additions & 0 deletions startupscript/butane/monitoring-utils.sh
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" "")"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

source metadata-util

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}
Copy link
Contributor

Choose a reason for hiding this comment

The 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() {
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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}"
}
16 changes: 13 additions & 3 deletions startupscript/butane/pre-devcontainer.sh
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
Copy link
Contributor

Choose a reason for hiding this comment

The 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}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it take in CLOUD_PLATFORM?

fi
touch "${FIRST_BOOT_START_FILE}"
12 changes: 12 additions & 0 deletions startupscript/butane/probe-proxy-readiness.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: status_code since it's not just success

Copy link
Contributor

Choose a reason for hiding this comment

The 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}"
else
echo "proxy-agent or application-server is not started"
exit 1
Expand Down
21 changes: 21 additions & 0 deletions startupscript/butane/service-utils.sh
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"
Copy link
Contributor

Choose a reason for hiding this comment

The 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}" ;;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be

Suggested change
"dev-stable") echo "https://workbench-dev.verily.com/api/${SERVER}" ;;
"dev-stable") echo "https://workbench-dev.verily.com/api/${1}" ;;

"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