Skip to content

Commit

Permalink
Introduced prestop-script for mariadb-galera
Browse files Browse the repository at this point in the history
Improved helm upgrade script for mariadb
Added Cron PHP config to configMap
  • Loading branch information
warrenchristian1telus committed Dec 11, 2024
1 parent 3216b62 commit 4eeac14
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
30 changes: 26 additions & 4 deletions openshift/scripts/deploy-mariadb-galera.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,28 @@ if helm list -q | grep -q "^$DB_DEPLOYMENT_NAME$"; then
echo "Timeout waiting for $DB_DEPLOYMENT_NAME to scale to 0"
exit 1
fi

# helm uninstall $DB_DEPLOYMENT_NAME
echo "Upgrading $DB_DEPLOYMENT_NAME..."

helm upgrade $DB_DEPLOYMENT_NAME \
oci://registry-1.docker.io/bitnamicharts/mariadb-galera \
--set rootUser.password=$DB_PASSWORD \
--set galera.mariabackup.password=$DB_PASSWORD
# Capture the output of the helm upgrade command into a variable
helm_upgrade_response=$(helm upgrade $DB_DEPLOYMENT_NAME oci://registry-1.docker.io/bitnamicharts/mariadb-galera --reuse-values -f ./config/mariadb/galera-values.yaml 2>&1)

# Output the response for debugging purposes
echo "$helm_upgrade_response"

# Check if the helm upgrade command failed
if [[ $? -ne 0 ]]; then
echo "Helm upgrade failed with the following output:"
echo "$helm_upgrade_response"
exit 1
fi

# helm upgrade $DB_DEPLOYMENT_NAME \
# oci://registry-1.docker.io/bitnamicharts/mariadb-galera \
# --set rootUser.password=$DB_PASSWORD \
# --set galera.mariabackup.password=$DB_PASSWORD
# -f ./config/mariadb/galera-values.yaml
# --set db.password=$DB_PASSWORD \
# --set db.user=$DB_USER \
# --set db.name=$DB_NAME \
Expand Down Expand Up @@ -58,3 +73,10 @@ else
--wait \
-f ./config/mariadb/galera-values.yaml
fi

# Handle graceful shutdown, and introduce some testing parameters
oc create configmap $DB_DEPLOYMENT_NAME-prestop-script --from-file=./openshift/scripts/mariadb-prestop.sh

# Add the ConfigMap as a volume and mount it to each container.
# Also, add the preStop hook to use the script
oc patch statefulset $DB_DEPLOYMENT_NAME --type=json -p '[{"op": "add", "path": "/spec/template/spec/volumes", "value": [{"name": "prestop-script", "configMap": {"name": "$DB_DEPLOYMENT_NAME-prestop-script"}}]}, {"op": "add", "path": "/spec/template/spec/containers/0/volumeMounts", "value": [{"name": "prestop-script", "mountPath": "/usr/local/bin/prestop.sh", "subPath": "mariadb-prestop.sh"}]}, {"op": "add", "path": "/spec/template/spec/containers/0/lifecycle", "value": {"preStop": {"exec": {"command": ["/bin/sh", "-c", "/usr/local/bin/prestop.sh"]}}}}]'
41 changes: 41 additions & 0 deletions openshift/scripts/mariadb-prestop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash

# Set the waiting period before the pod is terminated
# For failover testing purposes (can/should set to 0 in prod)
WAIT_PERIOD=600 # in seconds

# Function to gracefully stop MariaDB service
graceful_shutdown() {
echo "Initiating graceful shutdown of MariaDB service..."

# Check if the current pod is the primary (master) pod
if mysql -u root -p"$MARIADB_ROOT_PASSWORD" -e "SHOW STATUS LIKE 'wsrep_local_state_comment';" | grep -q "Synced"; then
echo "This pod is the primary (master) pod. Proceeding with shutdown..."

# Set wsrep_on to OFF to stop replication
mysql -u root -p"$MARIADB_ROOT_PASSWORD" -e "SET GLOBAL wsrep_on=OFF;"

# Wait for a few seconds to allow replication to stop
sleep 5

# Shutdown MariaDB service
mysqladmin -u root -p"$MARIADB_ROOT_PASSWORD" shutdown
else
echo "This pod is not the primary (master) pod. Proceeding with shutdown..."

# Shutdown MariaDB service
mysqladmin -u root -p"$MARIADB_ROOT_PASSWORD" shutdown
fi

echo "MariaDB service has been gracefully stopped."
}

# Run the graceful shutdown function
graceful_shutdown

# Introduce a waiting period before exiting
WAIT_MINUTES=$((WAIT_PERIOD / 60))
echo "Waiting for $WAIT_PERIOD seconds ($WAIT_MINUTES minutes) before exiting..."
sleep $WAIT_PERIOD

echo "PreStop hook script completed. Exiting..."
12 changes: 12 additions & 0 deletions openshift/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,11 @@
"name": "${CRON_NAME}-config",
"mountPath": "/var/www/html/config.php",
"subPath": "config.php"
},
{
"name": "${CRON_NAME}-php-config",
"mountPath": "/usr/local/etc/php/conf.d/moodle-php.ini",
"subPath": "moodle-php.ini"
}
],
"resources": {
Expand Down Expand Up @@ -544,6 +549,13 @@
"defaultMode": 420
}
},
{
"name": "${CRON_NAME}-php-config",
"configMap": {
"name": "${CRON_NAME}-php-config",
"defaultMode": 420
}
},
{
"name": "${APP_NAME}-env",
"configMap": {
Expand Down

0 comments on commit 4eeac14

Please sign in to comment.