-
Notifications
You must be signed in to change notification settings - Fork 4
/
oci.sh
executable file
·84 lines (65 loc) · 2.74 KB
/
oci.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/bash
set -euo pipefail
# include the common library
source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)/lib/common.sh"
#---------------------------------------------------------------
# OCI cleanup
#---------------------------------------------------------------
greenprint "🔮☁ Starting OCI cleanup"
if ! hash oci && [ ! -e /root/bin/oci ]; then
echo 'No oci cli, cannot proceed'
exit 1
fi
OCI_CONFIG=$(mktemp -p "${TEMPDIR}")
echo "$OCI_PRIV_KEY_DATA" > "${TEMPDIR}/priv_key.pem"
echo "$OCI_CONFIG_DATA" > "$OCI_CONFIG"
echo "key_file=${TEMPDIR}/priv_key.pem" >> "$OCI_CONFIG"
OCI_CMD="/root/bin/oci --config-file $OCI_CONFIG"
$OCI_CMD --version
$OCI_CMD setup repair-file-permissions --file "${TEMPDIR}/priv_key.pem"
$OCI_CMD setup repair-file-permissions --file "$OCI_CONFIG"
greenprint "🧹 Cleaning up instances"
INSTANCES=$($OCI_CMD compute instance list -c "$OCI_COMPARTMENT" | jq -r ".data[].id")
for i in $INSTANCES; do
INSTANCE_DATA=$($OCI_CMD compute instance get --instance-id "$i" | jq -r ".data")
if [[ $(echo "$INSTANCE_DATA" | jq -r '.["freeform-tags"].persist') = true ]]; then
echo "Instance $i is tagged with persist=true"
continue
fi
if [[ $(echo "$INSTANCE_DATA" | jq -r '.["lifecycle-state"]') = TERMINATED ]]; then
echo "Instance $i already terminated"
continue
fi
TIME_CREATED=$(echo "$INSTANCE_DATA" | jq -r '.["time-created"]')
if [[ $(date -d "$TIME_CREATED" +%s) -gt "$DELETE_TIME" ]]; then
echo "Instance $i was created less than $HOURS_BACK hours ago"
continue
fi
if [ "$DRY_RUN" == "true" ]; then
echo "Dry run, skipping termination of instance $i"
continue
fi
echo "Terminating instance $i"
$OCI_CMD compute instance terminate --force --instance-id "$i"
done
greenprint "🧹 Cleaning up images"
IMAGES=$($OCI_CMD compute image list -c "$OCI_COMPARTMENT" --all | jq -r ".data[] | select(.[\"compartment-id\"] == \"$OCI_COMPARTMENT\").id")
for i in $IMAGES; do
IMAGE_DATA=$($OCI_CMD compute image get --image-id "$i" | jq -r ".data")
if [[ $(echo "$IMAGE_DATA" | jq -r '.["freeform-tags"].persist') = true ]]; then
echo "Image $i is tagged with persist=true"
continue
fi
TIME_CREATED=$(echo "$IMAGE_DATA" | jq -r '.["time-created"]')
if [[ $(date -d "$TIME_CREATED" +%s) -gt "$DELETE_TIME" ]]; then
echo "IMAGE $i was created less than $HOURS_BACK hours ago"
continue
fi
if [ "$DRY_RUN" == "true" ]; then
echo "Dry run, skipping deletion of image $i"
continue
fi
echo "Deleting image $i"
$OCI_CMD compute image delete --force --image-id "$i"
done
greenprint "🔮☁ Finished OCI cleanup"