chiark / gitweb /
update snapshot ruby depends for jessie
[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 touch_stuff() {
48         local user="$1"
49         local home=$(getent passwd "$user" | awk -F: '{print $6}')
50
51         if ! test -e "$home"/NO-DAEMON-PLEASE; then
52                 if [ -e "$home"/build/buildd.pid ] ; then
53                         echo "Touching ${home}/NO-DAEMON-PLEASE ${home}/EXIT-DAEMON-PLEASE"
54                         sudo -u "$user" touch "$home"/NO-DAEMON-PLEASE "$home"/EXIT-DAEMON-PLEASE
55                         sudo -u "$user" sh -c "echo waiting-for-clean-shutdown > '${home}/'NO-DAEMON-PLEASE"
56                         sudo chgrp -v adm "$home"/NO-DAEMON-PLEASE
57
58                         echo "Sending HUP to $user:buildd"
59                         sudo -u "$user" kill -HUP "$(sudo cat "${home}"/build/buildd.pid)"
60                 else
61                         echo "$user: no-daemon-please does not exist, but there is no buildd.pid file either"
62                         if pgrep -u "$user" -x buildd ; then
63                                 echo "But there is a buildd running.  Bad?"
64                                 exit 3
65                         fi
66                 fi
67         else
68                 echo "no-daemon-please already exists"
69         fi
70 }
71
72
73 wall_counter=0
74 maybe_wall() {
75         wall_counter=$((wall_counter-1))
76         if [ "$wall_counter" -le 0 ]; then
77                 if [ "$halt" = 1 ]; then
78                         echo "System will halt for $reason when buildd has stopped" | wall
79                 else
80                         echo "System will reboot for $reason when buildd has stopped" | wall
81                 fi
82                 wall_counter=720
83         fi
84 }
85
86 buildd_wait_and_reboot() {
87         echo -n "Waiting for buildd to shut down"
88
89         while test -e ~buildd/build/buildd.pid  ||  test -e ~buildd2/build/buildd.pid; do
90                 echo -n "."
91                 sleep 5
92                 maybe_wall
93         done
94         echo
95
96         if pgrep -u buildd -x buildd || pgrep -u buildd2 -x buildd; then
97                 echo "pidfile is gone, but buildd process still runs"
98                 while pgrep -u buildd -x buildd ; do
99                         echo -n "."
100                         sleep 5
101                         maybe_wall
102                 done
103                 echo
104         fi
105
106         if grep 'waiting-for-clean-shutdown' ~buildd/NO-DAEMON-PLEASE > /dev/null; then
107                 sudo -u buildd sh -c 'echo delete-on-boot > ~buildd/NO-DAEMON-PLEASE'
108         fi
109         if grep 'waiting-for-clean-shutdown' ~buildd2/NO-DAEMON-PLEASE > /dev/null; then
110                 sudo -u buildd2 sh -c 'echo delete-on-boot > ~buildd2/NO-DAEMON-PLEASE'
111         fi
112         if [ "$halt" = 1 ]; then
113                 /sbin/shutdown -h 1 "$reason"
114         else
115                 /sbin/shutdown -r 1 "$reason"
116         fi
117 }
118
119 if [ "`id -u`" != 0 ]; then
120         echo "This probably wants running as root" >&2
121         exit 3
122 fi
123 if ! id buildd > /dev/null 2>&1; then
124         echo "Is there a buildd user?" >&2
125         exit 3
126 fi
127 if ! [ -d ~buildd ]; then
128         echo "No ~buildd?" >&2
129         exit 3
130 fi
131
132 touch_stuff buildd
133 [ -d ~buildd2 ] && touch_stuff buildd2
134
135 buildd_wait_and_reboot