# twibooru-on-rails
If you're reading this, may the gods have mercy 'pon you.
Ignore almost everything below this line. It's wrong.
## Installation (Easy mode - For development environments)
#### Prerequisites
* Latest version of VirtualBox and the VM Extension Pack ([download](https://www.virtualbox.org/wiki/Downloads))
* Latest version of Vagrant ([download](https://www.vagrantup.com))
#### Installation
1. Download and install the prerequisites
2. Open Command Prompt/Terminal and run the following commands:
```
vagrant plugin install vagrant-hostmanager
vagrant plugin install vagrant-vbguest
```
3. Download and extract the repo
4. `cd` into the repo using Command Prompt/Terminal
5. Run the following command:
`vagrant up`
5. This would be a good time to rewatch your favorite "PONIES The Anthology" installment, cook & have breakfast/lunch/dinner, walk the dog, clean your room, etc.
By the time you get back the install will surely have completed.1
6. ???
7. Profit!
1 If the install did not finish by the time an activity is complete please select another activity to avoid crippling boredom.
Some Bash scripts are provided in the `vagrant` directory for your convenience. You can use `vagrant/restart`, `vagrant/stop`, `vagrant/start` and `vagrant/status` from the project root directory to manage the systemd service within the container. Likewise, `vagrant/migrate` and `vagrant/rollback` can be used to run or roll back database migrations, respectively.
If you run into any other issues or need direct command line access you can use `vagrant ssh` to log into the virtual environment's shell and run any commands you deem necessary.
## Installation and requirements (Hard mode - For production, servers)
You should be able to install from top to bottom
#### OS
* Linux (Debian stretch or newer, Ubuntu 18.04 will work)
#### Packages
##### Root install commands
```bash
# Add repositories and keys
apt-get update; apt-get -qq -y install apt-transport-https
echo "deb http://deb.debian.org/debian stretch-backports main" > /etc/apt/sources.list.d/stretch-backports.list
echo "deb https://artifacts.elastic.co/packages/6.x/apt stable main" > /etc/apt/sources.list.d/elasticsearch-6.x.list
echo "deb http://apt.postgresql.org/pub/repos/apt/ stretch-pgdg main" > /etc/apt/sources.list.d/pgdg.list
echo "deb https://deb.nodesource.com/node_12.x stretch main" > /etc/apt/sources.list/nodesource.list
wget -qO - https://packages.elastic.co/GPG-KEY-elasticsearch | apt-key add -
wget -qO - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
wget -qO - https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add -
# Install dependencies
apt-get update
apt-get install elasticsearch ffmpeg gifsicle git libcurl4-openssl-dev \
libicu-dev libjpeg-progs libopencv-dev libpq-dev libreadline-dev libxml2-dev \
nodejs optipng libgit2-27 redis-server postgresql-11 nginx libbison-dev \
libffi-dev libgdbm-dev libncurses5-dev libssl-dev libyaml-dev
# Configure PostgreSQL
sed -i -e 's/md5/trust/' /etc/postgresql/11/main/pg_hba.conf
service postgresql restart
sudo -u postgres createuser -s $APP_USER
# Configure Elasticsearch
# Set this to whatever you think is reasonable
sed -i -e 's/\(-Xm[sx]\)1g/\1256m/' /etc/elasticsearch/jvm.options
systemctl enable elasticsearch
systemctl start elasticsearch
# Follow the instructions at
# to install chruby and ruby-install, using chruby 0.3.9 and ruby-install 0.7.0.
# Append these lines into ~/.bashrc for $APP_USER:
# source /usr/local/share/chruby/chruby.sh
# source /usr/local/share/chruby/auto.sh
# Then install ruby as follows:
ruby-install ruby 2.6.3
# You will need to manually configure nginx. Usually this is done by
# copying or symlinking config/nginx.conf.example into /etc/nginx/conf.d/.
#
# Then, run
systemctl enable nginx
systemctl start nginx
```
##### User install commands
```bash
# Install and configure the bundler gem
gem install bundler
bundler config github.https true
# Enter the app directory
cd $APP_DIR
# Run bundler
bin/bundle install
# Run setup
bin/setup
```
##### Compiling tools (needed for Ruby compilation and gems)
* GCC
* Make
##### Image processing
* ImageMagick (6.7 or greater)
* gifsicle (1.64 or greater)
* optipng
* jpegtran (libjpeg-progs)
* Inkscape (for SVG rendering)
* libopencv-dev (below 3, for dedupe)
* ffmpeg (for WebMs)
##### General
* Development headers for: libxml, libxslt, zlib1g, openssl (required for Ruby and Nokogiri gem; packages end in -dev)
* node.js (8 or greater, for script compilation)
* libicu-dev (for character conversion)
* curl.h (required by Patron gem; libcurl4-openssl-dev and similar provide)
* libreadline-dev (required for Ruby)
* curl
* Java runtime environment (default-jre-headless; required for Elasticsearch)
* libgit2 (27; instructions [here](https://github.com/libgit2/libgit2), use /usr for install path)
#### Services
* Elasticsearch (6.0 or newer)
* PostgreSQL (9.6 or newer)
* Redis (redis-server)
* nginx >1.6 (nginx)
#### Ruby
* Using `chruby`, [install](http://ryanbigg.com/2014/10/ubuntu-ruby-ruby-install-chruby-and-you) the version of Ruby specified in *.ruby-version*.
#### Gems
* Bundler gem (`gem install bundler`)
* `bundler config github.https true`
## Setup
### Services autostart
* `sudo systemctl start postgresql elasticsearch redis`
### PostgreSQL
#### User creation
* `sudo -u postgres createuser $APP_USER`
#### Localhost access
* In `/etc/postgresql//main/pg_hba.conf`, change ```md5``` on both lines to ```trust```
```
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
```
* One-liner: `sed -i -e 's/md5/trust/' /etc/postgresql/11/main/pg_hba.conf`
### Installation
* `git clone git@gitlab.com:derpibooru/derpibooru.git`
* `cd derpibooru`
* Replace the default nginx site configuration (*/etc/nginx/sites-enabled/default*) with *config/nginx.conf.example*.
Remember to adjust it if your app is somewhere other than */home/derpibooru*.
* `./bin/setup`
You can rerun `./bin/setup` whenever you need to reset your environment.
If setup is failing (particularly on schema loading), you can do:
* `rake db:drop`
* `rake db:create`
* `rake db:schema:load`
* `rake db:seed DEV_SEED='y'`
## Configuring serving endpoints
The default configuration serves all content locally.
In config/booru/settings.yml:
```yml
# What's your public hostname? Used in creating email links
public_host: ''
# Where is your site? No trailing slash.
public_url_root: ""
# Where is your CDN root? Set this to an Amazon CloudFront distribution domain or CNAME origined at the public_url_root,
# or just set this to the same as public_url_root if you're not using CloudFront or a similar CDN. No trailing slash.
cdn_url_root: ""
# Where are images located? Match this with your nginx configuration; normally should be "/path" on your CDN,
# with no trailing slash.
image_url_root: "/img"
```
## Testing
`rails test`
## Deployment
You need a key installed on the server you target, and the git remote installed in your ssh configuration.
git remote add production derpibooru@:derpibooru/
The general syntax is:
git push production master
And if everything goes wrong:
git reset HEAD^ --hard
git push -f production master
(to be repeated until it works again)