is_server_up and wait_for_server_to_boot up functions

main
Petr Nyc 1 month ago
parent 879f3fb0bf
commit 0cb372ee42
  1. 2
      .p10k.zsh
  2. 4
      .spacemacs
  3. 75
      bin/lib/common.sh

@ -108,7 +108,7 @@
# =========================[ Line #2 ]=========================
newline
# ip # ip address and bandwidth usage for a specified network interface
public_ip # public IP address
# public_ip # public IP address
proxy # system-wide http/https/ftp proxy
# battery # internal battery
# wifi # wifi speed

@ -770,6 +770,7 @@ before packages are loaded."
; testing freeze
(setq org-agenda-files '("~/Documents/org/inbox.org"
"~/Documents/org/projects.org"
"~/Documents/org/work/oracle.org"
"~/Documents/org/someday.org"
"~/Documents/org/calendar.org"
"~/Documents/org/synced_calendar.org"
@ -780,6 +781,7 @@ before packages are loaded."
(setq org-refile-targets '(("/Users/jetpac/Documents/org/projects.org" :maxlevel . 3)
("/Users/jetpac/Documents/org/inbox.org" :maxlevel . 3)
("/Users/jetpac/Documents/org/work/oracle.org" :maxlevel . 3)
("/Users/jetpac/Documents/org/someday.org" :maxlevel . 3)
("/Users/jetpac/Documents/org/inbox.org" :maxlevel . 3)
("/Users/jetpac/Documents/org/calendar.org" :maxlevel . 2)))
@ -789,7 +791,7 @@ before packages are loaded."
;; inspiration from https://emacs.stackexchange.com/questions/33179/how-to-fix-columns-of-org-agenda-clock-report
(setq org-agenda-clockreport-parameter-plist
(quote (:link t :maxlevel 7 :fileskip0 t :compact t :narrow 80 :formula %)))
(quote (:link t :maxlevel 7 :fileskip0 t :compact t :narrow 80 :formula % :match "-home")))
(setq org-agenda-custom-commands
'(

@ -0,0 +1,75 @@
# common useful functions
w3m_safe() {
url="$1"
output=$(/opt/homebrew/bin/w3m "$url" -M -cols 400 -graph -dump 2>&1)
if echo "$output" | grep -q "Can't load"; then
return 2
else
echo "$output"
return 0
fi
}
# Returns:
# 0 = server reachable & command ran
# 1 = network/host-not-up-yet
# 2 = SSH auth failure (likely passphrase/key issue) -> caller should abort
is_server_up() {
host=$1
# Capture only stderr (order of redirs matters: 2>&1 >/dev/null)
err=$(/usr/bin/ssh \
-o BatchMode=yes \
-o PreferredAuthentications=publickey \
-o ConnectTimeout=10 \
-o ServerAliveInterval=20 \
-o ServerAliveCountMax=3 \
-o ConnectionAttempts=1 \
-o StrictHostKeyChecking=no \
-o UserKnownHostsFile=/dev/null \
-o LogLevel=ERROR \
-n -q "$host" 'uname -n || hostname' 2>&1 >/dev/null)
st=$?
[ $st -eq 0 ] && return 0
# Normalize to lowercase for robust matching
err_lc=$(printf '%s' "$err" | tr '[:upper:]' '[:lower:]')
case $err_lc in
*"permission denied"*|\
*"no supported authentication methods available"*|\
*"too many authentication failures"*|\
*"agent admitted failure"*|\
*"publickey authentication failed"*)
printf 'FATAL: SSH authentication failed for "%s": %s\n' "$host" "$err" >&2
return 2
;;
esac
# Anything else (timeout, connection refused, kex read/reset, etc.)
return 1
}
wait_for_server_to_boot_up() {
server=$1
while :; do
if is_server_up "$server"; then
printf '"%s" is up and running.\n' "$server"
return 0
fi
rc=$?
if [ $rc -eq 2 ]; then
printf 'Aborting: SSH authentication failed for "%s" (likely passphrase needed or wrong key).\n' "$server" >&2
exit 2
fi
printf 'Server "%s" is not up\n' "$server"
sleep 10
done
}
# vim: set ts=4 sw=4 tw=0 noet ft=bash:
Loading…
Cancel
Save