Skip to content

Instantly share code, notes, and snippets.

@vladkosinov
Created June 21, 2020 09:42
Show Gist options
  • Save vladkosinov/4b19de3ef8c40e2b72c74c424bb7f2d0 to your computer and use it in GitHub Desktop.
Save vladkosinov/4b19de3ef8c40e2b72c74c424bb7f2d0 to your computer and use it in GitHub Desktop.
Delete Yandex Cloud VPS script
#!/bin/bash
YC_TOKEN="value"
YC_FOLDER_ID="value"
YC_INSTANCE_NAME="some name"
IAM_TOKEN=$(
curl -s -d "{\"yandexPassportOauthToken\":\"${YC_TOKEN}\"}" \
https://iam.api.cloud.yandex.net/iam/v1/tokens |\
jq --raw-output .iamToken
)
VPS_ID=$(
curl -s -H "Authorization: Bearer ${IAM_TOKEN}" \
https://compute.api.cloud.yandex.net/compute/v1/instances\?folder_id\=${YC_FOLDER_ID} |\
jq --raw-output '.instances[]? | select(.name == ${YC_INSTANCE_NAME}).id'
)
if [ -n "$VPS_ID" ]; then
echo "Found VPS, deleting..."
timeout --foreground 300s bash <<EOT
OPERATION_ID=\$(
curl -s -X DELETE -H "Authorization: Bearer ${IAM_TOKEN}" \
https://compute.api.cloud.yandex.net/compute/v1/instances/${VPS_ID} |\
jq --raw-output '.id'
)
while true; do
OPERATION_STATUS=\$(
curl -s -H "Authorization: Bearer ${IAM_TOKEN}" \
https://operation.api.cloud.yandex.net/operations/\${OPERATION_ID} |\
jq --raw-output '.done'
)
echo "Yandex answer, deletion status: \$OPERATION_STATUS"
if [ "\$OPERATION_STATUS" == 'true' ]; then
echo "Finally deleted"
break
fi
echo "Waiting 3s more..."
sleep 3
done
EOT
if [ $? -ne 0 ]; then
echo "Failed deleting VPS"
exit 1
fi
else
echo "No VPS to delete"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment