<commit_before># Redirus worker [![build status](https://secure.travis-ci.org/dice-cyfronet/redirus-worker.png)](https://travis-ci.org/dice-cyfronet/redirus-worker) [![Code Climate](https://codeclimate.com/github/dice-cyfronet/redirus-worker.png)](https://codeclimate.com/github/dice-cyfronet/redirus-worker) [![Dependency Status](https://gemnasium.com/dice-cyfronet/redirus-worker.png)](https://gemnasium.com/dice-cyfronet/redirus-worker) [![Coverage Status](https://coveralls.io/repos/dice-cyfronet/redirus-worker/badge.png?branch=master)](https://coveralls.io/r/dice-cyfronet/redirus-worker)

The redirus worker is responsible for consuming create/delete subdomain redirections,
generating the appropriate nginx configurations and reloading nginx.

## Requirements

**This project is designed for Linux operating systems.**

- Linux (tested on Ubuntu)
- Nginx
- Ruby 2.0+
- Redis (can be installed on separate server)

## Packages / Dependencies

Install the required packages (needed to compile Ruby and nginx):

```
sudo apt-get update

apt-get install autoconf bison build-essential libssl-dev libyaml-dev libreadline6 libreadline6-dev zlib1g zlib1g-dev g++ make libpcre3 libpcre3-dev libssl-dev
```

## Ruby

