From 0228e38f657dff325d24d6e3e4a7eae099af1938 Mon Sep 17 00:00:00 2001 From: Snogard Date: Fri, 8 Dec 2023 18:01:17 +0100 Subject: [PATCH] added foundry build script, provided you have private repository --- build/foundry/Dockerfile.10 | 31 +++++++ build/foundry/Dockerfile.11 | 31 +++++++ build/foundry/Dockerfile.9 | 31 +++++++ build/foundry/src/entrypoint.sh | 18 ++++ readme.md | 8 ++ src/commands/build.sh | 33 +++++++ src/commands/build/build-foundry.sh | 74 +++++++++++++++ src/libs/bash/envs.sh | 16 +++- src/libs/python/podman-generate-systemd.py | 102 +++++++++++++++++++++ 9 files changed, 341 insertions(+), 3 deletions(-) create mode 100644 build/foundry/Dockerfile.10 create mode 100644 build/foundry/Dockerfile.11 create mode 100644 build/foundry/Dockerfile.9 create mode 100755 build/foundry/src/entrypoint.sh create mode 100755 src/commands/build.sh create mode 100755 src/commands/build/build-foundry.sh create mode 100644 src/libs/python/podman-generate-systemd.py diff --git a/build/foundry/Dockerfile.10 b/build/foundry/Dockerfile.10 new file mode 100644 index 0000000..e57904d --- /dev/null +++ b/build/foundry/Dockerfile.10 @@ -0,0 +1,31 @@ +FROM docker.io/node:14-alpine3.13 + +LABEL name=foundry +LABEL version=1.0 + +ENV UID=1000 +ENV GUID=1000 + +RUN apk --update --no-cache add jq +RUN deluser node +RUN addgroup -g $GUID foundry \ + && adduser -u $UID -G foundry -s /bin/sh -D foundry + +USER foundry + +RUN mkdir -p /home/foundry/data + +## try +RUN mkdir -p /home/foundry/.local/share/FoundryVTT +COPY src /home/foundry/src + +RUN mkdir -p /home/foundry/tmp + +COPY ./foundry /home/foundry/instance + +WORKDIR /home/foundry/instance + +EXPOSE 30000/TCP + +ENTRYPOINT ["/home/foundry/src/entrypoint.sh"] +CMD ["resources/app/main.js", "--headless", "--noupdate", "--dataPath=/home/foundry/data"] \ No newline at end of file diff --git a/build/foundry/Dockerfile.11 b/build/foundry/Dockerfile.11 new file mode 100644 index 0000000..03947cb --- /dev/null +++ b/build/foundry/Dockerfile.11 @@ -0,0 +1,31 @@ +FROM docker.io/node:18-alpine3.18 + +LABEL name=foundry +LABEL version=1.0 + +ENV UID=1000 +ENV GUID=1000 + +RUN apk --update --no-cache add jq +RUN deluser node +RUN addgroup -g $GUID foundry \ + && adduser -u $UID -G foundry -s /bin/sh -D foundry + +USER foundry + +RUN mkdir -p /home/foundry/data + +## try +RUN mkdir -p /home/foundry/.local/share/FoundryVTT +COPY src /home/foundry/src + +RUN mkdir -p /home/foundry/tmp + +COPY ./foundry /home/foundry/instance + +WORKDIR /home/foundry/instance + +EXPOSE 30000/TCP + +ENTRYPOINT ["/home/foundry/src/entrypoint.sh"] +CMD ["resources/app/main.js", "--headless", "--noupdate", "--dataPath=/home/foundry/data"] \ No newline at end of file diff --git a/build/foundry/Dockerfile.9 b/build/foundry/Dockerfile.9 new file mode 100644 index 0000000..e57904d --- /dev/null +++ b/build/foundry/Dockerfile.9 @@ -0,0 +1,31 @@ +FROM docker.io/node:14-alpine3.13 + +LABEL name=foundry +LABEL version=1.0 + +ENV UID=1000 +ENV GUID=1000 + +RUN apk --update --no-cache add jq +RUN deluser node +RUN addgroup -g $GUID foundry \ + && adduser -u $UID -G foundry -s /bin/sh -D foundry + +USER foundry + +RUN mkdir -p /home/foundry/data + +## try +RUN mkdir -p /home/foundry/.local/share/FoundryVTT +COPY src /home/foundry/src + +RUN mkdir -p /home/foundry/tmp + +COPY ./foundry /home/foundry/instance + +WORKDIR /home/foundry/instance + +EXPOSE 30000/TCP + +ENTRYPOINT ["/home/foundry/src/entrypoint.sh"] +CMD ["resources/app/main.js", "--headless", "--noupdate", "--dataPath=/home/foundry/data"] \ No newline at end of file diff --git a/build/foundry/src/entrypoint.sh b/build/foundry/src/entrypoint.sh new file mode 100755 index 0000000..d4ae62f --- /dev/null +++ b/build/foundry/src/entrypoint.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +optionsPath=/home/foundry/data/Config/options.json + +if [ -f "${optionsPath}" ]; then +echo "${optionsPath} found" +echo $(jq ".hostname=\"$HOSTNAME\"" "${optionsPath}") > "${optionsPath}" +echo $(jq ".routePrefix=\"$ROUTE_PREFIX\"" "${optionsPath}") > "${optionsPath}" +echo $(jq ".proxySSL=true" "${optionsPath}") > "${optionsPath}" +echo $(jq ".proxyPort=443" "${optionsPath}") > "${optionsPath}" +else +echo "${optionsPath} not found the settings will be applied on next restart" +fi + + +node "$@" + +exit 0 \ No newline at end of file diff --git a/readme.md b/readme.md index 77b64e0..ccda037 100644 --- a/readme.md +++ b/readme.md @@ -25,6 +25,7 @@ every configuration must be under /etc/kaido. - system (not implemented) - user (not implemented) - update.conf + - envs.sh ### /etc/kaido/update.conf this file contains the definitions of what must be installed/configured/built when using the "kaido update" command @@ -44,6 +45,13 @@ regardless of the order in the file, the execution is in this order: warning: No parameters are supported yet +### /etc/kaido/envs.sh +This file will be sourced to overrwrite default envs + +CONTAINERS_FOLDER: where container folders are created by default +BUILDS_FOLDER: where files are downloaded for build purposes +KAIDO_CONFIG_FOLDER: where kaido will look for a kaido-config repository (not fully implemented) + # Container Installation: ### gitlab-runner diff --git a/src/commands/build.sh b/src/commands/build.sh new file mode 100755 index 0000000..814e3a3 --- /dev/null +++ b/src/commands/build.sh @@ -0,0 +1,33 @@ +#!/bin/bash +source /opt/kaido/src/libs/bash/lib.sh + +prefix=build + +rootFolder="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" +scriptFolder=${rootFolder}/${prefix} + + +scriptName=$1 +scriptPath=${scriptFolder}/${prefix}-${scriptName}.sh + +buildPath="$BUILDS_FOLDER/$scriptName" + +if [ -d "$buildPath" ]; then + sudo rm -rf "$buildPath" +fi + +mkdir -p "$buildPath" + +if test -f "$scriptPath"; then + + if [[ ! -x "$scriptPath" ]]; then + sudo chmod +x $scriptPath + fi + echo "Executing: $scriptPath $2 $3 $4 $5 $6 $7" + echo "in folder: $buildPath" + cp -ra "/opt/kaido/build/$scriptName/." "$buildPath" + cd "$buildPath" + $scriptPath $2 $3 $4 $5 $6 $7 +else + echo "Error file not found: ${scriptPath}" +fi diff --git a/src/commands/build/build-foundry.sh b/src/commands/build/build-foundry.sh new file mode 100755 index 0000000..b641924 --- /dev/null +++ b/src/commands/build/build-foundry.sh @@ -0,0 +1,74 @@ +#!/bin/bash +source /opt/kaido/src/libs/bash/lib.sh + + +containerName=foundry +version=$1 + + +# envs +repository=https://example.com/user/foundry.git + +if [ -f "$KAIDO_CONFIG_FOLDER/build/$containerName/envs.sh" ]; then + source "$KAIDO_CONFIG_FOLDER/build/$containerName/envs.sh" +fi + +#TODO: make a better check, this is dumb :) +if [[ "$repository" == "https://example.com/user/foundry.git" ]]; then + echo "ERROR: please set the 'repository' variable in '$KAIDO_CONFIG_FOLDER/build/$containerName/envs.sh'" + exit 1 +fi + + +if [ -d "./foundry" ]; then + if [ -d "./foundry/.git" ]; then + echo "Repo already present" + else + echo "Removing old foundry folder" + rm -r ./foundry + git clone $repository + fi +else + git clone $repository +fi + +cd foundry + +git pull +git fetch --tags + +if [ ! -n "${version}" ]; then + echo "Version not specified, selecting lastest version" + version=$(git describe) +fi + + +IFS='.' read -ra TMP <<< "$version" +major="${TMP[0]}" + + +tag=$containerName:version-${version} +dockerFile=Dockerfile +if [[ $major -lt 1 ]]; then + dockerFile="Dockerfile.9" +else + dockerFile="Dockerfile.$major" +fi + +if git tag | grep -qi $version ; then + git checkout $version +else + echo "Version not found, please choose one of the available: " + git tag + exit 1 +fi + +rm -rf ./.git + +cd .. + +chmod +x -R ./src + +podman build -f ./$dockerFile -t $tag + +exit 0 \ No newline at end of file diff --git a/src/libs/bash/envs.sh b/src/libs/bash/envs.sh index f95f2c1..d084f0e 100644 --- a/src/libs/bash/envs.sh +++ b/src/libs/bash/envs.sh @@ -1,3 +1,13 @@ -#TODO: make envs configurable -CONTAINERS_FOLDER="/mnt/storage/containers" -MASTER_CONFIG_FOLDER="$HOME/.config/kaido" \ No newline at end of file +CONTAINERS_FOLDER="$HOME/.local/share/kaido/containers" +BUILDS_FOLDER="$HOME/.cache/kaido/build" + +#TODO check if this is used +MASTER_CONFIG_FOLDER="$HOME/.config/kaido" + + +if [ -f "/etc/kaido/envs.sh" ]; then + source /etc/kaido/envs.sh +fi + +# keep here until fully implemented, maybe read it from kaido.conf? +KAIDO_CONFIG_FOLDER="/etc/kaido" \ No newline at end of file diff --git a/src/libs/python/podman-generate-systemd.py b/src/libs/python/podman-generate-systemd.py new file mode 100644 index 0000000..aaac2fd --- /dev/null +++ b/src/libs/python/podman-generate-systemd.py @@ -0,0 +1,102 @@ +import os +import json + + +VERSION="0.1" + +#fake passed arguments + +containerName="container-registry" + +#systemd unit +restart=always +restartSec=60 +timoutStopSec=70 +serviceType=forking +#---- + +#fake taken arguments + +userId=1000 +userName=snogard + +#---- + + +def command(command): + return os.system(command) == 0 + +def createSystemdUnit(prefix,name,containerName,pidFilePath,wants="",after="",restartPolicy="always",restartSec=60,timeoutStopSec=60,timeoutStartSec=60): + serviceFileName=f"{prefix}-{name}.service" + serviceFilePath=f"/home/snogard/.config/systemd/user/{serviceFileName}" + if os.path.isfile(serviceFilePath): + os.remove(serviceFilePath) + + command(f"echo '# {serviceFileName}' >> {serviceFilePath}") + command(f"echo '# autogenerated by kaido's script {VERSION}' >> {serviceFilePath}") + command(f"echo \"# $(date)\" >> {serviceFilePath}") + command(f"echo '' >> {serviceFilePath}") + command(f"echo '[Unit]' >> {serviceFilePath}") + command(f"echo 'Description=Podman {serviceFileName}' >> {serviceFilePath}") + command(f"echo 'Documentation=https://gitlab.com/Snogard/kaido' >> {serviceFilePath}") + command(f"echo 'Wants=network-online.target {wants}' >> {serviceFilePath}") + command(f"echo 'After=network-online.target {after}' >> {serviceFilePath}") + command(f"echo 'RequiresMountsFor=/run/user/{userId}/containers' >> {serviceFilePath}") + command(f"echo '' >> {serviceFilePath}") + command(f"echo '[Service]' >> {serviceFilePath}") + command(f"echo 'Environment=PODMAN_SYSTEMD_UNIT=%n' >> {serviceFilePath}") + command(f"echo 'Restart={restartPolicy}' >> {serviceFilePath}") + command(f"echo 'RestartSec={restartSec}' >> {serviceFilePath}") + command(f"echo 'TimeoutStopSec={timeoutStopSec}' >> {serviceFilePath}") + command(f"echo 'TimeoutStartSec={timeoutStartSec}' >> {serviceFilePath}") + command(f"echo 'ExecStart=/usr/bin/podman start {containerName}' >> {serviceFilePath}") + command(f"echo 'ExecStop=/usr/bin/podman stop -t 10 {containerName}' >> {serviceFilePath}") + command(f"echo 'ExecStopPost=/usr/bin/podman stop -t 10 {containerName}' >> {serviceFilePath}") + command(f"echo 'PIDFile={pidFilePath}' >> {serviceFilePath}") + command(f"echo 'Type=forking' >> {serviceFilePath}") + command(f"echo '' >> {serviceFilePath}") + command(f"echo '[Install]' >> {serviceFilePath}") + command(f"echo 'WantedBy=default.target' >> {serviceFilePath}") + + + return 0 + + + + +tmpfolder=f"/tmp/kaido/podman/{containerName}" +containerInfoPath=f"{tmpfolder}/{containerName}.json" + +# TODO: remove if exists +if not os.path.exists(tmpfolder): + os.makedirs(tmpfolder) + + +if command(f"podman pod inspect {containerName} > '{containerInfoPath}'"): + print("i'm a pod") + containerInfoFile=open(containerInfoPath,"r") + podInfo=json.loads(containerInfoFile.read()) + containerInfoFile.close() + + for item in podInfo["Containers"]: + childName=item["Name"] + jsonPath=f"{tmpfolder}/{childName}.json" + command(f"podman container inspect {childName} > '{jsonPath}'") + jsonFile=open(jsonPath,"r") + info=json.loads(jsonFile.read()) + jsonFile.close() + + pidFilePath=info["ConmonPidFile"] + if "infra" in childName: + createSystemdUnit("pod",containerName,childName,pidFilePath) + else + createSystemdUnit("container",childName,childName,pidFilePath) + + +elif command(f"podman container inspect {containerName} > '{containerInfoPath}'"): + print("i'm a container") + +else: + # print("container does not exists") + quit(1) +