Skip to content

Commit

Permalink
feat: Native Dockerfile (openedx-unsupported#580)
Browse files Browse the repository at this point in the history
* feat: analyticsapi config for native image
  • Loading branch information
aht007 authored Dec 1, 2022
1 parent 376d671 commit d25ac1b
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 62 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dockerfile
2 changes: 1 addition & 1 deletion .github/docker-compose-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: "2"
services:

analytics_api:
image: edxops/analytics_api:latest
image: edxops/analytics-api-dev:latest
container_name: analytics_api_testing
volumes:
- ..:/edx/app/analytics_api/analytics_api
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ jobs:
targets: [ 'quality','main.test','docs' ]

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v3
- name: setup python
uses: actions/setup-python@v2
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}

Expand Down
20 changes: 0 additions & 20 deletions .github/workflows/docker-publish.yml

This file was deleted.

52 changes: 52 additions & 0 deletions .github/workflows/push-docker-images.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Build and Push Docker Images

on:
push:
branches:
- master
tags:
- open-release/*
jobs:
push:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

# Use the release name as the image tag if we're building an open release tag.
# Examples: if we're building 'open-release/maple.1', tag the image as 'maple.1'.
# Otherwise, we must be building from a push to master, so use 'latest'.
- name: Get tag name
id: get-tag-name
uses: actions/github-script@v5
with:
script: |
const releasePrefix = 'refs/tags/open-release/';
const tagName = context.ref.split(releasePrefix)[1] || 'latest';
console.log('Will use tag: ' + tagName);
return tagName;
result-encoding: string

- name: Build and push Dev Docker image
uses: docker/build-push-action@v1
with:
push: true
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
target: dev
repository: edxops/analytics-api-dev
tags: ${{ steps.get-tag-name.outputs.result }},${{ github.sha }}

# This part is commented out for now as edxops/insights is the older image built using Ansible.
# For smooth transition we want to keep that image intact too. Apart from this, the current priority is to get
# the devstack off of Ansible based Images.
# - name: Build and push prod Docker image
# uses: docker/build-push-action@v1
# with:
# push: true
# username: ${{ secrets.DOCKERHUB_USERNAME }}
# password: ${{ secrets.DOCKERHUB_PASSWORD }}
# target: prod
# repository: edxops/insights
# tags: ${{ steps.get-tag-name.outputs.result }},${{ github.sha }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,6 @@ elasticsearch-*

# Visual Studio Code
.vscode

#Pyenv
.python-version
87 changes: 65 additions & 22 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,34 +1,77 @@
FROM ubuntu:focal as app
FROM ubuntu:focal as base

# System requirements.
RUN apt update && \
apt-get install -y software-properties-common && \
apt-add-repository -y ppa:deadsnakes/ppa && apt-get update && \
apt install -y git-core language-pack-en python3.8-dev python3.8-venv libmysqlclient-dev libffi-dev libssl-dev build-essential gettext openjdk-8-jdk && \
apt-get install -qy \
curl \
vim \
language-pack-en \
build-essential \
python3.8-dev \
python3-virtualenv \
python3.8-distutils \
libmysqlclient-dev \
libssl-dev && \
rm -rf /var/lib/apt/lists/*

ENV VIRTUAL_ENV=/venv
RUN python3.8 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

RUN pip install pip==20.2.3 setuptools==50.3.0

# 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
ENV ANALYTICS_API_CFG /edx/etc/analytics_api.yml

WORKDIR /edx/app/analytics_api
COPY requirements /edx/app/analytics_api/requirements
RUN pip install -r requirements/production.txt
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 python3.8 --always-copy ${ANALYTICS_API_VENV_DIR}

# Expose canonical Analytics port
EXPOSE 19001

FROM base as prod

ENV DJANGO_SETTINGS_MODULE "analyticsdataserver.settings.production"

COPY requirements/production.txt ${ANALYTICS_API_CODE_DIR}/requirements/production.txt

RUN pip install -r ${ANALYTICS_API_CODE_DIR}/requirements/production.txt

# Copy over rest of code.
# We do this AFTER requirements so that the requirements cache isn't busted
# every time any bit of code is changed.

COPY . .

# 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"

COPY requirements/dev.txt ${ANALYTICS_API_CODE_DIR}/requirements/dev.txt

RUN pip install -r ${ANALYTICS_API_CODE_DIR}/requirements/dev.txt

EXPOSE 8100
CMD gunicorn --bind=0.0.0.0:8100 --workers 2 --max-requests=1000 -c /edx/app/analytics_api/analytics_data_api/docker_gunicorn_configuration.py analyticsdataserver.wsgi:application
# Copy over rest of code.
# We do this AFTER requirements so that the requirements cache isn't busted
# every time any bit of code is changed.
COPY . .

RUN useradd -m --shell /bin/false app
USER app
COPY . /edx/app/analytics_api
# Devstack related step for backwards compatibility
RUN touch /edx/app/${ANALYTICS_API_SERVICE_NAME}/${ANALYTICS_API_SERVICE_NAME}_env

FROM app as newrelic
RUN pip install newrelic
CMD newrelic-admin run-program gunicorn --bind=0.0.0.0:8100 --workers 2 --max-requests=1000 -c /edx/app/analytics_api/analytics_data_api/docker_gunicorn_configuration.py analyticsdataserver.wsgi:application
CMD while true; do python ./manage.py runserver 0.0.0.0:8110; sleep 2; done
17 changes: 0 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -120,22 +120,5 @@ github_ci: test.requirements clean migrate-all ## Used by CI for testing
python manage.py generate_fake_course_data --database=analytics --num-weeks=2 --no-videos --course-id "edX/DemoX/Demo_Course"
python manage.py generate_fake_course_data --database=analytics_v1 --num-weeks=2 --no-videos --course-id "edX/DemoX/Demo_Course"

docker_build:
docker build . -f Dockerfile -t openedx/analytics-data-api
docker build . -f Dockerfile --target newrelic -t openedx/analytics-data-api:latest-newrelic

docker_tag: docker_build
docker tag openedx/analytics-data-api openedx/analytics-data-api:${GITHUB_SHA}
docker tag openedx/analytics-data-api:latest-newrelic openedx/analytics-data-api:${GITHUB_SHA}-newrelic

docker_auth:
echo "$$DOCKERHUB_PASSWORD" | docker login -u "$$DOCKERHUB_USERNAME" --password-stdin

docker_push: docker_tag docker_auth ## push to docker hub
docker push 'openedx/analytics-data-api:latest'
docker push "openedx/analytics-data-api:${GITHUB_SHA}"
docker push 'openedx/analytics-data-api:latest-newrelic'
docker push "openedx/analytics-data-api:${GITHUB_SHA}-newrelic"

docs: tox.requirements
tox -e docs
1 change: 1 addition & 0 deletions requirements/dev.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Local development dependencies go here

-r base.in # Core dependencies of edx-analytics-data-api
mysqlclient
2 changes: 2 additions & 0 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ markdown==3.4.1
# via -r requirements/base.in
markupsafe==2.1.1
# via jinja2
mysqlclient==2.1.1
# via -r requirements/dev.in
newrelic==8.4.0
# via edx-django-utils
ordered-set==4.1.0
Expand Down

0 comments on commit d25ac1b

Please sign in to comment.