pms-cli/utils/podman

128 lines
3.5 KiB
Bash

#!/usr/bin/env bash
# CONSTANTS
readonly IMAGEDIR=${ROOTPATH}/image
##
# checks if the defined image exists already
#
# $1 - string: software-name
# $2 - string: mc-version
# $3 - string: build-number
##
function checkImage() {
[[ -n $(podman image ls | grep "$2/$1" | grep "$3") ]] && echo true || echo false
}
##
# checks if the passed data is up-to-date
#
# $1 - string: software-name
# $2 - string: mc-version
# $3 - string: build-number
# $4 - string: runner-type
##
function checkUpdate() {
case "$4" in
"server")
local remotebuild=$(curl -s "$(getValueByKey SERVERRUNNERS.$1)/versions/$2" | jq -r '.builds | last')
;;
"proxy")
local remotebuild=$(curl -s "$(getValueByKey PROXYRUNNERS.$1)/versions/$2" | jq -r '.builds | last')
;;
esac
if [[ "$3" == "$remotebuild" ]]; then
echo false
else
echo true
fi
}
##
# updates the config-file to the latest build
#
# $1 - string: software-name
# $2 - string: mc-version
# $3 - string: config-dir
# $4 - string: runner-type
##
function updateBuild() {
case "$4" in
"server")
local latest=$(curl -s "$(getValueByKey SERVERRUNNERS.$1)/versions/$2" | jq -r '.builds | last')
;;
"proxy")
local latest=$(curl -s "$(getValueByKey PROXYRUNNERS.$1)/versions/$2" | jq -r '.builds | last')
;;
esac
log d "Updating build-no. to '$latest'"
setLocalConfValue "$3" "(.build=$latest)"
}
##
# builds the podman image with the proper settings
#
# $1 - string: software-name
# $2 - string: mc-version
# $3 - string: build-number
# $4 - string: download-url
#
##
function buildImage() {
log d "lookup jar-name for download"
local app_name=$(curl -s "$4" | jq -r '.downloads.application.name')
log d "downloading server.jar"
cp "${ROOTPATH}"/image/* "${BUILDDIR}/"
curl "$4/downloads/$app_name" -o "${BUILDDIR}/server.jar"
log d "building docker image"
podman build --rm "${BUILDDIR}" --tag "${2}/${1}:${3}"
[[ $? -eq 0 ]] && log s "Image '${2}/${1}:${3}' built!"
log d "removing temporary build-dir"
[[ -f "${BUILDDIR}" ]] && rm -rf "${BUILDDIR}"
}
##
# starts the specified container
#
# $1 - string: containername
# $2 - string: image
# $3 - int: port
##
function startContainer() {
log d "Starting container '$1'"
readarray -t additional_ports <<< "$(getLocalConfValue $(getValueByKey 'PODMAN_DIRECTORY')/${1} 'additional_ports[]')"
[[ ${#additional_ports} -gt 0 ]] && log d "Starting with additional ports: ${additional_ports[@]}"
podman run --name "${1}" --userns=keep-id:uid=1000 --rm -it -d --network "${5}" -p $([[ -n "${4}" ]] && echo ${4}:)${3}:25565 $([[ ${#additional_ports} -gt 0 ]] && for port in "${additional_ports[@]}"; do echo "-p $([[ -n ${4} ]] && echo ${4}:)${port}:${port}"; done) -v $(getValueByKey 'PODMAN_DIRECTORY')/${1}/data:/var/server "${2}" java -Dcom.mojang.eula.agree=true -jar /var/exec/server.jar --nogui --port 25565
[[ $? -eq 0 ]] && log s "Container '$1' started!"
}
##
# stops the specified container
#
# $1 - string: containername
##
function stopContainer() {
log d "Stopping container '$1'"
podman container stop --time 100 $1
log s "Container '$1' stopped"
}
##
# create a podman network
#
# $1 - string: network-name
##
function createNetwork() {
if [[ -z "$1" ]]; then
log e "Network name is unspecified. Please provide a network-name!"
exit 1
fi
if [[ -n $(podman network ls | grep "$1") ]]; then
log w "Network '$1' exists already, won't create!"
return 1
fi
podman network create "$1"
}