# Dockerfile

ARG         AWS_ACCOUNT=086679231553

FROM        ${AWS_ACCOUNT}.dkr.ecr.us-east-1.amazonaws.com/docker-parent-images:python311 AS base

WORKDIR     /var/app

USER        root
# Update and install common packages
RUN         apt update -y
RUN         apt install -y zip libffi-dev

# Create and configure virtualenv and uwsgi
ENV         VIRTUAL_ENV /var/venv
RUN         python3.11 -m venv ${VIRTUAL_ENV}
ENV         PATH "$VIRTUAL_ENV/bin:$PATH"

RUN         pip install --upgrade pip
RUN         apt-get -y update \
            && apt-get -y install gcc \
            && pip install uwsgi==2.0.23 \
            && apt-get -y remove --purge gcc \
            && apt-get -y clean \
            && rm -rf /var/lib/apt/lists/*

# Add application and install requirements
COPY        requirements.txt ./
COPY        requirements-dev.txt ./
RUN         pip install -I -r requirements.txt
ADD         . /var/app


#######################################
### Build image for live deployment ###
#######################################
FROM        base AS deploy

# Remove tests, scripts, etc.
RUN         rm -rf /var/app/tests/ /var/app/scripts/ /var/app/dev.py /var/app/.flake8

# Use startup script as entrypoint
COPY        uwsgi-start.sh /
RUN         chmod +x /uwsgi-start.sh

USER        worker
ENTRYPOINT  ["/uwsgi-start.sh"]

EXPOSE      8080


###########################
### Build image for dev ###
###########################
FROM        base AS dev

# httpretty install complains about some unicode error without this
ENV         LC_ALL=en_US.utf-8
ENV         LC_CTYPE=en_US.UTF-8

RUN         pip install -r /var/app/requirements-dev.txt

# Enables prints in python to flush to stdout
ENV         PYTHONUNBUFFERED 1
# Start dev server
ENTRYPOINT  ["/var/venv/bin/python"]
CMD         ["dev.py", "2>&1"]


#################################
### Build image for unit-lint ###
#################################
FROM        base as unit-lint

# Install dev/test requirements
RUN         pip install -I -r requirements-dev.txt
COPY        scripts/unit-lint.sh /
RUN         chmod +x scripts/unit-lint.sh

USER        worker
ENTRYPOINT  ["scripts/unit-lint.sh"]


########################################
### Build image for test-integration ###
########################################
FROM        base as test-integration

# Install test requirements
RUN         pip install -I -r tests/integration/requirements.txt
COPY        scripts/test-integration.sh /
RUN         chmod +x scripts/test-integration.sh

USER        worker
ENTRYPOINT  [ "scripts/test-integration.sh" ]
