forked from ikambarov/terraform-vpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setenv.sh
75 lines (68 loc) · 2.02 KB
/
setenv.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
#!/bin/bash
if [ "$0" = "$BASH_SOURCE" ]
then
echo "$0: Please source this file."
echo "e.g. source ./setenv configurations/data-rnd-us-vet1-v1"
return 1
fi
if [ -z "$1" ]
then
echo "setenv: You must provide the name of the configuration file."
echo "e.g. source ./setenv configurations/data-rnd-us-vet1-v1"
return 1
fi
# Get directory we are running from
DIR=$(pwd)
DATAFILE="$DIR/$1"
if [ ! -f "$DATAFILE" ]; then
echo "setenv: Configuration file not found: $DATAFILE"
return 1
fi
# Get env from DATAFILE
ENVIRONMENT=$(sed -nr 's/^\s*environment\s*=\s*"([^"]*)".*$/\1/p' "$DATAFILE")
S3BUCKET=$(sed -nr 's/^\s*s3_bucket\s*=\s*"([^"]*)".*$/\1/p' "$DATAFILE")
S3BUCKETPROJ=$(sed -nr 's/^\s*s3_folder_project\s*=\s*"([^"]*)".*$/\1/p' "$DATAFILE")
S3BUCKETREGION=$(sed -nr 's/^\s*s3_folder_region\s*=\s*"([^"]*)".*$/\1/p' "$DATAFILE")
S3BUCKETTYPE=$(sed -nr 's/^\s*s3_folder_type\s*=\s*"([^"]*)".*$/\1/p' "$DATAFILE")
S3TFSTATEFILE=$(sed -nr 's/^\s*s3_tfstate_file\s*=\s*"([^"]*)".*$/\1/p' "$DATAFILE")
if [ -z "$ENVIRONMENT" ]
then
echo "setenv: 'environment' variable not set in configuration file."
return 1
fi
if [ -z "$S3BUCKET" ]
then
echo "setenv: 's3_bucket' variable not set in configuration file."
return 1
fi
if [ -z "$S3BUCKETPROJ" ]
then
echo "setenv: 's3_folder_project' variable not set in configuration file."
return 1
fi
if [ -z "$S3BUCKETREGION" ]
then
echo "setenv: 's3_folder_region' variable not set in configuration file."
return 1
fi
if [ -z "$S3BUCKETTYPE" ]
then
echo "setenv: 's3_folder_type' variable not set in configuration file."
return 1
fi
if [ -z "$S3TFSTATEFILE" ]
then
echo "setenv: 's3_tfstate_file' variable not set in configuration file."
echo "e.g. s3_tfstate_file=\"infrastructure.tfstate\""
return 1
fi
cat << EOF > "$DIR/backend.tf"
terraform {
backend "s3" {
bucket = "${S3BUCKET}"
key = "${S3BUCKETPROJ}/${S3BUCKETREGION}/${S3BUCKETTYPE}/${ENVIRONMENT}/${S3TFSTATEFILE}"
region = "${S3BUCKETREGION}"
}
}
EOF
cat backend.tf