3 # script to update apache site configs
5 # this script is designed to be run under sudo by
6 # the maintainers of our various apache2 vhosts.
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.
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.
16 # All config changes are tracked in RCS.
18 # needs something like this in sudoers:
19 # %apachectrl ALL=(root) /usr/sbin/apache2-vhost-update
22 # Copyright (c) 2009,2010 Peter Palfrader <peter@palfrader.org>
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:
32 # The above copyright notice and this permission notice shall be
33 # included in all copies or substantial portions of the Software.
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.
45 STATINGDIR="/etc/apache2/sites-staging"
46 DESTINATION="/etc/apache2/sites-available"
50 echo "Usage: $bn -r|--reload"
51 echo " $bn <site name>"
54 echo "-r ... only reload apache"
55 echo "-h .. print this text"
57 echo "With no options copy site's config from etc/apache2/staging"
58 echo "to live config and reload."
61 if [ "$#" != 1 ]; then
65 if [ "$1" = "-h" ] || [ "$1" == "--help" ]; then
70 if ! [ -x /usr/sbin/apache2ctl ]; then
71 echo "Ehhh, there is no Apache2 on this machine" >&2
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
82 if [ "$1" = "-r" ] || [ "$1" == "--reload" ]; then
83 env -i /etc/init.d/apache2 reload
89 if echo "$site" | grep /; then
90 echo "Illegal site name" >&2
94 if ! [ -f "$STATINGDIR/$site" ]; then
95 echo "$STATINGDIR/$site does not exist or is not a file" >&2
99 rcsdiff -q "$DESTINATION/$site" > /dev/null
100 if [ "$?" != "0" ]; then
101 echo "RCS reported uncommitted differences in live version; aborting." >&2
105 if diff "$STATINGDIR/$site" "$DESTINATION/$site" > /dev/null; then
106 echo "No differences, not doing anything." >&2
110 co -l "$DESTINATION/$site" < /dev/null
111 if [ "$?" != "0" ]; then
112 echo "Could not get a lock/checkout $DESTINATION/$site." >&2
116 cp -f "$STATINGDIR/$site" "$DESTINATION/$site"
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"
125 if grep -i LoadModule "$DESTINATION/$site" > /dev/null; then
126 echo "New site may have LoadModule statements - rejecting." >&2
127 rm -f "$DESTINATION/$site"
128 co "$DESTINATION/$site"
132 if grep -i LoadFile "$DESTINATION/$site" > /dev/null; then
133 echo "New site may have LoadFile statements - rejecting." >&2
134 rm -f "$DESTINATION/$site"
135 co "$DESTINATION/$site"
139 /usr/sbin/apache2ctl configtest
140 if [ "$?" != "0" ]; then
141 echo "configtest returned errors; reverting." >&2
142 rm -f "$DESTINATION/$site"
143 co "$DESTINATION/$site"
148 rcsdiff -u "$DESTINATION/$site"
150 echo "update run by $USER($SUDO_USER) at `date -R -u` on `hostname -f`" | \
151 ci -u "$DESTINATION/$site"
152 if [ "$?" != "0" ]; then
153 echo "Commit failed?" >&2
157 env -i /etc/init.d/apache2 reload