diff --git a/startupscript/butane/004-git-clone-devcontainer.sh b/startupscript/butane/004-git-clone-devcontainer.sh index 429c8e62..87eddc8a 100755 --- a/startupscript/butane/004-git-clone-devcontainer.sh +++ b/startupscript/butane/004-git-clone-devcontainer.sh @@ -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: @@ -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 diff --git a/startupscript/butane/monitoring-utils.sh b/startupscript/butane/monitoring-utils.sh new file mode 100644 index 00000000..474e7a56 --- /dev/null +++ b/startupscript/butane/monitoring-utils.sh @@ -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 " + exit 1 + fi + SUCCESS="$1" + payload=$(cat <&2 + return 1 + fi + echo "VM state recorded successfully: ${response_body}" +} + +function record_devcontainer_start() { + payload=$(cat <&2 + return 1 + fi + echo "VM state recorded successfully: ${response_body}" +} diff --git a/startupscript/butane/pre-devcontainer.sh b/startupscript/butane/pre-devcontainer.sh index 1d58a053..a38d07e2 100644 --- a/startupscript/butane/pre-devcontainer.sh +++ b/startupscript/butane/pre-devcontainer.sh @@ -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 \ No newline at end of file +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 + # First boot file does not exist + # Record startup begin for monitoring + source "${MONITORING_UTILS_FILE}" + record_devcontainer_start "${CLOUD_PLATFORM}" +fi +touch "${FIRST_BOOT_START_FILE}" \ No newline at end of file diff --git a/startupscript/butane/probe-proxy-readiness.sh b/startupscript/butane/probe-proxy-readiness.sh index d36a1670..1c1f6ae6 100644 --- a/startupscript/butane/probe-proxy-readiness.sh +++ b/startupscript/butane/probe-proxy-readiness.sh @@ -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 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 diff --git a/startupscript/butane/service-utils.sh b/startupscript/butane/service-utils.sh new file mode 100644 index 00000000..8fee3994 --- /dev/null +++ b/startupscript/butane/service-utils.sh @@ -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" + fi + readonly SERVER + + + case "${SERVER}" in + "dev-stable") echo "https://workbench-dev.verily.com/api/${SERVER}" ;; + "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 \ No newline at end of file