Phala Cloud can add these scripts to your app compose as init_script. dstack runs them on every boot, before Docker starts. Each script’s SHA-256 is recorded into RTMR3 at boot, one init-script-hash event per script, so adding or changing an init script changes RTMR3.
Phala Cloud will add this script to your app compose as init_script. dstack runs it on every boot, before Docker starts. It merges your encrypted environment variables into a file that persists across restarts. If your app compose lists allowed_envs, it keeps only those variables.
Beta. Phala Cloud adds this script only to CVMs in workspaces that are part of the beta.
# Phala Cloud managed env merge v0.0.2
if command -v dstack-util >/dev/null 2>&1; then
dstack-util notify-host -e boot.progress -d "env-merge" || true
fi
STATE_DIR=/dstack/persistent/.managed-env
STATE="$STATE_DIR/state.json"
PAYLOAD=/dstack/.host-shared/.decrypted-env.json
COMPOSE=/dstack/app-compose.json
OUT_ENV=/dstack/.decrypted-env
OUT_JSON=/dstack/.decrypted-env.json
WATCHER="$STATE_DIR/app-compose-stall-watcher.sh"
PAYLOAD_JQ="$STATE_DIR/normalize-payload.jq"
MERGE_JQ="$STATE_DIR/merge.jq"
ENV_JQ="$STATE_DIR/env-file.jq"
mkdir -p "$STATE_DIR"
if [ ! -f "$PAYLOAD" ]; then
echo "managed-env-merge: missing payload $PAYLOAD" >&2
exit 1
fi
if [ ! -f "$COMPOSE" ]; then
echo "managed-env-merge: missing compose $COMPOSE" >&2
exit 1
fi
cat >"$PAYLOAD_JQ" <<'__PHALA_PAYLOAD_JQ__'
if type == "object" and has("env") and (.env | type == "array") then
(.env
| map(select(.key != null and .key != ""))
| map({(.key | tostring): (.value // "" | tostring)})
| add // {})
elif type == "object" then .
else error("payload must be a JSON object")
end
__PHALA_PAYLOAD_JQ__
cat >"$MERGE_JQ" <<'__PHALA_MERGE_JQ__'
($base + $payload) as $merged
| if ($allowed | length) == 0 then
$merged
else
($allowed | map({(.): true}) | add // {}) as $allow
| $merged
| with_entries(select($allow[.key]))
end
| to_entries
| sort_by(.key)
| from_entries
__PHALA_MERGE_JQ__
cat >"$ENV_JQ" <<'__PHALA_ENV_JQ__'
def needs_quote:
test("[ \t|&;<>()$`]")
or test("\\\\")
or test("\"")
or test("'")
or test("\n");
def escape_inner:
gsub("\n"; "\\n")
| gsub("\""; "\\\"")
| gsub("\\$"; "\\$")
| gsub("`"; "\\`");
to_entries[]
| .key as $k
| (.value // "" | tostring) as $v
| if ($v | needs_quote) then
"\($k)=\"\($v | escape_inner)\""
else
"\($k)=\($v)"
end
__PHALA_ENV_JQ__
if [ -f "$STATE" ]; then
jq -e 'type == "object"' "$STATE" >/dev/null
BASE_JSON=$(cat "$STATE")
else
BASE_JSON='{}'
fi
PAYLOAD_JSON=$(jq -c -f "$PAYLOAD_JQ" "$PAYLOAD")
ALLOWED_JSON=$(jq -c 'if (.allowed_envs | type) == "array" then .allowed_envs else [] end' "$COMPOSE")
MERGED_JSON=$(jq -nc --argjson base "$BASE_JSON" --argjson payload "$PAYLOAD_JSON" --argjson allowed "$ALLOWED_JSON" -f "$MERGE_JQ")
tmp_state="$STATE.tmp"
tmp_json="$OUT_JSON.tmp"
tmp_env="$OUT_ENV.tmp"
printf '%s\n' "$MERGED_JSON" >"$tmp_state"
mv -f "$tmp_state" "$STATE"
printf '%s\n' "$MERGED_JSON" >"$tmp_json"
mv -f "$tmp_json" "$OUT_JSON"
jq -r -f "$ENV_JQ" <<<"$MERGED_JSON" >"$tmp_env"
mv -f "$tmp_env" "$OUT_ENV"
# Watch app-compose startup from a transient unit so a slow boot never blocks init.
cat >"$WATCHER" <<'__PHALA_STALL_WATCHER__' || true
#!/bin/sh
elapsed=0
while [ "$elapsed" -lt 600 ]; do
active_state=$(systemctl show app-compose.service --property=ActiveState --value 2>/dev/null || true)
if [ "$active_state" = "active" ] || [ "$active_state" = "failed" ]; then
exit 0
fi
sleep 10 || true
elapsed=$((elapsed + 10))
done
journal_tail=$(timeout 10 sh -c 'journalctl -u app-compose.service -o cat -n 100 | tail -c 3072' 2>/dev/null || true)
timeout 10 dstack-util notify-host -e phase.stall -d "$journal_tail" || true
__PHALA_STALL_WATCHER__
systemd-run --no-block --unit=phala-app-compose-stall-watcher --property=Type=oneshot /bin/sh "$WATCHER" || true