-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure.sh
executable file
·236 lines (199 loc) · 5.47 KB
/
configure.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/bin/bash
INPUT="" # Input cache
DOCKER_COMPOSE="docker-compose.yml" # Docker compose file
APP_CONFIG="config/application.yml" # App config file
RABBITMQ_CONFIG="assets/rabbitmq/rabbitmq.conf" # RabbitMQ Config file
TRAEFIK_CONFIG="assets/traefik/config.yml" # Traefik static config file
TRAEFIK_DYN_CONFIG="assets/traefik/dynamic-config.yml" # Traefik dynamic config
# $1 text
function print() {
echo "[*] $1"
}
# $1 text
function error() {
echo "[!] Error: $1"
}
# $1 prompt
function input() {
read -p "[*] $1: " INPUT
INPUT=${INPUT:-None}
}
# $1 prompt
# $2 default value
function input_default() {
read -p "[*] $1 [$2]: " INPUT
INPUT=${INPUT:-$2}
}
# $1 pormpt
function password() {
read -s -p "[*] $1: " INPUT
echo
}
# $1 prompt
# $2 default
function password_default() {
read -s -p "[*] $1 [$2]: " INPUT
INPUT=${INPUT:-$2}
echo
}
# $1 prompt
function password_generate() {
read -s -p "[*] $1 [Generated]: " INPUT
INPUT=${INPUT:-$(pwgen 16 -1)}
echo
}
function reset() {
print "About to reset your current configuration. Press Ctl+C to cancel"
print "[ENTER]"
read
print "Resetting config ..."
git checkout -- $APP_CONFIG &>/dev/null
git checkout -- $DOCKER_COMPOSE &>/dev/null
git checkout -- $RABBITMQ_CONFIG &>/dev/null
git checkout -- $TRAEFIK_CONFIG &>/dev/null
git checkout -- $TRAEFIK_DYN_CONFIG &>/dev/null
git checkout -- $TRAEFIK_USERS &>/dev/null
print
}
# $1 file
# $2 find
# $3 replace
function replace() {
sed -i "s/$2/$3/g" "$1"
}
# $1 file
# $2 key
# $3 value
function write() {
yq w -i "$1" "$2" "$3"
}
# $1 file
# $2 key
function delete() {
yq d -i "$1" "$2"
}
# $1 user
# $2 password
# $3 root password
function save_db_config() {
write $DOCKER_COMPOSE "services.database.environment.MYSQL_USER" "$1"
write $APP_CONFIG "spring.datasource.username" "$1"
write $DOCKER_COMPOSE "services.database.environment.MYSQL_PASSWORD" "$2"
write $APP_CONFIG "spring.datasource.password" "$2"
write $DOCKER_COMPOSE "services.database.environment.MYSQL_ROOT_PASSWORD" "$3"
}
# $1 username
# $2 password
# $3 broker port
function save_rabbitmq_config() {
replace $RABBITMQ_CONFIG "default_user = kolibri" "default_user = $1"
replace $RABBITMQ_CONFIG "default_pass = kolibri" "default_pass = $2"
write $TRAEFIK_CONFIG "entryPoints.amqp.address" ":$3"
write $APP_CONFIG "spring.rabbitmq.username" "$1"
write $APP_CONFIG "spring.rabbitmq.password" "$2"
write $APP_CONFIG "spring.rabbitmq.ext-port" "$3"
}
# $1 domain
# $2 email
function save_traefik_config() {
if [ "$1" == "None" ] || [ "$2" == "None" ]; then
delete $TRAEFIK_CONFIG "certificatesResolvers"
write $TRAEFIK_DYN_CONFIG "http.routers.backend-router.tls" "{}"
write $TRAEFIK_DYN_CONFIG "http.routers.proxy-router.tls" "{}"
write $TRAEFIK_DYN_CONFIG "http.routers.mq-dashboard-router.tls" "{}"
replace $TRAEFIK_DYN_CONFIG "'{}'" "{}"
else
replace $TRAEFIK_CONFIG "<email-address>" "$2"
replace $TRAEFIK_DYN_CONFIG "<domain>" "$1"
fi
}
# $1 username
# $2 password
function save_init_admin_user() {
PASSWORD=($(echo -n $2 | sha256sum))
write $APP_CONFIG "admin.initial-username" "$1"
write $APP_CONFIG "admin.initial-password" "$PASSWORD"
}
function initial_config() {
print "Initial Config Requieres a config reset"
reset
print "Starting Initial Config ..."
print
print "Database"
input_default "Enter a database user" "kolibri"
DB_USER=$INPUT
password_generate "Enter a database password"
DB_PASSWORD=$INPUT
ROOT_PASSWORD=$(pwgen 16 -1)
print "Generated root password: $ROOT_PASSWORD"
save_db_config "$DB_USER" "$DB_PASSWORD" "$ROOT_PASSWORD"
print
print "RabbitMQ"
input_default "Enter a rabbitmq username" "kolibri"
RMQ_USER=$INPUT
password_generate "Enter a rabbitmq password"
RMQ_PASSWORD=$INPUT
input_default "Enter a broker tcp port" "5672"
RMQ_PORT=$INPUT
save_rabbitmq_config "$RMQ_USER" "$RMQ_PASSWORD" "$RMQ_PORT"
print
print "Traefik"
input_default "Enter the domain used for this server" "None"
DOMAIN=$INPUT
if ! [ "$DOMAIN" == "None" ]; then
EMAIL="None"
while [ "$EMAIL" == "None" ]; do
input "Enter a valid email address to register the Let's Encrypt SSL Certificates"
EMAIL=$INPUT
done
save_traefik_config "$DOMAIN" "$EMAIL"
else
print "Using no Domain. Traefik will generate a self signed certificate"
save_traefik_config "None" "None"
fi
print
input_default "Enter a admin user" "admin"
ADMIN_USER=$INPUT
password_default "Enter a password for the admin user" "admin"
ADMIN_PASSWORD=$INPUT
save_init_admin_user "$ADMIN_USER" "$ADMIN_PASSWORD"
print
print "Finished Initial Config"
}
function check_deps() {
missing="false"
if ! [ "$(command -v yq)" ]; then
error "yq not installed"
print "See https://mikefarah.gitbook.io/yq/" >&2
print
missing="true"
fi
if ! [ "$(command -v git)" ]; then
error "git not installed"
print
missing="true"
fi
if ! [ "$(command -v pwgen)" ]; then
error "pwgen not installed"
print
missing="true"
fi
if ! [ "$(command -v sha256sum)" ]; then
error "sha256sum not installed"
print
missing="true"
fi
if [ "$missing" == "true" ]; then
exit 0
fi
}
print "Kolibri Server Setup"
print
check_deps
if [ "$1" == "init" ]; then
initial_config
elif [ "$1" == "reset" ]; then
reset
else
print "Use one of the following options: init, reset"
fi