-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
executable file
·151 lines (135 loc) · 4.3 KB
/
deploy.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash
# Load environment variables from .env file
if [ -f .env ]; then
export $(cat .env | xargs)
fi
# set default values
ENVIRONMENT="${ENVIRONMENT:-}"
BUCKET="${BUCKET:-}"
DYNAMODB_TABLE="${DYNAMODB_TABLE:-}"
REGION="${REGION:-}"
TERRAFORM_ROLE="${TERRAFORM_ROLE:-}"
CI=false
# parse command line arguments
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-env|--env|-e)
ENVIRONMENT="$2"
shift
shift
;;
-bucket|--bucket|-b)
BUCKET="$2"
shift
shift
;;
-dynamodb-table|--dynamodb-table|-d)
DYNAMODB_TABLE="$2"
shift
shift
;;
-region|--region|-r)
REGION="$2"
shift
shift
;;
-terraform-role|--terraform-role)
TERRAFORM_ROLE="$2"
shift
shift
;;
-ci|--ci)
CI=true
shift
;;
-h|--help)
echo "Usage: ./deploy.sh [OPTIONS]"
echo "Options:"
echo " -e, --env | Set the environment (e.g., production, staging) [REQUIRED]"
echo " -b, --bucket | Set the bucket name [REQUIRED]"
echo " -d, --dynamodb-table | Set the DynamoDB table name [REQUIRED]"
echo " -r, --region | Set the AWS region [REQUIRED]"
echo " -ci, --ci | Skip creating files and assume all arguments have values"
echo " -h, --help | Show help"
exit 0
;;
*)
echo "Invalid argument: $1"
exit 1
;;
esac
done
if ! command -v terraform &> /dev/null; then
echo "Terraform is not installed. Please install Terraform and try again."
exit 1
fi
if [ -z "$ENVIRONMENT" ] || [ -z "$BUCKET" ] || [ -z "$DYNAMODB_TABLE" ] || [ -z "$REGION" ]; then
echo "Missing required arguments. Please provide all the required arguments."
echo "ENVIRONMENT: $ENVIRONMENT"
echo "BUCKET: $BUCKET"
echo "DYNAMODB_TABLE: $DYNAMODB_TABLE"
echo "REGION: $REGION"
./deploy.sh -h
exit 1
fi
if [ "$CI" = false ]; then
if [ ! -f "$ENVIRONMENT.tfvars" ]; then
echo "Creating $ENVIRONMENT.tfvars"
touch "$ENVIRONMENT.tfvars"
fi
if ! grep -q "owner" "$ENVIRONMENT.tfvars"; then
read -p "Who is the owner of this infrastructure? ( default=skylight ): " owner_choice
owner_choice=${owner_choice:-skylight}
echo "owner = \"$owner_choice\"" >> "$ENVIRONMENT.tfvars"
fi
if ! grep -q "project" "$ENVIRONMENT.tfvars"; then
read -p "What is this project called? ( default=dibbs-ce ): " project_choice
project_choice=${project_choice:-dibbs-ce}
echo "project = \"$project_choice\"" >> "$ENVIRONMENT.tfvars"
fi
if ! grep -q "region" "$ENVIRONMENT.tfvars"; then
read -p "What aws region are you setting up in? ( default=us-east-1 ): " region_choice
region_choice=${region_choice:-us-east-1}
echo "region = \"$region_choice\"" >> "$ENVIRONMENT.tfvars"
fi
fi
echo "Running Terraform with the following variables:"
echo "Environment: $ENVIRONMENT"
echo "Terraform Workspace: $ENVIRONMENT"
echo "Bucket: $BUCKET"
echo "DynamoDB Table: $DYNAMODB_TABLE"
echo "Region: $REGION"
cat "$ENVIRONMENT.tfvars"
echo ""
terraform init \
-var-file="$ENVIRONMENT.tfvars" \
-backend-config "bucket=$BUCKET" \
-backend-config "dynamodb_table=$DYNAMODB_TABLE" \
-backend-config "region=$REGION" \
|| (echo "terraform init failed, exiting..." && exit 1)
# Check if workspace exists
if terraform workspace list | grep -q "$ENVIRONMENT"; then
echo "Selecting $ENVIRONMENT terraform workspace"
terraform workspace select "$ENVIRONMENT"
else
if [ "$CI" = false ]; then
read -p "Workspace '$ENVIRONMENT' does not exist. Do you want to create it? (y/n): " choice
if [[ $choice =~ ^[Yy]$ ]]; then
echo "Creating '$ENVIRONMENT' terraform workspace"
terraform workspace new "$ENVIRONMENT"
else
echo "Workspace creation cancelled."
exit 1
fi
else
echo "Creating '$ENVIRONMENT' terraform workspace"
terraform workspace new "$ENVIRONMENT"
fi
fi
if [ "$CI" = false ]; then
terraform destroy -var-file="$ENVIRONMENT.tfvars"
else
terraform apply -auto-approve -var-file="$ENVIRONMENT.tfvars"
fi