This repository has been archived by the owner on Apr 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
console
executable file
·187 lines (159 loc) · 4.54 KB
/
console
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env bash
set -eu
# Check requirements.
command -v sed >/dev/null 2>&1 || { echo >&2 "I require sed but it's not installed. Aborting."; exit 1; }
command -v terraform >/dev/null 2>&1 || { echo >&2 "I require terraform but it's not installed. Aborting."; exit 1; }
command -v terragrunt >/dev/null 2>&1 || { echo >&2 "I require terragrunt but it's not installed. Aborting."; exit 1; }
pwd=$(pwd)
# Load Secrets, if exists.
if [ -f "$pwd/.secrets" ]; then
source "$pwd/.secrets"
fi
# Check if Hetzner API key is present.
if [ -z ${TF_VAR_hcloud_token+x} ]; then
read -p 'Hetzner API Token: ' hcloudTK
export TF_VAR_hcloud_token="$hcloudTK"
fi
# Check if acme email is present.
if [ -z ${TF_VAR_acme_email+x} ]; then
read -p 'Acme Email: ' acmeEmail
export TF_VAR_acme_email="$acmeEmail"
fi
# Check if env is set.
if [ -z ${GT_env+x} ]; then
read -p 'Env: ' hcloudEnv
if ! [[ "$hcloudEnv" =~ ^(prod|stage)$ ]]; then
echo 'Invalid env! Available: prod, stage.'
exit 1
fi
export GT_env="$hcloudEnv"
fi
# Save into local secret file.
if [ ! -f "$pwd/.secrets" ]; then
echo "export TF_VAR_hcloud_token=${hcloudTK}" > "$pwd/.secrets"
echo "export GT_env=${hcloudEnv}" >> "$pwd/.secrets"
fi
# If no command given, cancel.
if [[ ${1-help} == "help" ]]; then
cat << EOF
List of available commands:
apply-all - Apply-All currently selected env. (No help page)
clear - Cleanup all set secrets. (No help page)
dump - Show current secrets. (No help page)
env - Change environment.
set - Set secrets from current env.
tg - Run TerraGrunt (Use it like you would use TerraForm)
unset - Unsets secret from current env.
reset - Reset local state. (No help page)
See command specific help: ./console <command> help
EOF
exit 0
fi
# Apply-All. Apply all modules to currently selected env.
if [[ $1 == "apply-all" ]]; then
cd "$pwd/envs/$GT_env"
terragrunt apply-all --terragrunt-non-interactive
returnCode=$?
cd "$pwd"
exit $returnCode
fi
# Clear. Remove the secrets file.
if [[ $1 == "clear" ]]; then
rm "$pwd/.secrets"
echo "Secrets cleared!"
exit 0
fi
# Destroy-All. Destroy all modules from currently selected env.
if [[ $1 == "destroy-all" ]]; then
cd "$pwd/envs/$GT_env"
terragrunt destroy-all --terragrunt-non-interactive
returnCode=$?
cd "$pwd"
exit $returnCode
fi
# Env. Change env.
if [[ $1 == "env" ]]; then
# Show help for env command.
if [[ ${2-help} == "help" ]]; then
cat << EOF
Set current processing env.
Example usage: ./console env <prod|stage>
EOF
exit 0
fi
# If no env given as value, ask for it.
if [[ ${2-} == "" ]]; then
read -p 'Enter new env: ' hcloudEnv
else
hcloudEnv=$2
fi
# Validate env
if ! [[ "$hcloudEnv" =~ ^(prod|stage)$ ]]; then
echo 'Invalid env! Available: prod, stage.'
exit 1
fi
# Change env and persist
export GT_env="$hcloudEnv"
grep -v "^export GT_env=" "$pwd/.secrets" > "$pwd/.secrets.new"
mv "$pwd/.secrets.new" "$pwd/.secrets"
echo "export GT_env=${hcloudEnv}" >> "$pwd/.secrets"
exit 0
fi
# Dump. Dump current set secrets.
if [[ $1 == "dump" ]]; then
echo "Current secrets: "
cat "$pwd/.secrets" | sed "s/export //" | sed "s/TF_VAR_//"
exit 0
fi
# Set. Used to set secrets.
if [[ $1 == "set" ]]; then
# Show help for set command.
if [[ ${2-help} == "help" || ${3-} == "" ]]; then
cat << EOF
Set variable for Terraform.
Example usage: ./console set <variable> <value>
EOF
exit 0
fi
# Set variable into secrets file.
echo "export TF_VAR_${2}=${3}" >> "$pwd/.secrets"
echo "Set $2 to $3!"
exit 0
fi
# tg. Terragrunt. Just pass command in correct directory to the tg.
if [[ $1 == "tg" ]]; then
cd "$pwd/envs/$GT_env"
terragrunt "${@:2}"
returnCode=$?
cd "$pwd"
exit $returnCode
fi
# Unset. Used to unset secrets.
if [[ $1 == "unset" ]]; then
# Show help for unset command.
if [[ ${2-help} == "help" || ${2-} == "" ]]; then
cat << EOF
Unset variable for Terraform.
Example usage: ./console unset <variable>
EOF
exit 0
fi
# Remove variable from secrets.
grep -v "^export TF_VAR_${2}=" "$pwd/.secrets" > "$pwd/.secrets.new"
mv "$pwd/.secrets.new" "$pwd/.secrets"
echo "Removed $2 from secrets!"
exit 0
fi
# Reset. Remove local state.
if [[ $1 == "reset" ]]; then
while true; do
read -p "Do you wish reset local state (Cannot be reversed!)? [yN]: " yn
case $yn in
[Yy]* ) rm envs/prod/*/.terragrunt-cache/ -rf; exit;;
[Nn]* ) exit;;
* ) exit;;
esac
done
fi
echo "Command not found!"
exit 1