-
Notifications
You must be signed in to change notification settings - Fork 0
/
norse.sh
390 lines (288 loc) · 8.29 KB
/
norse.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/bin/bash
function isPortBinded {
STATUS=$(nc -z 127.0.0.1 $1 && echo "USE" || echo "FREE")
if [[ $STATUS == "USE" ]]; then
true
return
else
false
return
fi
}
function isSessionRunning {
tmux has-session -t $1 2>/dev/null
if [[ $? = 0 ]]; then
true
return
else
false
return
fi
}
function isServerRunning {
if doesFileExist "$DIR/.offline"; then
false
return
else
true
return
fi
}
doesFileExist(){
file=$1
if test -f "$file"; then
true
return
else
false
return
fi
}
doesFolderExist(){
folder=$1
if test -d "$folder"; then
true
return
else
false
return
fi
}
function setServerState {
source norse.config
if [ $1 = "offline" ]; then
touch $DIR/.offline 2>/dev/null
elif [ $1 = "online" ]; then
rm -rf $DIR/.offline 2>/dev/null
else
echo "Unknown argument!"
fi
}
function logGood {
echo "[ $(tput setaf 2)SUCCESS$(tput sgr 0) ] $1"
}
function logNeutral {
echo "[ $(tput setaf 3).....$(tput sgr 0) ] $1"
}
function logBad {
echo "[ $(tput setaf 1)ERROR$(tput sgr 0) ] $1 "
}
function haltServer {
tmux send-keys -t valheim-$ID C-c
}
function killServer {
tmux kill-session -t $1 2>/dev/null
}
function bootServer {
tmux new -d -s $1 ./norse.sh bootServerLoop
}
function bootServerLoop {
source norse.config
cd $DIR/serverfiles/valheim_server || exit
i=0
while [ $i -lt 4 ]
do
echo "Server started"
rm -rf $DIR/.offline
export templdpath=$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=./linux64:$LD_LIBRARY_PATH
export SteamAppId=892970
# Tip: Make a local copy of this script to avoid it being overwritten by steam.
# NOTE: Minimum password length is 5 characters & Password cant be in the server name.
# NOTE: You need to make sure the ports 2456-2458 is being forwarded to your server through your local router & firewal>
./valheim_server.x86_64 -name "$NAME" -port 2456 -world "$WORLD" -password "$PASS" -public "$PUBLIC" -savedir "$DIR/serverfiles"
export LD_LIBRARY_PATH=$templdpath
touch $DIR/.offline
echo "Server stopped"
sleep 120;
i=$[$i+1]
done
}
# Functions for controlling the server
textclear(){
tput rc
tput ed
}
setup() {
#!/bin/bash
clear
DIR=$PWD
clear
rm -rf $DIR/norse.config 2>/dev/null
touch $DIR/norse.config
mkdir $DIR/serverfiles 2>/dev/null
mkdir $DIR/backups 2>/dev/null
mkdir $DIR/bin 2>/dev/null
mkdir $DIR/serverfiles/valheim_server 2>/dev/null
mkdir $DIR/assets 2>/dev/null
cd $DIR/serverfiles/assets && curl -L -o logo.txt "https://raw.githubusercontent.com/z3orc/norse/main/logo.txt" --progress-bar 2>/dev/null
cd $DIR/serverfiles 2>/dev/null
clear && cat $DIR/assets/logo.txt && echo -e "\n"
IFS= read -r -p "Server name: " NAME
IFS= read -r -p "World name: " WORLD
IFS= read -r -p "Password: " PASS
IFS= read -r -p "Show server in community server browser (y/n): " PUBLIC
tput sc
ID=$RANDOM
PORT=2456
tput sc
#Writings settings to settingsfile
echo DIR="${DIR// /}" >> $DIR/norse.config | xargs
echo NAME="${NAME// /}" >> $DIR/norse.config | xargs
echo WORLD="${WORLD// /}" >> $DIR/norse.config | xargs
echo PASS="${PASS// /}" >> $DIR/norse.config | xargs
echo ID="${ID// /}" >> $DIR/norse.config | xargs
if [[ $PUBLIC == "y" || $PUBLIC == "Y" ]]; then
echo PUBLIC="1" >> $DIR/norse.config | xargs
else
echo PUBLIC="0" >> $DIR/norse.config | xargs
fi
textclear
clear && cat $DIR/assets/logo.txt && echo -e "\n"
echo " [Downloading dependencies]"
echo "----------------------------------------------------"
sleep 2
cd $DIR/bin && mkdir ./steamcmd && cd ./steamcmd
echo "[ $(tput setaf 3).....$(tput sgr 0) ] Downloading SteamCMD"
tput sc
curl -sqL "https://steamcdn-a.akamaihd.net/client/installer/steamcmd_linux.tar.gz" --progress-bar | tar zxvf -
rm -rf steamcmd_linux.tar.gz
textclear
./steamcmd.sh +quit
echo "[ $(tput setaf 2)SUCCESS$(tput sgr 0) ] SteamCMD downloaded"
sleep 2
echo "[ $(tput setaf 3).....$(tput sgr 0) ] Downloading Valheim server"
tput sc
./steamcmd.sh +@sSteamCmdForcePlatformType linux +force_install_dir $DIR/serverfiles/valheim_server +login anonymous +app_update 896660 validate +quit
textclear
echo "[ $(tput setaf 2)SUCCESS$(tput sgr 0) ] Valheim server downloaded."
sleep 2
echo "[ $(tput setaf 3).....$(tput sgr 0) ] Validating server integrity"
cd $DIR
tput sc
./norse.sh start
sleep 15
textclear
STATUS=$(nc -z 127.0.0.1 2456 && echo "USE" || echo "FREE")
tmux has-session -t valheim-$ID 2>/dev/null
if [[ $? = 0 ]]; then
echo "[ $(tput setaf 2)SUCCESS$(tput sgr 0) ] Server integrity validated"
exit
else
echo "[ $(tput setaf 1)ERROR$(tput sgr 0) ] Could not validate server integrity, server did not boot."
exit
fi
}
function start {
#!/bin/bash
source norse.config
if [[ $1 = "" ]]; then
logNeutral "Starting server!"
bootServer valheim-$ID
sleep 5
i=0
while [ $i -lt 60 ];
do
if isSessionRunning valheim-$ID && isServerRunning; then
logGood "Server booted successfully"
setServerState online
exit 0
else
echo "..."
i=$[$i+1]
sleep 2
fi
done
logBad "Could not boot server."
setServerState offline
killServer valheim-$ID
fi
}
function stop {
#!/bin/bash
source norse.config
logNeutral "Stopping server."
haltServer valheim-$ID
i=0
while [[ $i -lt 60 ]];
do
if ! isServerRunning; then
logGood "Server halted successfully"
killServer valheim-$ID
if ! isSessionRunning valheim-$ID; then
break
fi
else
echo "..."
i=$[$i+1]
sleep 2
fi
done
if isSessionRunning valheim-$ID; then
logBad "Could not halt server."
setServerState online
fi
}
function backup {
#!/bin/bash
source norse.config
function save {
logNeutral "Starting backups process and saving world. This might cause server instability"
logNeutral "Running rdiff-backup."
nice -n 10 rdiff-backup ./serverfiles ./backups
logGood "rdiff-backup complete."
logNeutral "Removing old backups."
nice -n 10 rdiff-backup --force --remove-older-than 2W $DIR/backups
logGood "Old backups removed."
logGood "Backup process complete."
}
if [[ $1 = "list" ]]; then
nice -n 10 rdiff-backup --list-increments $DIR/backups
elif [[ $1 = "revert" ]]; then
rdiff-backup -r now $DIR/backups $DIR/serverfiles
else
if isSessionRunning valheim-$ID; then
stop && save && start
else
save && start
fi
fi
}
function upgrade {
#!/bin/bash
source norse.config
logNeutral "Updating server"
stop
$DIR/bin/steamcmd/steamcmd.sh +@sSteamCmdForcePlatformType linux +force_install_dir $DIR/serverfiles/valheim_server +login anonymous +app_update 896660 +quit 2>/dev/null
logGood "Update process complete."
start
}
function console {
source norse.config
tmux a -t valheim-$ID
}
case "$1" in
setup)
setup
;;
start)
start "${2}"
;;
bootServerLoop)
bootServerLoop
;;
stop)
stop
;;
backup)
backup "${2}"
;;
upgrade)
upgrade "${2}"
;;
console)
console
;;
*)
echo "Usage: ./norse.sh {start|stop|upgrade|backup|console|setup}"
esac