#!/usr/bin/env lua5.1 local utils = {} function utils.join_table(table, separator) local string = "" for i=1,#table do string = string .. table[i] .. separator end return string end local env_string = "" local netns_string = "" local USE_NETNS = os.getenv "USE_NETNS" if USE_NETNS ~= nil then env_string = env_string .. "USE_NETNS="..USE_NETNS.." " netns_string = "ip netns exec "..USE_NETNS.." " end local dependencies_string = "" local DEPENDENCIES = os.getenv "DEPENDENCIES" if DEPENDENCIES ~= nil then env_string = env_string .. "DEPENDENCIES='"..DEPENDENCIES.."' " for dep in string.gmatch(DEPENDENCIES, "[%w-]+") do dependencies_string = dependencies_string .. "sv status "..dep.." || exit 1\n" end end local user = arg[1] local container_name = arg[2] local image_name = arg[3] local group = user local GROUP = os.getenv "GROUP" if GROUP ~= nil then env_string = env_string .. "GROUP="..GROUP.." " group = GROUP end local arguments = {unpack(arg, 4)} for i=1,#arguments do arguments[i] = "'"..arguments[i].."'" end local prefix = [[ #!/bin/sh # Generated with ]]..utils.join_table({env_string..arg[0], user, container_name, image_name, unpack(arguments)}, " ")..[[ ]]..dependencies_string..[[ IMAGE=']]..image_name..[[' CONTAINER_NAME=']]..container_name..[[' export USER=']]..user..[[' export GROUP=']]..group..[[' export HOME="/home/$USER" cd "$HOME" ]] local run_script = prefix..[[ chpst -u "$USER:$GROUP" podman pull "$IMAGE" 2>/dev/stdout || exit $? chpst -u "$USER:$GROUP" podman image prune -f 2>/dev/stdout || exit $? exec ]]..netns_string..[[chpst -u "$USER:$GROUP" podman run --tty --rm \ --name="$CONTAINER_NAME" \ --replace \ ]]..utils.join_table(arguments, " ")..[[ \ "$IMAGE" 2>/dev/stdout ]] local finish_script = prefix..[[ chpst -u "$USER:$GROUP" podman stop --ignore "$CONTAINER_NAME" 2>/dev/stdout || exit $? chpst -u "$USER:$GROUP" podman rm --force --ignore "$CONTAINER_NAME" 2>/dev/stdout || exit $? ]] local function write_script(file_name, content) local file = io.open(file_name, "w+") file:write(content) file:close() os.execute("chmod +x '"..file_name.."'") end write_script("run", run_script) write_script("finish", finish_script)