commit 540ef75dbfcce7e45657ca68783e66c8e9e04d25 Author: Nulo Date: Fri Sep 16 15:41:33 2022 -0300 init diff --git a/runit-podman-gen.lua b/runit-podman-gen.lua new file mode 100755 index 0000000..6ba8b2c --- /dev/null +++ b/runit-podman-gen.lua @@ -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)