Open main menu

Tool:Event Metrics

Toolforge tools
Crystal Clear app package utilities.png Event Metrics
Website https://eventmetrics.wmflabs.org/
Keywords statistics, analytics, events
Author(s) MusikAnimal, Samwilson, MaxSem, Mooeypoo
Maintainer(s) MusikAnimal, Samwilson, MaxSem, Mooeypoo (View all)
Source code GitHub
License GNU General Public License 3.0 or later
Issues Open tasks · Report a bug

This page documents the production and staging instances of Event Metrics. Some things in this guide still use the old name "Grant Metrics", such as references to the Toolforge account.

Note to maintainers: we don't backup any server configuration, so please document everything here.

Contents

Contact

The maintainers can be emailed at tools.grantmetrics tools.wmflabs.org (note that this means that the maintainers of the VPS account and the Toolforge account need to be in sync).

Production

Production Event Metrics is hosted on on a Wikimedia VPS instance. To log into the server, make sure you've been added as a maintainer to the eventmetrics project. Then set up SSH and connect via ssh eventmetrics-prod01.eventmetrics.eqiad.wmflabs and go to the /var/www directory. Not quite everything in this directory is in the Git repository.

Logs are written to /var/www/var/logs/prod.log, but only during a request where an error or high-priority log entry was made. This is why you'll see debug-level log entries in prod.log. You might also need to check /var/log/apache2/error.log for Apache-level errors.

OAuth consumer: Event Metrics 1.0

Database is s53550__grantmetrics on tools.labsdb, we're connecting as user s53550.

Web server configuration is all in /etc/apache2/sites-available/eventmetrics.conf.

There's a /var/www/deploy.sh script that runs every 10 minutes (from www-data's crontab) and if required updates to the latest release. The output of this is mailed to the maintainers.

Building a new instance

First create a new instance running on debian-jessie. Any production node should be at least a m1.medium with 4 GB of RAM, but for the main app server a m1.large is probably best.

