2 # $Id: mir-setup.squeeze,v 1.1.2.1 2010/12/16 18:31:08 ianb 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" }' "$INSTALLDIR/$SITEOVERLAYDIR/etc/config.properties"`"
105 DBUSER="`perl -ne 'if (/\s*Database\.Username\s*=\s*(\S+)/) { print "$1\n" }' "$INSTALLDIR/$SITEOVERLAYDIR/etc/config.properties"`"
106 DBPASS="`perl -ne 'if (/\s*Database\.Password\s*=\s*(\S+)/) { print "$1\n" }' "$INSTALLDIR/$SITEOVERLAYDIR/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 "Create user $USER and groups"
137 if [ -n "$RUNNING" ]; then
138 if ! id $USER >/dev/null 2>&1 ;then
139 adduser --disabled-login --gecos "$SITE mir user,,," $USER
141 if ! getent group $TOMCATGROUP >/dev/null 2>&1 ;then
142 addgroup $TOMCATGROUP
144 if id $TOMCATUSER >/dev/null 2>&1 ;then
145 adduser $TOMCATUSER $TOMCATGROUP
147 die "User $TOMCATUSER does not exist"
149 adduser $USER $TOMCATGROUP
152 checkpoint "Prepare install directory: $INSTALLDIR"
153 if [ -n "$RUNNING" ]; then
154 mkdir -p "$INSTALLDIR"
155 chown "$OWNER" "$INSTALLDIR"
156 chmod g+s "$INSTALLDIR"
159 if [ -n "$MIRGITROOT" ]; then
160 checkpoint "Check out Mir [$MIRVERSION] from git $MIRGITROOT"
161 if [ -n "$RUNNING" ]; then
163 sudo -u "$USER" git clone "$MIRGITROOT"
164 sudo -u "$USER" sh -c "(cd mir && git checkout $MIRVERSION)"
165 chown -R "$OWNER" mir
168 checkpoint "Check out Mir [$MIRVERSION] from CVS $MIRCVSROOT"
169 if [ -n "$RUNNING" ]; then
171 sudo -u "$USER" cvs -z3 -d"$MIRCVSROOT" checkout $MIRVERSIONOPT mir
172 chown -R "$OWNER" mir
176 if [ -n "$SITEGITROOT" ]; then
177 checkpoint "Check out site templates [$SITEVERSION] from git $SITEGITROOT"
178 if [ -n "$RUNNING" ]; then
180 sudo -u "$USER" git clone "$SITEGITROOT"
181 sudo -u "$USER" sh -c "(cd $SITEOVERLAYDIR && git checkout $SITEGITVERSION)"
182 chown -R "$OWNER" "$SITEOVERLAYDIR"
185 checkpoint "Check out site templates [$SITEVERSION] from CVS $SITECVSROOT"
186 if [ -n "$RUNNING" ]; then
188 sudo -u "$USER" cvs -z3 -d"$SITECVSROOT" checkout $SITEVERSIONOPT "$SITEOVERLAYDIR"
189 chown -R "$OWNER" "$SITEOVERLAYDIR"
193 checkpoint "Prepare production directory: $PRODUCTIONDIR"
194 if [ -n "$RUNNING" ]; then
195 mkdir -p "$PRODUCTIONDIR"
196 chown "$TOMCATOWNER" "$PRODUCTIONDIR"
197 chmod g+s "$PRODUCTIONDIR"
199 PRODUCTIONSUBDIRS="abstract comments content de en img inc style"
200 mkdir -p $PRODUCTIONSUBDIRS
201 chown "$TOMCATOWNER" $PRODUCTIONSUBDIRS
202 ln -snf en/index.html
219 perl -w "$SHAREDIR/munge_config_file.pl" <"$1" >"$2"
224 checkpoint "Install robots.txt file"
225 if [ -n "$RUNNING" ]; then
226 munge_config_file "$CONFDIR/robots.txt" "$PRODUCTIONDIR/robots.txt"
229 checkpoint "Fetch cities.inc"
230 if [ -n "$RUNNING" ]; then
231 wget -O - http://www.indymedia.org/cities.inc |
232 sed -e 's/<br \/>/<br>/gi' > "$PRODUCTIONDIR/cities.inc"
233 chown $USER:$TOMCATGROUP "$PRODUCTIONDIR/cities.inc"
236 checkpoint "Install site-specific Apache configuration file (non-SSL)"
237 if [ -n "$RUNNING" ]; then
238 mkdir -p "$APACHECONFDIR"
239 munge_config_file "$CONFDIR/site-httpd.conf" "$APACHECONFDIR/$SITE.conf"
242 checkpoint "Install site-specific Apache configuration file (SSL on dedicated IP)"
243 if [ -n "$RUNNING" ]; then
244 mkdir -p "$APACHECONFDIR/ssl-dedicated"
245 munge_config_file "$CONFDIR/site-ssl-dedicated-httpd.conf" "$APACHECONFDIR/ssl-dedicated/$SITE.conf"
248 checkpoint "Install site-specific Apache configuration file (SSL fragment for webapp via canonical host)"
249 if [ -n "$RUNNING" ]; then
250 mkdir -p "$APACHECONFDIR/ssl-fragments"
251 munge_config_file "$CONFDIR/site-ssl-httpd-fragment.conf" "$APACHECONFDIR/ssl-fragments/$SITE.conf"
254 checkpoint "Configure temporary snake-oil SSL cert"
255 if [ -n "$RUNNING" ]; then
256 if [ ! -f /etc/apache2/ssl/$SECUREFQDN.crt ]; then
257 ln -s /etc/ssl/certs/ssl-cert-snakeoil.pem /etc/apache2/ssl/$SECUREFQDN.crt
258 ln -s /etc/ssl/private/ssl-cert-snakeoil.key /etc/apache2/ssl/$SECUREFQDN.key
262 checkpoint "Restart Apache with new configuration"
263 if [ -n "$RUNNING" ]; then
264 "$APACHECTL" configtest
265 "$APACHECTL" graceful
268 checkpoint "Overlay /etc from site template"
269 if [ -n "$RUNNING" ]; then
272 ln -snf "../$SITEOVERLAYDIR/etc"
275 if [ -n "$SHAREDB" ]; then
276 checkpoint "Sharing existing database (not creating)"
277 checkpoint "Sharing existing database (not importing/installing)"
278 checkpoint "Sharing existing database (not setting permissions)"
280 checkpoint "Create database"
281 if [ -n "$RUNNING" ]; then
282 sudo -u postgres createdb --encoding=unicode "$DBNAME"
283 sudo -u postgres psql "$DBNAME" <<EOF
284 CREATE USER $DBUSER WITH PASSWORD '$DBPASS' NOCREATEDB NOCREATEUSER;
285 UPDATE pg_database SET datdba=(SELECT usesysid FROM pg_user WHERE usename='$DBUSER') WHERE datname='$DBNAME';
289 if [ -n "$DBDUMP" ]; then
290 checkpoint "Import database dump"
291 if [ -n "$RUNNING" ]; then
292 zcat "$DBDUMP" | sudo -u postgres pg_restore -d "$DBNAME" -O -x
295 checkpoint "Install default database"
296 if [ -n "$RUNNING" ]; then
297 for i in "$INSTALLDIR"/mir/dbscripts/{create_pg,help*,populate*}.sql; do
298 sudo -u postgres psql -f $i "$DBNAME"
303 checkpoint "Set database permissions"
304 if [ -n "$RUNNING" ]; then
305 # Clumsy -- produces errors which we should ignore
306 echo >&2 "Don't worry about errors from some of the GRANTs here"
307 sudo -u postgres psql -qto "|psql \"$DBNAME\"" "$DBNAME" <<EOF
308 SELECT 'GRANT ALL ON '||relname||' TO $DBUSER;'
310 WHERE relname not like 'pg%'
316 checkpoint "Creating links to needed jar files in mir/lib"
317 if [ -n "$RUNNING" ]; then
318 if [ -f /usr/share/java/servlet-2.3.jar ]; then
319 ln -sf /usr/share/java/servlet-2.3.jar "$INSTALLDIR/mir/lib"
321 die "/usr/share/java/servlet-2.3.jar not found"
323 ln -sf /etc/mir-setup/lib/rt.jar "$INSTALLDIR/mir/lib"
326 checkpoint "Installing config.properties"
327 if [ -n "$RUNNING" ]; then
328 PROPERTIESFILE="$INSTALLDIR/mir/etc/config.properties"
329 touch "$PROPERTIESFILE"
330 chmod "$PRIVMODE" "$PROPERTIESFILE"
331 munge_config_file "$CONFIGPROPERTIES" "$PROPERTIESFILE"
334 checkpoint "Running ant to build Mir"
335 if [ -n "$RUNNING" ]; then
337 sudo -u "$USER" TOMCAT_HOME="$TOMCAT_HOME" ant
340 checkpoint "Fixing up jar links in mir deployment"
341 if [ -n "RUNNING" ]; then
342 rm -f "$INSTALLDIR/mir/bin/mir/WEB-INF/lib/rt.jar"
343 rm -f "$INSTALLDIR/mir/bin/mir/WEB-INF/lib/servlet-2.3.jar"
344 ln -sf /usr/share/java/servlet-2.3.jar "$INSTALLDIR/mir/bin/mir/WEB-INF/lib"
345 ln -sf /etc/mir-setup/lib/rt.jar "$INSTALLDIR/mir/bin/mir/WEB-INF/lib"
348 checkpoint "Creating empty abuse.properties"
349 if [ -n "$RUNNING" ]; then
350 touch "$INSTALLDIR/mir/bin/mir/WEB-INF/abuse.properties"
353 checkpoint "Fixing file permissions"
354 if [ -n "$RUNNING" ]; then
356 sed -e "s/^GROUP=.*\$/GROUP=$TOMCATGROUP/" perms.sh-dist >perms.sh
361 checkpoint "Enabling symlinks in tomcat"
362 if [ -n "$RUNNING" ]; then
363 if ! fgrep -q 'allowLinking="true"' /etc/tomcat6/context.xml
365 cp /etc/tomcat6/context.xml /etc/tomcat6/context.xml.tmp
366 sed -e 's/<Context/<Context allowLinking="true"/;' /etc/tomcat6/context.xml.tmp > /etc/tomcat6/context.xml
367 rm /etc/tomcat6/context.xml.tmp
371 checkpoint "Linking into Tomcat Web apps directory"
372 if [ -n "$RUNNING" ]; then
374 ln -snf "$INSTALLDIR/mir/bin/mir" "$SITE"
377 checkpoint "Restarting tomcat"
378 if [ -n "$RUNNING" ]; then
379 if [ -x /etc/init.d/tomcat6 ]; then
380 /etc/init.d/tomcat6 force-reload
381 elif [ -x /etc/init.d/tomcat5.5 ]; then
382 /etc/init.d/tomcat5.5 force-reload
383 elif [ -x /etc/init.d/tomcat4 ]; then
384 /etc/init.d/tomcat4 force-reload
386 echo tomcat NOT reloaded, reload manually
390 checkpoint "All done!"