chiark / gitweb /
Fix comment in apache2-vhost-update
[dsa-metapackages.git] / apache2-vhost-update
1 #!/bin/bash
2
3 # script to update apache site configs
4 #
5 # this script is designed to be run under sudo by
6 # the maintainers of our various apache2 vhosts.
7 #
8 # It checks the validity of the current apache config and
9 # then updates one vhost config from the staging area that
10 # is writeable by the user.
11 #
12 # This script ensures that the resulting apache config is then
13 # still syntactically valid and reloads apache.  If the new config
14 # is not OK then the change is reverted.
15 #
16 # All config changes are tracked in RCS.
17 #
18 # needs something like this in sudoers:
19 # %apachectrl   ALL=(root)      /usr/sbin/apache2-vhost-update
20
21
22 # Copyright (c) 2009 Peter Palfrader <peter@palfrader.org>
23 #
24 # Permission is hereby granted, free of charge, to any person obtaining
25 # a copy of this software and associated documentation files (the
26 # "Software"), to deal in the Software without restriction, including
27 # without limitation the rights to use, copy, modify, merge, publish,
28 # distribute, sublicense, and/or sell copies of the Software, and to
29 # permit persons to whom the Software is furnished to do so, subject to
30 # the following conditions:
31 #
32 # The above copyright notice and this permission notice shall be
33 # included in all copies or substantial portions of the Software.
34 #
35 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
36 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
37 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
38 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
39 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
40 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
41 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
42
43
44
45 STATINGDIR="/etc/apache2/sites-staging"
46 DESTINATION="/etc/apache2/sites-available"
47
48 usage() {
49         bn="`basename "$0"`"
50         echo "Usage: $bn -r|--reload"
51         echo "       $bn <site name>"
52         echo "       $bn -h|--help"
53         echo
54         echo "-r ...            only reload apache"
55         echo "-h ..             print this text"
56         echo
57         echo "With no options copy site's config from etc/apache2/staging"
58         echo "to live config and reload."
59 }
60
61 if [ "$#" != 1 ]; then
62         usage >&2
63         exit 1
64 fi
65 if [ "$1" = "-h" ] || [ "$1" == "--help" ]; then
66         usage
67         exit 0
68 fi
69
70 if ! [ -x /usr/sbin/apache2ctl ]; then
71         echo "Ehhh, there is no Apache2 on this machine" >&2
72         exit 1
73 fi
74
75 # If the apache config is already broken, then don't do anything at all
76 /usr/sbin/apache2ctl configtest
77 if [ "$?" != "0" ]; then
78         echo "configtest returned errors already; not doing anything." >&2
79         exit 1
80 fi
81
82 if [ "$1" = "-r" ] || [ "$1" == "--reload" ]; then
83         env -i /etc/init.d/apache2 reload
84         exit 0
85 fi
86
87
88 site="$1"
89 if echo "$site" | grep /; then
90         echo "Illegal site name" >&2
91         exit 1
92 fi
93
94 if ! [ -f "$STATINGDIR/$site" ]; then
95         echo "$STATINGDIR/$site does not exist or is not a file" >&2
96         exit 1
97 fi
98
99 rcsdiff -q "$DESTINATION/$site" > /dev/null
100 if [ "$?" != "0" ]; then
101         echo "RCS reported uncommitted differences in live version; aborting." >&2
102         exit 1
103 fi
104
105 if diff "$STATINGDIR/$site" "$DESTINATION/$site" > /dev/null; then
106         echo "No differences, not doing anything." >&2
107         exit 1
108 fi
109
110 co -l "$DESTINATION/$site" < /dev/null
111 if [ "$?" != "0" ]; then
112         echo "Could not get a lock/checkout $DESTINATION/$site." >&2
113         exit 1
114 fi
115
116 cp -f "$STATINGDIR/$site" "$DESTINATION/$site"
117
118 if grep -i include "$DESTINATION/$site" > /dev/null; then
119         echo "New site may have include statements - rejecting." >&2
120         rm -f "$DESTINATION/$site"
121         co "$DESTINATION/$site"
122         exit 1
123 fi
124
125 /usr/sbin/apache2ctl configtest
126 if [ "$?" != "0" ]; then
127         echo "configtest returned errors; reverting." >&2
128         rm -f "$DESTINATION/$site"
129         co "$DESTINATION/$site"
130         exit 1
131 fi
132
133 echo "update run by $USER($SUDO_USER) at `date -R -u` on `hostname -f`" | \
134         ci -u "$DESTINATION/$site"
135 if [ "$?" != "0" ]; then
136         echo "Commit failed?" >&2
137         exit 1
138 fi
139
140 env -i /etc/init.d/apache2 reload