From 74c9609ec1b8d59a40a276603720dc3b79f2940b Mon Sep 17 00:00:00 2001 From: Muhammad Farhan Date: Wed, 16 Oct 2024 11:54:53 +0500 Subject: [PATCH] feat: add workflow and dockerfile for edx-analytics-data-api --- .../push-edx-analytics-data-api-image.yaml | 58 ++++++++++ dockerfiles/edx-analytics-data-api.Dockerfile | 104 ++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 .github/workflows/push-edx-analytics-data-api-image.yaml create mode 100644 dockerfiles/edx-analytics-data-api.Dockerfile diff --git a/.github/workflows/push-edx-analytics-data-api-image.yaml b/.github/workflows/push-edx-analytics-data-api-image.yaml new file mode 100644 index 0000000..1b60021 --- /dev/null +++ b/.github/workflows/push-edx-analytics-data-api-image.yaml @@ -0,0 +1,58 @@ +name: Build and Push Edx Analytics Data Api Image + +on: + workflow_dispatch: + inputs: + branch: + description: "Target branch from which the source dockerfile from image will be sourced" + + schedule: + - cron: "0 4 * * 1-5" # UTC Time + +jobs: + build-and-push-image: + runs-on: ubuntu-latest + + steps: + - name: Get tag name + id: get-tag-name + uses: actions/github-script@v5 + with: + script: | + const tagName = "${{ github.event.inputs.branch }}" || 'latest'; + console.log('Will use tag: ' + tagName); + return tagName; + result-encoding: string + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Login to DockerHub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_PASSWORD }} + + - name: Build and push Dev Docker image + uses: docker/build-push-action@v6 + with: + file: ./dockerfiles/edx-analytics-data-api.Dockerfile + push: true + target: dev + tags: edxops/analytics-api-dev:${{ steps.get-tag-name.outputs.result }} + + - name: Send failure notification + if: failure() + uses: dawidd6/action-send-mail@v3 + with: + server_address: email-smtp.us-east-1.amazonaws.com + server_port: 465 + username: ${{secrets.edx_smtp_username}} + password: ${{secrets.edx_smtp_password}} + subject: Push Image to docker.io/edxops failed in Edx Analytics Data Api + to: team-cosmonauts@edx.org + from: github-actions + body: Push Image to docker.io/edxops for Edx Analytics Data Api failed! For details see "github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" diff --git a/dockerfiles/edx-analytics-data-api.Dockerfile b/dockerfiles/edx-analytics-data-api.Dockerfile new file mode 100644 index 0000000..7e64cbf --- /dev/null +++ b/dockerfiles/edx-analytics-data-api.Dockerfile @@ -0,0 +1,104 @@ +FROM ubuntu:focal as base + +# System requirements. + +# ENV variables for Python 3.11 support +ARG PYTHON_VERSION=3.11 +ENV TZ=UTC +ENV TERM=xterm-256color +ENV DEBIAN_FRONTEND=noninteractive + +# software-properties-common is needed to setup Python 3.11 env +RUN apt-get update && \ + apt-get install -y software-properties-common && \ + apt-add-repository -y ppa:deadsnakes/ppa + +# pkg-config; mysqlclient>=2.2.0 requires pkg-config (https://github.com/PyMySQL/mysqlclient/issues/620) + +RUN apt-get update && \ + apt-get install -qy \ + build-essential \ + curl \ + vim \ + language-pack-en \ + build-essential \ + python${PYTHON_VERSION} \ + python${PYTHON_VERSION}-dev \ + python${PYTHON_VERSION}-distutils \ + libmysqlclient-dev \ + pkg-config \ + libssl-dev && \ + rm -rf /var/lib/apt/lists/* + +RUN update-alternatives --install /usr/bin/python python /usr/bin/python${PYTHON_VERSION} 1 +RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python${PYTHON_VERSION} 1 + +# need to use virtualenv pypi package with Python 3.11 +RUN curl -sS https://bootstrap.pypa.io/get-pip.py | python${PYTHON_VERSION} +RUN pip install virtualenv + +# Use UTF-8. +RUN locale-gen en_US.UTF-8 +ENV LANG en_US.UTF-8 +ENV LANGUAGE en_US:en +ENV LC_ALL en_US.UTF-8 + +ARG COMMON_APP_DIR="/edx/app" +ARG ANALYTICS_API_SERVICE_NAME="analytics_api" +ENV ANALYTICS_API_HOME "${COMMON_APP_DIR}/${ANALYTICS_API_SERVICE_NAME}" +ARG ANALYTICS_API_APP_DIR="${COMMON_APP_DIR}/${ANALYTICS_API_SERVICE_NAME}" +ARG ANALYTICS_API_VENV_DIR="${COMMON_APP_DIR}/${ANALYTICS_API_SERVICE_NAME}/venvs/${ANALYTICS_API_SERVICE_NAME}" +ARG ANALYTICS_API_CODE_DIR="${ANALYTICS_API_APP_DIR}/${ANALYTICS_API_SERVICE_NAME}" + +ENV ANALYTICS_API_CODE_DIR="${ANALYTICS_API_CODE_DIR}" +ENV PATH "${ANALYTICS_API_VENV_DIR}/bin:$PATH" +ENV COMMON_CFG_DIR "/edx/etc" +ENV ANALYTICS_API_CFG "/edx/etc/${ANALYTICS_API_SERVICE_NAME}.yml" + +# Working directory will be root of repo. +WORKDIR ${ANALYTICS_API_CODE_DIR} + +RUN virtualenv -p python${PYTHON_VERSION} --always-copy ${ANALYTICS_API_VENV_DIR} + +# Create a directory named 'requirements' to copy requirements files into it +RUN mkdir -p requirements + +# Expose canonical Analytics port +EXPOSE 19001 + +FROM base as prod + +ENV DJANGO_SETTINGS_MODULE "analyticsdataserver.settings.production" + + +RUN curl -L -o requirements/production.txt https://raw.githubusercontent.com/edx/edx-analytics-data-api/master/requirements/production.txt + +RUN pip install -r ${ANALYTICS_API_CODE_DIR}/requirements/production.txt + +# Download the remaining code. +# We do this AFTER requirements so that the requirements cache isn't busted +# every time any bit of code is changed. + +RUN curl -L https://github.com/edx/edx-analytics-data-api/archive/refs/heads/master.tar.gz | tar -xz --strip-components=1 + +# exec /edx/app/analytics_api/venvs/analytics_api/bin/gunicorn -c /edx/app/analytics_api/analytics_api_gunicorn.py analyticsdataserver.wsgi:application + +CMD ["gunicorn" , "-b", "0.0.0.0:8100", "--pythonpath", "/edx/app/analytics_api/analytics_api","analyticsdataserver.wsgi:application"] + +FROM base as dev + +ENV DJANGO_SETTINGS_MODULE "analyticsdataserver.settings.devstack" + +RUN curl -L -o requirements/dev.txt https://raw.githubusercontent.com/edx/edx-analytics-data-api/master/requirements/dev.txt + +RUN pip install -r ${ANALYTICS_API_CODE_DIR}/requirements/dev.txt + +# Download the remaining code. +# We do this AFTER requirements so that the requirements cache isn't busted +# every time any bit of code is changed. +RUN curl -L https://github.com/edx/edx-analytics-data-api/archive/refs/heads/master.tar.gz | tar -xz --strip-components=1 + +# Devstack related step for backwards compatibility +RUN touch /edx/app/${ANALYTICS_API_SERVICE_NAME}/${ANALYTICS_API_SERVICE_NAME}_env + +CMD while true; do python ./manage.py runserver 0.0.0.0:8110; sleep 2; done