2022-09-16 18:41:33 +00:00
|
|
|
#!/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
|
|
|
|
|
2022-09-16 19:06:33 +00:00
|
|
|
local env_string = ""
|
|
|
|
local netns_string = ""
|
|
|
|
local USE_NETNS = os.getenv "USE_NETNS"
|
|
|
|
if USE_NETNS ~= nil then
|
2022-09-16 19:17:37 +00:00
|
|
|
env_string = env_string .. "USE_NETNS="..USE_NETNS.." "
|
2022-09-16 19:06:33 +00:00
|
|
|
netns_string = "ip netns exec "..USE_NETNS.." "
|
|
|
|
end
|
2022-09-16 19:17:37 +00:00
|
|
|
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
|
2022-09-16 19:06:33 +00:00
|
|
|
|
2022-09-16 18:41:33 +00:00
|
|
|
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
|
2022-09-16 19:06:33 +00:00
|
|
|
# Generated with ]]..utils.join_table({env_string..arg[0], user, container_name, image_name, unpack(arguments)}, " ")..[[
|
2022-09-16 18:41:33 +00:00
|
|
|
|
2022-09-16 19:17:37 +00:00
|
|
|
]]..dependencies_string..[[
|
|
|
|
|
2022-09-16 18:41:33 +00:00
|
|
|
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 $?
|
|
|
|
|
2022-09-16 19:06:33 +00:00
|
|
|
exec ]]..netns_string..[[chpst -u "$USER:$USER" podman run --tty --rm \
|
2022-09-16 18:41:33 +00:00
|
|
|
--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)
|