Skip to content

Commit

Permalink
Prepare Alexia for Kubernetes deployment.
Browse files Browse the repository at this point in the history
- Add Dockerfile
- Add helper scripts for management
- Fallback settings to configure via environment variables
- Add healthz view
- Add GitHub Actions build pipeline
- Remove old TravisCI config
  • Loading branch information
Kurocon committed Sep 9, 2024
1 parent 6ca034f commit f1e422f
Show file tree
Hide file tree
Showing 18 changed files with 529 additions and 38 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# top-most EditorConfig file
root = true

# UNIX line endings, UTF-8 encoding, no trailing whitespace and a newline ending every file.
[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

# 4 space indentation in python source files
[*.py]
indent_style = space
indent_size = 4
83 changes: 83 additions & 0 deletions .github/workflows/build_docker_image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
name: Build docker image and run tests

on: [push, pull_request]

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

services:
mariadb:
image: mariadb:10.4
env:
MARIADB_USER: alexia_test
MARIADB_PASSWORD: alexia_test
MYSQL_DATABASE: alexia_test
MYSQL_ROOT_PASSWORD: alexia_test
ports: ['3306:3306']
options: --health-cmd="mysqladmin ping" --health-interval=5s --health-timeout=2s --health-retries=3

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: Build and push Docker image
id: push
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

- name: Verify MariaDB connection
run: |
while ! mysqladmin ping -h"127.0.0.1" -P"3306" --silent; do
sleep 1
done
- name: Run alexia tests
run: |
docker run --rm --entrypoint "/alexia/scripts/run_tests.sh" ghcr.io/inter-actief/alexia@${{ steps.push.outputs.digest }}
- name: Cleanup untagged images older than 1 week
uses: snok/container-retention-policy@v2
with:
image-names: alexia
cut-off: 1 week ago UTC
account-type: org
org-name: Inter-Actief
token: ${{ secrets.GITHUB_TOKEN }}
token-type: github-token
untagged-only: true

- name: Cleanup tagged images (except main and production) older than 1 month
uses: snok/container-retention-policy@v2
with:
image-names: alexia
cut-off: 1 month ago UTC
account-type: org
org-name: Inter-Actief
token: ${{ secrets.GITHUB_TOKEN }}
token-type: github-token
skip-tags: main, production
32 changes: 0 additions & 32 deletions .travis.yml

This file was deleted.

42 changes: 42 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Build the alexia docker image based on Debian 11 (Bullseye)
FROM debian:bullseye

# Copy alexia sources
COPY . /alexia

# Set /alexia as startup working directory
WORKDIR /alexia

# Install required packages for alexia and prepare the system to run alexia
RUN echo "Updating repostitories..." && \
apt-get update -y && \
echo "Upgrading base debian system..." && \
apt-get upgrade -y && \
echo "Installing alexia required packages..." && \
apt-get install -y apt-utils git net-tools python3 python3-pip mariadb-client libmariadb-dev xmlsec1 libssl-dev libldap-dev libsasl2-dev libjpeg-dev zlib1g-dev gettext locales acl && \
echo "Enabling 'nl_NL' and 'en_US' locales..." && \
sed -i -e 's/# nl_NL.UTF-8 UTF-8/nl_NL.UTF-8 UTF-8/' /etc/locale.gen && \
sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \
echo "Rebuilding locales..." && \
dpkg-reconfigure --frontend=noninteractive locales && \
echo "Creating directories for alexia..." && \
mkdir -p /alexia /config /static /media /var/log /var/run && \
echo "Installing python requirements..." && \
pip3 install -r requirements.txt && \
echo "Correcting permissions on directories..." && \
chown -R 1000:1000 /alexia /config /static /media /var/log

# Switch back to a local user
USER 1000:1000

# Check if Django can run
RUN python3 manage.py check

# Expose volumes
VOLUME ["/config", "/static", "/media"]

# Expose the web port
EXPOSE 8000

# Start the website
CMD ["/alexia/scripts/start_web_wsgi.sh"]
6 changes: 5 additions & 1 deletion alexia/apps/general/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.sites.shortcuts import get_current_site
from django.core.exceptions import PermissionDenied
from django.http import HttpResponseRedirect
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import get_object_or_404, resolve_url
from django.template.response import TemplateResponse
from django.urls import reverse_lazy
Expand Down Expand Up @@ -154,3 +154,7 @@ def get_context_data(self, **kwargs):

class HelpView(TemplateView):
template_name = 'general/help.html'


def healthz_view(request):
return HttpResponse('ok', content_type="text/plain")
7 changes: 7 additions & 0 deletions alexia/conf/settings/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from alexia.conf.settings.base import *

# Try to import local settings, fallback to config via environment variables if that fails
try:
from alexia.conf.settings.local import *
except ImportError:
from alexia.conf.settings.environ import *
Loading

0 comments on commit f1e422f

Please sign in to comment.