2 # $Id: mir-setup,v 1.2.2.1 2006/11/11 12:52:05 zak Exp $
4 # Script to install a new Mir site
6 # Initial version -zak 2005-01-23
7 # Moved to traven -zak 2005-04-05
8 # Added to CVS -zak 2005-06-13
12 CONFDIR="/etc/mir-setup"
13 SHAREDIR="/usr/local/share/mir-setup"
25 echo >&2 "Try $PROG --help";
32 source "$CONFDIR/tomcat-manager.conf"
34 if [ -z "$TOMCAT_MANAGER_URL" ]; then die "No TOMCAT_MANAGER_URL specified"; fi
36 echo "$TOMCAT_MANAGER_URL/html/$1?path=/$2" | wget -q -O /dev/null -i -
41 echo >&2 "Usage: $PROG options"
43 echo >&2 " Exactly one of the following is required:"
44 echo >&2 " -f|--config <file> mir-setup site config file"
45 echo >&2 " --start <site> start the given site's webapp"
46 echo >&2 " --stop <site> stop the given site's webapp"
47 echo >&2 " --reload <site> reload the given site's webapp"
49 echo >&2 " Debugging options:"
50 echo >&2 " -d|--debug Debugging output, including 'set -x' shell trace"
51 echo >&2 " -p|--checkpoint <start> Run from specified checkpoint after failed run"
56 -f|--config) shift; CONFIGFILE="$1" ;;
57 --start|--stop|--reload) ACTION="$1"; shift; SITE="$1" ;;
58 -d|--debug) set -x; DEBUG=1 ;;
59 -p|--checkpoint) shift; START_CHECKPOINT="$1" ;;
60 -h|-?|--help) usage; exit 0 ;;
61 -*) optdie "$PROG: unknown option $1" ;;
62 *) optdie "$PROG: unexpected argument '$1'" ;;
68 --start|--stop|--reload)
69 if [ -n "$CONFIGFILE" ]; then optdie "$PROG: config file specified with $ACTION"; fi
70 echo -n "$ACTION"ing "$SITE..."
71 manage "`echo "$ACTION" | sed -e 's/^--//'`" "$SITE"
77 if [ -z "$CONFIGFILE" ]; then optdie "$PROG: no site config file; use -f"; fi
81 if [ -z "$SITE" ]; then optdie "$PROG: no site name"; fi
82 if [ -z "$FQDN" ]; then optdie "$PROG: no site fqdn"; fi
83 if [ -z "$USER" ]; then optdie "$PROG: no user"; fi
86 TOMCATOWNER="$USER:$TOMCATGROUP"
88 if [ -n "$GROUP" ]; then
96 if [ -n "$MIRVERSION" ]; then MIRVERSIONOPT="-r$MIRVERSION"; fi
97 if [ -n "$SITEVERSION" ]; then SITEVERSIONOPT="-r$SITEVERSION"; fi
99 if [ -n "$SHAREDB" ]; then
100 if [ -n "$DBNAME" ]; then optdie "$PROG: SHAREDB and DBNAME set"; fi
101 if [ -n "$DBUSER" ]; then optdie "$PROG: SHAREDB and DBUSER set"; fi
102 if [ -n "$DBPASS" ]; then optdie "$PROG: SHAREDB and DBPASS set"; fi
104 DBNAME="`perl -ne 'if (/\s*Database\.Name\s*=\s*(\S+)/) { print "$1\n" }' /mir/uk/setup_uk/etc/config.properties`"
105 DBUSER="`perl -ne 'if (/\s*Database\.Username\s*=\s*(\S+)/) { print "$1\n" }' /mir/uk/setup_uk/etc/config.properties`"
106 DBPASS="`perl -ne 'if (/\s*Database\.Password\s*=\s*(\S+)/) { print "$1\n" }' /mir/uk/setup_uk/etc/config.properties`"
108 if [ -z "$DBNAME" ]; then DBNAME="$SITE"; fi
109 if [ -z "$DBUSER" ]; then DBUSER="$SITE"; fi
110 if [ -z "$DBPASS" ]; then DBPASS="`pwgen -s 8 1`"; fi
113 if [ -z "$CONFIGPROPERTIES" ]; then CONFIGPROPERTIES="$CONFDIR/config.properties"; fi
114 if [ ! -e "$CONFIGPROPERTIES" ]; then
115 optdie "$CONFIGPROPERTIES does not exist"
121 CUR_CHECKPOINT=$(($CUR_CHECKPOINT + 1))
122 if [ -n "$START_CHECKPOINT" ]; then
123 if [ "$CUR_CHECKPOINT" -lt "$START_CHECKPOINT" ]; then
124 echo "Skipping checkpoint $CUR_CHECKPOINT: $@"
127 echo "Running from checkpoint $CUR_CHECKPOINT: $@"
131 echo "Checkpoint $CUR_CHECKPOINT: $@"
136 checkpoint "Prepare install directory: $INSTALLDIR"
137 if [ -n "$RUNNING" ]; then
138 mkdir -p "$INSTALLDIR"
139 chown "$OWNER" "$INSTALLDIR"
140 chmod g+s "$INSTALLDIR"
143 checkpoint "Check out Mir [$MIRVERSION] from $MIRCVSROOT"
144 if [ -n "$RUNNING" ]; then
146 sudo -u "$USER" cvs -z3 -d"$MIRCVSROOT" checkout $MIRVERSIONOPT mir
147 chown -R "$OWNER" mir
149 checkpoint "Check out site templates [$SITEVERSION] from $SITECVSROOT"
150 if [ -n "$RUNNING" ]; then
152 sudo -u "$USER" cvs -z3 -d"$SITECVSROOT" checkout $SITEVERSIONOPT "$SITEOVERLAYDIR"
153 chown -R "$OWNER" "$SITEOVERLAYDIR"
156 checkpoint "Prepare production directory: $PRODUCTIONDIR"
157 if [ -n "$RUNNING" ]; then
158 mkdir -p "$PRODUCTIONDIR"
159 chown "$TOMCATOWNER" "$PRODUCTIONDIR"
160 chmod g+s "$PRODUCTIONDIR"
162 PRODUCTIONSUBDIRS="abstract comments content de en img inc style"
163 mkdir -p $PRODUCTIONSUBDIRS
164 chown "$TOMCATOWNER" $PRODUCTIONSUBDIRS
165 ln -snf en/index.html
182 perl -w "$SHAREDIR/munge_config_file.pl" <"$1" >"$2"
187 checkpoint "Install robots.txt file"
188 if [ -n "$RUNNING" ]; then
189 munge_config_file "$CONFDIR/robots.txt" "$PRODUCTIONDIR/robots.txt"
192 checkpoint "Install site-specific Apache configuration file (non-SSL)"
193 if [ -n "$RUNNING" ]; then
194 mkdir -p "$APACHECONFDIR"
195 munge_config_file "$CONFDIR/site-httpd.conf" "$APACHECONFDIR/$SITE.conf"
198 checkpoint "Install site-specific Apache configuration file (SSL on dedicated IP)"
199 if [ -n "$RUNNING" ]; then
200 mkdir -p "$APACHECONFDIR/ssl-dedicated"
201 munge_config_file "$CONFDIR/site-ssl-dedicated-httpd.conf" "$APACHECONFDIR/ssl-dedicated/$SITE.conf"
204 checkpoint "Install site-specific Apache configuration file (SSL fragment for webapp via canonical host)"
205 if [ -n "$RUNNING" ]; then
206 mkdir -p "$APACHECONFDIR/ssl-fragments"
207 munge_config_file "$CONFDIR/site-ssl-httpd-fragment.conf" "$APACHECONFDIR/ssl-fragments/$SITE.conf"
210 checkpoint "Restart Apache with new configuration"
211 if [ -n "$RUNNING" ]; then
212 "$APACHECTL" configtest
213 "$APACHECTL" graceful
216 checkpoint "Overlay /etc from site template"
217 if [ -n "$RUNNING" ]; then
220 ln -snf "../$SITEOVERLAYDIR/etc"
223 if [ -n "$SHAREDB" ]; then
224 checkpoint "Sharing existing database (not creating)"
225 checkpoint "Sharing existing database (not importing/installing)"
226 checkpoint "Sharing existing database (not setting permissions)"
228 checkpoint "Create database"
229 if [ -n "$RUNNING" ]; then
230 sudo -u postgres createdb --encoding=unicode "$DBNAME"
231 sudo -u postgres psql "$DBNAME" <<EOF
232 CREATE USER $DBUSER WITH PASSWORD '$DBPASS' NOCREATEDB NOCREATEUSER;
233 UPDATE pg_database SET datdba=(SELECT usesysid FROM pg_user WHERE usename='$DBUSER') WHERE datname='$DBNAME';
237 if [ -n "$DBDUMP" ]; then
238 checkpoint "Import database dump"
239 if [ -n "$RUNNING" ]; then
240 zcat "$DBDUMP" | sudo -u postgres pg_restore -d "$DBNAME" -O -x
243 checkpoint "Install default database"
244 if [ -n "$RUNNING" ]; then
245 for i in "$INSTALLDIR"/mir/dbscripts/{create_pg,help*,populate*}.sql; do
246 sudo -u postgres psql -f $i "$DBNAME"
251 checkpoint "Set database permissions"
252 if [ -n "$RUNNING" ]; then
253 # Clumsy -- produces errors which we should ignore
254 echo >&2 "Don't worry about errors from some of the GRANTs here"
255 sudo -u postgres psql -qto "|psql \"$DBNAME\"" "$DBNAME" <<EOF
256 SELECT 'GRANT ALL ON '||relname||' TO $DBUSER;'
258 WHERE relname not like 'pg%'
264 checkpoint "Install config.properties"
265 if [ -n "$RUNNING" ]; then
266 touch "$INSTALLDIR/mir/etc/config.properties"
267 chmod "$PRIVMODE" "$INSTALLDIR/mir/etc/config.properties"
268 munge_config_file "$CONFIGPROPERTIES" "$INSTALLDIR/mir/etc/config.properties"
271 checkpoint "Running ant to build Mir"
272 if [ -n "$RUNNING" ]; then
277 checkpoint "Fixing file permissions"
278 if [ -n "$RUNNING" ]; then
280 sed -e "s/^GROUP=.*\$/GROUP=$TOMCATGROUP/" perms.sh-dist >perms.sh
285 checkpoint "Linking into Tomcat Web apps directory"
286 if [ -n "$RUNNING" ]; then
288 ln -snf "$INSTALLDIR/mir/bin/mir" "$SITE"
291 checkpoint "All done!"