Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dbp 000 fix backup release name targeting & other #80

Merged
merged 18 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion charts/dbp-moodle/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description: |
The Chart can be deployed without any modification but it is advised to set own secrets acccording to this readme.
type: application
home: https://dbildungsplattform.github.io/dbp-moodle/
version: 0.0.11
version: 0.0.12
appVersion: "4.1.14"
dependencies:
- name: moodle
Expand Down
4 changes: 2 additions & 2 deletions charts/dbp-moodle/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# dbp-moodle

![Version: 0.0.11](https://img.shields.io/badge/Version-0.0.11-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: 4.1.14](https://img.shields.io/badge/AppVersion-4.1.14-informational?style=flat-square)
![Version: 0.0.12](https://img.shields.io/badge/Version-0.0.12-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: 4.1.14](https://img.shields.io/badge/AppVersion-4.1.14-informational?style=flat-square)

This is a Helm Chart bundling some of the bitnami resources to deploy Moodle for DBildungsplattform. Extending them with features such as
MariaDB and PostgreSQL support, Horizontal Autoscaling capabilities, Redis Session Store, Etherpad-Lite.
Expand Down Expand Up @@ -247,7 +247,7 @@ The Chart can be deployed without any modification but it is advised to set own
| moodle.image.pullPolicy | string | `"Always"` | |
| moodle.image.registry | string | `"ghcr.io"` | |
| moodle.image.repository | string | `"dbildungsplattform/moodle"` | |
| moodle.image.tag | string | `"4.1.14-debian-12-r0-dbp3"` | The dbp-moodle image which is build for this helm chart |
| moodle.image.tag | string | `"4.1.14-debian-12-r0-dbp4"` | The dbp-moodle image which is build for this helm chart |
| moodle.ingress.annotations."cert-manager.io/cluster-issuer" | string | `"sc-cert-manager-clusterissuer-letsencrypt"` | |
| moodle.ingress.annotations."nginx.ingress.kubernetes.io/proxy-body-size" | string | `"200M"` | |
| moodle.ingress.annotations."nginx.ingress.kubernetes.io/proxy-connect-timeout" | string | `"30s"` | |
Expand Down
84 changes: 69 additions & 15 deletions charts/dbp-moodle/scripts/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,42 @@ set -e

health_file="/tmp/healthy"
marhode marked this conversation as resolved.
Show resolved Hide resolved
backup_dir="/tmp/backup"

readiness_bckp="/tmp/readinessprobe.json"
liveness_bckp="/tmp/livenessprobe.json"

# Create liveness probe file
touch "${health_file}"

# Create destination dir if not exists
if [ ! -d "${backup_dir}" ]; then
mkdir -p "${backup_dir}"
fi
dummy_probe_patch=$(cat <<-EOF
[{
"op": "replace",
"path": "/spec/template/spec/containers/0/readinessProbe",
"value": {
"exec": {
"command": [
"sh",
"-c",
"exit 0"
]
},
"initialDelaySeconds": 5,
"periodSeconds": 10
}
},
{
"op": "replace",
"path": "/spec/template/spec/containers/0/livenessProbe",
"value": {
"exec": {
"command": [
"sh",
"-c",
"exit 0"
]
},
"initialDelaySeconds": 5,
"periodSeconds": 10
}
}]
EOF
)

# Cleanup after finish only if not an update backup (normal backup has no CliUpdate file)
# If update backup: depending on exit code create the signal for the update helper job with success or failure
Expand All @@ -25,13 +51,27 @@ function clean_up() {

echo "=== Turn on liveness and readiness probe ==="
if [ -e "${readiness_bckp}" ] && [ -s "${readiness_bckp}" ] && [ -e "${liveness_bckp}" ] && [ -s "${liveness_bckp}" ] ; then
kubectl patch deployment/{{ .Release.Name }} -n {{ .Release.Namespace }} --type=json -p="[{\"op\": \"add\", \"path\": \"/spec/template/spec/containers/0/readinessProbe\", \"value\": $(cat ${readiness_bckp})}, {\"op\": \"add\", \"path\": \"/spec/template/spec/containers/0/livenessProbe\", \"value\": $(cat ${liveness_bckp})}]"
# Careful, format the string with tabs only!
restore_probe_patch=$(cat <<-EOF
[{
"op": "add",
"path": "/spec/template/spec/containers/0/readinessProbe",
"value": $(cat ${readiness_bckp})
},
{
"op": "add",
"path": "/spec/template/spec/containers/0/livenessProbe",
"value": $(cat ${liveness_bckp})
}]
EOF
)
kubectl patch "deployment/${release_name}" -n "{{ .Release.Namespace }}" --type=json -p="$restore_probe_patch"
else
echo "Unable to turn on liveness and readiness probes. Either the readiness_bckp or the liveness_bckp does not exist or is empty."
fi

echo "=== Unsuspending moodle cronjob ==="
kubectl patch cronjobs {{ .Release.Name }}-moodlecronjob-{{ include "moodlecronjob.job_name" . }} -n {{ .Release.Namespace }} -p '{"spec" : {"suspend" : false }}'
kubectl patch cronjobs "{{ .Release.Name }}-moodlecronjob-{{ include "moodlecronjob.job_name" . }}" -n "{{ .Release.Namespace }}" -p '{"spec" : {"suspend" : false }}'
elif [ $exit_code -eq 0 ]; then
echo "=== Update backup was successful with exit code $exit_code ==="
rm -f /mountData/moodledata/UpdateBackupFailure
Expand All @@ -48,18 +88,32 @@ function clean_up() {

trap "clean_up" EXIT

# Create liveness probe file
touch "${health_file}"
marhode marked this conversation as resolved.
Show resolved Hide resolved

# Deployment has "-moodle" appended if the Release.Name does not contain "moodle"
release_name="{{ .Release.Name }}"
if [[ $release_name != "moodle" && $release_name != *"moodle"* ]]; then
release_name="${release_name}-moodle"
fi

# Create destination dir if not exists
if [ ! -d "${backup_dir}" ]; then
mkdir -p "${backup_dir}"
fi

# If the backup is done for the update it skips the preparation because the update helper already did this
if ! [ -a /mountData/moodledata/CliUpdate ]; then
# Suspend the cronjob to avoid errors due to missing moodle
echo "=== Suspending moodle cronjob ==="
kubectl patch cronjobs {{ .Release.Name }}-moodlecronjob-{{ include "moodlecronjob.job_name" . }} -n {{ .Release.Namespace }} -p '{"spec" : {"suspend" : true }}'
kubectl patch cronjobs "{{ .Release.Name }}-moodlecronjob-{{ include "moodlecronjob.job_name" . }}" -n "{{ .Release.Namespace }}" -p '{"spec" : {"suspend" : true }}'

echo "=== Turn off liveness and readiness probe ==="
kubectl get deployment/{{ .Release.Name }} -n {{ .Release.Namespace }} -o jsonpath="{.spec.template.spec.containers[0].readinessProbe}" > ${readiness_bckp}
kubectl get deployment/{{ .Release.Name }} -n {{ .Release.Namespace }} -o jsonpath="{.spec.template.spec.containers[0].livenessProbe}" > ${liveness_bckp}
kubectl patch deployment/{{ .Release.Name }} -n {{ .Release.Namespace }} --type=json -p="[{'op': 'remove', 'path': '/spec/template/spec/containers/0/readinessProbe'}, {'op': 'remove', 'path': '/spec/template/spec/containers/0/livenessProbe'}]"
kubectl get "deployment/${release_name}" -n "{{ .Release.Namespace }}" -o jsonpath="{.spec.template.spec.containers[0].readinessProbe}" > ${readiness_bckp}
kubectl get "deployment/${release_name}" -n "{{ .Release.Namespace }}" -o jsonpath="{.spec.template.spec.containers[0].livenessProbe}" > ${liveness_bckp}
kubectl patch "deployment/${release_name}" -n "{{ .Release.Namespace }}" --type=json -p="$dummy_probe_patch"

kubectl rollout status deployment/{{ .Release.Name }} -n {{ .Release.Namespace }}
kubectl rollout status "deployment/${release_name}" -n "{{ .Release.Namespace }}"

# Wait for running jobs to finish to avoid errors
echo "=== Waiting for jobs to finish ==="
Expand Down
24 changes: 15 additions & 9 deletions charts/dbp-moodle/scripts/moodleRestoreScript.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
#!/bin/bash
set -e

health_file="/tmp/healthy"

# Create liveness probe file
touch "${health_file}"

function clean_up() {
exit_code=$?
if [ $exit_code -eq 0 ]; then
Expand All @@ -20,14 +15,25 @@ function clean_up() {

trap "clean_up" EXIT

health_file="/tmp/healthy"

# Create liveness probe file
touch "${health_file}"

# Deployment has "-moodle" appended if the Release.Name does not contain "moodle"
release_name="{{ .Release.Name }}"
if [[ "$release_name" != "moodle" && "$release_name" != *"moodle"* ]]; then
release_name="${release_name}-moodle"
fi

# Get current replicas and scale down deployment
replicas=$(kubectl get deployment/{{ .Release.Name }} -n {{ .Release.Namespace }} -o=jsonpath='{.status.replicas}')
replicas=$(kubectl get "deployment/${release_name}" -n {{ .Release.Namespace }} -o=jsonpath='{.status.replicas}')
echo "=== Current replicas detected: $replicas ==="
if [ -z "$replicas" ] || [ "$replicas" -eq 0 ]; then
replicas=1
fi
echo "=== Scale moodle deployment to 0 replicas for restore operation ==="
kubectl scale deployment/{{ .Release.Name }} --replicas=0 -n {{ .Release.Namespace }}
kubectl scale "deployment/${release_name}" --replicas=0 -n {{ .Release.Namespace }}
echo "=== After restore operation is completed will scale back to: $replicas replicas ==="

# Restore
Expand Down Expand Up @@ -90,7 +96,7 @@ PGPASSWORD="$DATABASE_PASSWORD" psql -h "$DATABASE_HOST" -p "$DATABASE_PORT" -U
echo "=== Finished DB restore ==="

echo "=== Scaling deployment replicas to $replicas ==="
kubectl scale deployment/{{ .Release.Name }} --replicas=$replicas -n {{ .Release.Namespace }}
kubectl scale "deployment/${release_name}" --replicas=$replicas -n {{ .Release.Namespace }}
sleep 2
scaledTo=$(kubectl get deployment/{{ .Release.Name }} -n {{ .Release.Namespace }} -o=jsonpath='{.status.replicas}')
scaledTo=$(kubectl get "deployment/${release_name}" -n {{ .Release.Namespace }} -o=jsonpath='{.status.replicas}')
echo "=== Deployment scaled to: $scaledTo ==="
2 changes: 1 addition & 1 deletion charts/dbp-moodle/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ moodle:
registry: ghcr.io
repository: dbildungsplattform/moodle
# -- The dbp-moodle image which is build for this helm chart
tag: "4.1.14-debian-12-r0-dbp3"
tag: "4.1.14-debian-12-r0-dbp4"
pullPolicy: Always
# -- Debug mode for more detailed moodle installation and log output
debug: false
Expand Down
6 changes: 4 additions & 2 deletions moodle/scripts/init/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ printSystemStatus
# Can handle new version and existing version.
startBitnamiSetup

MODULE=dbp info "Create php.ini with redis config"
/bin/cp /moodleconfig/php-ini/php.ini /opt/bitnami/php/etc/conf.d/php.ini

if [[ ! -f "$update_failed_path" ]]; then
MODULE=dbp info "Starting Moodle Update Check"
if /scripts/updateCheck.sh; then
Expand All @@ -102,9 +105,8 @@ MODULE=dbp info "Start Bitnami setup script after checking for proper version"
/post-init.sh
upgrade_if_pending

MODULE=dbp info "Replacing config files with ours"
MODULE=dbp info "Replacing config.php file with ours"
/bin/cp -p /moodleconfig/config-php/config.php /bitnami/moodle/config.php
/bin/cp /moodleconfig/php-ini/php.ini /opt/bitnami/php/etc/conf.d/php.ini

if [ -f "/tmp/de.zip" ] && [ ! -d /bitnami/moodledata/lang/de ]; then \
MODULE=dbp info "Installing german language pack"
Expand Down