Skip to content

adding factorio mods downloader tool #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## Changelog

### V2.5.0

#### Breaking Changes

- Reformatted the mods section to allow users to specify a mod version so it wont try to pull the latest if you don't want it too. This refactor introduces a custom tool [factorio-mod-downloader](https://github.com/SQLJames/factorio-mod-downloader) as an image, which is a simple golang tool, which simplifies the script and improves logging.

### V2.4.1

#### Non-Breaking Changes
Expand Down
2 changes: 1 addition & 1 deletion charts/factorio-server-charts/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ sources:
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 2.4.1
version: 2.5.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
Expand Down
8 changes: 4 additions & 4 deletions charts/factorio-server-charts/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ spec:
- -ec
- |
mkdir -p /factorio/configs
mkdir -p /factorio/mods
mkdir -p /factorio/config
echo $VERSION > /factorio/mods/factorioVersion
cp --verbose /deployed-configs/* /factorio/configs
if [ -f "/rcon-pw/rconpw" ]; then
cp --verbose /rcon-pw/rconpw /factorio/configs/rconpw
Expand Down Expand Up @@ -82,14 +84,12 @@ spec:
{{- end }}
{{- if .Values.mods.enabled }}
- name: download-factorio-mods
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
image: "{{ .Values.mods.image.repository }}:{{ .Values.mods.image.tag }}"
imagePullPolicy: {{ .Values.mods.image.pullPolicy }}
command:
- /bin/bash
- -ecx
- |
mkdir -p /factorio/mods
ls -alth /scripts
echo "Running mod-downloader.sh script..."
bash /scripts/mod-downloader.sh
echo "Finished mod-downloader.sh script"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,103 +19,28 @@ data:
exit 0
fi
modDir=/factorio/mods
MOD_BASE_URL="https://mods.factorio.com"
declare -a officialMods
officialMods=(
{{- range .Values.mods.portal }}
{{ . }}
{{- end }}
)
declare -A unofficialMods
{{- range .Values.mods.unofficial }}
unofficialMods[{{ .name | quote}}]={{ .url | quote}}
{{- end }}
function print_step()
{
echo "$1"
}
function print_success()
{
echo "$1"
}
function print_failure()
{
echo "$1"
}
function downloadUnofficial() {
cd $modDir;curl -L -o $2 $1
}
function downloadofficial() {
MOD_NAME="$1"
MOD_NAME_ENCODED="${1// /%20}"
if [[ -z ${USERNAME:-} ]]; then
USERNAME="$(cat /account/username)"
fi

if [[ -z ${TOKEN:-} ]]; then
TOKEN="$(cat /account/token)"
fi

if [[ -z ${USERNAME:-} ]]; then
echo "You need to provide your Factorio username to update mods."
fi

if [[ -z ${TOKEN:-} ]]; then
echo "You need to provide your Factorio token to update mods."
fi
MOD_INFO_URL="$MOD_BASE_URL/api/mods/$MOD_NAME_ENCODED"
MOD_INFO_JSON=$(curl --silent "$MOD_INFO_URL")
# echo "$MOD_INFO_URL $MOD_INFO_JSON"
if ! echo "$MOD_INFO_JSON" | jq -e .name >/dev/null; then
print_success " Custom mod not on $MOD_BASE_URL, skipped."
return 0
fi
MOD_INFO=$(echo "$MOD_INFO_JSON" | jq -j --arg version "$VERSION" ".releases|reverse|map(select(.info_json.factorio_version as \$mod_version | \$version | startswith(\$mod_version)))[0]|.file_name, \";\", .download_url, \";\", .sha1")
echo $MOD_INFO
MOD_FILENAME=$(echo "$MOD_INFO" | cut -f1 -d";")
MOD_URL=$(echo "$MOD_INFO" | cut -f2 -d";")
MOD_SHA1=$(echo "$MOD_INFO" | cut -f3 -d";")
if [[ $MOD_FILENAME == null ]]; then
print_failure " Not compatible with version"
return 0
fi
print_step "Downloading..."
FULL_URL="$MOD_BASE_URL$MOD_URL?username=$USERNAME&token=$TOKEN"
echo $FULL_URL
HTTP_STATUS=$(curl --silent -L -w "%{http_code}" -o "$modDir/$MOD_FILENAME" "$FULL_URL")

if [[ $HTTP_STATUS != 200 ]]; then
print_failure " Download failed: Code $HTTP_STATUS."
rm -f "$modDir/$MOD_FILENAME"
return 1
fi

if [[ ! -f $modDir/$MOD_FILENAME ]]; then
print_failure " Downloaded file missing!"
return 1
fi
FACTORIOVERSION="$(cat /factorio/mods/factorioVersion)"
USERNAME="$(cat /account/username)"
TOKEN="$(cat /account/token)"
if [[ -z ${USERNAME:-} ]]; then
echo "You need to provide your Factorio username to update mods."
fi

if ! [[ $(sha1sum "$modDir/$MOD_FILENAME") =~ $MOD_SHA1 ]]; then
print_failure " SHA1 mismatch!"
rm -f "$modDir/$MOD_FILENAME"
return 1
if [[ -z ${TOKEN:-} ]]; then
echo "You need to provide your Factorio token to update mods."
fi
{{- range .Values.mods.portal }}
if [ -z {{ .version | quote }} ]; then
/usr/local/bin/factorio-mod-downloader download official --name {{ .name | quote}} --destination $modDir --factorioVersion $FACTORIOVERSION --user $USERNAME --token $TOKEN
else
/usr/local/bin/factorio-mod-downloader download official --name {{ .name | quote}} --destination $modDir --factorioVersion $FACTORIOVERSION --user $USERNAME --token $TOKEN --version {{ .version | quote}}
fi
{{- end }}

{{- range .Values.mods.unofficial }}
/usr/local/bin/factorio-mod-downloader download unofficial --url {{ .url | quote}} --name {{ .name | quote}} --destination $modDir
{{- end }}

print_success " Download complete."
}
mkdir -p $modDir
for key in "${!unofficialMods[@]}"; do
downloadUnofficial "${unofficialMods[$key]}" $key
done

if [ -f "/account/username" ]; then
if [ -f "/account/token" ]; then
echo "server is running version $VERSION"
for officialMod in ${officialMods[*]}; do
downloadofficial $officialMod $USERNAME $TOKEN
done
fi
fi
# Mark the update as complete by creating the flag file
touch "$FLAG_FILE"
echo "Mod update completed."
Expand Down
13 changes: 10 additions & 3 deletions charts/factorio-server-charts/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,19 @@ persistence:
## @param mods.portal List of official mods to be downloaded from Factorio Mod Portal
## @param mods.unofficial List of unofficial mods name and url to download into the mods folder
mods:
image:
repository: "ghcr.io/sqljames/factorio-mod-downloader"
## Container image pull policy
pullPolicy: Always
## Overrides the image tag whose default is the chart appVersion.
tag: "1.0.1"
## You should set an fix version, i.e.:
# tag: "1.1.37"
enabled: false
# in order to use the mods portal you will need to specify the username and token in the server_settings.
portal: []
# - Krastorio2
# - StorageTank2_Updated
# - early-robots
# - name: factorissimo-2-notnotmelon
# version: 3.5.11
# unofficial section is meant to just allow you to download and place folders into the mods folder.
# we will not check version compatibility automatically with these downloads.
# you can encounter an error if the file names dont match what the mod is expecting for example
Expand Down
Loading