#!/bin/bash # {{ ansible_managed }} # Power truenas.localdomain back on after an outage, but only when it is # genuinely safe to do so. Called by upssched once mains has been stable, # and once at boot by ups-restore.service (deep-drain recovery). # # Deliberately stateless: we ask the iDRAC whether the chassis is off # rather than tracking whether we were the ones who shut it down. A NAS # powered off by hand while on mains is safe, because no ONLINE event # fires in that case and the boot path checks UPS status first. # # NOTE: as of now this is a best-effort secondary path. The R415 has no # iDRAC6 Enterprise card ("iDRAC6 Ent Pres ... Absent"), so its shared-LOM # BMC has no standby power and goes unreachable whenever the chassis is # off - exactly when we would want it. An unreachable iDRAC is therefore # the EXPECTED case here, and we exit quietly rather than alarming. # # The primary restore path needs no IPMI: the R415 power restore policy is # set to always-on, so when the UPS cuts and then restores its output the # server powers itself back up. Fit an iDRAC6 Enterprise card and switch it # to dedicated mode and this script starts working with no changes. set -uo pipefail TAG=ups-restore UPS={{ ups_name }}@localhost log() { logger -t "$TAG" -- "$*"; echo "$TAG: $*"; } # At boot, upsd may not be serving yet. Wait a bounded amount of time. for _ in $(seq 1 30); do if upsc "$UPS" ups.status >/dev/null 2>&1; then break fi sleep 2 done status=$(upsc "$UPS" ups.status 2>/dev/null || echo UNKNOWN) if [[ "$status" != *OL* ]]; then log "UPS status is '$status', not on line - refusing to power TrueNAS on" exit 0 fi power=$(/usr/local/bin/truenas-power.sh status 2>&1 || true) case "$power" in *"is off"*) log "mains stable and chassis off - powering TrueNAS on" if /usr/local/bin/truenas-power.sh on; then log "power-on command accepted" else log "power-on command FAILED - check iDRAC at {{ idrac_host }}" exit 1 fi ;; *"is on"*) log "TrueNAS already on, nothing to do" ;; *) # Expected while the box has no iDRAC6 Enterprise card: the BMC is # simply not on the network with the chassis powered down. log "iDRAC at {{ idrac_host }} unreachable - relying on the" \ "always-on power restore policy instead ($power)" ;; esac