Once the instance has been spawned, SSH in and follow these steps:

  1. Install PHP and Apache, along with some dependencies (using a Ondřej Surý Debian package).
    sudo apt-get install apt-transport-https lsb-release ca-certificates
    sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
    echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list
    sudo apt-get update
    sudo apt-get install -y apache2
    sudo apt-get install -y php7.2 php7.2-cli php7.2-common php7.2-curl php7.2-json php7.2-apcu
    sudo apt-get install -y php7.2-mysql php7.2-intl php7.2-xml php7.2-mbstring libapache2-mod-php7.2
    sudo apt-get install -y zip unzip php7.2-zip php7.2-bz2
    sudo a2dismod mpm_event && sudo a2enmod mpm_prefork && sudo a2enmod php7.2
    
  2. Install Node and npm.
    sudo apt-get install -y nodejs
    sudo apt-get install -y npm
    
  3. Symlink nodejs to node, so the uglify-js plugin runs the right command.
    sudo ln -s /usr/bin/nodejs /usr/bin/node
    
  4. We need to upgrade npm once more
    sudo npm install -g npm@next
    
  5. Install composer by following these instructions, but make sure to install to the /usr/local/bin directory and with the filename composer, e.g.:
    sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
    
  6. Clone the repository, first removing the html directory created by Apache.
    cd /var/www && sudo rm -rf html
    sudo git clone https://github.com/wikimedia/eventmetrics.git .
    
  7. Become the root user with sudo su root
  8. Run sudo composer install and fill in the necessary parameters. For most options you can use the default. Moving forward we will not run composer as root or with sudo, but via the webserver (see step #13 below).
  9. Create the deploy script at /var/www/deploy.sh with the following:
    #!/bin/bash
    
    cd /var/www
    
    git fetch --quiet origin 2>&1
    
    ## Find the highest and current tags
    HIGHEST_TAG=$(git tag | sort --version-sort | tail --lines 1)
    CURRENT_TAG=$(git describe --tags)
    
    ## Exit and say nothing if we're already at the highest tag.
    if [[ $CURRENT_TAG == $HIGHEST_TAG ]]; then
        # The following line can be temporarily uncommented as-needed
        # to force www-data to clear the production cache:
        # ./bin/console cache:clear --env prod
        exit 0
    fi
    
    ## If there's an update, pull and install it.
    git checkout $HIGHEST_TAG
    export SYMFONY_ENV=prod
    /usr/local/bin/composer install --no-dev --optimize-autoloader
    ./bin/console cache:clear --env prod
    ./bin/console doctrine:migrations:migrate --env prod --no-interaction
    
  10. Make sure the scripts are executable, and that all the files in the repo are owned by www-data.
    sudo chmod 744 deploy.sh
    sudo chown -R www-data:www-data .
    
  11. Create the web server configuration file at /etc/apache2/sites-available/eventmetrics.conf with the following:
    <VirtualHost *:80>
            DocumentRoot /var/www/public
            ServerName eventmetrics.wmflabs.org
    
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
    
            <Directory /var/www/public/>
                 Options Indexes FollowSymLinks
                 AllowOverride All
                 Require all granted
            </Directory>
    
            # Deny access to known spiders.
            SetEnvIfNoCase User-Agent "(uCrawler|Baiduspider|CCBot|scrapy\.org|kinshoobot|YisouSpider|Sogou web spider|yandex\.com/bots|twitterbot|TweetmemeBot|SeznamBot|datasift\.com/bot|Googlebot|Yahoo! Slurp|Python-urllib|BehloolBot|MJ12bot|SemrushBot)" bad_bot
    
            <Directory /var/www/>
                    Options Indexes FollowSymLinks
                    AllowOverride None
                    Require all granted
                    Deny from env=bad_bot
            </Directory>
    
            ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
            <Directory /usr/lib/cgi-bin/>
                    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                    Require all granted
            </Directory>
    
            SetEnvIf Request_URI "^/robots\.txt$" dontlog
            
            RewriteEngine On
            RewriteCond %{HTTP:X-Forwarded-Proto} !https
            RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
    </VirtualHost>
    
  12. Enable the mod-rewrite Apache module, and enable the web server configuration.
    sudo a2enmod rewrite
    sudo a2ensite eventmetrics
    sudo service apache2 reload
    
  13. Setup the crontab to run the deploy script every 10 minutes. We'll do this under the www-data user. If you are not already root, run sudo su root first, then:
    sudo crontab -e -u www-data
    
    Then add this to the bottom of the crontab:
    MAILTO=tools.grantmetrics@tools.wmflabs.org
    */10 * * * * /var/www/deploy.sh
    
  14. Wait for the email indicating composer ran successfully. If all goes well, you need only to gracefully (re)start apache, which is how you should always restart apache on production:
    sudo apachectl -k graceful
    

Staging

SSH to eventmetrics-dev01.eventmetrics.eqiad.wmflabs (see notes above in #Production about getting access).

Database is s53713__grantmetrics on tools.labsdb, we're connecting as user s53713.

OAuth consumer: Event Metrics - staging 1.0

The code on the staging server is kept up to date with the master branch with the following /var/www/deploy.sh script (run every 10 minutes from www-data's crontab):

#!/bin/bash

export SYMFONY_ENV=prod
cd /var/www

## Quit out if we're not on master, which probably means we're using
## this instance to do some testing before merging code.
GITBRANCH=$(git symbolic-ref --short -q HEAD)
if [ "$GITBRANCH" != "master" ]; then
  exit 0
fi

## See if there's any update.
GITFETCH=$(git fetch && git diff origin/master 2>&1)
if [ -z "$GITFETCH" ]; then
  exit 0
fi

## If there's an update, pull and install it.
git pull origin master
/usr/local/bin/composer install --no-dev --optimize-autoloader
./bin/console cache:clear --env prod
./bin/console doctrine:migrations:migrate --env prod --no-interaction

Setting up the staging server is the same as production, except you would use a smaller box (m1.small), and use the above deploy script instead of the one that goes off of tags. You also need to update the ServerName in eventmetrics.conf accordingly.

Testing other branches on staging

In general staging should always be up-to-date with master. If you do need to test something in this environment, you can checkout another branch, but you will need to also grant permissions back to www-data:

sudo git fetch origin
sudo git checkout -t origin/my-branch
sudo ./bin/console cache:clear --env prod
sudo chown -R www-data:www-data .

You can also run sudo composer install if needed, but ideally this wouldn't include database or major config changes that will need to be undone when going back to master.