FROM ubuntu:14.04
LABEL maintainer="Richard Piacentini <richard.piacentini@nuxos.asia>"
ENV REFRESHED_AT 2019-04-16
ENV RUBY_VERSION 2.1.9
#ENV TZ Europe/Paris
#ENV LOCALE_STR fr_FR.UTF-8
ENV TZ Asia/Bangkok
ENV LOCALE_STR en_US.UTF-8

USER root

# Update 
RUN DEBIAN_FRONTEND=noninteractive apt-get update
RUN DEBIAN_FRONTEND=noninteractive apt-get upgrade -y
RUN DEBIAN_FRONTEND=noninteractive apt-get install locales -y 
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install --no-install-recommends rabbitmq-server     apache2 subversion build-essential \ 
    apache2-prefork-dev libreadline-dev libyaml-dev libapr1-dev sendemail libio-socket-ssl-perl \ 
    libnet-ssleay-perl imagemagick libmagickcore-dev libmagickwand-dev \
    python-software-properties libcurl4-openssl-dev nodejs zip htop nfs-common \
    gawk autoconf automake bison libffi-dev libgdbm-dev libncurses5-dev libtool sqlite3 libgmp-dev \
    git-core git openssl ca-certificates curl wget mysql-client
# Setup environment
RUN locale-gen ${LOCALE_STR}

# Install Sphinx
RUN cd /tmp
RUN wget http://sphinxsearch.com/files/sphinxsearch_2.2.6-release-0ubuntu12~trusty_amd64.deb && \
    dpkg -i sphinxsearch_2.2.6-release-0ubuntu12~trusty_amd64.deb
# Cleanup
RUN rm sphinxsearch_2.2.6-release-0ubuntu12~trusty_amd64.deb

# !! Create the directory /var/lock/apache2 before install Passenger Apache module
RUN /bin/bash -l -c 'mkdir -p /var/lock/apache2'
# !! Add ServerName directive to Apache conf to avoid warnings
RUN /bin/bash -l -c 'echo "ServerName localhost" >> /etc/apache2/apache2.conf'

# Setup pixpalace User with sudo & no password
ENV HOME="/home/pixpalace"
RUN adduser --system --shell /bin/bash --home ${HOME} pixpalace
RUN adduser pixpalace sudo
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

# !! Switch to pixpalace user
USER pixpalace

WORKDIR $HOME

# Install RVM
RUN for server in $(shuf -e hkp://pool.sks-keyservers.net \
    hkp://ipv4.pool.sks-keyservers.net \
    hkp://pgp.mit.edu \
    hkp://keyserver.pgp.com) ; do \
    gpg --keyserver "$server" --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB && break || : ; \
    done

# Error if --rails 
# ERROR:  Error installing rails:
#	The last version of i18n (>= 0.7, < 2) to support your Ruby & RubyGems was 1.5.1. 
#   Try installing it with `gem install i18n -v 1.5.1` and then running the current command again
#	i18n requires Ruby version >= 2.3.0. The current ruby version is 2.1.0.
#RUN \curl -sSL https://get.rvm.io | bash -s stable --ruby=$RUBY_VERSION --rails --autolibs=enabled
RUN \curl -sSL https://get.rvm.io | bash -s stable --ruby=$RUBY_VERSION --autolibs=enabled

# Load RVM
RUN /bin/bash -l -c 'source ~/.rvm/scripts/rvm'

# Create .bundle directory
RUN /bin/bash -l -c 'mkdir -p ${HOME}/.bundle'
RUN /bin/bash -l -c 'sudo chown -R pixpalace:  /home/pixpalace/.bundle'

ENV BUNDLE_PATH ${HOME}/.bundle       
ENV BUNDLE_APP_CONFIG ${HOME}/.bundle

# Install Ruby
RUN /bin/bash -l -c 'rvm use $RUBY_VERSION --default'
RUN /bin/bash -l -c 'rvm rubygems current'
RUN /bin/bash -l -c 'rvm ruby-2.1.9 do rvm gemset create pixpalace'
RUN /bin/bash -l -c 'rvm use ruby-2.1.9@pixpalace --default'

# Import Pixpalace CS 1.0 gemset
COPY ./docker/gems/docker-pixpalace_cs-1.0.gems /tmp
RUN /bin/bash -l -c 'sh /tmp/docker-pixpalace_cs-1.0.gems'

# Configure Passenger
RUN /bin/bash -l -c 'rvmsudo passenger-install-apache2-module --auto' 

# Add Passenger Apache module
COPY ./docker/apache/docker-apache-passenger-module.load \
    /etc/apache2/mods-available/passenger.load

# Add Passenger Apache vhost
COPY ./docker/apache/docker-apache-passenger-vhost.conf \
    /etc/apache2/sites-available/000-default.conf

# Activate Passenger moduleru   
RUN /bin/bash -l -c 'sudo a2enmod passenger'

# Pre-Bundle Gems in the container 
RUN /bin/bash -l -c 'mkdir -p ${HOME}/rails_app'
# Copy Pixpalace Gemfile
COPY ./Gemfile ${HOME}/rails_app
WORKDIR ${HOME}/rails_app
# Bundle install
RUN /bin/bash -l -c 'bundle install'

# TS Delta Rake tasks
COPY ./docker/tasks/docker.tsdelta.tasks.rb \ 
    ${HOME}/.rvm/gems/ruby-2.1.9@pixpalace/gems/ts-delayed-delta-2.0.2/lib/thinking_sphinx/deltas/delayed_delta/tasks.rb


CMD ['/bin/bash', '-l']