-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconnector-delete.sh
executable file
·80 lines (65 loc) · 2.44 KB
/
connector-delete.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
#!/usr/bin/env bash
# Exit immediately if a command exits with a non-zero status.
set -e
# Enable debug mode if needed for troubleshooting.
# set -x
# Function to print usage information
usage() {
echo "Usage: $0 [configuration_name] [number_of_deletes|all]"
echo " configuration_name: Optional. The configuration name to use (default is 'default')."
echo " number_of_deletes: Number of oldest connector builds to delete, or 'all' to delete all."
exit 1
}
# Check for required dependencies
if ! command -v jq &>/dev/null; then
echo "Error: 'jq' is required but not installed. Please install 'jq' and try again."
exit 1
fi
# Get the context
context="${1}"
# Check if the config_name is provided.
if [[ -z "$context" ]]; then
echo "Error: You must specify the context from .hasura/context.yaml."
usage
fi
# Get the number of connectors to delete or "all" to delete everything.
delete_count="${2}"
# Check if the delete_count is provided.
if [[ -z "$delete_count" ]]; then
echo "Error: You must specify the number of connector builds to delete or use 'all'."
usage
fi
# Validate the delete_count argument.
if [[ "$delete_count" != "all" && ! "$delete_count" =~ ^[0-9]+$ ]]; then
echo "Error: Please provide a positive integer for the number of connector builds to delete or use 'all'."
usage
fi
# Change working dir to the Hasura directory
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
cd "$SCRIPT_DIR/../hasura" || {
echo "Error: Unable to change to the 'hasura' directory."
exit 1
}
# Get all connector build IDs, sorted by creation time (oldest last).
connector_ids=$(ddn connector build get -c "$context" --out json | jq -r '.[].connectorBuildID')
# Check if there are any connector IDs to delete.
if [[ -z "$connector_ids" ]]; then
echo "No connector builds found for configuration '$context'."
exit 0
fi
# Select connector IDs to delete based on the delete_count.
if [[ "$delete_count" == "all" ]]; then
echo "Deleting all connector builds for configuration '$context'..."
selected_ids="$connector_ids"
else
# Use 'tail' to get the specified number of oldest IDs.
selected_ids=$(echo "$connector_ids" | tail -n "$delete_count")
fi
# Loop through each selected ID and delete it.
for id in $selected_ids; do
echo "Deleting connector build ID: $id"
ddn connector build delete "$id" &
done
# Wait for all background jobs to complete.
wait
echo "Selected connector builds have been deleted successfully."