From cda4dc36bcd37425832b43d9a4206cb514587d43 Mon Sep 17 00:00:00 2001 From: James Rhoat Date: Thu, 13 Jan 2022 13:26:05 -0500 Subject: [PATCH] updating to allow users to add mods --- charts/factorio-server-charts/Chart.yaml | 2 +- charts/factorio-server-charts/README.md | 24 ++++ .../templates/deployment.yaml | 22 +++- .../templates/mod-downloader-configmap.yaml | 111 ++++++++++++++++++ charts/factorio-server-charts/values.yaml | 20 +++- 5 files changed, 174 insertions(+), 5 deletions(-) create mode 100644 charts/factorio-server-charts/templates/mod-downloader-configmap.yaml diff --git a/charts/factorio-server-charts/Chart.yaml b/charts/factorio-server-charts/Chart.yaml index 6a86e24..94ad8c3 100644 --- a/charts/factorio-server-charts/Chart.yaml +++ b/charts/factorio-server-charts/Chart.yaml @@ -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: 1.0.10 +version: 1.0.11 # 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 diff --git a/charts/factorio-server-charts/README.md b/charts/factorio-server-charts/README.md index 501d6fb..3caba2c 100644 --- a/charts/factorio-server-charts/README.md +++ b/charts/factorio-server-charts/README.md @@ -249,5 +249,29 @@ https://github.com/kubernetes/kubernetes/blob/59876df736c41093363f4c198aeec05e29 Releases are published using the official helm release action in github. https://github.com/helm/chart-releaser-action +## installing mods + +change enabled to true, follow comments below. + +If the factorio server doesn't start, check that the logs don't have an error with the mods. They are pretty verbose +``` +mods: + enabled: true +# in order to use the mods portal you will need to specify the username and token in the server_settings. +# name is determined by the url, it will be the last part of the url, not the title of the mod. + portal: + - Krastorio2 + - StorageTank2_Updated + - early-robots +# 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 +#Error Util.cpp:83: Failed to load mod "Squeak-Through": Filename of mod +# /factorio/mods/Squeak-Through.zip doesn't match the expected Squeak Through_1.8.2.zip (case sensitive!) + unofficial: + - url: "https://github.com/Suprcheese/Squeak-Through/archive/refs/tags/1.8.2.zip" + name: "Squeak Through_1.8.2.zip" +``` + ## Readme Readme was generated from the [chart-doc-gen](https://github.com/kubepack/chart-doc-gen) tool. \ No newline at end of file diff --git a/charts/factorio-server-charts/templates/deployment.yaml b/charts/factorio-server-charts/templates/deployment.yaml index 1511282..9d99188 100644 --- a/charts/factorio-server-charts/templates/deployment.yaml +++ b/charts/factorio-server-charts/templates/deployment.yaml @@ -41,6 +41,24 @@ spec: mountPath: /factorio - name: {{ template "factorio-server-charts.fullname" . }}-serversettingsconfig mountPath: /deployed-configs + {{- if .Values.mods.enabled }} + - name: download-factorio-mods + image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + command: + - /bin/bash + - -ec + - | + mkdir -p /factorio/mods + bash /scripts/mod-downloader.sh + securityContext: + runAsUser: 0 + volumeMounts: + - name: datadir + mountPath: /factorio + - name: {{ template "factorio-server-charts.fullname" . }}-mod-downloader-configmap + mountPath: /scripts + {{- end }} containers: - name: {{ template "factorio-server-charts.fullname" . }} image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" @@ -90,4 +108,6 @@ spec: - name: {{ template "factorio-server-charts.fullname" . }}-serversettingsconfig configMap: name: {{ template "factorio-server-charts.fullname" . }}-serversettingsconfig - + - name: {{ template "factorio-server-charts.fullname" . }}-mod-downloader-configmap + configMap: + name: {{ template "factorio-server-charts.fullname" . }}-mod-downloader-configmap diff --git a/charts/factorio-server-charts/templates/mod-downloader-configmap.yaml b/charts/factorio-server-charts/templates/mod-downloader-configmap.yaml new file mode 100644 index 0000000..4937176 --- /dev/null +++ b/charts/factorio-server-charts/templates/mod-downloader-configmap.yaml @@ -0,0 +1,111 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ template "factorio-server-charts.fullname" . }}-mod-downloader-configmap + labels: + app: {{ template "factorio-server-charts.fullname" . }} + chart: {{ template "factorio-server-charts.fullname" . }} + release: "{{ .Release.Name }}" + heritage: "{{ .Release.Service }}" +data: + #credit to the factoriotools/factorio-docker team, most of this logic came from them + # https://github.com/factoriotools/factorio-docker/blob/master/docker/files/update-mods.sh + mod-downloader.sh: | + 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() { + if [[ -z ${USERNAME:-} ]]; then + USERNAME="$(jq -j ".username" "/factorio/configs/server-settings.json")" + fi + + if [[ -z ${TOKEN:-} ]]; then + TOKEN="$(jq -j ".token" "/factorio/configs/server-settings.json")" + 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/$1" + 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 + + if ! [[ $(sha1sum "$modDir/$MOD_FILENAME") =~ $MOD_SHA1 ]]; then + print_failure " SHA1 mismatch!" + rm -f "$modDir/$MOD_FILENAME" + return 1 + fi + + print_success " Download complete." + } + mkdir -p $modDir + for key in "${!unofficialMods[@]}"; do + downloadUnofficial "${unofficialMods[$key]}" $key + done + {{- if and .Values.server_settings.username .Values.server_settings.token }} + echo "server is running version $VERSION" + for officialMod in ${officialMods[*]}; do + downloadofficial $officialMod $USERNAME $TOKEN + done + {{- end }} + + + + + diff --git a/charts/factorio-server-charts/values.yaml b/charts/factorio-server-charts/values.yaml index e7291e5..a2d5863 100644 --- a/charts/factorio-server-charts/values.yaml +++ b/charts/factorio-server-charts/values.yaml @@ -21,7 +21,7 @@ image: ## The best way ist to define a ClusterIP service and define one or more externalIPs. Of course this IPs must be avaiable on the node the factorio runs the pod! service: type: LoadBalancer - port: 34197 + port: 30000 ## If you are able in your cluster to map an external IP, set it here # externalIPs: # - "192.168.0.1" @@ -30,8 +30,7 @@ service: # nodePort: "" ## Examples: ## NodePort setup - # type: NodePort - # nodePort: "" + ## ClusterIp setup # ClusterIP: # port: 34197 @@ -90,6 +89,21 @@ persistence: #### factorio application configuration #### +mods: + 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 +# 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 +#Error Util.cpp:83: Failed to load mod "Squeak-Through": Filename of mod +# /factorio/mods/Squeak-Through.zip doesn't match the expected Squeak Through_1.8.2.zip (case sensitive!) + unofficial: + - url: "https://github.com/Suprcheese/Squeak-Through/archive/refs/tags/1.8.2.zip" + name: "Squeak Through_1.8.2.zip" factorioServer: # specify a save name