generated from fuchs-fabian/simbashlog-debian-docker-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
343 lines (282 loc) · 13.1 KB
/
install.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
#!/usr/bin/env sh
PROJECT_GIT_REPO_URL="https://github.com/fuchs-fabian/satissavebak.git"
PROJECT_NAME=$(basename $PROJECT_GIT_REPO_URL .git)
# The host of the git repository for the backup
GIT_HOST_FOR_BACKUP_REPO="github.com"
# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░
# ░░ ░░
# ░░ ░░
# ░░ PREPARATIONS ░░
# ░░ ░░
# ░░ ░░
# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░
# ╔═════════════════════╦══════════════════════╗
# ║ ║
# ║ CHECK COMMANDS ║
# ║ ║
# ╚═════════════════════╩══════════════════════╝
check_command() {
cmd="$1"
echo "Checking if the '$cmd' command is available..."
command -v "$cmd" >/dev/null 2>&1 ||
{
echo "The '$cmd' command is not available. Please install it and try again."
exit 1
}
echo "The '$cmd' command is available."
}
check_command "git"
check_command "docker"
check_command "ssh-keygen"
check_command "ssh-keyscan"
echo "Checking if 'docker compose' is available..."
docker compose version >/dev/null 2>&1 ||
{
echo "The 'docker compose' command is not available."
exit 1
}
echo "The 'docker compose' command is available."
# ╔═════════════════════╦══════════════════════╗
# ║ ║
# ║ CHECK FOR PROJECT ARTIFACTS ║
# ║ ║
# ╚═════════════════════╩══════════════════════╝
if [ -d "$PROJECT_NAME" ]; then
echo
echo "The directory '$PROJECT_NAME' already exists. Remove it? (y/n)"
read -r REMOVE_DIR
if [ "$REMOVE_DIR" = "y" ]; then
rm -rf "$PROJECT_NAME" ||
{
echo "Failed to remove the directory '$PROJECT_NAME'."
exit 1
}
echo
else
echo "The install script cannot continue. You have to set up manually."
echo "Aborting..."
exit 1
fi
fi
# ╔═════════════════════╦══════════════════════╗
# ║ ║
# ║ CLONE REPOSITORY ║
# ║ ║
# ╚═════════════════════╩══════════════════════╝
echo "Cloning the repository '$PROJECT_GIT_REPO_URL'..."
git clone $PROJECT_GIT_REPO_URL ||
{
echo "Failed to clone the repository '$PROJECT_GIT_REPO_URL'."
exit 1
}
echo "The repository '$PROJECT_GIT_REPO_URL' has been cloned."
# ╔═════════════════════╦══════════════════════╗
# ║ ║
# ║ MOVE TO PROJECT DIR ║
# ║ ║
# ╚═════════════════════╩══════════════════════╝
cd "$PROJECT_NAME" ||
{
echo "The directory '$PROJECT_NAME' does not exist. Something went wrong."
exit 1
}
# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░
# ░░ ░░
# ░░ ░░
# ░░ INSTALLATION ░░
# ░░ ░░
# ░░ ░░
# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░
echo "Installing '$PROJECT_NAME'..."
# ╔═════════════════════╦══════════════════════╗
# ║ ║
# ║ CREATE THE '.env' FILE ║
# ║ ║
# ╚═════════════════════╩══════════════════════╝
echo "Setting up the '.env' file..."
LOG_LEVEL=6
CRON_JOB_MINUTES=10
CRON_SCHEDULE="*/$CRON_JOB_MINUTES * * * *"
echo
echo "Enter the git repo url for the 'simbashlog' notifier (leave empty if not needed):"
read -r GIT_REPO_URL_FOR_SIMBASHLOG_NOTIFIER ||
{
echo "Failed to read the git repo url for the 'simbashlog' notifier."
exit 1
}
export GIT_REPO_URL_FOR_SIMBASHLOG_NOTIFIER
echo
echo "Enter the absolute path to Satisfactory save games you want to backup:"
read -r SATISFACTORY_SAVE_GAMES_PATH || {
echo "Failed to read the save game path."
exit 1
}
export SATISFACTORY_SAVE_GAMES_PATH
echo
echo "Enter the absolute path to Satisfactory blueprints you want to backup:"
read -r SATISFACTORY_BLUEPRINTS_PATH || {
echo "Failed to read the blueprints path."
exit 1
}
export SATISFACTORY_BLUEPRINTS_PATH
echo
echo "Enter the ssh git url (from '$GIT_HOST_FOR_BACKUP_REPO') that comes up when you want to clone your 'backup' repository with ssh (Starts with 'git@' and ends with '.git'):"
read -r GIT_REPO_URL_FOR_SATISFACTORY_BACKUP || {
echo "Failed to read the ssh git url."
exit 1
}
export GIT_REPO_URL_FOR_SATISFACTORY_BACKUP
echo
echo "Creating the '.env' file..."
cat <<EOF >.env
LOG_LEVEL=$LOG_LEVEL
CRON_SCHEDULE=$CRON_SCHEDULE
GIT_REPO_URL_FOR_SIMBASHLOG_NOTIFIER='$GIT_REPO_URL_FOR_SIMBASHLOG_NOTIFIER'
SATISFACTORY_SAVE_GAMES_PATH=$SATISFACTORY_SAVE_GAMES_PATH
SATISFACTORY_BLUEPRINTS_PATH=$SATISFACTORY_BLUEPRINTS_PATH
GIT_REPO_URL_FOR_SATISFACTORY_BACKUP='$GIT_REPO_URL_FOR_SATISFACTORY_BACKUP'
EOF
cat .env ||
{
echo "Failed to create the '.env' file."
exit 1
}
echo "The '.env' file has been created."
echo
echo "The log level is set to '$LOG_LEVEL'."
echo " (0 = emergency, 1 = alert, 2 = critical, 3 = error, 4 = warning, 5 = notice, 6 = info, 7 = debug)"
echo "The cron job will run every $CRON_JOB_MINUTES minutes."
if [ -n "$GIT_REPO_URL_FOR_SIMBASHLOG_NOTIFIER" ]; then
echo "The git repo url for the 'simbashlog' notifier is set to '$GIT_REPO_URL_FOR_SIMBASHLOG_NOTIFIER'."
else
echo "The git repo url for the 'simbashlog' notifier is not set."
fi
echo "The absolute path to the Satisfactory save games is set to '$SATISFACTORY_SAVE_GAMES_PATH'."
echo "The absolute path to the Satisfactory blueprints is set to '$SATISFACTORY_BLUEPRINTS_PATH'."
echo "The git repo url for the 'backup' repository is set to '$GIT_REPO_URL_FOR_SATISFACTORY_BACKUP'."
echo
# ╔═════════════════════╦══════════════════════╗
# ║ ║
# ║ CREATE NECESSARY FILES FOR VOLUMES ║
# ║ ║
# ╚═════════════════════╩══════════════════════╝
echo "Creating the necessary files for the volumes..."
SSH_DIR="./volumes/ssh"
echo "Creating the ssh directory..."
mkdir -p "$SSH_DIR" ||
{
echo "Failed to create the directory '$SSH_DIR'."
exit 1
}
echo "The directory '$SSH_DIR' has been created."
echo "Generating the ssh key without a passphrase..."
ssh-keygen -t ed25519 -C "$PROJECT_NAME" -f "$SSH_DIR/${PROJECT_NAME}_key" -N "" ||
{
echo "Failed to generate the ssh key."
exit 1
}
echo "The ssh key has been generated."
echo "Creating the ssh config file..."
cat <<EOF >$SSH_DIR/config
Host $GIT_HOST_FOR_BACKUP_REPO
IdentityFile /root/.ssh/${PROJECT_NAME}_key
EOF
cat $SSH_DIR/config ||
{
echo "Failed to create the ssh config file."
exit 1
}
echo "The ssh config file has been created."
echo "Creating the 'known_hosts' file..."
touch $SSH_DIR/known_hosts ||
{
echo "Failed to create the 'known_hosts' file."
exit 1
}
echo "The 'known_hosts' file has been created."
echo "Adding the $GIT_HOST_FOR_BACKUP_REPO key to the 'known_hosts' file..."
ssh-keyscan $GIT_HOST_FOR_BACKUP_REPO >>$SSH_DIR/known_hosts ||
{
echo "Failed to add the $GIT_HOST_FOR_BACKUP_REPO key to the 'known_hosts' file."
exit 1
}
cat $SSH_DIR/known_hosts ||
{
echo "Failed to add the $GIT_HOST_FOR_BACKUP_REPO key to the 'known_hosts' file."
exit 1
}
echo "The $GIT_HOST_FOR_BACKUP_REPO key has been added to the 'known_hosts' file."
# ╔═════════════════════╦══════════════════════╗
# ║ ║
# ║ CLEANUP ║
# ║ ║
# ╚═════════════════════╩══════════════════════╝
# ┌─────────────────────┬──────────────────────┐
# │ GIT ARTIFACTS │
# └─────────────────────┴──────────────────────┘
echo "Removing unnecessary git artifacts..."
rm -rf .git ||
{
echo "Failed to remove the '.git' directory from the project directory."
exit 1
}
echo "The '.git' directory has been removed from the project directory."
rm install.sh ||
{
echo "Failed to remove the install script from the project directory."
exit 1
}
echo "The install script has been removed from the project directory."
# ┌─────────────────────┬──────────────────────┐
# │ INSTALL SCRIPT │
# └─────────────────────┴──────────────────────┘
echo "Removing the current install script..."
rm ../install.sh ||
{
echo "Failed to remove the current install script."
exit 1
}
echo "The current install script has been removed."
# ╔═════════════════════╦══════════════════════╗
# ║ ║
# ║ FINAL EXECUTIONS ║
# ║ ║
# ╚═════════════════════╩══════════════════════╝
echo
echo "Add the public key to your 'backup' repository's ('$GIT_REPO_URL_FOR_SATISFACTORY_BACKUP') deploy keys!"
echo "In GitHub, go to your repository -> Settings -> Deploy keys -> Add deploy key -> Paste the public key from the file '$SSH_DIR/${PROJECT_NAME}_key.pub' -> Allow write access -> Add key"
echo "Add as title '$PROJECT_NAME'"
echo "The deploy key is:"
cat "$SSH_DIR/${PROJECT_NAME}_key.pub" ||
{
echo "Failed to show the public key."
exit 1
}
check_public_key_added() {
echo "Is the public key added to your 'backup' repository's ('$GIT_REPO_URL_FOR_SATISFACTORY_BACKUP') deploy keys? (y/n)"
read -r IS_PUBLIC_KEY_ADDED || {
echo "Failed to read if the public key is added."
exit 1
}
if [ "$IS_PUBLIC_KEY_ADDED" = "y" ]; then
echo "Public key is added. Continuing..."
elif [ "$IS_PUBLIC_KEY_ADDED" = "n" ]; then
echo "The public key is not added to your 'backup' repository's deploy keys. Please add it otherwise the backup will not work."
else
echo "Invalid input. Please enter 'y' for yes or 'n' for no."
check_public_key_added
fi
return 0
}
check_public_key_added
echo
echo "Running the Docker container for '$PROJECT_NAME'..."
docker compose up -d ||
{
echo "Failed to run the Docker container."
exit 1
}
echo "The Docker container is running."
echo "The installation for '$PROJECT_NAME' is complete."
echo
echo "INFO: If a 'simbashlog' notifier is set and you have to configure it, you have to shut down the container, adjust the configuration and restart the container."