From 71a4a3c9de21a4e7887568e6dbc4c97c7cd06437 Mon Sep 17 00:00:00 2001 From: Zack Siri Date: Fri, 29 Dec 2023 21:55:10 +0700 Subject: [PATCH] Update environment to support embedded variables. --- .../bootstrap/templates/environment.eex | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/lib/pakman/bootstrap/templates/environment.eex b/lib/pakman/bootstrap/templates/environment.eex index c5ab961..2290ad9 100644 --- a/lib/pakman/bootstrap/templates/environment.eex +++ b/lib/pakman/bootstrap/templates/environment.eex @@ -2,27 +2,38 @@ set -e +function fetch_from_instellar() { + endpoint="$(curl -s --unix-socket /dev/lxd/sock x/1.0/config/user.INSTELLAR_INSTALLATION_ENDPOINT)" + token="$(curl -s --unix-socket /dev/lxd/sock x/1.0/config/user.INSTELLAR_BOT_TOKEN)" + code="$(curl -s -o /dev/null -w "%{http_code}" "${endpoint}" -H "Authorization: Bearer ${token}" -H 'Content-Type: application/json; charset=utf-8')" + + if [[ "$code" == "200" ]]; then + echo "----- Fetching ${endpoint} -----" + + variables="$(curl -s "${endpoint}" -H "Authorization: Bearer ${token}" -H 'Content-Type: application/json; charset=utf-8' | jq '.data.attributes.variables')" + else + echo "----- Fetching from backup -----" + + variables="$(cat /root/.<%= name %>/variables.json | jq '.data.attributes.variables')" + fi +} + manager="$(curl -s --unix-socket /dev/lxd/sock x/1.0/config/user.managed_by)" export HOSTNAME="$(echo $HOSTNAME)" if [ "$manager" = "uplink" ] -then -endpoint="$(curl -s --unix-socket /dev/lxd/sock x/1.0/config/user.install_variables_endpoint)" +then + endpoint="$(curl -s --unix-socket /dev/lxd/sock x/1.0/config/user.install_variables_endpoint)" -echo "----- Fetching ${endpoint} -----" + echo "----- Fetching ${endpoint} -----" -variables="$(curl -s "${endpoint}" -H 'Content-Type: application/json; charset=utf-8' | jq '.data.attributes.variables')" + variables="$(curl -s "${endpoint}" -H 'Content-Type: application/json; charset=utf-8' | jq '.data.attributes.variables')" else -endpoint="$(curl -s --unix-socket /dev/lxd/sock x/1.0/config/user.INSTELLAR_INSTALLATION_ENDPOINT)" -token="$(curl -s --unix-socket /dev/lxd/sock x/1.0/config/user.INSTELLAR_BOT_TOKEN)" - -echo "----- Fetching ${endpoint} -----" - -variables="$(curl -s "${endpoint}" -H "Authorization: Bearer ${token}" -H 'Content-Type: application/json; charset=utf-8' | jq '.data.attributes.variables')" + fetch_from_instellar fi while read -rd $'' line do export "$line" done < <(jq -r <<<"$variables" \ - 'to_entries|map("\(.key)=\(.value)\u0000")[]') + 'to_entries|map("\(.key)=\(.value)\u0000")[]') \ No newline at end of file