]> 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 53df0eb6d5fd43b00e4bd46ca79e1027ad2f11cb..e9c60f793a5a91d606aaf079495a8dc2baaff1f3 100755 (executable)
@@ -10,11 +10,12 @@ die() {
 print_help() {
     cat <<EOF
 Target commands:
-- console
+- console (default command)
 - reset
 - on
 - off
 - rsync ...
+- get-config
 
 Management commands:
 - help
@@ -52,6 +53,9 @@ add_key() {
 
 exec_shell() {
     [ "$NB_ADMIN" ] || die "Permission denied"
+    if ! tty > /dev/null; then
+       echo "novaboot-shell: Consider starting the shell with 'ssh -t'"
+    fi
     exec /bin/bash || exec /bin/sh
 }
 
@@ -74,11 +78,7 @@ print_queue() {
 
 locked() {
     print_queue
-    tmp=$(mktemp)
-    no_fork=
-    flock -h 2>&1 | grep -q -e "--no-fork" && no_fork=--no-fork
-    rm -f "$tmp"
-    exec flock $no_fork "$RUN_DIR" "$@"
+    exec flock --no-fork "$RUN_DIR" "$@"
 }
 
 unlocked() {
@@ -89,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
@@ -98,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:?}";;
@@ -109,9 +125,13 @@ run_subcommand() {
            cd "$HOME/tftproot"
            exec "$@";;
        "on")
-           eval exec "${on_cmd:?}";;
+           power on
+           exit;;
        "off")
-           eval exec "${off_cmd:?}";;
+           power off
+           exit;;
+       *)
+           die "Unknown command: $*";;
     esac
 }
 
@@ -125,15 +145,17 @@ 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
 
-    if [ $# -eq 0 ]; then print_help; fi
-
     IP=${SSH_CONNECTION%% *}
-    HOST=$(getent hosts $IP) || HOST=$IP
+    if [ "$IP" ]; then
+       HOST=$(getent hosts $IP) || HOST=$IP
+    else
+       HOST=localhost
+    fi
     REMOTE=${HOST##* }
     DATE=$(LANG=C date +'%F_%T')
     export NOVABOOT_ID="${NB_USER:-?} $DATE ${REMOTE}"
@@ -143,7 +165,7 @@ main() {
 
     case "$1" in
        # Commands allowed at any time
-       "console") locked $0 console;;
+       "console"|"") locked $0 console;;
        "get-config") read_config && echo -n "${target_config}"; exit;;
        "add-key") shift; add_key "$@"; exit;;
        "shell") exec_shell; exit;;
@@ -152,7 +174,7 @@ main() {
        # Commands allowed only when nobody or the same user is connected
        # 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 cline side.
+       # using SSH connection sharing on client side.
        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
@@ -164,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 "$@"
@@ -182,22 +209,22 @@ novaboot-shell - provides novaboot with unified SSH-based interface for controll
 
 =head1 SYNOPSIS
 
-B<novaboot-shell> -c "command [arguments...]"
+B<novaboot-shell> -c "[command [arguments...]]"
 
-B<novaboot-shell> command [arguments...]
+B<novaboot-shell> [command [arguments...]]
 
-B<ssh target@server> command [arguments...]
+B<ssh target@server> [command [arguments...]]
 
 =head1 DESCRIPTION
 
-B<novaboot-shell> provides L<novaboot(1)> with unified SSH-based
-interface for controlling target hardware. This simplifies client-side
-configuration, because typically only I<-ssh=...> option is needed on
-the client side. B<novaboot-shell> is typically configured as a login
-shell of special user accounts associated with target hardware. It
-ensures that users are able to perform only a limited set of action
-(see L</COMMANDS> below) with the target and have no shell access on
-the server.
+B<novaboot-shell> provides L<novaboot(1)> with unified SSH-based
+interface for controlling the target hardware. This simplifies
+client-side configuration, because clients typically need only the
+I<--ssh=...> option. B<novaboot-shell> is typically configured as a
+login shell of special user accounts associated with the target
+hardware (as set by L<adduser-novaboot(8)>). It ensures that users can
+perform only a limited set of actions (see L</COMMANDS> below) with
+the target and have no shell access on the server.
 
 =head1 COMMANDS
 
@@ -215,6 +242,9 @@ SSH connection. This can be accomplished by using SSH connection
 sharing, which is what L<novaboot(1)> uses (see I<-M> and I<-S> in
 L<ssh(1)>).
 
+This is the default command when no command is specified on command
+line.
+
 =item reset
 
 Reset the target hardware.
@@ -247,6 +277,11 @@ When "admin" is specified after the user name, this user is considered
 an administrator and is allowed to run L</add-key> and L</shell>
 commands.
 
+=item get-config
+
+Prints novaboot configuration options needed for the target. One
+option per line.
+
 =back
 
 =head2 Administration commands