<filename>images/airflow/Dockerfile
# Copyright 2018 AT&T Intellectual Property.  All other rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Docker image to run Airflow on Kubernetes
ARG FROM=ubuntu:16.04
FROM ${FROM}

LABEL org.opencontainers.image.authors='<EMAIL>, irc://#airshipit@freenode'
LABEL org.opencontainers.image.url='https://airshipit.org'
LABEL org.opencontainers.image.documentation='https://airship-shipyard.readthedocs.org'
LABEL org.opencontainers.image.source='https://git.openstack.org/openstack/airship-shipyard'
LABEL org.opencontainers.image.vendor='The Airship Authors'
LABEL org.opencontainers.image.licenses='Apache-2.0'

# Do not prompt user for choices on installation/configuration of packages
# Set port 8080 for Airflow Web
# Set port 5555 for Airflow Flower
# Set port 8793 for Airflow Worker
ENV container docker
ENV WEB_PORT 8080
ENV FLOWER_PORT 5555
ENV WORKER_PORT 8793
ENV SLUGIFY_USES_TEXT_UNIDECODE yes

# Expose port for applications
EXPOSE $WEB_PORT
EXPOSE $FLOWER_PORT
EXPOSE $WORKER_PORT

# Set ARG for usage during build
ARG AIRFLOW_HOME=/usr/local/airflow
ARG DEBIAN_FRONTEND=noninteractive
ARG ctx_base=src/bin

# Kubectl version
ARG KUBECTL_VERSION=1.10.2

RUN set -ex && \
    apt-get -qq update && \
    apt-get -y install \
    ca-certificates \
    curl \
    gcc \
    git \
    g++ \
    libffi-dev \
    libssl-dev \
    libpq-dev \
    locales \
    netcat \
    netbase \
    python3 \
    python3-setuptools \
    python3-pip \
    python3-dev \
    python3-dateutil \
    make \
    --no-install-recommends \
    && python3 -m pip install -U pip \
    && apt-get clean \
    && rm -rf \
        /var/lib/apt/lists/* \
        /tmp/* \
        /var/tmp/* \
        /usr/share/man \
        /usr/share/doc \
        /usr/share/doc-base

# Things that change mostly infrequently
RUN useradd -ms /bin/bash -d ${AIRFLOW_HOME} airflow \
    && curl -L -o /usr/local/bin/kubectl \
       https://storage.googleapis.com/kubernetes-release/release/v${KUBECTL_VERSION}/bin/linux/amd64/kubectl \
    && chmod +x /usr/local/bin/kubectl

# Dependency requirements
# Note - removing snakebite (python 2 vs. 3). See:
#    https://github.com/puckel/docker-airflow/issues/77
COPY images/airflow/requirements.txt /tmp/
RUN pip3 install -r /tmp/requirements.txt \
    && pip3 uninstall -y snakebite || true

# Copy scripts used in the container:
#   entrypoint.sh, airflow_start_service.sh and airflow_logrotate.sh
COPY images/airflow/script/*.sh ${AIRFLOW_HOME}/

# Change permissions
RUN chown -R airflow: ${AIRFLOW_HOME}

# Shipyard
#
# Shipyard provides core functionality used by the airflow plugins/operators
# Since Shipyard and Airflow are built together as images, this should prevent
# stale or out-of-date code between these parts.
# Shipyard requirements, source and installation
COPY ${ctx_base}/shipyard_airflow/requirements.txt /tmp/api_requirements.txt
RUN pip3 install -r /tmp/api_requirements.txt

COPY ${ctx_base}/shipyard_airflow /tmp/shipyard/
RUN cd /tmp/shipyard \
    && python3 setup.py install

# Set work directory
USER airflow
WORKDIR ${AIRFLOW_HOME}

# Execute entrypoint
ENTRYPOINT ["./entrypoint.sh"]
