-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctrl.sh
executable file
·373 lines (270 loc) · 7.6 KB
/
ctrl.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/usr/bin/env bash
# Load custom libraries
source deploy/inc/lib.sh
source deploy/inc/input.sh
# Load the default environment configuration into the shell
load_env_default
action=${1:-}
intact=${2:-1}
# Confirm with the user what action to take if not provided
input_action
action_configure () {
interactive=0
if [ "$1" == "1" ]; then
interactive=1
fi
# Load the default environment configuration into the shell
load_env_default
# Load the environment configuration into the shell
load_env_config
# Prompt the user to configure the environment if required
if [ "$interactive" == "1" ]; then
input_env
fi
# Update the environment configuration with the user's input
build_env_config
}
action_prepare () {
interactive=0
if [ "$1" == "1" ]; then
interactive=1
fi
# Confirm that the user wants to prepare the system
if [ "$interactive" == "1" ]; then
input_confirm_action "prepare"
fi
sudo apt update && sudo apt dist-upgrade -y
sudo apt install -y ca-certificates curl gnupg lsb-release
# Download Docker GPG key and add it to the keyring if it doesn't already exist
if [ ! -f /usr/share/keyrings/docker-archive-keyring.gpg ]; then
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
fi
# Add the Docker repository to the APT sources if it doesn't already exist
if [ ! -f /etc/apt/sources.list.d/docker.list ]; then
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
fi
# Update APT and install Docker
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io net-tools
# Reconfigure Docker networking to a non-conflicting subnet if a configuration file doesn't already exist
if [ ! -f /etc/docker/daemon.json ]; then
echo '{
"bip": "192.168.228.1/23",
"default-address-pools": [{"base":"192.168.230.0/23","size":27}]
}
' | sudo tee /etc/docker/daemon.json > /dev/null
fi
# Restart Docker
sudo systemctl restart docker
# Add the current user to the Docker group
sudo adduser "$USER" docker
}
action_build () {
interactive=0
if [ "$1" == "1" ]; then
interactive=1
fi
version=${2:-}
# Load the environment configuration
load_env_config
# Prompt the user to configure the Kea version if not provided
if [ "$interactive" == "1" ] && [ "$version" == "" ]; then
# Prompt the user to configure the Kea version
input_kea_version
elif [ "$version" == "" ]; then
# Set the version to the default value
version="${KEA_VERSION}"
else
# Set the Kea version to the provided value
export KEA_VERSION="$version"
fi
# Build the environment configuration to save potentially changed Kea version
build_env_config
# Build the Docker image
build_image "${KEA_VERSION}"
}
action_push () {
interactive=0
if [ "$1" == "1" ]; then
interactive=1
fi
# Load the environment configuration
load_env_config
# Push the container image to a container repository
push_image "${KEA_IMAGE_TAG}"
}
action_install () {
interactive=0
if [ "$1" == "1" ]; then
interactive=1
fi
# Load the environment configuration into the shell
load_env_config
if [ "$interactive" == "1" ]; then
input_confirm_action "install"
action_configure
# Update the environment configuration with potential user changes
build_env_config
fi
# Clean build files
clean_build
# Build the Docker Compose files
build_docker_compose
# Create/Update/Start the Docker service containers
compose_command "up -d"
}
action_uninstall () {
interactive=0
if [ "$1" == "1" ]; then
interactive=1
fi
# Load the environment configuration into the shell
load_env_config
if [ "$interactive" == "1" ]; then
input_confirm_action "uninstall"
fi
# Stop the Docker services and remove the containers
compose_command "down"
compose_command "rm"
# Clean the build files
clean_build
}
action_reinstall () {
interactive=0
if [ "$1" == "1" ]; then
interactive=1
fi
# Uninstall the container services
action_uninstall "$interactive"
# Reinstall the services
action_install "$interactive"
}
action_purge () {
interactive=0
if [ "$1" == "1" ]; then
interactive=1
fi
# Load the environment configuration into the shell
load_env_config
# Confirm that the user wants to purge the services
if [ "$interactive" == "1" ]; then
input_confirm_action "purge"
fi
# Uninstall the container services
action_uninstall "$interactive"
# Remove the build files
rm -fr build
# Remove the Docker images
docker rmi "${KEA_IMAGE_REPO_USERNAME}/${KEA_IMAGE_NAME}"
# Purge Docker build cache
docker builder prune -a -f
}
action_update () {
interactive=0
if [ "$1" == "1" ]; then
interactive=1
fi
# Load the environment configuration into the shell
load_env_config
# Confirm that the user wants to update the services
if [ "$interactive" == "1" ]; then
input_confirm_action "update"
# Prompt the user to configure the Kea version
input_kea_version
# Build the environment configuration with potentially changed Kea version
build_env_config
fi
echo ""
echo "Updating to Kea version $KEA_VERSION"
echo ""
# Reinstall the services
action_reinstall "$interactive"
}
action_start () {
interactive=0
if [ "$1" == "1" ]; then
interactive=1
fi
# Confirm that the user wants to start the services
if [ "$interactive" == "1" ]; then
input_confirm_action "start"
fi
# Start the Docker services
compose_command "start"
}
action_stop () {
interactive=0
if [ "$1" == "1" ]; then
interactive=1
fi
# Confirm that the user wants to stop the services
if [ "$interactive" == "1" ]; then
input_confirm_action "stop"
fi
# Stop the Docker services
compose_command "stop"
}
action_restart () {
interactive=0
if [ "$1" == "1" ]; then
interactive=1
fi
# Confirm that the user wants to restart the services
if [ "$interactive" == "1" ]; then
input_confirm_action "restart"
fi
# Restart the Docker services
compose_command "restart"
}
action_status () {
# Display the Docker service status
compose_command "ps"
}
action_version () {
# Display the Kea HA tool version
# shellcheck disable=SC2153
echo "Kea HA Tool Version: ${KHA_VERSION}"
}
# Run the appropriate action
if [ "$action" == "configure" ]; then
action_configure "$intact"
elif [ "$action" == "prepare" ]; then
action_prepare "$intact"
elif [ "$action" == "build" ]; then
action_build "$intact"
elif [ "$action" == "push" ]; then
action_push "$intact"
elif [ "$action" == "install" ]; then
# Prompt the user to configure the environment
action_configure
# Install the services interactively
action_install "$intact"
elif [ "$action" == "uninstall" ]; then
action_uninstall "$intact"
elif [ "$action" == "reinstall" ]; then
action_reinstall "$intact"
elif [ "$action" == "purge" ]; then
action_purge "$intact"
elif [ "$action" == "update" ]; then
action_update "$intact"
elif [ "$action" == "start" ]; then
action_start "$intact"
elif [ "$action" == "stop" ]; then
action_stop "$intact"
elif [ "$action" == "restart" ]; then
action_restart "$intact"
elif [ "$action" == "status" ]; then
action_status
elif [ "$action" == "version" ]; then
action_version
elif [ "$action" == "exit" ]; then
exit 0
# Handle invalid actions
else
printf 'Invalid action: %s\n' "$action"
exit 1
fi
# Default exit path
exit 0