deploy.sh: force PAGER=cat to defeat pager over ssh -tt TTY
With -tt allocating a remote PTY, systemctl and journalctl would sometimes open a pager (less/more) even with --no-pager, leaving the script blocked until the user hits q or Ctrl-C. Force PAGER=cat and SYSTEMD_PAGER=cat inside every remote sudo call and inside the status_block journalctl command. Add --output=cat to journalctl too as belt-and-suspenders. Status output is also piped through | head -3 / | head -20 to guarantee a finite output even if the pager or color escape handling misbehaves.
This commit is contained in:
+9
-8
@@ -46,20 +46,19 @@ sudo_run() {
|
||||
}
|
||||
|
||||
# run_remote_sudo <ssh-prefix> <password> <cmd...>
|
||||
# Runs `cmd` on the remote host, with the wrapped command preceded
|
||||
# by sudo_run. Used so the rest of the script can stay readable.
|
||||
# Runs `cmd` on the remote host under sudo, piping the password
|
||||
# through `sudo -S -p ''`. We also force PAGER/SYSTEMD_PAGER=cat
|
||||
# inside the sh -c so any systemctl / journalctl call (even if
|
||||
# piped further) doesn't open a pager over the ssh -tt TTY.
|
||||
run_remote_sudo() {
|
||||
local ssh_prefix="$1"
|
||||
local pw="$2"
|
||||
shift 2
|
||||
# Build the inner shell command: pipe pw to sudo -S -p '' "cmd".
|
||||
# We pre-marshal the command into a single string the SSH side can
|
||||
# `eval` safely (single-quote the arguments).
|
||||
local inner=""
|
||||
for a in "$@"; do
|
||||
inner+=" '"$(printf %s "$a" | sed "s/'/'\\\\''/g")"'"
|
||||
done
|
||||
$ssh_prefix "printf '%s\n' '$pw' | sudo -S -p '' sh -c '$inner'"
|
||||
$ssh_prefix "printf '%s\n' '$pw' | sudo -S -p '' sh -c 'export PAGER=cat SYSTEMD_PAGER=cat; $inner'"
|
||||
}
|
||||
|
||||
# ponytail: Wipe-and-replace. The deploys are stateful on the VM only via
|
||||
@@ -82,12 +81,14 @@ install_unit() {
|
||||
# Prints a short status + last 10 journal lines. Uses sudo -S for
|
||||
# journalctl (always root-only) but `systemctl status` works for
|
||||
# the administrator's own services without sudo on most distros.
|
||||
# PAGER/SYSTEMD_PAGER=cat and --output=cat on journalctl defeat
|
||||
# the pager that would otherwise kick in over the ssh -tt TTY.
|
||||
status_block() {
|
||||
local ssh_prefix="$1"
|
||||
local unit="$2"
|
||||
local pw="$3"
|
||||
$ssh_prefix "echo ' status:'; systemctl --no-pager --full status $unit 2>/dev/null | head -3"
|
||||
$ssh_prefix "echo ' journal (last 10):'; printf '%s\n' '$pw' | sudo -S -p '' journalctl -u $unit -n 10 --no-pager"
|
||||
$ssh_prefix "echo ' status:'; PAGER=cat SYSTEMD_PAGER=cat systemctl --no-pager --full status $unit 2>/dev/null | head -3"
|
||||
$ssh_prefix "echo ' journal (last 10):'; export PAGER=cat SYSTEMD_PAGER=cat; printf '%s\n' '$pw' | sudo -S -p '' journalctl -u $unit -n 10 --no-pager --output=cat 2>/dev/null | head -20"
|
||||
}
|
||||
|
||||
# restart_unit <ssh-prefix> <unit> <password>
|
||||
|
||||
Reference in New Issue
Block a user