You can use ruby installed by ruby version managers such as [RVM](http://rvm.io/)
or [rbenv](https://github.com/sstephenson/rbenv), or install it globally from
sources. The following manual presents global installation.

Remove the old Ruby 1.8 if present

```
sudo apt-get remove ruby1.8
```

Download Ruby and compile it:

```
mkdir /tmp/ruby && cd /tmp/ruby
curl --progress ftp://ftp.ruby-lang.org/pub/ruby/2.1/ruby-2.1.2.tar.gz | tar xz
cd ruby-2.1.2
./configure --disable-install-rdoc
make
sudo make install
```

Install the Bundler Gem:

```
sudo gem install bundler --no-ri --no-rdoc
```

## Nginx installation

Download and compile nginx:

```
mkdir /tmp/nginx && cd /tmp/nginx
curl http://nginx.org/download/nginx-1.7.4.tar.gz | tar xz
cd nginx-1.7.4
./configure --with-http_ssl_module --prefix=/nginx/installation/path
make
make install
```

Update nginx configuration:

```
edit /nginx/installation/path/conf/nginx.conf
```

the simplest configuration may look as follows:

```
#user  nobody;
worker_processes  1;


#pid        logs/nginx.pid;
pid /nginx/installation/path/nginx.pid;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;
    types_hash_max_size 2048;
    server_names_hash_bucket_size  128;

    include /path/to/generated/nginx/configurations/*;
}
```
Two elements from presented configuration need to be customized:
+ `/nginx/installation/path/nginx.pid` file where nginx pid will be written.
+ `/path/to/generated/nginx/configurations/*` path to the place where redirus
worker will be generating configurations specific to registered redirections. `*`
is necessary at the end in order to load all configurations from this directory.

If  nginx is to bind to a low-numbered port, e.g. port 80,
the following command needs to be executed as root:

```
setcap 'cap_net_bind_service=+ep' /path/to/nginx/sbin/nginx
```

## Redirus worker installation

```bash
# Get code
git clone https://github.com/dice-cyfronet/redirus-worker.git
<commit_msg>Fix #7: mention how to generate self signed cert<commit_after># Redirus worker [![build status](https://secure.travis-ci.org/dice-cyfronet/redirus-worker.png)](https://travis-ci.org/dice-cyfronet/redirus-worker) [![Code Climate](https://codeclimate.com/github/dice-cyfronet/redirus-worker.png)](https://codeclimate.com/github/dice-cyfronet/redirus-worker) [![Dependency Status](https://gemnasium.com/dice-cyfronet/redirus-worker.png)](https://gemnasium.com/dice-cyfronet/redirus-worker) [![Coverage Status](https://coveralls.io/repos/dice-cyfronet/redirus-worker/badge.png?branch=master)](https://coveralls.io/r/dice-cyfronet/redirus-worker)

The redirus worker is responsible for consuming create/delete subdomain redirections,
generating the appropriate nginx configurations and reloading nginx.

## Requirements

**This project is designed for Linux operating systems.**

- Linux (tested on Ubuntu)
- Nginx
- Ruby 2.0+
- Redis (can be installed on separate server)

## Packages / Dependencies

Install the required packages (needed to compile Ruby and nginx):

```
sudo apt-get update

apt-get install autoconf bison build-essential libssl-dev libyaml-dev libreadline6 libreadline6-dev zlib1g zlib1g-dev g++ make libpcre3 libpcre3-dev libssl-dev
```

## Ruby

You can use ruby installed by ruby version managers such as [RVM](http://rvm.io/)
or [rbenv](https://github.com/sstephenson/rbenv), or install it globally from
sources. The following manual presents global installation.

Remove the old Ruby 1.8 if present

```
sudo apt-get remove ruby1.8
```

Download Ruby and compile it:

```
mkdir /tmp/ruby && cd /tmp/ruby
curl --progress ftp://ftp.ruby-lang.org/pub/ruby/2.1/ruby-2.1.2.tar.gz | tar xz
cd ruby-2.1.2
./configure --disable-install-rdoc
make
sudo make install
```

Install the Bundler Gem:

```
sudo gem install bundler --no-ri --no-rdoc
```

## Nginx installation

Download and compile nginx:

```
mkdir /tmp/nginx && cd /tmp/nginx
curl http://nginx.org/download/nginx-1.7.4.tar.gz | tar xz
cd nginx-1.7.4
./configure --with-http_ssl_module --prefix=/nginx/installation/path
make
make install
```

Update nginx configuration:

```
edit /nginx/installation/path/conf/nginx.conf
```

the simplest configuration may look as follows:

```
#user  nobody;
worker_processes  1;


#pid        logs/nginx.pid;
pid /nginx/installation/path/nginx.pid;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;
    types_hash_max_size 2048;
    server_names_hash_bucket_size  128;

    include /path/to/generated/nginx/configurations/*;
}
```
Two elements from presented configuration need to be customized:
+ `/nginx/installation/path/nginx.pid` file where nginx pid will be written.
+ `/path/to/generated/nginx/configurations/*` path to the place where redirus
worker will be generating configurations specific to registered redirections. `*`
is necessary at the end in order to load all configurations from this directory.

If  nginx is to bind to a low-numbered port, e.g. port 80,
the following command needs to be executed as root:

```
setcap 'cap_net_bind_service=+ep' /path/to/nginx/sbin/nginx
```

## Self signed certificate

In production environment valid certificate (with `*` in CN section) signed by trusted organization should be used. If you don't have such certificate than
you can generate self signed certificate:

```
mkdir /usr/share/ssl/certs/my-domain.pl
cd /usr/share/ssl/certs/my-domain.pl
(umask 077 && touch host.key host.cert host.info host.pem)
openssl genrsa 2048 > host.key
openssl req -new -x509 -nodes -sha1 -days 3650 -key host.key > host.cert
...[enter *.my-domain.pl for the Common Name]...
openssl x509 -noout -fingerprint -text < host.cert > host.info
cat host.cert host.key > host.pem
chmod 400 host.key host.pem
```

## Redirus worker installation

```bash
# Get code
git clone https://github.com/dice-cyfronet/redirus-worker.git

{'fraction_non_alphanumeric': 0.12102341952077975, 'fraction_numerical': 0.01434953296331393, 'mean_word_length': 4.046448087431694, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 7, 'https://': 20, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4566442', 'n_tokens_mistral': 2634, 'n_tokens_neox': 2418, 'n_words': 681}