2
0
Fork 0
rootfs/usr/local/bin/sh.zio.pod-systemd

57 lines
No EOL
1.6 KiB
Bash
Executable file

#!/usr/bin/env bash
if [[ "$(realpath "$(dirname "$(realpath "$0")")/../../../")" == "/" ]]; then
. /usr/local/libexec/zio/helpers/bash.sh
else
. "$(dirname "$(realpath "$0")")/../libexec/zio/helpers/bash.sh"
fi
test_prog "podlet"
test_prog "podman"
service_name="$1"
command_args=("${@:2}")
if [[ -z "$service_name" ]]; then
say warning "No service name given, exiting"
exit 1
fi
if [[ ${#command_args[@]} -eq 0 ]]; then
say warning "No command args given, exiting"
exit 1
fi
# Force the container name to service_name: overwrite any --name passed in the
# podman arguments, or inject one if absent. This keeps the systemd unit name
# (which podlet derives from the container name) always equal to service_name.
has_name=false
for ((i = 0; i < ${#command_args[@]}; i++)); do
case "${command_args[i]}" in
--name)
command_args[i+1]="$service_name"
has_name=true
break
;;
--name=*)
command_args[i]="--name=$service_name"
has_name=true
break
;;
esac
done
if [[ "$has_name" == false ]]; then
command_args=(--name "$service_name" "${command_args[@]}")
fi
say info "Creating systemd service for $service_name"
podlet -u -i --overwrite -d "$service_name container" podman run "${command_args[@]}"
if [[ $(id -u) -eq 0 ]]; then
systemctl daemon-reload
systemctl start "$service_name"
say primary "System service file for $service_name created and started"
else
systemctl --user daemon-reload
systemctl --user start "$service_name"
say primary "User service file for $service_name created and started"
fi