]> rtime.felk.cvut.cz Git - novaboot.git/blobdiff - server/novaboot-shell
Simplify systemd logic for automated delayed power off
[novaboot.git] / server / novaboot-shell
index 1842d7ae0201653f0d5a0196c1685259b4cdf692..e9c60f793a5a91d606aaf079495a8dc2baaff1f3 100755 (executable)
@@ -16,7 +16,6 @@ Target commands:
 - off
 - rsync ...
 - get-config
-- ssh [options]
 
 Management commands:
 - help
@@ -79,9 +78,7 @@ print_queue() {
 
 locked() {
     print_queue
-    no_fork=
-    #flock -h 2>&1 | grep -q -e "--no-fork" && no_fork=--no-fork
-    exec flock $no_fork "$RUN_DIR" "$@"
+    exec flock --no-fork "$RUN_DIR" "$@"
 }
 
 unlocked() {
@@ -92,6 +89,21 @@ read_config() {
     . "${NOVABOOT_SHELL_CONFIG:-$HOME/.novaboot-shell}"
 }
 
+power() {
+    local cmd
+    case "$1" in
+       "on")  cmd="${on_cmd:?}";;
+       "off") cmd="${off_cmd:?}";;
+       *) die "Unexpected power parameter";;
+    esac
+
+    if [ "$PPID" -ne 1 ] && systemctl --user is-enabled novaboot-power-off.service; then
+       sudo novaboot-power "$1"
+    else
+       eval "$cmd"
+    fi
+}
+
 # run_subcommand should be called only after permission checks and/or locking
 run_subcommand() {
     read_config
@@ -101,6 +113,7 @@ run_subcommand() {
            echo $NOVABOOT_PPID > $RUN_DIR/ppid
            echo 'novaboot-shell: Connected'
            # TODO: $reset_begin_cmd
+           [ -n "${on_cmd}" ] && power on
            eval exec "${console_cmd:?}";;
        "reset")
            eval exec "${reset_cmd:?}";;
@@ -112,15 +125,13 @@ run_subcommand() {
            cd "$HOME/tftproot"
            exec "$@";;
        "on")
-           eval exec "${on_cmd:?}";;
+           power on
+           exit;;
        "off")
-           eval exec "${off_cmd:?}";;
-       "ssh"|"ssh "*)
-           shift
-           # TODO: sanitize ssh arguments
-           exec ssh "$@" "${ssh_dest:?}";;
+           power off
+           exit;;
        *)
-           die "Unsupported command";;
+           die "Unknown command: $*";;
     esac
 }
 
@@ -134,7 +145,7 @@ main() {
     NB_ADMIN=
     if [ "$1" = "user" ]; then
        # Get user name encoded in ~/.ssh/authorized_keys
-       NB_USER="$2";
+       export NB_USER="$2";
        [ "$3" = "admin" ] && NB_ADMIN=1
        set -- $SSH_ORIGINAL_COMMAND
     fi
@@ -164,7 +175,7 @@ main() {
        # to the console. "The same user" means that we were executed by
        # the same sshd process that has the lock. This is ensured by
        # using SSH connection sharing on client side.
-       reset | rsync | on | off | ssh)
+       reset | rsync | on | off)
            ALLOWED_PPID=$(cat $RUN_DIR/ppid 2>/dev/null || :)
            if [ "$PPID" -eq "${ALLOWED_PPID:-0}" ]; then run=unlocked; else run=locked; fi
            $run $0 "$@";;
@@ -175,9 +186,14 @@ main() {
     esac
 }
 
-RUN_DIR="$HOME"
+if [ -d "$HOME" ]; then
+    RUN_DIR="$HOME"
+else
+    RUN_DIR="/tmp/novaboot-shell@$USER"
+    mkdir -p "$RUN_DIR"
+fi
 
-if [ -z "$NOVABOOT_ID" ]; then
+if [ -z "$NOVABOOT_ID" ] && [ "$PPID" -ne 1 ]; then
     main "$@"
 else
     run_subcommand "$@"