-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap-db.sh
executable file
·77 lines (66 loc) · 2.88 KB
/
bootstrap-db.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
#!/bin/bash
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
if ! command -v aws &> /dev/null; then
echo "AWS CLI is not installed"
exit 1
fi
if ! aws configure list &> /dev/null; then
echo "AWS credentials are not configured. Please configure them using 'aws configure'."
exit 1
fi
echo "AWS CLI is installed and credentials are configured."
if [ ! -f .env ]; then
touch "${SCRIPT_DIR}/.env"
# write default cluster name to env file: awsfundamentals
echo "CLUSTER_IDENTIFIER=awsfundamentals" >> "${SCRIPT_DIR}/.env"
fi
# load cluster name
source "${SCRIPT_DIR}/.env"
# doesn't work in many regions yet
AWS_REGION='us-east-1'
# check if our cluster already exists
CLUSTERS=$(aws dsql list-clusters --region $AWS_REGION --query 'clusters[*].[arn,identifier]' --output text)
CLUSTER_EXISTS=false
# describe each cluster and check for our tag
while read -r CLUSTER; do
CLUSTER_ARN=$(echo $CLUSTER | awk '{print $1}')
CLUSTER_IDENTIFIER=$(echo $CLUSTER | awk '{print $2}')
NAME=$(aws dsql list-tags-for-resource --resource-arn $CLUSTER_ARN --region $AWS_REGION | jq -r '.tags.Name')
if [ "$NAME" == "$CLUSTER_NAME" ]; then
CLUSTER_EXISTS=true
echo "Cluster with name $CLUSTER_NAME already exists: $CLUSTER_IDENTIFIER"
# write the cluster identifier to the .env file if it doesn't exist yet
if ! grep -q "CLUSTER_IDENTIFIER" .env; then
echo "CLUSTER_IDENTIFIER=$CLUSTER_IDENTIFIER" >> "${SCRIPT_DIR}/.env"
fi
# if it already exists, overwrite it
if [ "$CLUSTER_IDENTIFIER" != "$CLUSTER_NAME" ]; then
echo "Overwriting CLUSTER_IDENTIFIER in .env file"
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i '' "s/CLUSTER_IDENTIFIER=.*/CLUSTER_IDENTIFIER=$CLUSTER_IDENTIFIER/" .env
else
sed -i "s/CLUSTER_IDENTIFIER=.*/CLUSTER_IDENTIFIER=$CLUSTER_IDENTIFIER/" .env
fi
fi
fi
done <<< "$CLUSTERS"
if [ "$CLUSTER_EXISTS" = false ]; then
echo "Creating cluster with name $CLUSTER_NAME"
CREATE_RESPONSE=$(aws dsql create-cluster --region $AWS_REGION \
--tags Name=$CLUSTER_NAME \
--no-deletion-protection-enabled)
CLUSTER_IDENTIFIER=$(echo $CREATE_RESPONSE | jq -r '.identifier')
echo "Cluster created with identifier: $CLUSTER_IDENTIFIER"
if ! grep -q "CLUSTER_IDENTIFIER" .env; then
echo "CLUSTER_IDENTIFIER=$CLUSTER_IDENTIFIER" >> "${SCRIPT_DIR}/.env"
fi
# if it already exists, overwrite it
if [ "$CLUSTER_IDENTIFIER" != "$CLUSTER_NAME" ]; then
echo "Overwriting CLUSTER_IDENTIFIER in .env file"
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i '' "s/CLUSTER_IDENTIFIER=.*/CLUSTER_IDENTIFIER=$CLUSTER_IDENTIFIER/" .env
else
sed -i "s/CLUSTER_IDENTIFIER=.*/CLUSTER_IDENTIFIER=$CLUSTER_IDENTIFIER/" .env
fi
fi
fi