This commit is contained in:
Cat /dev/Nulo 2022-09-16 15:41:33 -03:00
commit 540ef75dbf

60
runit-podman-gen.lua Executable file
View file

@ -0,0 +1,60 @@
#!/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 user = arg[1]
local container_name = arg[2]
local image_name = arg[3]
local arguments = {unpack(arg, 4)}
for i=1,#arguments do
arguments[i] = "'"..arguments[i].."'"
end
local prefix = [[
#!/bin/sh
# Generated with ]]..utils.join_table({arg[0], user, container_name, image_name, unpack(arguments)}, " ")..[[
IMAGE=']]..image_name..[['
CONTAINER_NAME=']]..container_name..[['
export USER=']]..user..[['
export HOME="/home/$USER"
cd "$HOME"
]]
local run_script = prefix..[[
chpst -u "$USER:$USER" podman pull "$IMAGE" 2>/dev/stdout || exit $?
chpst -u "$USER:$USER" podman image prune -f 2>/dev/stdout || exit $?
exec chpst -u "$USER:$USER" podman run --tty --rm \
--name="$CONTAINER_NAME" \
--replace \
]]..utils.join_table(arguments, " ")..[[ \
"$IMAGE" 2>/dev/stdout
]]
local down_script = prefix..[[
chpst -u "$USER:$USER" podman stop --ignore "$CONTAINER_NAME" 2>/dev/stdout || exit $?
chpst -u "$USER:$USER" 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("down", down_script)