mysociety/alaveteli

View on GitHub
script/install-as-user

Summary

Maintainability
Test Coverage
#!/bin/bash

set -e
set -x

if [ $# -ne 6 ]
then
    cat >&2 <<EOUSAGE
Usage: $0 <UNIX-USER> <HOST> <INSTALLATION-DIRECTORY> <RUBY-VERSION> <USE_RBENV> <DEVELOPMENT_INSTALL>
EOUSAGE
    exit 1
fi

UNIX_USER="$1"
HOST="$2"
DIRECTORY="$3"
RUBY_VERSION="$4"
USE_RBENV="$5"
DEVELOPMENT_INSTALL="$6"
DB_NAME="alaveteli"

# 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."
    exit 1
fi

if [ "$(whoami)" != "$UNIX_USER" ]
then
    echo "This script should be run by the user '$UNIX_USER'."
    exit 1
fi

REPOSITORY="$DIRECTORY/alaveteli"
LINK_DESTINATION="$HOME/alaveteli"

if [ "$REPOSITORY" != "$LINK_DESTINATION" ]
then
  ln -sfn "$REPOSITORY" $LINK_DESTINATION
fi

cd "$REPOSITORY"

BASHRC="$HOME/.bashrc"

BASHRC_GEM_COMMENT="Set up local gem directory for Alaveteli"
BASHRC_START="# START $BASHRC_GEM_COMMENT"
BASHRC_END="# END $BASHRC_GEM_COMMENT"

# Remove the old lines we added:
sed -ibackup "/$BASHRC_START/,/$BASHRC_END/d" "$BASHRC"

# Create a temporary file, so we can prepend the lines we need.  They
# need to be prepended since the Ubuntu skeleton .bashrc begins with
# '[ -z "$PS1" ] && return', skipping the rest of the .bashrc for
# non-interactive use, but we need the gem settings when invoking
# commands in the shell non-interactively.
TMP_BASHRC="$(mktemp "$BASHRC.XXXXXXX")"

if [ $USE_RBENV = true ]; then
  RBENV="$HOME/.rbenv"
  if ! [ -d $RBENV ]; then
    echo 'Install rbenv & ruby-build'
    git clone https://github.com/rbenv/rbenv.git "$RBENV"
    cd $RBENV && src/configure && make -C src
    mkdir -p "$RBENV/plugins"
    git clone https://github.com/rbenv/ruby-build.git "$RBENV/plugins/ruby-build"
  else
    echo 'Update rbenv & ruby-build'
    cd $RBENV && git pull --ff-only
    cd $RBENV/plugins/ruby-build && git pull --ff-only
  fi
  cd "$REPOSITORY"

  cat >>$TMP_BASHRC <<EOBRC
$BASHRC_START
export PATH="$RBENV/bin:\$HOME/.gem/ruby/$RUBY_VERSION/bin:\$PATH"
eval "\$(rbenv init -)"
$BASHRC_END
EOBRC

else
  cat >>$TMP_BASHRC <<EOBRC
$BASHRC_START
export PATH="\$HOME/.gem/ruby/$RUBY_VERSION/bin:\$PATH"
$BASHRC_END
EOBRC
fi

cat "$BASHRC" >> "$TMP_BASHRC"
mv "$TMP_BASHRC" "$BASHRC"

source "$BASHRC"

if [ $USE_RBENV = true ]; then
  echo "Installing Ruby $RUBY_VERSION"
  # Compile and install Ruby and set as global default for this user
  rbenv install -s $RUBY_VERSION
  rbenv global $RUBY_VERSION
  gem install bundler
  gem pristine --all
fi

# Speed up the installation of gems:
echo 'gem: --no-document' > "$HOME/.gemrc"

# Write sensible values into the config file:

function random_alphanumerics() {
    < /dev/urandom tr -dc A-Za-z0-9 | head -c$1
}

RANDOM_EMAIL_SECRET=$(random_alphanumerics 32)
RANDOM_EMERGENCY_PASSWORD=$(random_alphanumerics 10)
RANDOM_COOKIE_SECRET=$(random_alphanumerics 100)

if [ "$DEVELOPMENT_INSTALL" = true ]; then
  STAGING_SITE=1
else
  STAGING_SITE=0
fi

if ! [ -f config/general.yml ]
then
    sed -r \
        -e "s,^( *DOMAIN:).*,\\1 '$HOST'," \
        -e "s,^( *FORCE_SSL:).*,\\1 false," \
        -e "s,^( *TIME_ZONE:).*,\\1 'Europe/London'," \
        -e "s,^( *BLOG_FEED:).*,\\1 ''," \
        -e "s,^( *TWITTER_USERNAME:).*,\\1 ''," \
        -e "s,^( *INCOMING_EMAIL_DOMAIN:).*,\\1 '$HOST'," \
        -e "s,^( *INCOMING_EMAIL_PREFIX:).*,\\1 'foi+'," \
        -e "s,^( *INCOMING_EMAIL_SECRET:).*,\\1 '$RANDOM_EMAIL_SECRET'," \
        -e "s,^( *ADMIN_USERNAME:).*,\\1 'emergency'," \
        -e "s,^( *ADMIN_PASSWORD:).*,\\1 '$RANDOM_EMERGENCY_PASSWORD'," \
        -e "s,^( *CONTACT_EMAIL:).*,\\1 'postmaster@$HOST'," \
        -e "s,^( *TRACK_SENDER_EMAIL:).*,\\1 'postmaster@$HOST'," \
        -e "s,^( *SECRET_KEY_BASE:).*,\\1 '$RANDOM_COOKIE_SECRET'," \
        -e "s,^( *STAGING_SITE:).*,\\1 '$STAGING_SITE'," \
        -e "s,^( *FORWARD_NONBOUNCE_RESPONSES_TO:).*,\\1 'user-support@$HOST'," \
        -e "s,^( *HTML_TO_PDF_COMMAND:).*,\\1 '/usr/local/bin/wkhtmltopdf'," \
        -e "s,^( *EXCEPTION_NOTIFICATIONS_FROM:).*,\\1 'do-not-reply-to-this-address@$HOST'," \
        -e "/EXCEPTION_NOTIFICATIONS_TO:/,/^$/c EXCEPTION_NOTIFICATIONS_TO:\n - team@$HOST\n" \
        -e "s,^( *MTA_LOG_PATH:).*,\\1 '/var/log/mail/mail.log-*'," \
        -e "s,^( *MTA_LOG_TYPE:).*,\\1 'postfix'," \
        -e "s,^( *DONATION_URL:).*,\\1 null," \
        -e "s,^( *THEME_BRANCH:).*,\\1 'develop'," \
        -e "s,^( *USE_MAILCATCHER_IN_DEVELOPMENT:).*,\\1 false," \
        -e "s,^( *BUNDLE_PATH:).*,\\1 $HOME/bundle/," \
        -e "s,^( *POP_MAILER_USER_NAME:).*,\\1 'alaveteli-incoming'," \
        -e "s,^( *POP_MAILER_PASSWORD:).*,\\1 'alaveteli-incoming'," \
        -e "s,^( *POP_MAILER_PORT:).*,\\1 '110'," \
        -e "s,^( *POP_MAILER_ENABLE_SSL:).*,\\1 false," \
        config/general.yml-example > config/general.yml
fi

# add database.yml
sed -r \
    -e "s,^( *database: *)foi_(.*),\\1${DB_NAME}_\\2," \
    -e "s,^( *username: *).*,\\1${UNIX_USER}," \
    -e "s,^( *password: *).*,\\1null," \
    -e "s,^( *host: *).*,\\1/var/run/postgresql/," \
    -e "s,# constraint_disabling: false,  constraint_disabling: false," \
    config/database.yml-example > config/database.yml

for SUFFIX in production test development
do
    REAL_DB_NAME="${DB_NAME}_$SUFFIX"
    echo Creating the database $REAL_DB_NAME
    # Create each database if it doesn't exist:
    if ! psql -l | egrep "^ *$REAL_DB_NAME *\|" > /dev/null
    then
        createdb -T template_utf8 --owner "$UNIX_USER" "$REAL_DB_NAME"
    fi
done

# add sidekiq.yml
cp config/sidekiq.yml-example config/sidekiq.yml

# add storage.yml
cp config/storage.yml-example config/storage.yml

echo Running rails-post-deploy
script/rails-post-deploy

# We created the test database above and migrated the development / production
# database through `script/rails-post-deploy`, but in a development install
# we're left without a ready-to-go test database. We don't want to run
# `script/rails-post-deploy` with `RAILS_ENV=test` since that runs all sorts of
# other things, so just migrate the test database as a separate task.
if [ "$DEVELOPMENT_INSTALL" = true ]; then
  echo Migrating test database
  RAILS_ENV=test bundle exec rake db:migrate
fi

LOADED_INDICATOR="$HOME/.alaveteli-sample-data-loaded"

if [ ! -f "$LOADED_INDICATOR" ]
then
    echo Running load-sample-data
    bundle exec script/load-sample-data

    echo Running destroy-and-rebuild-xapian-index
    script/destroy-and-rebuild-xapian-index

    touch "$LOADED_INDICATOR"
fi