FROM docker.pkgs.xarth.tv/devtools/bionic/base:latest
LABEL authors="Dylan Reichstadt"

#===================================
# Selenium Environment Configuration
#===================================

# Additional ports (42, 43) for Selenium 4. Ordering is important as Beanstalk picks the first Expose for Port 80
EXPOSE 4444 4442 4443

# As integer, maps to "maxSession"
ENV GRID_MAX_SESSION 5
# In milliseconds, maps to "newSessionWaitTimeout"
ENV GRID_NEW_SESSION_WAIT_TIMEOUT -1
# As a boolean, maps to "throwOnCapabilityNotPresent"
ENV GRID_THROW_ON_CAPABILITY_NOT_PRESENT true
# As an integer
ENV GRID_JETTY_MAX_THREADS -1
# In milliseconds, maps to "cleanUpCycle"
ENV GRID_CLEAN_UP_CYCLE 5000
# In seconds, maps to "browserTimeout"
ENV GRID_BROWSER_TIMEOUT 0
# In seconds, maps to "timeout"
ENV GRID_TIMEOUT 1800
# Debug
ENV GRID_DEBUG false
# As integer, maps to "port"
ENV GRID_HUB_PORT 4444
# As string, maps to "host"
ENV GRID_HUB_HOST "0.0.0.0"

#===============
# Grid Companion
#===============
ENV CBG_RUN_COMPANION true
#ENV CBG_COMPANION_ROUTER_URL "https://cbg-dev.internal.justin.tv:443" # Commented out - this is a required variable. Left for documentation.
ENV CBG_COMPANION_HUB_EXTERNAL_PORT 80
ENV CBG_COMPANION_POLL_WAIT 5

# No interactive frontend during docker build
ENV DEBIAN_FRONTEND=noninteractive \
    DEBCONF_NONINTERACTIVE_SEEN=true

RUN apt-get update \
 && apt-get install -y \
   openjdk-8-jre-headless \
   supervisor \
   wget \
   sudo \
   tzdata \
   python3 \
   python3-pip \
   python3-setuptools

#===================
# Timezone settings
# Possible alternative: https://github.com/docker/docker/issues/3359#issuecomment-32150214
#===================
ENV TZ "UTC"
RUN echo "${TZ}" > /etc/timezone \
  && dpkg-reconfigure --frontend noninteractive tzdata


##################
# Install AWS CLI
##################
RUN pip3 install --upgrade pip
RUN pip3 --no-cache-dir install --upgrade awscli

#========================================
# Add normal user with passwordless sudo
#========================================
RUN useradd seluser \
         --shell /bin/bash  \
         --create-home \
  && usermod -a -G sudo seluser \
  && echo 'ALL ALL = (ALL) NOPASSWD: ALL' >> /etc/sudoers \
  && echo 'seluser:secret' | chpasswd
ENV HOME=/home/seluser

#==========
# Create necessary folders & files
#==========
RUN mkdir -p /opt/selenium_3 /opt/selenium_4 /var/run/supervisor /var/log/supervisor /var/log/cbg \
  && touch /opt/selenium_3/config.json \

#===================================================
# Run the following commands as non-privileged user
#===================================================
USER seluser

# Supervisor Configuration
COPY configs/supervisor/supervisord.conf /etc
COPY configs/supervisor/conf.d/ /etc/supervisor/conf.d/

# Move Selenium artifacts and configs over
COPY artifacts/ /opt/
COPY configs/selenium_logging.config /opt/selenium_3/
COPY configs/cbg_config.yaml /opt/selenium_3/
COPY configs/se4_config.toml /opt/selenium_4/

# Move over launch scripts
COPY launch_scripts/ /opt/bin/

# Generate the default config
COPY generate_config.sh /opt/bin/
RUN /opt/bin/generate_config.sh > /opt/selenium_3/config.json

#==========
# Selenium & relaxing permissions for OpenShift and other non-sudo environments
#==========
RUN chmod -R 777 /opt/selenium_3 /opt/selenium_4 /var/log/cbg/ /var/run/supervisor /var/log/supervisor /etc/passwd \
    && chgrp -R 0 /opt/selenium_3 ${HOME} /var/run/supervisor /var/log/supervisor \
    && chmod -R g=u /opt/selenium_3 ${HOME} /var/run/supervisor /var/log/supervisor

CMD ["/opt/bin/entry_point.sh"]
