File indexing completed on 2024-04-28 05:02:35

0001 #!/usr/bin/env bash
0002 
0003 set -euo pipefail
0004 
0005 TEMPDIR="$(mktemp --tmpdir -d "$(basename "${0/.sh}")"-XXXXX.d)"
0006 export TEMPDIR
0007 function cleanup_on_exit {
0008         xhost -local:root
0009         rm -rf "${TEMPDIR}"
0010 }
0011 trap cleanup_on_exit EXIT
0012 
0013 function buildahsetx() {
0014         set -x
0015         buildah "${@}"
0016         exitcode=$?
0017         { set +x ; } 2>/dev/null
0018         return ${exitcode}
0019 }
0020 
0021 function podmansetx() {
0022         set -x
0023         podman "${@}"
0024         exitcode=$?
0025         { set +x ; } 2>/dev/null
0026         return ${exitcode}
0027 }
0028 
0029 function create_imagename() {
0030         local fromimage="$1"
0031         sed -r 's!([:/]+|localhost|-kde-devel)!!g' <<<"${fromimage}"
0032 }
0033 
0034 function prepare_image() {
0035         local fromimage="$1"
0036         local imagename="$2"
0037 
0038         buildahsetx rm "working-${imagename}" 2>/dev/null >&2
0039         podmansetx rmi -f "${imagename}" 2>/dev/null >&2
0040         
0041         buildahsetx from --name "working-${imagename}" "${fromimage}" || exit 1
0042 }
0043 
0044 function prepare_environment() {
0045         local id="$1"
0046         local checkout
0047         checkout="$2"
0048 
0049         cat <<EOF >"${TEMPDIR}/runtestprograms.sh"
0050 QT_QPA_PLATFORM="offscreen"
0051 export QT_QPA_PLATFORM
0052 set -x
0053 # FIXME kbibtexfilestest  requires to have testsets files available
0054 /tmp/build/bin/kbibtexnetworkingtest || exit 1
0055 /tmp/build/bin/kbibtexiotest || exit 1
0056 /tmp/build/bin/kbibtexdatatest || exit 1
0057 set +x
0058 EOF
0059         buildahsetx copy "${id}" "${TEMPDIR}/runtestprograms.sh" /tmp/ || exit 1
0060 
0061         buildahsetx copy "${id}" ../../../ /tmp/source || exit 1
0062         buildahsetx run --workingdir /tmp/source "${id}" -- git config --global --add safe.directory /tmp/source || exit 1
0063         if [[ -n "${checkout}" ]] ; then
0064                 buildahsetx run --workingdir /tmp/source "${id}" -- git reset --hard || exit 1
0065                 buildahsetx run --workingdir /tmp/source "${id}" -- git clean -fd || exit 1
0066                 buildahsetx run --workingdir /tmp/source "${id}" -- git checkout "${checkout}" || exit 1
0067         fi
0068 }
0069 
0070 function build_sources() {
0071         local id="$1"
0072 
0073         buildahsetx config --workingdir /tmp/build "${id}" || exit 1
0074         buildahsetx config --user root --env VERBOSE=1 "${id}"
0075         buildahsetx run --user root "${id}" -- /usr/bin/cmake --log-level=VERBOSE -DBUILD_TESTING:BOOL=ON -DCMAKE_BUILD_TYPE=debug -DCMAKE_INSTALL_PREFIX:PATH=/usr ../source || exit 1
0076         buildahsetx run --user root "${id}" -- /usr/bin/make -j$(( $(nproc) - 1 )) || exit 1
0077 }
0078 
0079 function install_program() {
0080         local id="$1"
0081 
0082         buildahsetx run --user root "${id}" -- make install || exit 1
0083         buildahsetx run --user root "${id}" -- kbuildsycoca5 || exit 1
0084 }
0085 
0086 function cleaning() {
0087         local id="$1"
0088 
0089         buildahsetx run --user root "${id}" -- rm -rf '/tmp/source'
0090 }
0091 
0092 function setting_ownerships() {
0093         local id="$1"
0094         local username="$2"
0095 
0096         for d in build xdg-config-home ; do
0097                 buildahsetx run --user root "${id}" -- chown -R "${username}:${username}" "/tmp/${d}" || exit 1
0098         done
0099         for d in runtime cache config ; do
0100                 buildahsetx run --user root "${id}" -- chown -R "${username}:${username}" "/tmp/xdg-${d}-dir" || exit 1
0101         done
0102 }
0103 
0104 function finalizing_image() {
0105         local id="$1"
0106         local username="$2"
0107 
0108         buildahsetx config --workingdir /tmp "${id}" || exit 1
0109         buildahsetx config --cmd "/usr/bin/sudo -u \"${username}\" /usr/bin/kbibtex" "${id}" || exit 1
0110         buildahsetx commit "${id}" "${id}_img" || exit 1
0111 }
0112 
0113 function run_test_programs() {
0114         local id="$1"
0115         local username="$2"
0116 
0117         mkdir -p /tmp/kbibtex-podman
0118         rm -f /tmp/kbibtex-podman/output-{kbibtexnetworkingtest,kbibtexiotest,kbibtexdatatest}.txt
0119 
0120         podmansetx container run --rm --net=host -v /tmp/kbibtex-podman:/tmp/kbibtex-podman --tty --interactive "${id}_img" /usr/bin/sudo -u "${username}" /bin/bash /tmp/runtestprograms.sh 2>&1 | tee -a /tmp/kbibtex-podman/output-testprograms.txt || { echo "For log output, see '/tmp/kbibtex-podman/output-testprograms.txt'" >&2 ; exit 1 ; }
0121 }
0122 
0123 function run_kbibtex_program() {
0124         local id="$1"
0125         local username="$2"
0126 
0127         mkdir -p /tmp/kbibtex-podman
0128         xhost local:root
0129         podmansetx container run --rm --net=host -v /tmp/.X11-unix:/tmp/.X11-unix -v /tmp/kbibtex-podman:/tmp/kbibtex-podman --env DISPLAY --tty --interactive "${id}_img" /usr/bin/sudo -u "${username}" /usr/bin/kbibtex 2>&1 | tee /tmp/kbibtex-podman/output-kbibtex.txt || { echo "For log output, see '/tmp/kbibtex-podman/output-kbibtex.txt'" >&2 ; exit 1 ; }
0130         xhost -local:root
0131 }
0132 
0133 function run_bash() {
0134         local id="$1"
0135         local username="$2"
0136 
0137         mkdir -p /tmp/kbibtex-podman
0138         podmansetx container run --rm --net=host -v /tmp/kbibtex-podman:/tmp/kbibtex-podman --tty --interactive "${id}_img" /usr/bin/sudo -u "${username}" /bin/bash || exit 1
0139 }
0140 
0141 if (( $# == 1 || $# == 2)) ; then
0142         if [[ $1 == "archlinux" || $1 == "debian10" || $1 == "debian11" || $1 == "debian12" || $1 == "fedora" || $1 == "fedora3"* || $1 == "fedora4"* || $1 == "ubuntu"* || $1 == "kdeneon" ]] ; then
0143                 DIST="$1"
0144                 [[ ${DIST} == "ubuntu" ]] && DIST="ubuntu2210"
0145                 DISTVARIANT=""
0146                 [[ ${DIST} == "fedora" ]] && DISTVARIANT="-latest"
0147                 [[ ${DIST} == "fedora3"* || ${DIST} == "fedora4"* ]] && DISTVARIANT="-${DIST:6}" && DIST="fedora"
0148                 USERNAME="kdeuser"
0149                 [[ ${DIST} == "kdeneon" ]] && USERNAME="neon"
0150 
0151                 FROMIMAGE="localhost/${DIST}${DISTVARIANT}-kde-devel"
0152                 IMAGENAME="$(create_imagename "${FROMIMAGE}")"
0153                 ID="$(prepare_image "${FROMIMAGE}" "${IMAGENAME}")"
0154                 prepare_environment "${ID}" "${2:-}" || exit 1
0155                 # KDE Neon supports Qt6, so enable it
0156                 [[ ${DIST} == "kdeneon" ]] && buildahsetx run --user root "${ID}" -- sed -i -e 's!set(QT_MAJOR_VERSION "5")!set(QT_MAJOR_VERSION "6")!' /tmp/source/CMakeLists.txt
0157                 build_sources "${ID}" || exit 1
0158                 install_program "${ID}" || exit 1
0159                 cleaning "${ID}" || exit 1
0160                 setting_ownerships "${ID}" "${USERNAME}" || exit 1
0161                 finalizing_image "${ID}" "${USERNAME}" || exit 1
0162                 run_test_programs "${ID}" "${USERNAME}" || exit 1
0163                 run_kbibtex_program "${ID}" "${USERNAME}" || exit 1
0164         elif [[ $1 == "--cleanup" ]] ; then
0165                 podmansetx image ls
0166                 podmansetx container ls
0167                 buildahsetx images -f dangling=true
0168                 buildahsetx containers
0169                 echo
0170                 buildahrmoutput="$(buildah containers | awk '/  [*]  / {printf " "$1} END {print ""}')"
0171                 podmanimagelsoutput="$(podman image ls | awk '/^localhost\// {printf " "$3}')"
0172                 buildahcontainersoutput="$(buildah containers | awk '/  [*]  / {printf " "$3    } END {print ""}')"
0173                 [[ -n "${buildahrmoutput}" || -n "${podmanimagelsoutput}" || -n "${buildahcontainersoutput}" ]] && echo "To clean, run:"
0174                 [[ -n "${buildahrmoutput}" ]] && echo "  buildah rm ${buildahrmoutput}"
0175                 [[ -n "${podmanimagelsoutput}" || -n "${buildahcontainersoutput}" ]] && echo "  podman rmi -f ${podmanimagelsoutput} ${buildahcontainersoutput}"
0176                 echo
0177                 echo "If all fails, run:"
0178                 echo "  sudo rm -rf ~/.local/share/containers ~/.config/containers"
0179                 exit 0
0180         else
0181                 echo "Unknown argument, expecting one of the following:  archlinux  debian10  debian11  debian12  fedora  fedora36  fedora37  fedora38  ubuntu2204  ubuntu2210  kdeneon" >&2
0182                 echo "To get help how to clean up previously created images or containers, run  $(basename "$0") --cleanup" >&2
0183                 exit 1
0184         fi
0185 else
0186         echo "Missing argument, expecting one of the following:  archlinux  debian10  debian11  debian12  fedora  fedora36  fedora37  fedora38  ubuntu2204  ubuntu2210  kdeneon" >&2
0187         echo "To get help how to clean up previously created images or containers, run  $(basename "$0") --cleanup" >&2
0188         exit 1
0189 fi