cf0f916121ce6c49e8196a8a57a3e249e584f02b
[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.1 2009/01/18 03:00:22 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     cp conf/* /etc/mir-setup
74 fi
75
76 checkpoint "Installing packages"
77 if [ -n "$RUNNING" ]
78 then
79     apt-get install $PACKAGES
80     echo "You probably want to do 'apt-get clean'"
81 fi
82
83 checkpoint "Selecting $JAVA java with mir-choose-java"
84 if [ -n "$RUNNING" ]
85 then
86     mir-choose-java --subprocess $JAVA
87 fi
88
89 checkpoint "Enabling apache modules"
90 if [ -n "$RUNNING" ]
91 then
92     a2enmod headers ssl include deflate jk rewrite
93 fi
94
95 checkpoint "Anonymise apache error log"
96 if [ -n "$RUNNING" ]
97 then
98     if [ ! -f /etc/apache2/conf.d/mir-anon-error-log ]
99     then
100         echo 'ErrorLog "| /usr/local/bin/strip-ips-from-apache-error-log /var/log/apache2/error.log"' > /etc/apache2/conf.d/mir-anon-error-log
101     fi
102     cp strip-ips-from-apache-error-log /usr/local/bin
103 fi
104
105 checkpoint "Configure 'anon' log format"
106 if [ -n "$RUNNING" ]
107 then
108     if [ ! -f /etc/apache2/conf.d/mir-anon-access-log ]
109     then
110         echo 'LogFormat "noip %l %u %t \"%r\" %>s %b" anon' > /etc/apache2/conf.d/mir-anon-access-log
111     fi
112 fi
113
114 checkpoint "Creating directory for mir sites in apache"
115 if [ -n "$RUNNING" ]
116 then
117     if [ ! -d /etc/apache2/mir-sites ]
118     then
119         mkdir /etc/apache2/mir-sites
120     fi
121 fi
122
123 checkpoint "Enabling mir sites in apache"
124 if [ -n "$RUNNING" ]
125 then
126     if [ ! -f /etc/apache2/sites-available/mir-sites ]
127     then
128         echo "Include /etc/apache2/mir-sites/*.conf" > /etc/apache2/sites-available/mir-sites
129         a2ensite mir-sites
130     fi
131 fi
132
133 checkpoint "Configuring apache jakarta workers"
134 if [ -n "$RUNNING" ]
135 then
136     if [ ! -f /etc/apache2/conf.d/mir-jk ]
137     then
138         echo "JkWorkersFile  /etc/libapache2-mod-jk/workers.properties" > /etc/apache2/conf.d/mir-jk
139     fi
140 fi
141
142 checkpoint "Disabling tomcat security (yes, this is bad)"
143 if [ -n "$RUNNING" ]
144 then
145     if ! grep -q "^TOMCAT5_SECURITY=no" /etc/default/tomcat5.5
146     then
147         echo "TOMCAT5_SECURITY=no" >> /etc/default/tomcat5.5
148     fi
149 fi
150
151 checkpoint "Configuring password for tomcat manager"
152 if [ -n "$RUNNING" ]
153 then
154     if ! grep -q 'user username="mir-setup"' /etc/tomcat5.5/tomcat-users.xml
155     then
156         PASSWORD=$(pwgen -n 10 1)
157         ROLE='  <role rolename="manager"\/>'
158         USERNAME='  <user username="mir-setup" password="'"$PASSWORD"'" roles="manager"\/>'
159         perl -pi.bak -e 's/(mir-setup:)[^@]+/$1'"$PASSWORD"'/;' /etc/mir-setup/tomcat-manager.conf
160         perl -pi.bak -e 's/(<tomcat-users>)/$1\n'"$ROLE"'\n'"$USERNAME"'\n/;' /etc/tomcat5.5/tomcat-users.xml
161     fi
162 fi
163
164 checkpoint "Creating directory for SSL certs"
165 if [ -n "$RUNNING" ]
166 then
167     if [ ! -d /etc/apache2/ssl ]
168     then
169         mkdir /etc/apache2/ssl
170     fi
171 fi
172
173 checkpoint "Restarting apache and tomcat"
174 if [ -n "$RUNNING" ]
175 then
176     invoke-rc.d tomcat5.5 force-reload
177     invoke-rc.d apache2 force-reload
178 fi
179
180 checkpoint "All done!"