Skip to content

Commit 28598a4

Browse files
SuperSandro2000fank
authored andcommitted
1 parent 4ba56ee commit 28598a4

6 files changed

Lines changed: 122 additions & 4 deletions

File tree

0.17/Dockerfile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ ENV PORT=34197 \
2020
PGID="$PGID"
2121

2222
RUN mkdir -p /opt /factorio && \
23-
apk add --update --no-cache pwgen su-exec binutils gettext libintl shadow && \
24-
apk add --update --no-cache --virtual .build-deps curl && \
23+
apk add --update --no-cache pwgen su-exec binutils gettext libintl shadow curl jq && \
2524
curl -sSL "https://www.factorio.com/get-download/$VERSION/headless/linux64" \
2625
-o /tmp/factorio_headless_x64_$VERSION.tar.xz && \
2726
echo "$SHA1 /tmp/factorio_headless_x64_$VERSION.tar.xz" | sha1sum -c && \
@@ -32,7 +31,6 @@ RUN mkdir -p /opt /factorio && \
3231
ln -s "$MODS" /opt/factorio/mods && \
3332
ln -s "$SCENARIOS" /opt/factorio/scenarios && \
3433
ln -s "$SCRIPTOUTPUT" /opt/factorio/script-output && \
35-
apk del .build-deps && \
3634
addgroup -g "$PGID" -S "$GROUP" && \
3735
adduser -u "$PUID" -G "$GROUP" -s /bin/sh -SDH "$USER" && \
3836
chown -R "$USER":"$GROUP" /opt/factorio /factorio

0.17/docker-compose.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ services:
1010
# environment:
1111
# - PUID=1000
1212
# - PGID=1000
13+
# - UPDATE_MODS_ON_START=true
14+
# - USERNAME=FactorioUsername
15+
# - TOKEN=FactorioToken

0.17/files/docker-entrypoint.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh -x
1+
#!/bin/bash -x
22
set -euo pipefail
33

44
id
@@ -35,6 +35,10 @@ if [ "$NRTMPSAVES" -gt 0 ]; then
3535
rm -f "$SAVES"/*.tmp.zip
3636
fi
3737

38+
if [[ $UPDATE_MODS_ON_START ]]; then
39+
./docker-update-mods.sh
40+
fi
41+
3842
if [ "$(id -u)" = '0' ]; then
3943
# Update the User and Group ID based on the PUID/PGID variables
4044
usermod -o -u "$PUID" factorio

0.17/files/docker-update-mods.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
if [[ -f /run/secrets/username ]]; then
4+
USERNAME=$(cat /run/secrets/username)
5+
fi
6+
7+
if [[ -f /run/secrets/username ]]; then
8+
TOKEN=$(cat /run/secrets/token)
9+
fi
10+
11+
if [[ -z $TOKEN ]]; then
12+
set -- "$(jq -j ".username, \" \", .token" "$CONFIG/server-settings.json")"
13+
USERNAME=$1
14+
TOKEN=$2
15+
fi
16+
17+
./update-mods.sh "$VERSION" "$MODS" "$USERNAME" "$TOKEN"

0.17/files/update-mods.sh

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/bin/bash
2+
3+
FACTORIO_VERSION=$1
4+
MOD_DIR=$2
5+
USERNAME=$3
6+
TOKEN=$4
7+
8+
MOD_BASE_URL="https://mods.factorio.com"
9+
10+
print_step()
11+
{
12+
echo "$1"
13+
}
14+
15+
print_success()
16+
{
17+
echo "$1"
18+
}
19+
20+
print_failure()
21+
{
22+
echo "$1"
23+
}
24+
25+
update_mod()
26+
{
27+
MOD_NAME="$1"
28+
29+
print_step "Checking for update of mod $MOD_NAME..."
30+
31+
MOD_INFO_URL="$MOD_BASE_URL/api/mods/$MOD_NAME"
32+
MOD_INFO_JSON=$(curl --silent "$MOD_INFO_URL")
33+
34+
MOD_INFO=$(echo "$MOD_INFO_JSON" | jq -j --arg version "$FACTORIO_VERSION" ".releases|reverse|map(select(.info_json.factorio_version as \$mod_version | \$version | startswith(\$mod_version)))[0]|.file_name, \";\", .download_url, \";\", .sha1")
35+
36+
MOD_FILENAME=$(echo "$MOD_INFO" | cut -f1 -d";")
37+
MOD_URL=$(echo "$MOD_INFO" | cut -f2 -d";")
38+
MOD_SHA1=$(echo "$MOD_INFO" | cut -f3 -d";")
39+
40+
if [[ -z $MOD_URL ]]; then
41+
return 1
42+
fi
43+
44+
if [[ $MOD_FILENAME == null ]]; then
45+
print_failure " Not compatible with version"
46+
return 1
47+
fi
48+
49+
if [[ -f $MOD_DIR/$MOD_FILENAME ]]; then
50+
print_success " Already up-to-date."
51+
return 0
52+
fi
53+
54+
print_step "Downloading..."
55+
FULL_URL="$MOD_BASE_URL$MOD_URL?username=$USERNAME&token=$TOKEN"
56+
HTTP_STATUS=$(curl --silent -L -w "%{http_code}" -o "$MOD_DIR/$MOD_FILENAME" "$FULL_URL")
57+
58+
if [[ $HTTP_STATUS != 200 ]]; then
59+
print_failure " Download failed: Code $HTTP_STATUS."
60+
rm "$MOD_DIR/$MOD_FILENAME"
61+
return 1
62+
fi
63+
64+
if [[ ! -f $MOD_DIR/$MOD_FILENAME ]]; then
65+
print_failure " Downloaded file missing!"
66+
return 1
67+
fi
68+
69+
set -- "$(sha1sum "$MOD_DIR/$MOD_FILENAME")"
70+
if [[ $1 != "$MOD_SHA1" ]]; then
71+
print_failure " SHA1 mismatch!"
72+
rm "$MOD_DIR/$MOD_FILENAME"
73+
return 1
74+
fi
75+
76+
print_success " Download complete."
77+
78+
for file in "$MOD_DIR/${MOD_NAME}_"*".zip"; do # wildcard does usually not work in quotes: https://unix.stackexchange.com/a/67761
79+
if [[ $file != $MOD_DIR/$MOD_FILENAME ]]; then
80+
print_success " Deleting old version: $file"
81+
rm "$file"
82+
fi
83+
done
84+
85+
return 0
86+
}
87+
88+
if [[ -f $MOD_DIR/mod-list.json ]]; then
89+
jq -r ".mods|map(select(.enabled))|.[].name" "$MOD_DIR/mod-list.json" | while read -r mod; do
90+
if [[ $mod != base ]]; then
91+
update_mod "$mod"
92+
fi
93+
done
94+
fi

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ To generate a new map stop the server, delete all of the saves and restart the s
112112

113113
Copy mods into the mods folder and restart the server.
114114

115+
As of 0.17 a new environment variable was added ``UPDATE_MODS_ON_START`` which if set to ``true`` will cause the mods get to updated on server start. If set a valid [Factorio Username and Token](https://www.factorio.com/profile) must be supplied or else the server will not start. They can either be set as docker secrets, environment variables, or pulled from the server-settings.json file.
116+
115117

116118
## Scenarios
117119

0 commit comments

Comments
 (0)