diff --git a/usr/local/bin/sh.zio.pod-systemd b/usr/local/bin/sh.zio.pod-systemd index 8d55465..97e16e8 100755 --- a/usr/local/bin/sh.zio.pod-systemd +++ b/usr/local/bin/sh.zio.pod-systemd @@ -1,36 +1,57 @@ #!/usr/bin/env bash -if [[ "$(realpath "$(dirname "$(realpath -s "$0")")/../../../")" == "/" ]]; then +if [[ "$(realpath "$(dirname "$(realpath "$0")")/../../../")" == "/" ]]; then . /usr/local/libexec/zio/helpers/bash.sh else - . "$(dirname "$(realpath -s "$0")")/../libexec/zio/helpers/bash.sh" + . "$(dirname "$(realpath "$0")")/../libexec/zio/helpers/bash.sh" fi test_prog "podlet" test_prog "podman" service_name="$1" -command_args="${@:2}" +command_args=("${@:2}") -if [[ -z $service_name ]]; then +if [[ -z "$service_name" ]]; then say warning "No service name given, exiting" - exit + exit 1 +fi +if [[ ${#command_args[@]} -eq 0 ]]; then + say warning "No command args given, exiting" + exit 1 fi -if [[ -z $command_args ]]; then - say warning "No command args given, exiting" - exit +# 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[@]}" -podlet -u -i -d \"$service_name container\" podman run $command_args -if [[ $(id -u) = 0 ]]; then +if [[ $(id -u) -eq 0 ]]; then systemctl daemon-reload - systemctl start $service_name + 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 + systemctl --user start "$service_name" say primary "User service file for $service_name created and started" -fi +fi \ No newline at end of file