Skip to content

Commit 2291d4a

Browse files
committed
feat: support dry-run mode
1 parent 532ef9f commit 2291d4a

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

shutdowncheck.sh

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,16 @@ if [ -n "${failed:-}" ]; then
105105
exit 1
106106
fi
107107

108+
if [ -n "${ATX_RASPI_DRY_RUN:-}" ]; then
109+
handle_press() {
110+
echo "[dry-run] $*"
111+
}
112+
else
113+
handle_press() {
114+
"$@"
115+
}
116+
fi
117+
108118
if { command -v gpioset && command -v gpiomon ; } 1>/dev/null 2>&1; then
109119
init_shutdown_pin() {
110120
# NOP
@@ -130,10 +140,11 @@ if { command -v gpioset && command -v gpiomon ; } 1>/dev/null 2>&1; then
130140

131141
if [ "$pulseDuration" -gt "$REBOOTPULSEMAXIMUM" ]; then
132142
header 12 "SHUTDOWN request on chip ${CHIP?} from GPIO${SHUTDOWN}, halting Rpi ..."
143+
handle_press poweroff
133144
return
134145
elif [ "$pulseDuration" -gt "$REBOOTPULSEMINIMUM" ]; then
135146
header 12 "REBOOT request on chip ${CHIP?} from GPIO${SHUTDOWN?}, recycling Rpi ..."
136-
reboot
147+
handle_press reboot
137148
return
138149
else
139150
unset pulseStart pulseEnd
@@ -171,15 +182,15 @@ elif [ -e /sys/class/gpio/export ]; then
171182
sleep 0.02
172183
if [ "$(( "$(date +%s%N)" - pulseStart ))" -gt "$REBOOTPULSEMAXIMUM" ]; then
173184
header 12 "SHUTDOWN request from GPIO${SHUTDOWN}, halting Rpi ..."
174-
poweroff
185+
handle_press poweroff
175186
return
176187
fi
177188
shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value)
178189
done
179190
#pulse went LOW, check if it was long enough, and trigger reboot
180191
if [ "$(( "$(date +%s%N)" - pulseStart ))" -gt "$REBOOTPULSEMINIMUM" ]; then
181192
header 12 "REBOOT request from GPIO${SHUTDOWN}, recycling Rpi ..."
182-
reboot
193+
handle_press reboot
183194
return
184195
fi
185196
fi

shutdownirq.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ def handle_press(*command):
3939
print("[dry-run] ", " ".join(command))
4040
sys.exit()
4141

42+
if os.environ.get("ATX_RASPI_DRY_RUN", "") == "":
43+
def handle_press(*command):
44+
os.system(*command)
45+
sys.exit()
46+
else:
47+
def handle_press(*command):
48+
print("[dry-run] ", " ".join(command))
49+
sys.exit()
50+
4251
def diag(*msgs):
4352
linelen = max([len(msg) for msg in msgs]) + 2
4453
wrapper = "=" * linelen
@@ -92,12 +101,12 @@ def announce():
92101
if pulse_duration >= REBOOTPULSEMAXIMUM:
93102
print()
94103
diag("SHUTDOWN request on chip {0} from GPIO{1}, halting Rpi ...".format(CHIP, SHUTDOWN))
95-
os.system("poweroff")
104+
handle_press("poweroff")
96105
sys.exit()
97106
elif pulse_duration >= REBOOTPULSEMINIMUM:
98107
print()
99108
diag("REBOOT request on chip {0} from GPIO{1}, recycling Rpi ...".format(CHIP, SHUTDOWN))
100-
os.system("reboot")
109+
handle_press("reboot")
101110
sys.exit()
102111
else:
103112
pulse_start = None
@@ -125,13 +134,13 @@ def announce():
125134
if(time.time() - pulse_start >= REBOOTPULSEMAXIMUM):
126135
print()
127136
diag("SHUTDOWN request from GPIO{0}, halting Rpi ...".format(SHUTDOWN))
128-
os.system("poweroff")
137+
handle_press("poweroff")
129138
sys.exit()
130139
shutdown_signal = GPIO.input(SHUTDOWN)
131140
if time.time() - pulse_start >= REBOOTPULSEMINIMUM:
132141
print()
133142
diag("REBOOT request from GPIO{0}, recycling Rpi ...".format(SHUTDOWN))
134-
os.system("reboot")
143+
handle_press("reboot")
135144
sys.exit()
136145
if GPIO.input(SHUTDOWN): # before looping we must make sure the shutdown signal went low
137146
GPIO.wait_for_edge(SHUTDOWN, GPIO.FALLING)

0 commit comments

Comments
 (0)