# This class is used to package a single container into an image
# The container image is a recipe that inherit the coreos-container-image class


# Theses variables are used to configure which and how containers are bundled:
# ==============================================================================

CONTAINER_IMAGE ??= ""

# At the moment, only podman is supported. Support for for `systemd-container`
# or another runtime can be added later if needed
CONTAINER_RUNTIME ??= "podman"

OCI_STORAGE_DIR ??= "/usr/share/coreos-oci"

# OCI image is needed for podman, container image for systemd
INSTALL_OCI_IMAGE ??= "${@bb.utils.contains("CONTAINER_RUNTIME", "podman", "1", "0", d)}"

# Set this variable to "0" to turn off the installation of a generated systemd
# service file
INSTALL_GENERATED_SYSTEMD_SERVICE ??= "1"
GENERATED_SYSTEMD_SERVICE_NAME ??= "${PN}"
GENERATED_SYSTEMD_SERVICE_WANTED_BY ??= "multi-user.target"

PODMAN_RUN_OPTIONS ??= ""
PODMAN_RUN_CMD ??= ""

# Package configuration
# ==============================================================================

LICENSE ?= "CLOSED"
PACKAGE_ARCH="${MACHINE_ARCH}"

# Dependencies
# ==============================================================================

INHIBIT_DEFAULT_DEPS = "1"
do_install[depends] += "${CONTAINER_IMAGE}:do_image_complete"
RDEPENDS:${PN} += "${CONTAINER_RUNTIME}"


# SystemD integration
# ==============================================================================

inherit systemd 
SYSTEMD_SERVICE:${PN} ??= "${GENERATED_SYSTEMD_SERVICE_NAME}.service"


# Tasks
# ==============================================================================

# Disable the do_compile tasks
do_compile[noexec] = "1"

DEPLOYDIR = "${DEPLOY_DIR}/images/${MACHINE}"
FILES:${PN} = " \
    ${OCI_STORAGE_DIR} \
    ${CONTAINER_STORAGE_DIR} \
"

do_install() {
    if [ "${INSTALL_OCI_IMAGE}" = "1" ]; then
        install -d "${D}${OCI_STORAGE_DIR}"

        # Get the real directory name from the symlinks
        oci_image=$(readlink -f "${DEPLOYDIR}/${CONTAINER_IMAGE}-${MACHINE}.rootfs-oci")

        cp --no-preserve=ownership -r "${oci_image}" "${D}${OCI_STORAGE_DIR}/${CONTAINER_IMAGE}"

        # Using skopeo doesn't work yet, but should be the way to go in the futures
        #skopeo copy oci:${DEPLOYDIR}/${image}-${MACHINE}.rootfs-oci:latest "containers-storage:[vfs@${D}${CONTAINERS_STORAGE_DIR}+${TMPDIR}/skopeo]${image}"
    fi

    # Installing the systemd service file
    if [ "${INSTALL_GENERATED_SYSTEMD_SERVICE}" = "1" ]; then
        install -d "${D}${systemd_unitdir}/system"

        if [ "${CONTAINER_RUNTIME}" = "podman" ]; then
            # This generate a portable service file, like described in
            # https://www.redhat.com/sysadmin/podman-shareable-systemd-services
            # %t and %n are described in
            # https://www.freedesktop.org/software/systemd/man/systemd.unit.html
            cat >${D}${systemd_unitdir}/system/${GENERATED_SYSTEMD_SERVICE_NAME}.service <<EOF
[Unit]
Description=Run ${image} with Podman

[Service]
Restart=on-failure
ExecStartPre=/usr/bin/rm -f /%t/%n-pid /%t/%n-cid
ExecStart=/usr/bin/podman run --conmon-pidfile /%t/%n-pid --cidfile /%t/%n-cid -d ${PODMAN_RUN_OPTIONS} oci:${OCI_STORAGE_DIR}/${CONTAINER_IMAGE} ${PODMAN_RUN_CMD}
ExecStop=/usr/bin/sh -c "/usr/bin/podman rm -f \`cat /%t/%n-cid\`"
KillMode=none
Type=forking
PIDFile=/%t/%n-pid

[Install]
WantedBy=${GENERATED_SYSTEMD_SERVICE_WANTED_BY}
EOF
        fi
    fi

}
