-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnollo-script.sh
executable file
·220 lines (177 loc) · 5.69 KB
/
nollo-script.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/bin/bash
# Constants and default values.
TF_AUTO_VARS_PATH="./infrastructure/terraform.auto.tfvars"
# Settings the default values for all flags.
should_generate_wireguard_keys=0
should_create_admin_config=0
function usage {
cat <<-EOF
Usage: nollo-script [options] ...
This script deploys the infrastructure defined in the
"infrastructure" directory.
Options:
-h, --help This help output
-k, --gen-keys Generate Wireguard keys
-c, --wg-admin-config file Output the Wireguard admin config to a file
Example:
nollo-script --gen-keys
EOF
if [ -n "$*" ] ; then
echo 1>&2
echo "Error: $*" 1>&2
exit 1
else
exit 0
fi
}
function generate_wireguard_keys {
if grep -q WG_ADMIN_PBK "$1"; then
printf "Error: WG_ADMIN_PBK variable already exists in $1\n"
exit 1
fi
if grep -q WG_BE_SERVER_PVK "$1"; then
printf "Error: WG_BE_SERVER_PVK variable already exists in $1\n"
exit 1
fi
if grep -q WG_BE_SERVER_PBK "$1"; then
printf "Error: WG_BE_SERVER_PBK variable already exists in $1\n"
exit 1
fi
if grep -q WG_FE_SERVER_PVK "$1"; then
printf "Error: WG_FE_SERVER_PVK variable already exists in $1\n"
exit 1
fi
if grep -q WG_FE_SERVER_PBK "$1"; then
printf "Error: WG_FE_SERVER_PBK variable already exists in $1\n"
exit 1
fi
# Generate Wireguard keys.
printf "Generating Wireguard keys...."
admin_private_key=$(wg genkey)
admin_public_key=$(echo $admin_private_key | wg pubkey)
printf "WG_ADMIN_PBK = \"$admin_public_key\"\n" >> $1
printf "WG_ADMIN_PVK = \"$admin_private_key\"\n"
backend_wireguard_private_key=$(wg genkey)
backend_wireguard_public_key=$(echo $backend_wireguard_private_key | wg pubkey)
printf "WG_BE_SERVER_PVK = \"$backend_wireguard_private_key\"\n" >> $1
printf "WG_BE_SERVER_PBK = \"$backend_wireguard_public_key\"\n" >> $1
frontend_wireguard_private_key=$(wg genkey)
frontend_wireguard_public_key=$(echo $frontend_wireguard_private_key | wg pubkey)
printf "WG_FE_SERVER_PVK = \"$frontend_wireguard_private_key\"\n" >> $1
printf "WG_FE_SERVER_PBK = \"$frontend_wireguard_public_key\"\n" >> $1
printf "Done\n"
}
function create_wireguard_admin_config {
cat > $1 <<-EOF
[Interface]
Address = 10.10.150.45/32
SaveConfig = true
ListenPort = 51820
PrivateKey = $admin_private_key
[Peer]
PublicKey = $backend_wireguard_public_key
AllowedIPs = 10.10.150.20/32, 10.0.1.0/24, 10.0.2.0/24, 10.0.3.0/24, 10.0.101.0/24, 10.0.102.0/24, 10.0.103.0/24
Endpoint = 54.174.225.244:51820
PersistentKeepalive = 15
EOF
}
function validate_tf_vars_file {
# Validate Terraform variables.
printf "Validating Terraform variables...."
if ! grep -q AWS_ACCESS_KEY_ID "$1"; then
printf "\nError: Missing AWS_ACCESS_KEY_ID variable in $1\n"
exit 1
fi
if ! grep -q AWS_SECRET_ACCESS_KEY "$1"; then
printf "\nError: Missing AWS_SECRET_ACCESS_KEY variable in $1\n"
exit 1
fi
if ! grep -q NOLLO_DB_ROOT_PW "$1"; then
printf "\nError: Missing NOLLO_DB_ROOT_PW variable in $1\n"
exit 1
fi
if ! grep -q NOLLO_DB_ADMIN_PW "$1"; then
printf "\nError: Missing NOLLO_DB_ADMIN_PW variable in $1\n"
exit 1
fi
if ! grep -q NOLLO_DB_API_PW "$1"; then
printf "\nError: Missing NOLLO_DB_API_PW variable in $1\n"
exit 1
fi
if ! grep -q NOLLO_API_DSN "$1"; then
printf "\nError: Missing NOLLO_API_DSN variable in $1\n"
exit 1
fi
if ! grep -q WG_ADMIN_PBK "$1"; then
printf "\nError: Missing WG_ADMIN_PBK variable in $1\n"
exit 1
fi
if ! grep -q WG_BE_SERVER_PVK "$1"; then
printf "\nError: Missing WG_BE_SERVER_PVK variable in $1\n"
exit 1
fi
if ! grep -q WG_BE_SERVER_PBK "$1"; then
printf "\nError: Missing WG_BE_SERVER_PBK variable in $1\n"
exit 1
fi
if ! grep -q WG_FE_SERVER_PVK "$1"; then
printf "\nError: Missing WG_FE_SERVER_PVK variable in $1\n"
exit 1
fi
if ! grep -q WG_FE_SERVER_PBK "$1"; then
printf "\nError: Missing WG_FE_SERVER_PBK variable in $1\n"
exit 1
fi
printf "Done\n"
}
# Parse command line arguments.
while [ -n "$1" ] ; do
case $1 in
-k | --gen-keys)
should_generate_wireguard_keys=1
;;
--wg-admin-config)
should_create_admin_config=1
if [ "$2" = "" ]; then
printf "Missing file for wg-admin-config"
exit 1
fi
admin_config_fp=$2
shift
;;
-h | --help)
usage
exit 0
;;
-*)
usage "unknown option '$*'"
exit 1
;;
*)
echo "No options..."
exit 1
;;
esac
shift
done
# Call function to generate Wireguard keys.
if [ "$should_generate_wireguard_keys" = "1" ]; then
generate_wireguard_keys $TF_AUTO_VARS_PATH
fi
# # Call function to create admin Wireguard configuation file.
# if [ "$should_create_admin_config" = "1" ]; then
# create_wireguard_admin_config $admin_config_fp
# fi
# Call function to validate Terraform variables file
validate_tf_vars_file $TF_AUTO_VARS_PATH
# Init any new modules
cd ./infrastructure
terraform init
# Destroy old state
printf "Destroying previous Terraform state...."
terraform destroy --auto-approve
printf "\nDone\n"
# Create new state
printf "Creating new infrastructure...."
terraform apply --auto-approve
printf "\nDone\n"