-
Notifications
You must be signed in to change notification settings - Fork 0
/
tf
executable file
·74 lines (67 loc) · 2.26 KB
/
tf
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
#!/bin/bash
show_help() {
echo "Usage: $0 [command]"
echo ""
echo "Commands:"
echo " apply Apply Terraform changes"
echo " plan Show Terraform execution plan"
echo " destroy Destroy Terraform managed infrastructure"
echo " update_image Build and push Docker image (not a terraform command)"
echo " * Delegate to another Terraform command e.g. './tf workspace show'"
echo ""
echo "Remember to provide credentials: 'AWS_PROFILE=[profile] ./tf apply'"
echo ""
}
# Check for arguments
if [ $# -eq 0 ]; then
show_help
exit 1
fi
select_var_file() {
workspace=$(terraform workspace show)
var_file="terraform.${workspace}.tfvars"
if [[ -f "$var_file" ]]; then
echo "-var-file=${var_file}"
else
echo "No variable file found for workspace ${workspace}."
exit 1
fi
}
cd ./infra > /dev/null
if [[ ! -d ".terraform" ]]; then
echo "Terraform not initialized. Running 'terraform init'..."
terraform init
fi
case $1 in
apply|plan|destroy)
var_file_arg=$(select_var_file)
terraform $1 $var_file_arg "${@:2}"
;;
update_image)
if ! command -v jq >/dev/null 2>&1; then
echo "Error: 'jq' is not installed. Please install it to continue."
exit 1
fi
workspace=$(terraform workspace show)
state_file="./terraform.tfstate.d/${workspace}/terraform.tfstate"
if [ ! -f "$state_file" ]; then
echo "Error: Terraform state file for workspace '${workspace}' not found at ${state_file}. Make sure it exists."
exit 1
fi
# Extract ECR Repository URL and EMR Release Label from workspace state
REPO_URL=$(jq -r '.resources[] | select(.type == "aws_ecr_repository").instances[].attributes.repository_url' "$state_file")
EMR_RELEASE_LABEL="emr-6.9.0"
if [ -z "$REPO_URL" ]; then
echo "Error: ECR Repository URL not found in terraform state for workspace '${workspace}'."
exit 1
fi
./manage_docker.sh push $REPO_URL $EMR_RELEASE_LABEL
;;
shell)
./manage_docker.sh shell
;;
*)
# Directly passing other commands and arguments to Terraform
terraform "$@"
;;
esac