diff options
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/ec2-rewrite-conf | 20 | ||||
-rwxr-xr-x | bin/install-as-user | 126 | ||||
-rwxr-xr-x | bin/open311-populate-service-list | 9 | ||||
-rwxr-xr-x | bin/pre-install-as-root | 307 | ||||
-rwxr-xr-x | bin/problem-creation-graph | 14 | ||||
-rwxr-xr-x | bin/problems-filed-graph | 6 |
6 files changed, 471 insertions, 11 deletions
diff --git a/bin/ec2-rewrite-conf b/bin/ec2-rewrite-conf new file mode 100755 index 000000000..0163ef511 --- /dev/null +++ b/bin/ec2-rewrite-conf @@ -0,0 +1,20 @@ +#!/bin/sh + +# This is a helper script for writing the current EC2 hostname into +# the FixMyStreet configuration file. Its intended usage is for lines +# like these to be added to /etc/rc.local: +# +# su -l -c /home/fms/fixmystreet/bin/ec2-rewrite-conf fms +# /etc/init.d/apache2 restart + +set -e + +BIN_DIR=$(dirname $(readlink -f $0)) +CONF_DIR=$BIN_DIR/../conf + +HOST=`curl -s http://169.254.169.254/latest/meta-data/public-hostname` + +sed -i -r \ + -e "s,^( *BASE_URL:).*,\\1 'http://$HOST'," \ + -e "s,^( *EMAIL_DOMAIN:).*,\\1 '$HOST'," \ + $CONF_DIR/general.yml diff --git a/bin/install-as-user b/bin/install-as-user new file mode 100755 index 000000000..4757090aa --- /dev/null +++ b/bin/install-as-user @@ -0,0 +1,126 @@ +#!/bin/sh + +set -e +set -x + +DEFAULT_SERVER=false +if [ x"$1" = x"--default" ] +then + DEFAULT_SERVER=true + shift +fi + +if [ $# -ne 2 ] +then + 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 + +UNIX_USER="$1" +HOST="$2" +DB_NAME="fixmystreet" + +# Check that the arguments we've been passed are sensible: + +IP_ADDRESS_FOR_HOST="$(dig +short $HOST)" + +if [ x = x"$IP_ADDRESS_FOR_HOST" ] +then + echo "The hostname $HOST didn't resolve to an IP address" + exit 1 +fi + +if ! id "$UNIX_USER" 2> /dev/null > /dev/null +then + echo "The user '$UNIX_USER' didn't exist." + echo "(You should have run \"sudo pre-install-as-root '$UNIX_USER' '$HOST'\" before this.)" + exit 1 +fi + +if [ "$(whoami)" != "$UNIX_USER" ] +then + echo "This script should be run by the user '$UNIX_USER'." + exit 1 +fi + +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" + +ln -sfn "$FMS_REPOSITORY" $FMS_LINK_DESTINATION +cd "$FMS_REPOSITORY" + +# Add regularly scheduled tasks to cron: + +TEMPORARY_CRONTAB=$(mktemp) + +echo crontab file is $TEMPORARY_CRONTAB + +cp "$FMS_REPOSITORY"/conf/crontab.example "$TEMPORARY_CRONTAB" + +sed -i \ + -e 's,$FMS,'"$FMS_REPOSITORY,g" \ + -e 's,$LOCK_DIR,'"$FMS_DIRECTORY,g" \ + "$TEMPORARY_CRONTAB" + +crontab $TEMPORARY_CRONTAB + +# Install the compass gem locally - it's required for generating the +# CSS: + +export GEM_HOME="$FMS_DIRECTORY/gems" +mkdir -p "$GEM_HOME" +export GEM_PATH= +export PATH="$GEM_HOME/bin:$PATH" + +gem install --no-ri --no-rdoc compass + +# Use compass to generate the CSS, if it doesn't seem to already +# exist: + +if [ ! -f web/cobrands/default/base.css ] +then + bin/make_css +fi + +# Write sensible values into the config file: + +sed -r \ + -e "s,^( *FMS_DB_HOST:).*,\\1 ''," \ + -e "s,^( *FMS_DB_NAME:).*,\\1 '$DB_NAME'," \ + -e "s,^( *FMS_DB_USER:).*,\\1 '$UNIX_USER'," \ + -e "s,^( *BASE_URL:).*,\\1 'http://$HOST'," \ + -e "s,^( *EMAIL_DOMAIN:).*,\\1 '$HOST'," \ + -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 + psql -U "$UNIX_USER" "$DB_NAME" < "$FMS_REPOSITORY"/db/generate_secret.sql +fi + +# Install the required Perl modules - this may take a very long time: + +cd "$FMS_REPOSITORY" +bin/install_perl_modules + +# Generate po and mo files (these invocations taken from Kagee's script): + +./bin/cron-wrapper ./bin/make_emptyhomes_po +./bin/cron-wrapper ./bin/make_emptyhomes_welsh_po + +commonlib/bin/gettext-makemo FixMyStreet diff --git a/bin/open311-populate-service-list b/bin/open311-populate-service-list index 33be61af7..99f8502a7 100755 --- a/bin/open311-populate-service-list +++ b/bin/open311-populate-service-list @@ -4,11 +4,18 @@ use strict; use warnings; use FixMyStreet::App; use Open311::PopulateServiceList; +use Getopt::Long::Descriptive; +my ($opt, $usage) = describe_options( + '%c %o', + ['verbose|v', "print out all services as they are found"], + ['help', "print usage message and exit" ], +); +print($usage->text), exit if $opt->help; my $council_list = FixMyStreet::App->model('DB::Open311conf')->search( { send_method => 'Open311' } ); -my $p = Open311::PopulateServiceList->new( council_list => $council_list ); +my $p = Open311::PopulateServiceList->new( council_list => $council_list, verbose => $opt->verbose? 1:0 ); $p->process_councils; diff --git a/bin/pre-install-as-root b/bin/pre-install-as-root new file mode 100755 index 000000000..8d4c9dd26 --- /dev/null +++ b/bin/pre-install-as-root @@ -0,0 +1,307 @@ +#!/bin/sh + +# On a clean Debian squeeze or Ubuntu precise installation you should +# be able to install FixMyStreet with: +# +# curl https://raw.github.com/mysociety/fixmystreet/master/bin/pre-install-as-root | \ +# sh -s fms whatever.compute.amazonaws.com +# +# ... where the first argument is the Unix user who will own the code +# and the application will run as, and the second argument is the +# public hostname for the server, which will be used for creating the +# named virtualhost. + +set -e +set -x + +SITE=fixmystreet + +DEFAULT_SERVER=false +if [ x"$1" = x"--default" ] +then + DEFAULT_SERVER=true + shift +fi + +# Install some packages that we will definitely need: +apt-get install -y git-core lockfile-progs rubygems curl dnsutils lsb-release + +# 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 || true` + +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 + HOST="$EC2_HOSTNAME" + fi +elif [ $# = 2 ] +then + HOST="$2" +else + usage_and_exit +fi + +UNIX_USER="$1" + +if [ $DEFAULT_SERVER = true ] +then + DIRECTORY="/var/www/$SITE" +else + DIRECTORY="/var/www/$HOST" +fi +REPOSITORY="$DIRECTORY/$SITE" + +REPOSITORY_URL=git://github.com/mysociety/$SITE.git +BRANCH=master + +DISTRIBUTION="$(lsb_release -i -s | tr A-Z a-z)" +VERSION="$(lsb_release -c -s)" + +IP_ADDRESS_FOR_HOST="$(dig +short $HOST)" + +if [ x = x"$IP_ADDRESS_FOR_HOST" ] +then + echo "The hostname $HOST didn't resolve to an IP address" + exit 1 +fi + +generate_locales() { + # If language-pack-en is present, install that: + apt-get install -y language-pack-en || true + + # We get lots of locale errors if the en_GB.UTF-8 locale isn't + # present. (This is from Kagee's script.) + if [ "$(locale -a | egrep -i '^en_GB.utf-?8$' | wc -l)" = "1" ] + then + echo "en_GB.utf8 activated and generated" + else + echo "en_GB.utf8 not generated" + if [ x"$(grep -c '^en_GB.UTF-8 UTF-8' /etc/locale.gen)" = x1 ] + then + echo "'en_GB.UTF-8 UTF-8' already in /etc/locale.gen we will only generate" + else + echo "Appending 'en_GB.UTF-8 UTF-8' and 'cy_GB.UTF-8 UTF-8'" + echo "to /etc/locale.gen for generation" + echo "\nen_GB.UTF-8 UTF-8\ncy_GB.UTF-8 UTF-8" >> /etc/locale.gen + fi + echo "Generating new locales" + locale-gen + fi +} + +set_locale() { + echo 'LANG="en_GB.UTF-8"' > /etc/default/locale + export LANG="en_GB.UTF-8" +} + +add_unix_user() { + # Create the required user if it doesn't already exist: + if id "$1" 2> /dev/null > /dev/null + then + echo "The user $1 already exists." + else + adduser --disabled-password --gecos 'The FixMyStreet User' "$1" + fi +} + +add_postgresql_user() { + su -l -c "createuser --createdb --no-createrole --no-superuser '$UNIX_USER'" postgres || true +} + +update_apt_sources() { + if [ x"$DISTRIBUTION" = x"ubuntu" ] && [ x"$VERSION" = x"precise" ] + then + cat > /etc/apt/sources.list.d/mysociety-extra.list <<EOF +deb http://eu-west-1.ec2.archive.ubuntu.com/ubuntu/ precise multiverse +deb-src http://eu-west-1.ec2.archive.ubuntu.com/ubuntu/ precise multiverse +deb http://eu-west-1.ec2.archive.ubuntu.com/ubuntu/ precise-updates multiverse +deb-src http://eu-west-1.ec2.archive.ubuntu.com/ubuntu/ precise-updates multiverse +EOF + elif [ x"$DISTRIBUTION" = x"debian" ] && [ x"$VERSION" = x"squeeze" ] + then + # Install the basic packages we require: + cat > /etc/apt/sources.list.d/mysociety-extra.list <<EOF +# Debian mirror to use, including contrib and non-free: +deb http://the.earth.li/debian/ squeeze main contrib non-free +deb-src http://the.earth.li/debian/ squeeze main contrib non-free + +# Security Updates: +deb http://security.debian.org/ squeeze/updates main non-free +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 +EOF + else + echo Unsupport distribution and version combination $DISTRIBUTION $VERSION + exit 1 + fi + apt-get update +} + +clone_or_update_repository() { + # Clone the repository into place if the directory isn't already + # present: + if [ -d $REPOSITORY ] + then + echo the directory $REPOSITORY already exists + cd $REPOSITORY + git remote set-url origin "$REPOSITORY_URL" + git fetch origin + # Check that there are no uncommitted changes before doing a + # git reset --hard: + git diff --quiet || { echo "There were changes in the working tree in $REPOSITORY; exiting."; exit 1; } + git diff --cached --quiet || { echo "There were staged but uncommitted changes in $REPOSITORY; exiting."; exit 1; } + # If that was fine, carry on: + git reset --hard origin/"$BRANCH" + git submodule sync + git submodule update --recursive + else + PARENT="$(dirname $REPOSITORY)" + echo creating $PARENT + mkdir -p $PARENT + git clone --recursive --branch "$BRANCH" "$REPOSITORY_URL" "$REPOSITORY" + fi +} + +install_nginx() { + apt-get install -y nginx libfcgi-procmanager-perl +} + +add_website_to_nginx() { + UNIX_USER="$1" + HOST="$2" + REPOSITORY="$3" + NGINX_SITE="$HOST" + if [ $DEFAULT_SERVER = true ] + then + NGINX_SITE=default + fi + SITE_FILENAME=/etc/nginx/sites-available/"$NGINX_SITE" + SITE_LINK=/etc/nginx/sites-enabled/"$NGINX_SITE" + cp $REPOSITORY/conf/nginx.conf.example $SITE_FILENAME + sed -i "s,/var/www/$SITE,$DIRECTORY," $SITE_FILENAME + if [ $DEFAULT_SERVER = false ] + then + sed -i "/listen 80/a\ +\ server_name $HOST; +" $SITE_FILENAME + 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 +} + +install_sysvinit_script() { + SYSVINIT_FILENAME=/etc/init.d/$SITE + cp $REPOSITORY/conf/sysvinit.example $SYSVINIT_FILENAME + sed -i "s,/var/www/$SITE,$DIRECTORY,g" $SYSVINIT_FILENAME + sed -i "s/^ *USER=.*/USER=$UNIX_USER/" $SYSVINIT_FILENAME + chmod a+rx $SYSVINIT_FILENAME + update-rc.d $SITE start 20 2 3 4 5 . stop 20 0 1 6 . + /etc/init.d/$SITE restart +} + +install_website_packages() { + EXACT_PACKAGES="$1/conf/packages.$DISTRIBUTION-$VERSION" + PRECISE_PACKAGES="$1/conf/packages.ubuntu-precise" + SQUEEZE_PACKAGES="$1/conf/packages.debian-squeeze" + GENERIC_PACKAGES="$1/conf/packages" + # If there's an exact match for the distribution and release, use that: + if [ -e "$EXACT_PACKAGES" ] + then + PACKAGES_FILE="$EXACT_PACKAGES" + # Otherwise, if this is Ubuntu, and there's a version specifically + # for precise, use that: + elif [ x"$DISTRIBUTION" = x"ubuntu" ] && [ -e "$PRECISE_PACKAGES" ] + then + PACKAGES_FILE="$PRECISE_PACKAGES" + # Otherwise, if this is Debian, and there's a version specifically + # for squeeze, use that: + elif [ x"$DISTRIBUTION" = x"debian" ] && [ -e "$SQUEEZE_PACKAGES" ] + then + PACKAGES_FILE="$SQUEEZE_PACKAGES" + else + PACKAGES_FILE="$GENERIC_PACKAGES" + fi + xargs -a "$PACKAGES_FILE" apt-get -y install +} + +overwrite_rc_local() { + cat > /etc/rc.local <<EOF +#!/bin/sh -e + +su -l -c $REPOSITORY/bin/ec2-rewrite-conf $UNIX_USER +/etc/init.d/$SITE restart + +exit 0 + +EOF + chmod a+rx /etc/rc.local +} + +generate_locales +set_locale + +add_unix_user "$UNIX_USER" + +update_apt_sources + +# And remove one crippling package, if it's installed: +apt-get remove -y --purge apt-xapian-index || true + +clone_or_update_repository $REPOSITORY + +chown -R "$UNIX_USER"."$UNIX_USER" "$DIRECTORY" + +install_nginx + +install_website_packages "$REPOSITORY" + +su -l -c "touch '$DIRECTORY/admin-htpasswd'" "$UNIX_USER" + +add_website_to_nginx "$UNIX_USER" "$HOST" "$REPOSITORY" + +add_postgresql_user "$UNIX_USER" + +DEFAULT_PARAMETER='' +if [ $DEFAULT_SERVER = true ] +then + DEFAULT_PARAMETER='--default' +fi + +su -l -c "$REPOSITORY/bin/install-as-user $DEFAULT_PARAMETER '$UNIX_USER' '$HOST'" "$UNIX_USER" + +install_sysvinit_script + +if [ $DEFAULT_SERVER = true ] && [ x != x$EC2_HOSTNAME ] +then + # If we're setting up as the default on an EC2 instance, + # make sure the ec2-rewrite-conf script is called from + # /etc/rc.local + overwrite_rc_local +fi + +# Tell the user what to do next: + +echo Installation complete - you should now be able to view the site at: +echo http://$HOST/ +echo Or you can run the tests by switching to the "'$UNIX_USER'" user and +echo running: $REPOSITORY/bin/cron-wrapper prove -r t diff --git a/bin/problem-creation-graph b/bin/problem-creation-graph index 6692ae724..b96d45540 100755 --- a/bin/problem-creation-graph +++ b/bin/problem-creation-graph @@ -26,12 +26,12 @@ else DATE="2007-02-01" fi -SOURCEA=/tmp/bci-creation-rate-graph-data-$RANDOM$RANDOM -SOURCEB=/tmp/bci-creation-rate-graph-data-$RANDOM$RANDOM -SOURCEC=/tmp/bci-creation-rate-graph-data-$RANDOM$RANDOM -SOURCED=/tmp/bci-creation-rate-graph-data-$RANDOM$RANDOM -SOURCEE=/tmp/bci-creation-rate-graph-data-$RANDOM$RANDOM -GPSCRIPT=/tmp/bci-creation-rate-graph-script-$RANDOM$RANDOM +SOURCEA=/tmp/fms-creation-rate-graph-data-$RANDOM$RANDOM +SOURCEB=/tmp/fms-creation-rate-graph-data-$RANDOM$RANDOM +SOURCEC=/tmp/fms-creation-rate-graph-data-$RANDOM$RANDOM +SOURCED=/tmp/fms-creation-rate-graph-data-$RANDOM$RANDOM +SOURCEE=/tmp/fms-creation-rate-graph-data-$RANDOM$RANDOM +GPSCRIPT=/tmp/fms-creation-rate-graph-script-$RANDOM$RANDOM # where status in ('draft') @@ -104,6 +104,6 @@ END #echo "gpscript $GPSCRIPT" export GDFONTPATH=/usr/share/fonts/truetype/ttf-bitstream-vera -gnuplot < $GPSCRIPT > fixmystreet/web/bci-live-creation$EXTENSION 2>/dev/null +gnuplot < $GPSCRIPT > fixmystreet/web/fms-live-creation$EXTENSION 2>/dev/null diff --git a/bin/problems-filed-graph b/bin/problems-filed-graph index 8addacd62..e5946b078 100755 --- a/bin/problems-filed-graph +++ b/bin/problems-filed-graph @@ -20,8 +20,8 @@ source fixmystreet/commonlib/shlib/deployfns read_conf fixmystreet/conf/general.yml -SOURCEO=/tmp/bci-report-rate-graph-data-nonwmc-$RANDOM$RANDOM -GPSCRIPT=/tmp/bci-report-rate-graph-script-$RANDOM$RANDOM +SOURCEO=/tmp/fms-report-rate-graph-data-nonwmc-$RANDOM$RANDOM +GPSCRIPT=/tmp/fms-report-rate-graph-script-$RANDOM$RANDOM echo "select date(created), count(*) @@ -57,5 +57,5 @@ END #echo "gpscript $GPSCRIPT" export GDFONTPATH=/usr/share/fonts/truetype/ttf-bitstream-vera -gnuplot < $GPSCRIPT > fixmystreet/web/bci-live-line$EXTENSION 2>/dev/null +gnuplot < $GPSCRIPT > fixmystreet/web/fms-live-line$EXTENSION 2>/dev/null |