# Start off with a bootstrap container just so that we can pin down the dependencies of the current version of nscli
FROM python:alpine3.7 as bootstrap
ADD . .
RUN python setup.py egg_info
# This grep is intended to convert requires.txt into a useable requirements.txt
RUN grep -v '\[.*\]' nscli.egg-info/requires.txt > /tmp/requirements.txt


FROM debian:latest as builder

LABEL name="NSCLI Jenkins Testing Image" \
      maintainer="Network Security Infrastructure <nsi@rackspace.com>" \
      author="Micah Culpepper <micah.culpepper@rackspace.com>" \
      repo="https://github.rackspace.com/NSI/nscli"

ENV DEBIAN_FRONTEND=noninteractive
ENV DEBEMAIL="nsi@rackspace.com"
ENV DEBFULLNAME="GTS NetSec Automation Engineering"
ENV PS1='[\u@\h \W]\$ '
ENV LC_ALL=C.UTF-8

# Disable logging and metrics. We don't want unit tests to count as legit usage of nscli.
RUN printf '%b' '127.0.0.1\tappstats.rackspace.com\n' >> /etc/hosts && \
    printf '%b' '127.0.0.1\tgatekeeper.nsi.rackspace.com\n' >> /etc/hosts

# Use the rax mirror as our preferred source for debian files
RUN apt-get -y update && apt-get -y install apt-transport-https ca-certificates && apt-get -y clean && \
    mv /etc/apt/sources.list /etc/apt/sources.list.old && \
    printf '%b' 'deb https://mirror.rackspace.com/debian/ stretch main\n' > /etc/apt/sources.list && \
    cat /etc/apt/sources.list.old >> /etc/apt/sources.list

# Install Rackspace CA certs.
COPY ["docker/ca_certs/*.crt", "/usr/local/share/ca-certificates/"]
RUN update-ca-certificates

# Install required packages
# bash: required by Jenkins, see https://issues.jenkins-ci.org/browse/JENKINS-41374
# build-base: Make
# coreutils: gnu version of date for tarball building
# curl: nscli install script
# git: Some nscli Makefile recipes need git
# libffi-dev: needed to compile cffi, from cryptography from secretstorage from keyring
# ssh: core nscli functionality
# openssl: nscli https interactions; RAX CA cert installation
# openssl-dev: needed to compile openssl, from cryptography from secretstorage from keyring
# python3, python3-dev, python3-pip, python3-setuptools, python3-venv: nscli dependencies
# libkrb5-dev: dependency of kerberos python library, which is a dependency of atlassian-python-api
RUN apt-get -y update && \
    apt-get -y upgrade && \
    apt-get -y install \
        bash \
        build-essential \
        coreutils \
        debhelper \
        devscripts \
        dh-virtualenv \
        equivs \
        gcc \
        git \
        gnupg2 \
        libffi-dev \
        libgmp3-dev \
        libkrb5-dev \
        man \
        openssl \
        python3 \
        python3-dev \
        python3-pip \
        python3-setuptools \
        python3-venv \
        ssh \
        sudo && \
    apt-get -y clean

# Add our artifactory repo as a trusted source
RUN echo 'deb https://artifacts.rackspace.net/artifactory/nscli-debian-local stretch main' >> /etc/apt/sources.list && \
    curl -fsSL https://artifacts.rackspace.net/artifactory/api/gpg/key/public | apt-key add -

# Set up Jenkins user and group
RUN addgroup --gid 989 jenkins && \
    adduser --disabled-password --uid 992 --home /var/lib/jenkins --shell /bin/bash --ingroup jenkins jenkins && \
    echo "jenkins ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers && \
    mkdir -p /var/lib/jenkins && \
    chown -R jenkins:jenkins /var/lib/jenkins && \
    mkdir -p /var/pip/cache /var/pip/download /var/pip/wheel && \
    chown -R jenkins:jenkins /var/pip
VOLUME /srv/jenkins/workspace
WORKDIR /var/lib/jenkins
USER jenkins

# Give Jenkins a bash profile that will help simulate a user's environment
COPY --chown=jenkins:jenkins ["docker/profile", ".bash_profile"]

# Copy over a pre-configured NSCLI settings file
COPY --chown=jenkins:jenkins ["docker/settings.ini", ".config/nscli/"]

# Add global pip config
COPY ["docker/pip.conf", "/etc/pip.conf"]

# Set up virtual env + Cache Python packages for faster re-builds
COPY --chown=jenkins:jenkins --from=bootstrap ["/tmp/requirements.txt", "."]
RUN sudo pip3 install -U pip setuptools wheel && \
    python3 -m venv . && \
    bin/pip3 install -U pip setuptools wheel && \
    pip3 wheel -r requirements.txt


# Copy the current working tree into the container and set up a dev environment
ONBUILD ADD --chown=jenkins:jenkins . .
ONBUILD ADD --chown=root:root debian /dpkg-build
ONBUILD USER jenkins
# We re-try the `make db` bit because it likes to fail due to network issues that we haven't figured out yet.
# I do appreciate the irony.
ONBUILD RUN cd /var/lib/jenkins && . /var/lib/jenkins/.bash_profile && . bin/activate && \
    pip install -e '.[dev]' && \
    scripts/load_credentials.py --credpath docker/credentials.json && \
    rm -f docker/credentials.json && \
    make nscli/data/config.db || make nscli/data/config.db || make nscli/data/config.db

#ARG VERSION
#WORKDIR /dpkg-build
#RUN dch --create --distribution unstable --package "nscli" --newversion "$VERSION" "release notes are published at https://pages.github.rackspace.com/nsi/nscli/Changelog.html"
#RUN dpkg-buildpackage -us -uc -b && mkdir -p /dpkg && cp -pl /nscli[-_]* /dpkg
