-
Notifications
You must be signed in to change notification settings - Fork 0
/
tfenv.sh
executable file
·66 lines (62 loc) · 2.23 KB
/
tfenv.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
#!/usr/bin/env bash
if [ ! -d .tfenv ]
then
echo "This is your first time running tfenv.sh. Creating .tfenv directory, switch file, and back up directory."
echo "After that is complete you will need to update .tfenv/switch to list all of the environments you want to"
echo "manage in this instance. If you have existing tfstate or tfvars files you will need to rename them "
echo "appropriately so that they reflect the environment they are tied to. i.e.:"
echo " $ mv terraform.tfstate terraform.production.tfstate"
echo " $ mv terraform.tfvars terraform.production.tfvars"
echo ""
mkdir -p ./.tfenv/backups
touch ./.tfenv/switch
else
if [ -s ./.tfenv/switch ]
then
echo "Switching environments"
CURRENT_ENVS=`cat ./.tfenv/switch`
DEFAULT_ENVS=`echo "$CURRENT_ENVS" | cut -d " " -f 1`
CURRENT_ENV=`grep ".*[CURRENT]" ./.tfenv/switch | cut -d " " -f 1`
echo ""
echo "$CURRENT_ENVS"
echo ""
echo "Environment to switch to: "
read e
echo "Setting state and variable files to use $e configurations: "
echo "terraform.${e}.tfstate"
echo "terraform.${e}.tfvars"
D=`date +%s`
if [ -f "terraform.${CURRENT_ENV}.tfstate" ]
then
cp terraform.${CURRENT_ENV}.tfstate ./.tfenv/backups/terraform.${CURRENT_ENV}.tfstate.${D}
fi
cp terraform.${CURRENT_ENV}.tfvars ./.tfenv/backups/terraform.${CURRENT_ENV}.tfvars.${D}
if [ -f "terraform.tfstate" ]
then
cat terraform.tfstate > terraform.${CURRENT_ENV}.tfstate
fi
cat terraform.tfvars > terraform.${CURRENT_ENV}.tfvars
if [[ ! -f "terraform.${e}.tfstate" && ! -f "terraform.${e}.tfvars" ]]
then
# Empty vars, remove state
echo "It appears you have added a new configuration. Creating a blank tfvars file (the state file will create when you use terraform)."
touch terraform.${e}.tfvars
touch terraform.tfvars
echo "" > terraform.tfvars
rm terraform.tfstate
else
if [ -f "terraform.${e}.tfstate" ]
then
cat terraform.${e}.tfstate > terraform.tfstate
fi
cat terraform.${e}.tfvars > terraform.tfvars
fi
O=`echo "$DEFAULT_ENVS" | sed -e 's/^'"$e"'$/'"$e"' [CURRENT]/g'`
echo "$O" > ./.tfenv/switch
echo "Complete"
exit 0
else
echo ".tfenv/switch is either empty or missing. Create it with one environment per line."
exit -1
fi
fi