factorio build to use custom functions and sperates from glibc build process

This commit is contained in:
Hyatt 2022-01-28 09:46:33 -06:00
parent 8ff1c8676e
commit fa6bf6b241
Signed by: nhyatt
GPG Key ID: C50D0BBB5BC40BEA

View File

@ -1,125 +1,87 @@
def label = "jenkins-${UUID.randomUUID().toString()}"
#!groovy
def repository = "registry.c.test-chamber-13.lan"
def repositoryCreds = "harbor-repository-creds"
def kanikoImage = "${repository}/library/kaniko:latest"
def imageName = "library/factorio"
def imageTag = "latest"
def FactorioDownloadURL="https://factorio.com/get-download/stable/headless/linux64"
def factorioScript = """#!/bin/sh
set -e
def AlpineGlibcBaseURL="https://github.com/sgerrand/alpine-pkg-glibc/releases/download"
def AlpineGlibcPackageVersion = "2.34-r0"
def AlpineGlibcBasePackageFilename = "glibc-${AlpineGlibcPackageVersion}.apk"
def AlpineGlibcBinPackageFilename = "glibc-bin-${AlpineGlibcPackageVersion}.apk"
def AlpineGlibcI18nPackageFilename = "glibc-i18n-${AlpineGlibcPackageVersion}.apk"
id
[ ! -d "/data/saves" ] && mkdir -p /data/saves
[ ! -d "/data/config" ] && mkdir -p /data/config
[ ! -d "/data/mods" ] && mkdir -p /data/mods
[ ! -d "/data/scenarios" ] && mkdir -p /data/scenarios
[ ! -d "/data/script-output" ] && mkdir -p /data/script-output
[ ! -f "/data/config/rconpw" ] && printf '%s' "\$(pwgen 15 1)" > /data/config/rconpw
[ ! -f "/data/config/server-settings.json" ] && cp /opt/factorio/data/server-settings.example.json /data/config/server-settings.json
[ ! -f "/data/config/map-gen-settings.json" ] && cp /opt/factorio/data/map-gen-settings.example.json /data/config/map-gen-settings.json
[ ! -f "/data/config/map-settings.json" ] && cp /opt/factorio/data/map-settings.example.json /data/config/map-settings.json
def GlibcLanguage = "C.UTF-8"
if find -L /data/saves -iname \\*.tmp.zip -mindepth 1 -print | grep -q .; then
rm -f /data/saves/*.tmp.zip
fi
def TimeZone = "America/Chicago"
if ! find -L /data/saves -iname \*.zip -mindepth 1 -print | grep -q .; then
/opt/factorio/bin/x64/factorio \
--create /data/saves/_autosave1.zip \
--map-gen-settings /data/config/map-gen-settings.json \
--map-settings /data/config/map-settings.json
fi
/opt/factorio/bin/x64/factorio \
--port \${PORT} \
--start-server-load-latest \
--server-settings /data/config/server-settings.json \
--server-banlist /data/config/server-banlist.json \
--rcon-port \${RCON_PORT} \
--server-whitelist /data/config/server-whitelist.json \
--use-server-whitelist \
--server-adminlist /data/config/server-adminlist.json \
--rcon-password "\$(cat /data/config/rconpw)" \
--server-id /factorio/config/server-id.json \
\${@}
"""
def dockerFile = """
FROM registry.c.test-chamber-13.lan/library/alpine:latest
FROM registry.c.test-chamber-13.lan/library/alpine-glibc:latest
MAINTAINER The_Spider <spider@smoothnet.org>
LABEL org.opencontainers.image.authors="The_Spider <spider@smoothnet.org>"
ARG SAVES=/data/saves \\
CONFIG=/data/config \\
MODS=/data/mods \\
SCENARIOS=/data/scenarios \\
SCRIPT_OUTPUT=/data/script-output
ENV PORT=34197 \\
RCON_PORT=27015 \\
SAVES=/data/saves \\
CONFIG=/data/config \\
MODS=/data/mods \\
SCENARIOS=/data/scenarios \\
SCRIPT_OUTPUT=/data/script-output \\
TZ=${TimeZone}
RCON_PORT=27015
COPY start-server.sh /opt/factorio/start-server.sh
RUN apk add --update --no-cache \\
pwgen \\
binutils \\
gettext \\
libintl \\
shadow && \\
apk add --update --no-cache --virtual=.build-dependencies \\
curl \\
ca-certificates && \\
printf '%s\\n' \\
"-----BEGIN PUBLIC KEY-----" \\
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApZ2u1KJKUu/fW4A25y9m" \\
"y70AGEa/J3Wi5ibNVGNn1gT1r0VfgeWd0pUybS4UmcHdiNzxJPgoWQhV2SSW1JYu" \\
"tOqKZF5QSN6X937PTUpNBjUvLtTQ1ve1fp39uf/lEXPpFpOPL88LKnDBgbh7wkCp" \\
"m2KzLVGChf83MS0ShL6G9EQIAUxLm99VpgRjwqTQ/KfzGtpke1wqws4au0Ab4qPY" \\
"KXvMLSPLUp7cfulWvhmZSegr5AdhNw5KNizPqCJT8ZrGvgHypXyiFvvAH5YRtSsc" \\
"Zvo9GI2e2MaZyo9/lvb+LbLEJZKEQckqRj4P26gmASrZEPStwc+yqy1ShHLA0j6m" \\
"1QIDAQAB" \\
"-----END PUBLIC KEY-----" > "/etc/apk/keys/sgerrand.rsa.pub" && \\
curl -sSLO "${AlpineGlibcBaseURL}/${AlpineGlibcPackageVersion}/${AlpineGlibcBasePackageFilename}" && \\
curl -sSLO "${AlpineGlibcBaseURL}/${AlpineGlibcPackageVersion}/${AlpineGlibcBinPackageFilename}" && \\
curl -sSLO "${AlpineGlibcBaseURL}/${AlpineGlibcPackageVersion}/${AlpineGlibcI18nPackageFilename}" && \\
apk add --no-cache \\
"${AlpineGlibcBasePackageFilename}" \\
"${AlpineGlibcBinPackageFilename}" \\
"${AlpineGlibcI18nPackageFilename}" && \\
rm -v "/etc/apk/keys/sgerrand.rsa.pub" && \\
/usr/glibc-compat/bin/localedef --force --inputfile POSIX --charmap UTF-8 "${GlibcLanguage}" || true && \\
printf '%s\\n' \\
"export LANG=${GlibcLanguage}" && \\
mkdir -p /opt/factorio \\
/data \\
\${SAVES} \\
\${CONFIG} \\
\${MODS} \\
\${SCENARIOS} \\
\${SCRIPT_OUTPUT} && \\
curl --location --silent "${FactorioDownloadURL}" --output /tmp/factorio.tar.xz && \\
tar -x -f /tmp/factorio.tar.xz --directory /opt && \\
apk del glibc-i18n && \\
apk del .build-dependencies && \\
rm -v \\
${AlpineGlibcBasePackageFilename} \\
${AlpineGlibcBinPackageFilename} \\
${AlpineGlibcI18nPackageFilename} \\
/tmp/factorio.tar.xz && \\
ln -s \${SAVES} /opt/factorio/saves && \\
ln -s \${MODS} /opt/factorio/mods && \\
ln -s \${SCENARIOS} /opt/factorio/scenarios && \\
ln -s \${SCRIPT_OUTPUT} /opt/factorio/script-output && \\
addgroup -g 1000 -S factorio && \\
adduser -u 1000 -G factorio -s /bin/sh -SDH factorio && \\
chown -R factorio:factorio /opt/factorio /data && \\
printf '%s\\n' \\
"#!/bin/sh -x" \\
"set -e" \\
"id" \\
"[ ! -d \\\"\${SAVES}\\\" ] && mkdir -p \${SAVES}" \
"[ ! -d \\\"\${CONFIG}\\\" ] && mkdir -p \${CONFIG}" \
"[ ! -d \\\"\${MODS}\\\" ] && mkdir -p \${MODS}" \
"[ ! -d \\\"\${SCENARIOS}\\\" ] && mkdir -p \${SCENARIOS}" \
"[ ! -d \\\"\${SCRIPT_OUTPUT}\\\" ] && mkdir -p \${SCRIPT_OUTPUT}" \
"[ ! -f \\\"\${CONFIG}/rconpw\\\" ] && printf '%s' \\\"\\\$(pwgen 15 1)\\\" > \${CONFIG}/rconpw" \\
"[ ! -f \\\"\${CONFIG}/server-settings.json\\\" ] && cp /opt/factorio/data/server-settings.example.json \${CONFIG}/server-settings.json" \\
"[ ! -f \\\"\${CONFIG}/map-gen-settings.json\\\" ] && cp /opt/factorio/data/map-gen-settings.example.json \${CONFIG}/map-gen-settings.json" \\
"[ ! -f \\\"\${CONFIG}/map-settings.json\\\" ] && cp /opt/factorio/data/map-settings.example.json \${CONFIG}/map-settings.json" \\
"if find -L \${SAVES} -iname \\*.tmp.zip -mindepth 1 -print | grep -q .; then" \\
" rm -f \${SAVES}/*.tmp.zip" \\
"fi" \\
"if ! find -L \${SAVES} -iname \\*.zip -mindepth 1 -print | grep -q .; then" \\
" /opt/factorio/bin/x64/factorio \\\\" \\
" --create \${SAVES}/_autosave1.zip \\\\" \\
" --map-gen-settings \${CONFIG}/map-gen-settings.json \\\\" \\
" --map-settings \${CONFIG}/map-settings.json" \\
"fi" \\
"/opt/factorio/bin/x64/factorio \\\\" \\
" --port \${PORT} \\\\" \\
" --start-server-load-latest \\\\" \\
" --server-settings \${CONFIG}/server-settings.json \\\\" \\
" --server-banlist \${CONFIG}/server-banlist.json \\\\" \\
" --rcon-port \${RCON_PORT} \\\\" \\
" --server-whitelist \${CONFIG}/server-whitelist.json \\\\" \\
" --use-server-whitelist \\\\" \\
" --server-adminlist \${CONFIG}/server-adminlist.json \\\\" \\
" --rcon-password \\\"\\\$(cat \${CONFIG}/rconpw)\\\" \\\\" \\
" --server-id /factorio/config/server-id.json \\\\" \\
" \${@}" > /opt/factorio/start-server.sh && \\
chmod +x /opt/factorio/start-server.sh
pwgen \\
binutils \\
gettext \\
libintl \\
shadow && \\
mkdir -p /opt/factorio \\
/data \\
\${SAVES} \\
\${CONFIG} \\
\${MODS} \\
\${SCENARIOS} \\
\${SCRIPT_OUTPUT} && \\
curl --location --silent "${FactorioDownloadURL}" --output /tmp/factorio.tar.xz && \\
tar -x -f /tmp/factorio.tar.xz --directory /opt && \\
rm -v /tmp/factorio.tar.xz && \\
ln -s \${SAVES} /opt/factorio/saves && \\
ln -s \${MODS} /opt/factorio/mods && \\
ln -s \${SCENARIOS} /opt/factorio/scenarios && \\
ln -s \${SCRIPT_OUTPUT} /opt/factorio/script-output && \\
addgroup -g 1000 -S factorio && \\
adduser -u 1000 -G factorio -s /bin/sh -SDH factorio && \\
chown -R factorio:factorio /opt/factorio /data
USER factorio
@ -128,52 +90,27 @@ WORKDIR /opt/factorio
ENTRYPOINT [ "/bin/sh", "-c", "/opt/factorio/start-server.sh" ]
"""
def label = "kubernetes-${UUID.randomUUID().toString()}"
def templateName = "pipeline-worker"
podTemplate(
label: label,
name: "pipeline-worker",
yaml: """---
apiVersion: v1
kind: Pod
metadata:
name: pipeline-worker
spec:
containers:
- name: kaniko
imagePullPolicy: Always
image: ${kanikoImage}
tty: true
command:
- /busybox/cat
""",
label: label,
name: templateName,
yaml: functions.podYaml(
repo: repository,
templateName: templateName,
kaniko: true
)
) {
node(label) {
node (label) {
def workspace = pwd()
stage ("Prepare Kaniko") {
container ("kaniko") {
withCredentials([usernameColonPassword(
credentialsId: repositoryCreds,
variable: "dCreds",
)]) {
def dockerJSON = """{
"auths": {
"${repository}": {
"auth": "${dcreds.bytes.encodeBase64().toString()}"
}
}
}"""
sh """
set +x
echo '${dockerJSON}' > /kaniko/.docker/config.json
"""
}
}
}
writeFile(file: workspace + "/start-server.sh", text: factorioScript)
stage("Build Factorio Image") {
container("kaniko") {
writeFile(file: workspace + "/Dockerfile", text: dockerFile)
sh "/kaniko/executor --cleanup --context \"${workspace}\" -f \"${workspace}/Dockerfile\" --destination \"${repository}/${imageName}:${imageTag}\""
}
}
}
}
functions.buildContainer(
repository: repository,
imageDest: "${repository}/library/factorio:latest",
dockerFile: dockerFile,
repoCreds: repositoryCreds,
)
}
}