Skip to content

Commit

Permalink
feat: Add Helm chart for wagtail (#382)
Browse files Browse the repository at this point in the history
* feat: Add Helm chart for wagtail

* fix: Fix indentation issue in config map

* chore: Set DJANGO_SETTINGS_MODULE to ietf.settings.dev

* chore: Format local.py using Black

* chore: Prepend new line before importing file

prepends a new line

* chore: Comment out livenessProbe

* fix: Specify tag name for wagtail image

* fix: Use correct port for wagtail service

* chore: Expand ALLOWED_HOST to include 127.0.0.1

* chore: Use app user instead of superuser

* docs: Add how to run K8s locally to CONTRIBUTING.md

* chore: Pin wagtail_website image to v2.0.0

* chore: Use target port
  • Loading branch information
Sangho Na authored Mar 21, 2024
1 parent 4dc2ec7 commit 09026b6
Show file tree
Hide file tree
Showing 15 changed files with 868 additions and 0 deletions.
118 changes: 118 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,121 @@ See the [deployment section](README.md#deployment)

Read [Contributing to IETF Tools projects](https://github.com/ietf-tools/.github/blob/main/CONTRIBUTING.md).
For this project treat `deploy/preview` brach as `main`.

## Running Kubernetes Locally

### Prerequisites

- `kubectl`

See [Install Tools](https://kubernetes.io/docs/tasks/tools/) (kubernetes.io) for more info.

- `minikube`

See [Install Tools](https://kubernetes.io/docs/tasks/tools/) (kubernetes.io) for more info.

- `helm`

See [Installing Helm](https://helm.sh/docs/intro/install/) (helm.sh) for more info.

- Docker image: `postgres`

``` bash
docker pull postgres:14.6-alpine
```

- Database backup: `wagtail_backup_latest.gz`

``` bash
gunzip wagtail_backup_latest.gz
```

Note: The name of your backup file will be different.

### Quick Start

1. Start a `minikube` cluster unless running already.

``` bash
minikube status
```

``` bash
minikube start
```

2. Run a PostgreSQL instance in a new container.

``` bash
docker run --name my-postgres \
-v $(pwd)/wagtail_backup_latest:/wagtail_backup_latest \
-v pgdata:/var/lib/postgresql/data \
-e POSTGRES_PASSWORD=postgres \
-p 5432:5432 \
postgres:14.6-alpine
```

3. Start a new `bash` session in the container.

``` bash
docker exec -it 188fcfec0bf9 bash
```

where `188fcfec0bf9` is the ID of the container.

1. (Required only once) Using `psql`, create a new database called `wagtail`, and a new role called `www_iab`.

``` bash
psql -U postgres
```

``` sql
CREATE DATABASE wagtail;
CREATE ROLE www_iab WITH LOGIN PASSWORD 'www_iab';
```

``` text
\q
```

2. (Required only once) Restore `wagtail_backup_latest` to the `wagtail` database using `pg_restore`.

``` bash
pg_restore -U postgres -d wagtail wagtail_backup_latest
```

4. Run

``` bash
helm install wagtail helm
```

to install the Helm chart.

5. Initiate a port-forwarding session for the pod that is running the `wagtail` service.

``` bash
kubectl --namespace default port-forward $POD_NAME 8080:$CONTAINER_PORT
```

e.g.

``` bash
kubectl --namespace default port-forward wagtail-wagtail-5bf8f48bf5-28dvr 8080:8000
```

6. Go to localhost:8080 on your web browser, and perform basic testing.

7. Create an admin user.

``` bash
kubectl exec -it $POD_NAME --container wagtail -- python manage.py createsuperuser
```

e.g.

``` bash
kubectl exec -it wagtail-wagtail-845c6b54b5-dkfqw --container wagtail -- python manage.py createsuperuser
```

8. Using the admin username and password, log in to localhost:8080/admin.
23 changes: 23 additions & 0 deletions helm/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
20 changes: 20 additions & 0 deletions helm/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: v2
name: wagtail
description: Helm chart for IETF Wagtail services
maintainers:
- name: IETF Tools Team
email: [email protected]
url: https://github.com/ietf-tools

type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 1.0.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "1.0.0"
73 changes: 73 additions & 0 deletions helm/local.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import os

DEFAULT_FROM_EMAIL = "[email protected]"
SERVER_EMAIL = "[email protected]"
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = os.environ["EMAIL_HOST"]

DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"HOST": os.environ["DBHOST"],
"NAME": os.environ["DBNAME"],
"USER": os.environ["DBUSER"],
"PASSWORD": os.environ["DBPASS"],
"CONN_MAX_AGE": 600, # number of seconds database connections should persist for
},
}

SECRET_KEY = os.environ["SECRET_KEY"]

FILE_UPLOAD_PERMISSIONS = 0o664

ALLOWED_HOSTS = os.environ["ALLOWED_HOSTS"].split(",")

WAGTAILADMIN_BASE_URL = os.environ["WAGTAILADMIN_BASE_URL"]

ADMINS = (("Django Server", "[email protected]"),)

# Logging

LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"mail_admins": {
"level": "ERROR",
"class": "django.utils.log.AdminEmailHandler",
},
},
"loggers": {
"django.request": {
"handlers": ["mail_admins"],
"level": "ERROR",
"propagate": False,
},
"django.security": {
"handlers": ["mail_admins"],
"level": "ERROR",
"propagate": False,
},
},
}

MATOMO_DOMAIN_PATH = "analytics.ietf.org"
MATOMO_SITE_ID = "1"
MATOMO_DISABLE_COOKIES = True

MEMCACHED_HOST = os.environ["MEMCACHED_SERVICE_HOST"]
MEMCACHED_PORT = os.environ["MEMCACHED_SERVICE_PORT"]

CACHES = {
"default": {
"BACKEND": "django.core.cache.backends.memcached.PyMemcacheCache",
"LOCATION": f"{MEMCACHED_HOST}:{MEMCACHED_PORT}",
"KEY_PREFIX": "ietf",
},
"sessions": {
"BACKEND": "django.core.cache.backends.memcached.PyMemcacheCache",
"LOCATION": f"{MEMCACHED_HOST}:{MEMCACHED_PORT}",
"KEY_PREFIX": "ietf",
},
"dummy": {"BACKEND": "django.core.cache.backends.dummy.DummyCache"},
}
112 changes: 112 additions & 0 deletions helm/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "wagtail.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "wagtail.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create a fully qualified wagtail name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
*/}}
{{- define "wagtail.wagtail.fullname" -}}
{{- if .Values.wagtail.fullnameOverride -}}
{{- .Values.wagtail.fullnameOverride | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- if contains $name .Release.Name -}}
{{- printf "%s-%s" .Release.Name .Values.wagtail.name | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- printf "%s-%s-%s" .Release.Name $name .Values.wagtail.name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{/*
Create a fully qualified memcached name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
*/}}
{{- define "wagtail.memcached.fullname" -}}
{{- if .Values.memcached.fullnameOverride -}}
{{- .Values.memcached.fullnameOverride | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- if contains $name .Release.Name -}}
{{- printf "%s-%s" .Release.Name .Values.memcached.name | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- printf "%s-%s-%s" .Release.Name $name .Values.memcached.name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "wagtail.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "wagtail.labels" -}}
helm.sh/chart: {{ include "wagtail.chart" . }}
{{ include "wagtail.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "wagtail.selectorLabels" -}}
app.kubernetes.io/name: {{ include "wagtail.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "wagtail.serviceAccountName.wagtail" -}}
{{- if .Values.serviceAccounts.wagtail.create -}}
{{ default (include "wagtail.wagtail.fullname" .) .Values.serviceAccounts.wagtail.name }}
{{- else -}}
{{ default "default" .Values.serviceAccounts.wagtail.name }}
{{- end -}}
{{- end }}

{{- define "wagtail.serviceAccountName.celery" -}}
{{- if .Values.serviceAccounts.celery.create -}}
{{ default (include "wagtail.celery.fullname" .) .Values.serviceAccounts.celery.name }}
{{- else -}}
{{ default "default" .Values.serviceAccounts.celery.name }}
{{- end -}}
{{- end }}

{{- define "wagtail.serviceAccountName.memcached" -}}
{{- if .Values.serviceAccounts.memcached.create -}}
{{ default (include "wagtail.memcached.fullname" .) .Values.serviceAccounts.memcached.name }}
{{- else -}}
{{ default "default" .Values.serviceAccounts.memcached.name }}
{{- end -}}
{{- end }}
7 changes: 7 additions & 0 deletions helm/templates/configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: django-configmap
data:
local.py: |-
{{ .Files.Get "local.py" | nindent 4 }}
Loading

0 comments on commit 09026b6

Please sign in to comment.