chiark / gitweb /
add ncdu to -recommended
[dsa-metapackages.git] / buildd-reboot
1 #!/bin/bash
2
3 # script to shutdown a system running buildd in a controlled way
4
5 # Copyright (c) 2009 Peter Palfrader <peter@palfrader.org>
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining
8 # a copy of this software and associated documentation files (the
9 # "Software"), to deal in the Software without restriction, including
10 # without limitation the rights to use, copy, modify, merge, publish,
11 # distribute, sublicense, and/or sell copies of the Software, and to
12 # permit persons to whom the Software is furnished to do so, subject to
13 # the following conditions:
14 #
15 # The above copyright notice and this permission notice shall be
16 # included in all copies or substantial portions of the Software.
17 #
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 set -e
27 set -u
28
29
30 usage() {
31         echo "Usage: $0 [-h] <reason>" >&2
32         echo "        -h is for halt, not for help"
33         exit 3
34 }
35
36 halt=0
37 if [ "${1:-""}" == '-h' ] ; then
38         halt=1
39         shift
40 fi
41
42 if [ "$#" != 1 ]; then
43         usage
44 fi
45 reason="$1"
46
47
48
49 wall_counter=0
50 maybe_wall() {
51         wall_counter=$((wall_counter-1))
52         if [ "$wall_counter" -le 0 ]; then
53                 if [ "$halt" = 1 ]; then
54                         echo "System will halt for $reason when buildd has stopped" | wall
55                 else
56                         echo "System will reboot for $reason when buildd has stopped" | wall
57                 fi
58                 wall_counter=720
59         fi
60 }
61
62 buildd_wait_and_reboot() {
63         echo -n "Waiting for buildd to shut down"
64
65         while test -e ~buildd/build/buildd.pid ; do
66                 echo -n "."
67                 sleep 5
68                 maybe_wall
69         done
70         echo
71
72         if pgrep -u buildd -x buildd ; then
73                 echo "pidfile is gone, but buildd process still runs"
74                 while pgrep -u buildd -x buildd ; do
75                         echo -n "."
76                         sleep 5
77                         maybe_wall
78                 done
79                 echo
80         fi
81
82         if grep 'waiting-for-clean-shutdown' ~buildd/NO-DAEMON-PLEASE > /dev/null; then
83                 sudo -u buildd sh -c 'echo delete-on-boot > ~buildd/NO-DAEMON-PLEASE'
84         fi
85         if [ "$halt" = 1 ]; then
86                 /sbin/shutdown -h 1 "$reason"
87         else
88                 /sbin/shutdown -r 1 "$reason"
89         fi
90 }
91
92 if [ "`id -u`" != 0 ]; then
93         echo "This probably wants running as root" >&2
94         exit 3
95 fi
96 if ! id buildd > /dev/null 2>&1; then
97         echo "Is there a buildd user?" >&2
98         exit 3
99 fi
100 if ! [ -d ~buildd ]; then
101         echo "No ~buildd?" >&2
102         exit 3
103 fi
104
105 if ! test -e ~buildd/NO-DAEMON-PLEASE; then
106         if [ -e ~buildd/build/buildd.pid ] ; then
107                 echo "Touching ~buildd/NO-DAEMON-PLEASE ~buildd/EXIT-DAEMON-PLEASE"
108                 sudo -u buildd touch ~buildd/NO-DAEMON-PLEASE ~buildd/EXIT-DAEMON-PLEASE
109                 sudo -u buildd sh -c 'echo waiting-for-clean-shutdown > ~buildd/NO-DAEMON-PLEASE'
110                 sudo chgrp -v adm ~buildd/NO-DAEMON-PLEASE
111
112                 echo "Sending HUP to buildd"
113                 sudo -u buildd kill -HUP `sudo cat ~buildd/build/buildd.pid`
114         else
115                 echo "no-daemon-please does not exist, but there is no buildd.pid file either"
116                 if pgrep -u buildd -x buildd ; then
117                         echo "But there is a buildd running.  Bad?"
118                         exit 3
119                 fi
120         fi
121 else
122         echo "no-daemon-please already exists"
123 fi
124
125 buildd_wait_and_reboot