diff options
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/install-as-user | 60 | ||||
-rwxr-xr-x | bin/pre-install-as-root | 193 |
2 files changed, 120 insertions, 133 deletions
diff --git a/bin/install-as-user b/bin/install-as-user index ecd2d99b6..c5e68764b 100755 --- a/bin/install-as-user +++ b/bin/install-as-user @@ -1,10 +1,22 @@ #!/bin/sh set -e +set -x + +DEFAULT_SERVER=false +if [ x"$1" = x"--default" ] +then + DEFAULT_SERVER=true + shift +fi if [ $# -ne 2 ] then - echo "Usage: $0 <UNIX-USER> <HOST>" + cat >&2 <<EOUSAGE +Usage: $0 [--default] <UNIX-USER> <HOST> +--default means to install as the default site for this server, +rather than a virtualhost for HOST. +EOUSAGE exit 1 fi @@ -35,7 +47,12 @@ then exit 1 fi -FMS_DIRECTORY="/var/www/$HOST" +if [ $DEFAULT_SERVER = true ] +then + FMS_DIRECTORY="/var/www/fixmystreet" +else + FMS_DIRECTORY="/var/www/$HOST" +fi FMS_REPOSITORY="$FMS_DIRECTORY/fixmystreet" FMS_LINK_DESTINATION="$HOME/fixmystreet" @@ -48,32 +65,12 @@ TEMPORARY_CRONTAB=$(mktemp) echo crontab file is $TEMPORARY_CRONTAB -cat > $TEMPORARY_CRONTAB <<EOF -# Timed tasks for FixMyStreet. -# -# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. -# Email: matthew@mysociety.org. WWW: http://www.mysociety.org/ - -PATH=/usr/local/bin:/usr/bin:/bin +cp "$FMS_REPOSITORY"/conf/crontab.example "$TEMPORARY_CRONTAB" -5,10,15,20,25,30,35,40,45,50,55 * * * * "$FMS_REPOSITORY/commonlib/bin/run-with-lockfile.sh" -n "$FMS_REPOSITORY/send-reports.lock" "$FMS_REPOSITORY/bin/cron-wrapper send-reports" || echo "stalled?" -0 0-8,10,11,13,14,16,17,19-23 * * * "$FMS_REPOSITORY/commonlib/bin/run-with-lockfile.sh" -n "$FMS_DIRECTORY/send-reports.lock" "$FMS_REPOSITORY/bin/cron-wrapper send-reports" || echo "stalled?" -0 9,12,15,18 * * * "$FMS_REPOSITORY/commonlib/bin/run-with-lockfile.sh" -n "$FMS_DIRECTORY/send-reports.lock" "$FMS_REPOSITORY/bin/cron-wrapper send-reports --verbose" || echo "stalled?" -2 * * * * "$FMS_REPOSITORY/commonlib/bin/run-with-lockfile.sh" -n "$FMS_DIRECTORY/send-alerts.lock" "$FMS_REPOSITORY/bin/cron-wrapper send-alerts" || echo "stalled?" -0,30 * * * * "$FMS_REPOSITORY/commonlib/bin/run-with-lockfile.sh" -n "$FMS_DIRECTORY/send-questionnaires.lock" "$FMS_REPOSITORY/bin/cron-wrapper send-questionnaires" || echo "stalled?" -5,10,15,20,25,30,35,40,45,50,55 * * * * "$FMS_REPOSITORY/commonlib/bin/run-with-lockfile.sh" -n "$FMS_DIRECTORY/send-comments.lock" "$FMS_REPOSITORY/bin/cron-wrapper send-comments" || echo "stalled?" -5,10,15,20,25,30,35,40,45,50,55 * * * * "$FMS_REPOSITORY/commonlib/bin/run-with-lockfile.sh" -n "$FMS_DIRECTORY/fetch-comments.lock" "$FMS_REPOSITORY/bin/cron-wrapper fetch-comments" || echo "stalled?" -0,30 * * * * "$FMS_REPOSITORY/commonlib/bin/run-with-lockfile.sh" -n "$FMS_DIRECTORY/open311-populate-service-list.lock" "$FMS_REPOSITORY/bin/cron-wrapper open311-populate-service-list" || echo "stalled?" - -# Once an hour, update the all reports stats -13 * * * * "$FMS_REPOSITORY/bin/cron-wrapper" update-all-reports - -# Once a day on all servers -39 2 * * * "$FMS_REPOSITORY/bin/problems-filed-graph" -43 2 * * * "$FMS_REPOSITORY/bin/problem-creation-graph" -00 8 * * * "$FMS_REPOSITORY/bin/check-for-zombies" $UNIX_USER - -EOF +sed -i \ + -e 's,$FMS,'"$FMS_REPOSITORY,g" \ + -e 's,$LOCK_DIR,'"$FMS_DIRECTORY,g" \ + "$TEMPORARY_CRONTAB" crontab $TEMPORARY_CRONTAB @@ -106,6 +103,15 @@ sed -r \ -e "s,^( *CONTACT_EMAIL:).*,\\1 'help@$HOST'," \ conf/general.yml-example > conf/general.yml +# Create the database if it doesn't exist: +if ! psql -l | egrep "^ *$DB_NAME *\|" > /dev/null +then + createdb --owner "$UNIX_USER" "$DB_NAME" + echo 'CREATE LANGUAGE plpgsql;' | psql -U "$UNIX_USER" "$DB_NAME" || true + psql -U "$UNIX_USER" "$DB_NAME" < "$FMS_REPOSITORY"/db/schema.sql + psql -U "$UNIX_USER" "$DB_NAME" < "$FMS_REPOSITORY"/db/alert_types.sql +fi + # Install the required Perl modules - this may take a very long time: cd "$FMS_REPOSITORY" diff --git a/bin/pre-install-as-root b/bin/pre-install-as-root index d0615e370..e460c0e7a 100755 --- a/bin/pre-install-as-root +++ b/bin/pre-install-as-root @@ -12,17 +12,53 @@ # named virtualhost. set -e +set -x -if [ $# -ne 2 ] +DEFAULT_SERVER=false +if [ x"$1" = x"--default" ] then - echo "Usage: $0 <UNIX-USER> <HOST>" + DEFAULT_SERVER=true + shift +fi + +# If we're not running on an EC2 instance, an empty body is returned +# by this request: +EC2_HOSTNAME=`curl -s http://169.254.169.254/latest/meta-data/public-hostname` + +usage_and_exit() { + cat >&2 <<EOUSAGE +Usage: $0 [--default] <UNIX-USER> [HOST] +HOST is only optional if you are running this on an EC2 instance. +--default means to install as the default site for this server, +rather than a virtualhost for HOST. +EOUSAGE exit 1 +} + +if [ $# = 1 ] +then + if [ x = x$EC2_HOSTNAME ] + then + usage_and_exit + else + echo "setting host" + HOST="$EC2_HOSTNAME" + fi +elif [ $# = 2 ] +then + HOST="$2" +else + usage_and_exit fi UNIX_USER="$1" -HOST="$2" -FMS_DIRECTORY="/var/www/$HOST" +if [ $DEFAULT_SERVER = true ] +then + FMS_DIRECTORY="/var/www/fixmystreet" +else + FMS_DIRECTORY="/var/www/$HOST" +fi FMS_REPOSITORY="$FMS_DIRECTORY/fixmystreet" REPOSITORY_URL=git://github.com/mysociety/fixmystreet.git @@ -65,22 +101,6 @@ set_locale() { export LANG="en_GB.UTF-8" } -move_default_virtualhosts() { - # If there are any occurences of /var/www or /var/www/ in - # /etc/apache2/sites-available/(default|default-ssl) change them - # to /var/www/default and /var/www/default/ respectively: - for name in default default-ssl - do - ORIGINAL=/etc/apache2/sites-available/$name - sed -i -r \ - -e 's,(/var/www/)([^A-Za-z0-9]|$),\1default/\2,g' \ - -e 's,(/var/www)([^/A-Za-z0-9]|$),\1/default\2,g' \ - $ORIGINAL - done - mkdir -p /var/www/default - cp /var/www/index.html /var/www/default -} - add_unix_user() { # Create the required user if it doesn't already exist: if id "$1" 2> /dev/null > /dev/null @@ -121,9 +141,6 @@ deb-src http://security.debian.org/ squeeze/updates main non-free # Debian Backports deb http://backports.debian.org/debian-backports squeeze-backports main contrib non-free deb-src http://backports.debian.org/debian-backports squeeze-backports main contrib non-free - -# mySociety repository -deb http://debian.mysociety.org squeeze main EOF else echo Unsupport distribution and version combination $DISTRIBUTION $VERSION @@ -157,96 +174,51 @@ clone_or_update_repository() { fi } -install_apache() { - # Make sure that Apache is installed: - apt-get install -y apache2-mpm-worker libapache2-mod-fastcgi apache2-suexec - - # Actually enable the suexec wrapper: - sed -i -r 's/^( *)#( *FastCgiWrapper.*)/\1\2/' /etc/apache2/mods-available/fastcgi.conf - - # Since this may be run on an EC2 instance with very low memory, - # limit the number of FastCGI processes to 2: - if ! egrep '^ *FastCgiConfig -maxClassProcesses' /etc/apache2/mods-available/fastcgi.conf - then - sed '/<\/IfModule>/i\ - FastCgiConfig -maxClassProcesses 2 - -' /etc/apache2/mods-available/fastcgi.conf - fi - - /etc/init.d/apache2 restart -} - -install_website_packages() { - PACKAGES_FILE="$1/conf/packages.debian-squeeze" - xargs -a "$PACKAGES_FILE" apt-get -y install +install_nginx() { + apt-get install -y nginx libfcgi-procmanager-perl } -add_website_to_apache() { +add_website_to_nginx() { UNIX_USER="$1" HOST="$2" REPOSITORY="$3" - + SITE="$HOST" + if [ $DEFAULT_SERVER = true ] + then + SITE=default + fi + SITE_FILENAME=/etc/nginx/sites-available/"$SITE" + SITE_LINK=/etc/nginx/sites-enabled/"$SITE" + cp $FMS_REPOSITORY/conf/nginx.conf.example $SITE_FILENAME + sed -i "s,/var/www/fixmystreet,$FMS_DIRECTORY," $SITE_FILENAME + if [ $DEFAULT_SERVER = false ] + then + sed -i 's/listen 80/# listen 80/' $SITE_FILENAME + sed "/listen 80/i\ + server_name $HOST; +" /etc/apache2/mods-available/fastcgi.conf + fi + ln -nsf "$SITE_FILENAME" "$SITE_LINK" LOG_DIRECTORY="$(readlink -f $REPOSITORY/../logs)" mkdir -p "$LOG_DIRECTORY" chown -R "$UNIX_USER"."$UNIX_USER" "$LOG_DIRECTORY" + /etc/init.d/nginx restart +} - APACHE_CONFIG_FILE=$REPOSITORY/conf/httpd.conf - - cp $APACHE_CONFIG_FILE-example $APACHE_CONFIG_FILE - - cat > /etc/apache2/sites-available/"$HOST" <<EOF -<VirtualHost *:80> - ServerName $HOST - DocumentRoot $REPOSITORY/web/ - - # Pull in the specific config - Include $APACHE_CONFIG_FILE - - SuexecUserGroup $UNIX_USER $UNIX_USER - - <Directory $REPOSITORY/web> - # You also need to enable cgi files to run as CGI scripts. For example: - # on production servers these are run under fastcgi - Options +ExecCGI - AddHandler fastcgi-script .cgi - AllowOverride None - </Directory> - - <Location /admin> - # - # WARNING - enable auth here on production machine - # - </Location> - - Alias /admin/ $REPOSITORY/web-admin/ - - # Set up commonlib jslib directory - Alias /jslib/ $REPOSITORY/commonlib/jslib/ - <Location /jslib> - AddOutputFilter DEFLATE js - Header append Cache-Control "no-transform" - </Location> - - LogLevel info - ErrorLog $LOG_DIRECTORY/error.log - CustomLog $LOG_DIRECTORY/access.log combined - -</VirtualHost> -EOF - - move_default_virtualhosts - - a2ensite $HOST - - a2enmod rewrite - a2enmod proxy_http - a2enmod expires - a2enmod headers - a2enmod suexec - a2enmod fastcgi +install_sysvinit_script() { + SYSVINIT_NAME=fms-catalyst-fastcgi + SYSVINIT_FILENAME=/etc/init.d/$SYSVINIT_NAME + cp $FMS_REPOSITORY/conf/sysvinit-catalyst-fastcgi.example $SYSVINIT_FILENAME + sed -i "s,/var/www/fixmystreet,$FMS_DIRECTORY,g" $SYSVINIT_FILENAME + sed -i "s/^ *USER=.*/USER=$UNIX_USER/" $SYSVINIT_FILENAME + chmod a+rx $SYSVINIT_FILENAME + update-rc.d fms-catalyst-fastcgi start 20 2 3 4 5 . stop 20 0 1 6 . + /etc/init.d/$SYSVINIT_NAME restart +} - /etc/init.d/apache2 restart +install_website_packages() { + PACKAGES_FILE="$1/conf/packages.debian-squeeze" + xargs -a "$PACKAGES_FILE" apt-get -y install } generate_locales @@ -263,11 +235,20 @@ clone_or_update_repository $FMS_REPOSITORY chown -R "$UNIX_USER"."$UNIX_USER" "$FMS_DIRECTORY" -install_apache +install_nginx + install_website_packages "$FMS_REPOSITORY" +add_website_to_nginx "$UNIX_USER" "$HOST" "$FMS_REPOSITORY" + add_postgresql_user "$UNIX_USER" -add_website_to_apache "$UNIX_USER" "$HOST" "$FMS_REPOSITORY" +DEFAULT_PARAMETER='' +if [ $DEFAULT_SERVER = true ] +then + DEFAULT_PARAMETER='--default' +fi + +su -l -c "$FMS_REPOSITORY/bin/install-as-user $DEFAULT_PARAMETER '$UNIX_USER' '$HOST'" "$UNIX_USER" -su -l -c "$FMS_REPOSITORY/bin/install-as-user '$UNIX_USER' '$HOST'" "$UNIX_USER" +install_sysvinit_script |