Skip to content

Commit f41ceb4

Browse files
authored
CLOUD-126261: Github Action: Add cleaning serverless instances to the cleaner job (#564)
1 parent 88245c7 commit f41ceb4

File tree

2 files changed

+65
-17
lines changed

2 files changed

+65
-17
lines changed

.github/actions/cleanup/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ FROM alpine:latest
22

33
# Copies your code file from your action repository to the filesystem path `/` of the container
44
COPY entrypoint.sh /home/entrypoint.sh
5+
56
RUN apk update && \
67
apk add jq && \
78
apk add curl && \

.github/actions/cleanup/entrypoint.sh

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,40 @@
11
#!/bin/bash
2+
# shellcheck shell=bash disable=SC1091
23

34
# For Deleting empty(!) PROJECTs which live more then (MAX_PROJECT_LIFETIME_INPUT) hours
45
# It deletes all if INPUT_CLEAN_ALL is true
56

67
mongocli config set skip_update_check true
8+
set -e
9+
10+
# ------------------------------------------------------------------------------
11+
# delete global API key by API request (mongocli does not support it yet)
12+
BASE_URL="https://cloud-qa.mongodb.com/api/atlas/v1.0"
13+
14+
get_api_keys() {
15+
curl -s -u "${MCLI_PUBLIC_API_KEY}:${MCLI_PRIVATE_API_KEY}" --digest "${BASE_URL}/orgs/${MCLI_ORG_ID}/apiKeys"
16+
}
17+
18+
delete_test_apikeys() {
19+
API_KEY_ID=$1
20+
curl -s -u "${MCLI_PUBLIC_API_KEY}:${MCLI_PRIVATE_API_KEY}" --digest --request DELETE "${BASE_URL}/orgs/${MCLI_ORG_ID}/apiKeys/${API_KEY_ID}"
21+
}
22+
23+
get_all_serverless() {
24+
curl -s -u "${MCLI_PUBLIC_API_KEY}:${MCLI_PRIVATE_API_KEY}" --digest "${BASE_URL}/groups/${projectID}/serverless"
25+
}
26+
27+
delete_serverless_request() {
28+
instance=$1
29+
curl -s -u "${MCLI_PUBLIC_API_KEY}:${MCLI_PRIVATE_API_KEY}" --digest --request DELETE "${BASE_URL}/groups/${projectID}/serverless/${instance}"
30+
}
31+
32+
get_serverless_instance_request() {
33+
instance=$1
34+
curl -s -u "${MCLI_PUBLIC_API_KEY}:${MCLI_PRIVATE_API_KEY}" --digest "${BASE_URL}/groups/${projectID}/serverless/${instance}"
35+
}
36+
37+
# ------------------------------------------------------------------------------
738

839
command_result_waiter() {
940
command=$1
@@ -75,6 +106,35 @@ delete_clusters() {
75106
done
76107
}
77108

109+
delete_serverless() {
110+
projectID=$1
111+
echo "====== Cleaning Serverless Instances in Project: $projectID"
112+
all=$(get_all_serverless)
113+
echo "all instances: $all"
114+
serverless=$(echo "$all" | jq -r '.results | .[] | .name')
115+
echo "list name: ${serverless[*]}"
116+
total_serverless=$(echo "$all" | jq -r '.totalCount // 0')
117+
echo "total serverless instances: $total_serverless"
118+
if [[ $total_serverless -gt 0 ]]; then
119+
# shellcheck disable=SC2068
120+
# multiline
121+
for instance in ${serverless[@]}; do
122+
echo "== Cleaning Serverless instance: $instance"
123+
instance_atlas=$(get_serverless_instance_request "$instance")
124+
echo "inside: $instance_atlas"
125+
126+
state=$(get_serverless_instance_request "$instance" | jq -r '.stateName')
127+
echo "Current instance: $instance. State: $state"
128+
if [[ -n $state ]] && [[ $state != "DELETING" ]]; then
129+
echo "Deleting instance $instance in $projectID"
130+
delete_serverless_request "$instance"
131+
command="get_serverless_instance_request $instance"
132+
command_result_waiter "$command" "providerSettings" 10 20 # if no providerSetting - no serverless instance
133+
fi
134+
done
135+
fi
136+
}
137+
78138
delete_project() {
79139
peDeleted=$(mongocli atlas privateEndpoints aws list --projectId "$projectID" | awk 'NR!=1{print $1}')$(mongocli atlas privateEndpoints azure list --projectId "$projectID" | awk 'NR!=1{print $1}')
80140
[[ $peDeleted == "" ]] && mongocli iam projects delete "$id" --force
@@ -85,7 +145,7 @@ delete_old_project() {
85145
output=$(
86146
exist=is_project_exist
87147
if [[ -n ${exist:-} ]] || [[ -z "${count:-}" ]] || [[ ${count:-} == "null" ]] && [[ "$existance_hours" -gt $MAX_PROJECT_LIFETIME_INPUT ]]; then
88-
echo "====== Cleaning Project: $id"
148+
echo "======================= Cleaning Project: $id"
89149
delete_endpoints_for_project "$id" "aws"
90150
delete_endpoints_for_project "$id" "azure"
91151
delete_endpoints_for_project "$id" "gcp"
@@ -105,32 +165,18 @@ delete_all() {
105165
output=$(
106166
exist=is_project_exist
107167
if [[ -n ${exist:-} ]]; then
108-
echo "====== Cleaning Project: $id"
168+
echo "======================= Cleaning Project: $id"
109169
delete_endpoints_for_project "$id" "aws"
110170
delete_endpoints_for_project "$id" "azure"
111171
delete_endpoints_for_project "$id" "gcp"
172+
delete_serverless "$id"
112173
delete_clusters "$id"
113174
delete_project
114175
fi
115176
)
116177
echo "${output[@]}"
117178
}
118179

119-
# ------------------------------------------------------------------------------
120-
# delete global API key by API request (mongocli does not support it yet)
121-
BASE_URL="https://cloud-qa.mongodb.com/api/atlas/v1.0"
122-
123-
get_api_keys() {
124-
curl -s -u "${MCLI_PUBLIC_API_KEY}:${MCLI_PRIVATE_API_KEY}" --digest "${BASE_URL}/orgs/${MCLI_ORG_ID}/apiKeys"
125-
}
126-
127-
delete_test_apikeys() {
128-
API_KEY_ID=$1
129-
curl -s -u "${MCLI_PUBLIC_API_KEY}:${MCLI_PRIVATE_API_KEY}" --digest --request DELETE "${BASE_URL}/orgs/${MCLI_ORG_ID}/apiKeys/${API_KEY_ID}"
130-
}
131-
132-
# ------------------------------------------------------------------------------
133-
134180
echo "The process could take a while. Please, wait..."
135181

136182
projects=$(mongocli iam projects list -o json | jq -c .)
@@ -175,5 +221,6 @@ if [[ "${INPUT_CLEAN_ALL:-}" == "true" ]]; then
175221
fi
176222
done
177223
fi
224+
# ------------------------------------------------------------------------------
178225

179226
echo "Job Done"

0 commit comments

Comments
 (0)