-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactorioctl
More file actions
executable file
·128 lines (112 loc) · 3.2 KB
/
factorioctl
File metadata and controls
executable file
·128 lines (112 loc) · 3.2 KB
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
#! /bin/bash
LOGFILE=/var/log/factorioctl.log
function log(){
echo "$1" | tee -a "$LOGFILE"
}
function create_service() {
cat > /etc/systemd/system/factorio.service <<- EOF
[Unit]
Description=Factorio Headless Server
[Service]
Type=simple
User=factorio
ExecStart=-/srv/factorio/factorio --start-server /srv/factorio/saves/map.zip --server-settings /srv/factorio/data/server-settings.json
[Install]
WantedBy=multi-user.target
EOF
}
function backup() {
log "Backing up save files"
mkdir /srv/factorio/saves.bak
cp /srv/factorio/saves/* /srv/factorio/saves.bak
}
function uninstall(){
log "Removing systemd service"
systemctl disable --now factorio
rm /etc/systemd/system/factorio.service
log "Removing factorio server"
rm -rf /srv/factorio
log "Removing user and group"
deluser factorio
}
function upgrade(){
log "Stopping server"
systemctl stop factorio
backup
log "Downloading version $1"
URL="https://factorio.com/get-download/$1/headless/linux64"
wget $URL -O factorio.tar.gz
log "Extracting to /srv/factorio"
tar -xf factorio.tar.gz -C /srv
sudo chown -R factorio:factorio /srv/factorio
log "Starting server"
systemctl start factorio
rm factorio.tar.gz
log "Done"
}
function stop() {
log "Stopping server"
systemctl stop factorio
}
function start() {
log "Starting server"
systemctl start factorio
}
function reinstall() {
uninstall
log "Downloading version $1"
URL="https://factorio.com/get-download/$1/headless/linux64"
wget $URL -O factorio.tar.gz
log "Extracting to /srv/factorio"
tar -xf factorio.tar.gz -C /srv
ln -s /srv/factorio/bin/x64/factorio /srv/factorio/factorio
log "Copying settings"
cp /srv/factorio/data/server-settings.example.json /srv/factorio/data/server-settings.json
cp /srv/factorio/data/map-gen-settings.example.json /srv/factorio/data/map-gen-settings.json
log "Generating map"
/srv/factorio/factorio --create /srv/factorio/saves/map.zip --map-gen-settings /srv/factorio/data/map-gen-settings.json
log "Adding user"
adduser --disabled-login --no-create-home --gecos factorio factorio
sudo chown -R factorio:factorio /srv/factorio
log "Starting server"
create_service
systemctl enable --now factorio
rm factorio.tar.gz
log "Server reinstalled"
}
function show_help() {
echo "Usage: factorioctl <uninstall|remove|upgrade|stop|start|restart|reinstall> [version]"
echo "Version is required for upgrade and reinstall"
echo "Uninstall removes the server"
echo "Remove deletes this tool"
echo "Reinstall WILL DELETE ALL OF THE SERVER DATA"
echo "Upgrade won't delete server data"
}
if [ "$USER" != "root" ]; then
log "Must run as root"
exit 1
fi
echo "Logs will go to $LOGFILE"
log "Factorioctl $(date)"
if [ "$1" == "uninstall" ]; then
uninstall
log "To remove factorioctl run factorioctl remove"
log "To reinstall the server run factorioctl reinstall <verion>"
elif [ "$1" == "remove" ]; then
rm /sbin/factorioctl
rm /var/log/factorioctl.log
echo "Removed factorioctl"
elif [ "$1" == "upgrade" ]; then
upgrade "$2"
elif [ "$1" == "stop" ]; then
stop
elif [ "$1" == "start" ]; then
start
elif [ "$1" == "restart" ]; then
stop
start
elif [ "$1" == "reinstall" ]; then
reinstall "$2"
else
show_help
fi