]> rtime.felk.cvut.cz Git - novaboot.git/commitdiff
Simplify systemd logic for automated delayed power off
authorMichal Sojka <michal.sojka@cvut.cz>
Thu, 21 Nov 2019 11:21:39 +0000 (12:21 +0100)
committerMichal Sojka <michal.sojka@cvut.cz>
Thu, 21 Nov 2019 11:33:50 +0000 (12:33 +0100)
TODO documentation

debian/novaboot-server.install
server/Makefile
server/adduser-novaboot
server/novaboot-shell
server/systemd/novaboot-server-session@.service [deleted file]
server/systemd/novaboot-target-off@.target [deleted file]
server/systemd/novaboot-target-off@.timer [deleted file]
server/systemd/novaboot-target-on@.service [deleted file]
server/systemd/user@999.d/novaboot-server.conf [deleted file]

index 5e1fdda3c07eecfbdaae552507d30c9631f26a38..43001b2fa6d8e70e72db3947cada44ea91e689d3 100644 (file)
@@ -1,8 +1,10 @@
-/lib/systemd/system/novaboot-target-off@.target
-/lib/systemd/system/novaboot-target-off@.timer
-/lib/systemd/system/novaboot-target-on@.service
-/lib/systemd/system/novaboot-server-session@.service
+/etc/sudoers.d/novaboot-server
+/lib/systemd/system/novaboot-power-off@.target
+/lib/systemd/system/novaboot-power-off@.timer
+/lib/systemd/system/novaboot-power@.service
+/usr/bin/novaboot-power
 /usr/bin/novaboot-shell
+/usr/lib/systemd/user/novaboot-power-off.service
 /usr/lib/sysusers.d/novaboot-server.conf
 /usr/sbin/adduser-novaboot
 /usr/share/man/man1/novaboot-shell.1
index 44d31110930df32b116c15967448436b77d8e8bf..9d0413fe7240651b032b1687434d036eda71a1a0 100644 (file)
@@ -14,7 +14,9 @@ install: all
        install -D -m 755 -t $(DESTDIR)$(PREFIX)/sbin adduser-novaboot
        install -D -m 644 -t $(DESTDIR)$(PREFIX)/share/man/man8 adduser-novaboot.8
        install -D -m 644 -t $(DESTDIR)$(PREFIX)/lib/sysusers.d sysusers.d/novaboot-server.conf
-       install -D -m 644 -t $(DESTDIR)/lib/systemd/system systemd/novaboot-target-off@.target
-       install -D -m 644 -t $(DESTDIR)/lib/systemd/system systemd/novaboot-target-off@.timer
-       install -D -m 644 -t $(DESTDIR)/lib/systemd/system systemd/novaboot-target-on@.service
-       install -D -m 644 -t $(DESTDIR)/lib/systemd/system systemd/novaboot-server-session@.service
+       install -D -m 644 -t $(DESTDIR)/lib/systemd/system systemd/novaboot-power-off@.target
+       install -D -m 644 -t $(DESTDIR)/lib/systemd/system systemd/novaboot-power-off@.timer
+       install -D -m 644 -t $(DESTDIR)/lib/systemd/system systemd/novaboot-power@.service
+       install -D -m 644 -t $(DESTDIR)/usr/lib/systemd/user systemd/novaboot-power-off.service
+       install -D -m 440 systemd/sudoers.novaboot-server $(DESTDIR)/etc/sudoers.d/novaboot-server
+       install -D -m 755 -t $(DESTDIR)$(PREFIX)/bin systemd/novaboot-power
index 928e76358cb4bc01dbee7449da8ef380c469e602..b4417d42e10303fef30561ca5b86dc2445efe668 100755 (executable)
@@ -69,17 +69,6 @@ else
     echo "NOT creating /srv/tftp/$user and symlink to it from $home/tftproot."
 fi
 
-if [ -d /run/systemd/system ]; then
-    echo "Installing systemd services and timers in /etc/systemd/system/user@$uid.service.d"
-    mkdir -p /etc/systemd/system/user@$uid.service.d
-    cat <<EOF > /etc/systemd/system/user@$uid.service.d/novaboot-server.conf
-[Unit]
-Requires=novaboot-server-session@$user.service
-After=novaboot-server-session@$user.service
-EOF
-    systemctl daemon-reload
-fi
-
 echo "Creating configuration template in $home/.novaboot-shell"
 cat <<'CONFIG_EOF' > $home/.novaboot-shell
 #!/bin/sh
@@ -105,6 +94,18 @@ cat <<'CONFIG_EOF' > $home/.novaboot-shell
 CONFIG_EOF
 chown $user: $home/.novaboot-shell
 
+if [ -d /run/systemd/system ]; then
+    cat <<EOF
+To enable automatic delayed power-off run 'systemctl --user enable novaboot-power-off'
+as the created user (e.g. via shell subcommand)
+EOF
+    # TODO: Add a way to run in automatically from here (allow shell
+    #subcommand to accept command lines)
+    #systemctl --user enable novaboot-power-off
+fi
+
+
+
 echo "Done"
 exit 0
 
index a9300b725f9f26f9c248824b02673d45862a9454..e9c60f793a5a91d606aaf079495a8dc2baaff1f3 100755 (executable)
@@ -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
 }
 
@@ -173,7 +193,7 @@ else
     mkdir -p "$RUN_DIR"
 fi
 
-if [ -z "$NOVABOOT_ID" ]; then
+if [ -z "$NOVABOOT_ID" ] && [ "$PPID" -ne 1 ]; then
     main "$@"
 else
     run_subcommand "$@"
diff --git a/server/systemd/novaboot-server-session@.service b/server/systemd/novaboot-server-session@.service
deleted file mode 100644 (file)
index 6efe679..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-[Unit]
-Description=Novaboot target on/off manager for %i
-StopWhenUnneeded=true
-
-Conflicts=novaboot-target-off@%i.timer
-
-[Service]
-Type=oneshot
-RemainAfterExit=yes
-ExecStart=/bin/systemctl start novaboot-target-on@%i.service
-ExecStop=/bin/systemctl start novaboot-target-off@%i.timer
diff --git a/server/systemd/novaboot-target-off@.target b/server/systemd/novaboot-target-off@.target
deleted file mode 100644 (file)
index bcaee09..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-[Unit]
-Description=Power off a novaboot target %i
-Conflicts=novaboot-target-on@%i.service
-Conflicts=novaboot-target-off@%i.timer
-After=novaboot-target-on@%i.service
diff --git a/server/systemd/novaboot-target-off@.timer b/server/systemd/novaboot-target-off@.timer
deleted file mode 100644 (file)
index 04dd422..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-[Timer]
-OnActiveSec=10min
-Unit=novaboot-target-off@%i.target
diff --git a/server/systemd/novaboot-target-on@.service b/server/systemd/novaboot-target-on@.service
deleted file mode 100644 (file)
index 63a5639..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-[Unit]
-Description=Power on novaboot target %i
-
-[Service]
-Type=oneshot
-RemainAfterExit=true
-ExecStart=/usr/bin/novaboot-shell -c on
-ExecStop=/usr/bin/novaboot-shell -c off
-User=%i
diff --git a/server/systemd/user@999.d/novaboot-server.conf b/server/systemd/user@999.d/novaboot-server.conf
deleted file mode 100644 (file)
index 1221da4..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-[Unit]
-Requires=novaboot-server-session@tx1.service
-After=novaboot-server-session@tx1.service