#
# Ubuntu Dockerfile
#
# https://github.com/dockerfile/ubuntu
# 
# // first build the image based on this dockerfile. From the same dir of this file run
# docker build -t eae-ubuntu-generic .
# // now run the container based on the image eae-ubuntu-generic 
# docker run -d --name bucherer-dev -v /var/www/bucherer:/var/www -p 1027:22 -p 1028:80 eae-ubuntu-generic
#

# Pull base image.
FROM ubuntu:14.04

RUN apt-get -y update && \
    apt-get -y upgrade && \
    #apt-get install -y build-essential && \
    apt-get install -y git && \
    apt-get install -y curl && \
    apt-get install -y vim && \
    apt-get install -y software-properties-common && \
    apt-get install -y mysql-client && \
    apt-get install -y apache2 php5 libapache2-mod-php5 php5-mysql

RUN apt-get install -y openssh-server
RUN apt-get install -y mcrypt
RUN apt-get install -y php5-curl
RUN apt-get install -y php5-mcrypt
RUN apt-get install -y php5-intl
RUN apt-get install -y rsyslog

RUN php5enmod mcrypt
RUN a2enmod headers

COPY ["php-apache-conf.ini", "/etc/php5/apache2/php.ini"] 
COPY ["php-cli-conf.ini", "/etc/php5/cli/php.ini"]

# this is used to run multiple services
RUN apt-get install -y supervisor 
ADD supervisord.conf /etc/supervisor/conf.d/supervisord.conf
    
RUN mkdir /var/run/sshd
RUN chmod 0755 /var/run/sshd
RUN useradd --create-home --shell /bin/bash --groups sudo eae ## includes 'sudo'
RUN echo "root:l0c42016" | chpasswd
RUN echo "eae:l0c42016" | chpasswd
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config

# Add authorized_keys
RUN mkdir /root/.ssh
RUN mkdir /home/eae/.ssh

# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile
RUN sudo a2enmod rewrite
RUN sudo a2enmod expires

ADD apache-config.conf /etc/apache2/sites-enabled/000-default.conf
ADD mpm_prefork.conf /etc/apache2/mods-enabled/mpm_prefork.conf

RUN curl –sS https://getcomposer.org/installer | php
RUN mv composer.phar /usr/local/bin/composer

RUN add-apt-repository ppa:chris-lea/redis-server
RUN apt-get update
RUN apt-get install redis-server -y
RUN apt-get install redis-tools -y


RUN groupadd -r rabbitmq && useradd -r -d /var/lib/rabbitmq -m -g rabbitmq rabbitmq

# add rabbitmq repo
RUN apt-get update && \
apt-get install wget --assume-yes && \
wget https://www.rabbitmq.com/rabbitmq-signing-key-public.asc && \
sudo apt-key add rabbitmq-signing-key-public.asc && \
sed -i -e '1ideb http://www.rabbitmq.com/debian/ testing main\' /etc/apt/sources.list && \
apt-get update && \
apt-get install rabbitmq-server --assume-yes --force-yes

# Enable plugins
RUN rabbitmq-plugins enable rabbitmq_management && \
rabbitmq-plugins enable rabbitmq_web_stomp && \
rabbitmq-plugins enable rabbitmq_mqtt

# expose ports
# Management
EXPOSE  15672
# Web-STOMP plugin
EXPOSE  15674
# MQTT:
EXPOSE  1883


# configure RabbitMQ
COPY ["rabbitmq-env.conf", "/etc/rabbitmq/rabbitmq-env.conf"]
RUN chmod 755 /etc/rabbitmq/rabbitmq-env.conf

# Create users for the apps
COPY ["rabbitmq-setup.sh", "/tmp/rabbitmq/rabbitmq-setup.sh"]
RUN mkdir /var/run/rabbitmq && \
chmod -R 755 /var/run/rabbitmq && \
chown -R rabbitmq:rabbitmq /var/run/rabbitmq && \
service rabbitmq-server start && \
sh /tmp/rabbitmq/rabbitmq-setup.sh && \
rm /tmp/rabbitmq/rabbitmq-setup.sh && \
service rabbitmq-server stop

COPY ["rabbitmq-supervisord.sh", "/opt/rabbitmq-supervisord.sh"]

EXPOSE 22 80 9001
CMD ["/usr/bin/supervisord"]

