Files
podi/recipe/run/container_webcli
2022-02-22 22:23:32 +01:00

71 lines
2.5 KiB
Plaintext

# info: containerized cli-template, perfect for outputting terminal-cmds to web
export POD=$(which podman || which docker || echo "")
hint_systemd(){
service=/etc/systemd/system/$PODI_APP.service
silent which podman || return 0
test -f $service && return 0
try podman generate systemd $PODI_APP 2>/dev/null > $PODI_APP.service
print "to survive server-reboots please run as root:"
echo "podman generate systemd $PODI_APP $service" | soften
echo "systemctl enable $PODI_APP.service" | soften
}
build(){
header .pod/app/container_webcli
test -f Dockerfile || { print "'Dockerfile' not found..skipping build"; }
test -f Dockerfile && {
silent which $POD || print '[!] please install podman (or docker)'
silent which $POD && verbose $POD build -t $PODI_APP .
} | soften
return 0
}
stop(){
header .pod/app/container_webcli
test -z $POD || {
silent try $POD kill $PODI_APP
silent try $POD rm -f $PODI_APP
}
}
start(){
header .pod/app/container_webcli
test -z $PODI_APP && PODI_APP=$(basename $(pwd))
silent test -z $POD && error "please install podman (or docker)"
test -z $POD || {
export PODI_APP=$PODI_APP
eval "$(cat .env)"
POD=$(which podman || which docker)
verbose $POD run -d --volume $(pwd):/home/app -w /home/app -e PORT -p $PORT:$PORT --name $PODI_APP --entrypoint=sh $PODI_APP -c ./app
verbose $POD logs $PODI_APP 2>&1 | awk "{print \"[app.log] \"\$0 }"
hint_systemd
print ""
print "your container is running at $PODI_REMOTE:$PORT"
}
return 0
}
init_runtime(){
test -f app || {
generate(){
echo '#!/bin/sh'
echo 'export JOB="cat hello.txt ; ls -la"'
echo 'which socat &>/dev/null || { echo "please install socat as root"; exit 1; }'
echo 'socat -t2 TCP4-LISTEN:$PORT,fork,max-children=3,forever,reuseaddr SYSTEM:"$JOB",pty,echo=0;'
}
prompt "generate + commit 'app' startfile?" "$(generate | soften)" "[y/n] "
test $answer = "y" || error aborting
generate > app
chmod 755 app
echo "HTTP/1.1 200\n\n H E L L O " > hello.txt
echo "FROM docker.io/alpine/socat" > Dockerfile
echo "export PORT=$(awk 'BEGIN{ srand(); print int(rand()*1000)+8000 }')" >> .env
git add app Dockerfile .env hello.txt .pod podi && git commit -m "adding podi"
}
test -f .pod/checkout/rollback_simple || recipe checkout/rollback_simple
test -f .pod/start/envfile || recipe start/envfile
test -f .pod/init/server/sshkey || recipe init/server/sshkey
}