Skip to content

Commit 6e77204

Browse files
author
zone-zero
committed
add save-importer init container
1 parent a22ba07 commit 6e77204

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

charts/factorio-server-charts/templates/deployment.yaml

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,27 @@ spec:
102102
- name: account-data
103103
mountPath: /account
104104
{{- end }}
105-
{{- end }}
105+
{{- end }}
106+
{{- if .Values.factorioServer.import_save }}
107+
- name: import-factorio-save
108+
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
109+
imagePullPolicy: {{ .Values.image.pullPolicy }}
110+
command:
111+
- /bin/bash
112+
- -ec
113+
- |
114+
mkdir -p /factorio/save_to_import
115+
bash /scripts/save-importer.sh
116+
{{- with .Values.securityContext }}
117+
securityContext:
118+
{{- toYaml . | nindent 12 }}
119+
{{- end }}
120+
volumeMounts:
121+
- name: datadir
122+
mountPath: /factorio
123+
- name: {{ template "factorio-server-charts.fullname" . }}-save-importer-configmap
124+
mountPath: /scripts
125+
{{- end }}
106126
containers:
107127
- name: {{ template "factorio-server-charts.fullname" . }}
108128
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
@@ -192,6 +212,6 @@ spec:
192212
- name: {{ template "factorio-server-charts.fullname" . }}-serversettingsconfig
193213
configMap:
194214
name: {{ template "factorio-server-charts.fullname" . }}-serversettingsconfig
195-
- name: {{ template "factorio-server-charts.fullname" . }}-mod-downloader-configmap
215+
- name: {{ template "factorio-server-charts.fullname" . }}-scripts-configmap
196216
configMap:
197-
name: {{ template "factorio-server-charts.fullname" . }}-mod-downloader-configmap
217+
name: {{ template "factorio-server-charts.fullname" . }}-scripts-configmap
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: {{ template "factorio-server-charts.fullname" . }}-save-importer-configmap
5+
labels:
6+
app: {{ template "factorio-server-charts.fullname" . }}
7+
chart: {{ template "factorio-server-charts.fullname" . }}
8+
release: "{{ .Release.Name }}"
9+
heritage: "{{ .Release.Service }}"
10+
data:
11+
save-importer.sh: |
12+
message_prefix="save-importer.sh:"
13+
source_dir="/factorio/save_to_import"
14+
target_dir="/factorio/saves"
15+
target_filename="{{ .Values.factorioServer.save_name }}.zip"
16+
17+
18+
# Check if the source dir exists
19+
if [ ! -d "$source_dir" ]; then
20+
mkdir -p "$source_dir"
21+
fi
22+
23+
# Count the number of save files in the source dir
24+
# luckily the factorio docker image incldues find
25+
count=$(find "$source_dir" -maxdepth 1 -type f -name "*.zip" | wc -l)
26+
27+
if [ "$count" -eq 1 ]; then
28+
save_file=$(find "$source_dir" -maxdepth 1 -type f -name "*.zip")
29+
echo "$message_prefix Found save file to import - '$save_file'."
30+
echo "$message_prefix Starting save import process."
31+
# Copy and rename the file
32+
cp -f "$save_file" "$target_dir/$target_filename"
33+
if [ $? -eq 0 ]; then
34+
# Copy was successful
35+
echo "$message_prefix File copied to '$target_dir/$target_filename'."
36+
37+
# Touch the new copy to be _certain_ it's the latest file
38+
touch "$target_dir/$target_filename"
39+
40+
# Delete the original file, so we don't reimport it again
41+
rm "$save_file"
42+
echo "$message_prefix Original file '$save_file' deleted."
43+
else
44+
echo "$message_prefix Error copying the file."
45+
exit 1
46+
fi
47+
else
48+
echo "$message_prefix No save file found in '$source_dir' or multiple save files found."
49+
echo "$message_prefix Skipping save import process."
50+
fi
51+
52+
53+
54+
55+

charts/factorio-server-charts/values.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ factorioServer:
153153
update_mods_on_start: false
154154
# lets the game know if you want to load the latest save
155155
load_latest_save: true
156+
# import a save file if it exists in /factorio/save_to_import
157+
# WARNING: this will overwrite your existing save game with the name specified in factorioServer.save_name.
158+
import_save: true
156159

157160
## @param account.accountSecret Existing secret containing a valid factorio.com username and either a password or a token (or both)
158161
## @param account.username Factorio.com username, ignored if `account.accountSecret` is set

0 commit comments

Comments
 (0)