Explicitly mark perms.sh-dist as a bash script.
[mir.git] / scripts / mir-setup / mir-prepare
1 #!/bin/sh
2 # ianb@erislabs.net 20081122
3 # checkpoint code taken from zak's mir-setup
4 # $Id: mir-prepare,v 1.1.2.2 2009/01/18 04:43:02 ianb Exp $
5
6 set -e
7
8 CONFDIR="/etc/mir-setup"
9 PROG="`basename $0`"
10
11 PACKAGES="openssh-server pwgen apache2 ant tomcat5.5 tomcat5.5-admin imagemagick postgresql libjmagick6-java libapache2-mod-jk procmail cvs rsync sudo wget"
12
13 CUR_CHECKPOINT=0
14 checkpoint()
15 {
16   CUR_CHECKPOINT=$(($CUR_CHECKPOINT + 1))
17   if [ -n "$START_CHECKPOINT" ]; then
18     if [ "$CUR_CHECKPOINT" -lt "$START_CHECKPOINT" ]; then
19       echo "Skipping checkpoint $CUR_CHECKPOINT: $@"
20       unset RUNNING
21     else
22       echo "Running from checkpoint $CUR_CHECKPOINT: $@"
23       RUNNING=1
24     fi
25   else
26     echo "Checkpoint $CUR_CHECKPOINT: $@"
27     RUNNING=1
28   fi
29 }
30
31 die()
32 {
33   echo >&2 "$@";
34   exit 1;
35 }
36
37 optdie()
38 {
39   echo >&2 "$@";
40   echo >&2 "Try $PROG --help";
41   exit 1;
42 }
43
44 usage() {
45     echo >&2 "Usage: $PROG options"
46     echo >&2 "  Prepares system for a mir install"
47     echo >&2 "  Run before mir-setup"
48     echo >&2 "    -d|--debug                Debugging output, including 'set -x' shell trace"
49     echo >&2 "    -p|--checkpoint <start>   Run from specified checkpoint after failed run"
50     echo >&2 "    -j|--java sun|gcj         Select Java implementation. Default: sun"
51 }
52
53 # default
54 JAVA=sun
55 while [ $# != 0 ]; do
56     case "$1" in
57         -d|--debug)  set -x; DEBUG=1 ;;
58         -p|--checkpoint) shift; START_CHECKPOINT="$1" ;;
59         -j|--java) shift; JAVA="$1" ;;
60         -h|-?|--help) usage; exit 0 ;;
61         -*) optdie "$PROG: unknown option $1" ;;
62         *) optdie "$PROG: unexpected argument '$1'" ;;
63     esac
64     shift
65 done
66
67 checkpoint "Installing mir-setup"
68 if [ -n "$RUNNING" ]
69 then
70     mkdir -p /etc/mir-setup /usr/local/share/mir-setup
71     cp mir-setup mir-choose-java /usr/local/sbin
72     cp munge_config_file.pl  /usr/local/share/mir-setup
73     CONFFILES="config config-examplesite config.properties-default env robots.txt site-httpd.conf site-ssl-dedicated-httpd.conf site-ssl-httpd-fragment.conf tomcat-manager.conf"
74     (cd conf && cp $CONFFILES /etc/mir-setup)
75 fi
76
77 checkpoint "Installing packages"
78 if [ -n "$RUNNING" ]
79 then
80     apt-get install $PACKAGES
81     echo "You probably want to do 'apt-get clean'"
82 fi
83
84 checkpoint "Selecting $JAVA java with mir-choose-java"
85 if [ -n "$RUNNING" ]
86 then
87     mir-choose-java --subprocess $JAVA
88 fi
89
90 checkpoint "Enabling apache modules"
91 if [ -n "$RUNNING" ]
92 then
93     a2enmod headers ssl include deflate jk rewrite
94 fi
95
96 checkpoint "Anonymise apache error log"
97 if [ -n "$RUNNING" ]
98 then
99     if [ ! -f /etc/apache2/conf.d/mir-anon-error-log ]
100     then
101         echo 'ErrorLog "| /usr/local/bin/strip-ips-from-apache-error-log /var/log/apache2/error.log"' > /etc/apache2/conf.d/mir-anon-error-log
102     fi
103     cp strip-ips-from-apache-error-log /usr/local/bin
104 fi
105
106 checkpoint "Configure 'anon' log format"
107 if [ -n "$RUNNING" ]
108 then
109     if [ ! -f /etc/apache2/conf.d/mir-anon-access-log ]
110     then
111         echo 'LogFormat "noip %l %u %t \"%r\" %>s %b" anon' > /etc/apache2/conf.d/mir-anon-access-log
112     fi
113 fi
114
115 checkpoint "Creating directory for mir sites in apache"
116 if [ -n "$RUNNING" ]
117 then
118     if [ ! -d /etc/apache2/mir-sites ]
119     then
120         mkdir /etc/apache2/mir-sites
121     fi
122 fi
123
124 checkpoint "Enabling mir sites in apache"
125 if [ -n "$RUNNING" ]
126 then
127     if [ ! -f /etc/apache2/sites-available/mir-sites ]
128     then
129         echo "Include /etc/apache2/mir-sites/*.conf" > /etc/apache2/sites-available/mir-sites
130         a2ensite mir-sites
131     fi
132 fi
133
134 checkpoint "Configuring apache jakarta workers"
135 if [ -n "$RUNNING" ]
136 then
137     if [ ! -f /etc/apache2/conf.d/mir-jk ]
138     then
139         echo "JkWorkersFile  /etc/libapache2-mod-jk/workers.properties" > /etc/apache2/conf.d/mir-jk
140     fi
141 fi
142
143 checkpoint "Disabling tomcat security (yes, this is bad)"
144 if [ -n "$RUNNING" ]
145 then
146     if ! grep -q "^TOMCAT5_SECURITY=no" /etc/default/tomcat5.5
147     then
148         echo "TOMCAT5_SECURITY=no" >> /etc/default/tomcat5.5
149     fi
150 fi
151
152 checkpoint "Configuring password for tomcat manager"
153 if [ -n "$RUNNING" ]
154 then
155     if ! grep -q 'user username="mir-setup"' /etc/tomcat5.5/tomcat-users.xml
156     then
157         PASSWORD=$(pwgen -n 10 1)
158         ROLE='  <role rolename="manager"\/>'
159         USERNAME='  <user username="mir-setup" password="'"$PASSWORD"'" roles="manager"\/>'
160         perl -pi.bak -e 's/(mir-setup:)[^@]+/$1'"$PASSWORD"'/;' /etc/mir-setup/tomcat-manager.conf
161         perl -pi.bak -e 's/(<tomcat-users>)/$1\n'"$ROLE"'\n'"$USERNAME"'\n/;' /etc/tomcat5.5/tomcat-users.xml
162     fi
163 fi
164
165 checkpoint "Creating directory for SSL certs"
166 if [ -n "$RUNNING" ]
167 then
168     if [ ! -d /etc/apache2/ssl ]
169     then
170         mkdir /etc/apache2/ssl
171     fi
172 fi
173
174 checkpoint "Restarting apache and tomcat"
175 if [ -n "$RUNNING" ]
176 then
177     invoke-rc.d tomcat5.5 force-reload
178     invoke-rc.d apache2 force-reload
179 fi
180
181 checkpoint "All done!"