summaryrefslogtreecommitdiff
path: root/src/helpers/commit-confirm-notify.py
blob: 69dda511232fe4a46af776fab00b8607b4b3b9a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python3
import os
import sys
import time

# Minutes before reboot to trigger notification.
intervals = [1, 5, 15, 60]


def notify(interval):
    s = '' if interval == 1 else 's'
    time.sleep((minutes - interval) * 60)
    message = (
        '"[commit-confirm] System is going to reboot in '
        f'{interval} minute{s} to rollback the last commit.\n'
        'Confirm your changes to cancel the reboot."'
    )
    os.system('wall -n ' + message)


if __name__ == '__main__':
    # Must be run as root to call wall(1) without a banner.
    if len(sys.argv) != 2 or os.getuid() != 0:
        print('This script requires superuser privileges.', file=sys.stderr)
        exit(1)
    minutes = int(sys.argv[1])
    # Drop the argument from the list so that the notification
    # doesn't kick in immediately.
    if minutes in intervals:
        intervals.remove(minutes)
    for interval in sorted(intervals, reverse=True):
        if minutes >= interval:
            notify(interval)
            minutes -= minutes - interval
    exit(0)