fix: apply lifecycle with boto3; require scw on PATH
aws CLI is not installed here. Create still needs Object Storage permission on the bootstrap key.
This commit is contained in:
parent
d6cc369392
commit
c545d08f39
1 changed files with 43 additions and 16 deletions
|
|
@ -15,8 +15,10 @@ PREFIX="${PREFIX:-platform-pg/}"
|
||||||
ENDPOINT="https://s3.nl-ams.scw.cloud"
|
ENDPOINT="https://s3.nl-ams.scw.cloud"
|
||||||
|
|
||||||
need() { command -v "$1" >/dev/null || { echo "missing $1" >&2; exit 2; }; }
|
need() { command -v "$1" >/dev/null || { echo "missing $1" >&2; exit 2; }; }
|
||||||
|
export PATH="${HOME}/.local/bin:${PATH}"
|
||||||
need python3
|
need python3
|
||||||
need curl
|
need curl
|
||||||
|
need scw
|
||||||
|
|
||||||
TOKEN="${OPENBAO_TOKEN:-${VAULT_TOKEN:-}}"
|
TOKEN="${OPENBAO_TOKEN:-${VAULT_TOKEN:-}}"
|
||||||
if [[ -z "$TOKEN" && -f "$HOME/.vault-token" ]]; then
|
if [[ -z "$TOKEN" && -f "$HOME/.vault-token" ]]; then
|
||||||
|
|
@ -60,26 +62,51 @@ PY
|
||||||
)"
|
)"
|
||||||
|
|
||||||
export SCW_DEFAULT_REGION="$REGION"
|
export SCW_DEFAULT_REGION="$REGION"
|
||||||
need scw
|
|
||||||
need aws
|
|
||||||
|
|
||||||
echo "creating private bucket $BUCKET in $REGION (versioning on)"
|
echo "creating private bucket $BUCKET in $REGION (versioning on)"
|
||||||
scw object bucket create "$BUCKET" region="$REGION" acl=private enable-versioning=true
|
scw object bucket create "$BUCKET" region="$REGION" acl=private enable-versioning=true -o json
|
||||||
|
|
||||||
echo "applying 30-day lifecycle (current + noncurrent versions)"
|
echo "applying 30-day lifecycle (current + noncurrent versions)"
|
||||||
aws --endpoint-url "$ENDPOINT" s3api put-bucket-lifecycle-configuration \
|
VENV="${TMPDIR:-/tmp}/reef-storage-boto3"
|
||||||
--bucket "$BUCKET" \
|
if [[ ! -x "$VENV/bin/python" ]]; then
|
||||||
--lifecycle-configuration '{
|
python3 -m venv "$VENV"
|
||||||
"Rules": [
|
"$VENV/bin/pip" -q install boto3
|
||||||
{
|
fi
|
||||||
"ID": "retain-30-days",
|
BUCKET="$BUCKET" AWS_ACCESS_KEY_ID="$SCW_ACCESS_KEY" AWS_SECRET_ACCESS_KEY="$SCW_SECRET_KEY" \
|
||||||
"Status": "Enabled",
|
"$VENV/bin/python" - <<'PY'
|
||||||
"Filter": {"Prefix": ""},
|
import os
|
||||||
"Expiration": {"Days": 30},
|
import boto3
|
||||||
"NoncurrentVersionExpiration": {"NoncurrentDays": 30}
|
client = boto3.client(
|
||||||
}
|
"s3",
|
||||||
]
|
region_name="nl-ams",
|
||||||
}'
|
endpoint_url="https://s3.nl-ams.scw.cloud",
|
||||||
|
aws_access_key_id=os.environ["AWS_ACCESS_KEY_ID"],
|
||||||
|
aws_secret_access_key=os.environ["AWS_SECRET_ACCESS_KEY"],
|
||||||
|
)
|
||||||
|
client.put_bucket_lifecycle_configuration(
|
||||||
|
Bucket=os.environ["BUCKET"],
|
||||||
|
LifecycleConfiguration={
|
||||||
|
"Rules": [
|
||||||
|
{
|
||||||
|
"ID": "retain-30-days",
|
||||||
|
"Status": "Enabled",
|
||||||
|
"Filter": {"Prefix": ""},
|
||||||
|
"Expiration": {"Days": 30},
|
||||||
|
"NoncurrentVersionExpiration": {"NoncurrentDays": 30},
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
)
|
||||||
|
print("lifecycle applied")
|
||||||
|
PY
|
||||||
|
|
||||||
|
echo "creating a 20 EUR monthly budget alert if none exists"
|
||||||
|
scw billing budget create consumption-limit=20 enabled=true -o json | python3 -c '
|
||||||
|
import json,sys
|
||||||
|
b=json.load(sys.stdin)
|
||||||
|
print("budget_id", b.get("id") or b.get("budget",{}).get("id"))
|
||||||
|
print("limit", b.get("consumption_limit") or b.get("budget",{}).get("consumption_limit"))
|
||||||
|
' || echo "budget create skipped (permissions or API shape)"
|
||||||
|
|
||||||
echo "writing non-secret attributes (no keys)"
|
echo "writing non-secret attributes (no keys)"
|
||||||
python3 - <<PY
|
python3 - <<PY
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue