Skip to content

Commit 04ad2f2

Browse files
committed
update 2
1 parent cf576fb commit 04ad2f2

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

sdgym/benchmark.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,45 +1656,54 @@ def _get_user_data_script(credentials, script_content, compute_service='aws'):
16561656
termination_trap = textwrap.dedent("""\
16571657
# AWS termination
16581658
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id || true)
1659-
if [ ! -z "$INSTANCE_ID" ]; then
1659+
if [ -n "$INSTANCE_ID" ]; then
16601660
echo "Terminating AWS EC2 instance: $INSTANCE_ID"
1661-
aws ec2 terminate-instances --instance-ids $INSTANCE_ID || true
1661+
aws ec2 terminate-instances --instance-ids "$INSTANCE_ID" || true
16621662
fi
1663-
""")
1663+
""").strip()
1664+
16641665

16651666
elif compute_service == 'gcp':
16661667
termination_trap = textwrap.dedent("""\
16671668
# GCP termination via Compute API (no gcloud required)
1668-
echo "Detected GCP environment — terminating instance via Compute API"
1669+
echo "Terminating GCP instance via Compute API"
16691670
16701671
TOKEN=$(curl -s -H "Metadata-Flavor: Google" \
16711672
http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/token \
1672-
| python3 -c "import sys, json; print(json.load(sys.stdin)['access_token'])")
1673+
| jq -r ".access_token" || true)
16731674
16741675
PROJECT_ID=$(curl -s -H "Metadata-Flavor: Google" \
1675-
http://169.254.169.254/computeMetadata/v1/project/project-id)
1676+
http://169.254.169.254/computeMetadata/v1/project/project-id || true)
16761677
16771678
ZONE=$(curl -s -H "Metadata-Flavor: Google" \
1678-
http://169.254.169.254/computeMetadata/v1/instance/zone | awk -F/ '{print $4}')
1679-
1680-
curl -s -X DELETE \
1681-
-H "Authorization: Bearer $TOKEN" \
1682-
-H "Content-Type: application/json" \
1683-
"https://compute.googleapis.com/compute/v1/projects/$PROJECT_ID/zones/$ZONE/" \
1684-
"instances/$HOSTNAME" \
1685-
|| true
1686-
""")
1679+
http://169.254.169.254/computeMetadata/v1/instance/zone \
1680+
| awk -F/ '{print $4}' || true)
1681+
1682+
if [ -n "$TOKEN" ] && [ -n "$PROJECT_ID" ] && [ -n "$ZONE" ]; then
1683+
curl -s -X DELETE \
1684+
-H "Authorization: Bearer $TOKEN" \
1685+
-H "Content-Type: application/json" \
1686+
"https://compute.googleapis.com/compute/v1/projects/$PROJECT_ID/zones/$ZONE/instances/" \
1687+
"$HOSTNAME" \
1688+
|| true
1689+
else
1690+
echo "GCP termination skipped (missing TOKEN/PROJECT_ID/ZONE)."
1691+
fi
1692+
""").strip()
1693+
1694+
trap_block = textwrap.dedent(f"""\
1695+
trap '{{
1696+
echo "======== Auto-Termination Triggered =========="
1697+
{termination_trap}
1698+
}}' EXIT
1699+
""").strip()
16871700

16881701
# --- Final script assembly ---
16891702
return textwrap.dedent(f"""\
16901703
#!/bin/bash
16911704
set -e
16921705
1693-
# Auto-termination trap
1694-
trap '
1695-
echo "======== Auto-Termination Triggered =========="
1696-
{termination_trap}
1697-
' EXIT
1706+
{trap_block}
16981707
16991708
exec > >(tee /var/log/user-data.log | logger -t user-data -s 2>/dev/console) 2>&1
17001709

0 commit comments

Comments
 (0)