Skip to content

Commit

Permalink
Merge pull request #738 from City-of-Helsinki/UHF-8943
Browse files Browse the repository at this point in the history
UHF-8943: Deployment fixes
  • Loading branch information
tuutti authored Nov 29, 2023
2 parents bf096be + 452e807 commit 49829c3
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 58 deletions.
6 changes: 3 additions & 3 deletions docker/openshift/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ COPY / /var/www/html/
WORKDIR /var/www/html
RUN composer install --no-progress --profile --prefer-dist --no-interaction --no-dev --optimize-autoloader

# Copy ALL deploy scripts
COPY docker/openshift/init.sh /init.sh

RUN mkdir /entrypoints
COPY docker/openshift/entrypoints/ /entrypoints
RUN chmod +x /entrypoints/*


# Copy cron scripts
RUN mkdir /crons
COPY docker/openshift/crons/ /crons
RUN chmod +x /crons/*
Expand Down
23 changes: 11 additions & 12 deletions docker/openshift/crons/base.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
#!/bin/bash

# Checking if a new deployment is in progress, as we should not run cron while deploying.
if [ ! -n "$OPENSHIFT_BUILD_NAME" ]; then
echo "OPENSHIFT_BUILD_NAME is not defined. Exiting early."
exit 1
fi
source /init.sh

while [ "$(drush state:get deploy_id)" != "$OPENSHIFT_BUILD_NAME" ]
ATTEMPTS=0
# Checking if a new deployment is in progress, as we should not run cron while deploying.
while [ deployment_in_progress ]
do
echo "Current deploy_id $OPENSHIFT_BUILD_NAME not found in state. Probably a deployment is in progress - waiting for completion..."
sleep 60
done
let ATTEMPTS++

while [ "$(drush state:get system.maintenance_mode)" = "1" ]
do
echo "Maintenance mode on. Probably a deployment is in progress - waiting for completion..."
if (( ATTEMPTS > 10 )); then
echo "Failed to start a new cron pod - deployment probably failed."
exit 1
fi

echo "A deployment is in progress - waiting for completion ..."
sleep 60
done

Expand Down
60 changes: 17 additions & 43 deletions docker/openshift/entrypoints/20-deploy.sh
Original file line number Diff line number Diff line change
@@ -1,80 +1,54 @@
#!/bin/bash

cd /var/www/html/public
source /init.sh

function output_error_message {
echo ${1}
php ../docker/openshift/notify.php "${1}" || true
}

# Make sure we have active Drupal configuration.
if [ ! -f "../conf/cmi/system.site.yml" ]; then
output_error_message "Container start error: Codebase is not deployed properly. Exiting early."
function rollback_deployment {
output_error_message "Deployment failed: ${1}"
set_deploy_id ${2}
exit 1
fi

if [ ! -n "$OPENSHIFT_BUILD_NAME" ]; then
output_error_message "Container start error: OPENSHIFT_BUILD_NAME is not defined. Exiting early."
exit 1
fi

function get_deploy_id {
echo $(drush state:get deploy_id)
}

# Populate twig caches.
if [ ! -d "/tmp/twig" ]; then
drush twig:compile || true
fi

# Attempt to set deploy ID in case this is the first deploy.
if [[ -z "$(get_deploy_id)" ]]; then
drush state:set deploy_id $OPENSHIFT_BUILD_NAME
fi

# Exit early if deploy ID is still not set. This usually means either Redis or
# something else is down.
if [[ -z "$(get_deploy_id)" ]]; then
output_error_message "Container start error: Could not fetch deploy ID. Exiting early."
exit 1
fi
# Capture the current deploy ID so we can roll back to previous version in case
# deployment fails.
CURRENT_DEPLOY_ID=$(get_deploy_id)

# This script is run every time a container is spawned and certain environments might
# start more than one Drupal container. This is used to make sure we run deploy
# tasks only once per deploy.
if [ "$(get_deploy_id)" != "$OPENSHIFT_BUILD_NAME" ]; then
drush state:set deploy_id $OPENSHIFT_BUILD_NAME
if [ "$CURRENT_DEPLOY_ID" != "$OPENSHIFT_BUILD_NAME" ]; then
set_deploy_id $OPENSHIFT_BUILD_NAME

if [ $? -ne 0 ]; then
output_error_message "Deployment failed: Failed set deploy_id"
exit 1
rollback_deployment "Failed to set deploy_id" $CURRENT_DEPLOY_ID
fi
# Put site in maintenance mode
drush state:set system.maintenance_mode 1 --input-format=integer

if [ $? -ne 0 ]; then
output_error_message "Deployment failed: Failed to enable maintenance_mode"
exit 1
rollback_deployment "Failed to enable maintenance_mode" $CURRENT_DEPLOY_ID
fi
# Run helfi specific pre-deploy tasks. Allow this to fail in case
# the environment is not using the 'helfi_api_base' module.
# @see https://github.com/City-of-Helsinki/drupal-module-helfi-api-base
# Run pre-deploy tasks.
# @see https://github.com/City-of-Helsinki/drupal-module-helfi-api-base/blob/main/documentation/deploy-hooks.md
drush helfi:pre-deploy || true
# Run maintenance tasks (config import, database updates etc)
drush deploy

if [ $? -ne 0 ]; then
output_error_message "Deployment failed: drush deploy failed with {$?} exit code. See logs for more information."
rollback_deployment "drush deploy failed with {$?} exit code. See logs for more information." $CURRENT_DEPLOY_ID
exit 1
fi
# Run helfi specific post deploy tasks. Allow this to fail in case
# the environment is not using the 'helfi_api_base' module.
# @see https://github.com/City-of-Helsinki/drupal-module-helfi-api-base
# Run post-deploy tasks.
# @see https://github.com/City-of-Helsinki/drupal-module-helfi-api-base/blob/main/documentation/deploy-hooks.md
drush helfi:post-deploy || true
# Disable maintenance mode
drush state:set system.maintenance_mode 0 --input-format=integer

if [ $? -ne 0 ]; then
output_error_message "Deployment failure: Failed to disable maintenance_mode"
rollback_deployment "Failed to disable maintenance_mode" $CURRENT_DEPLOY_ID
fi
fi
48 changes: 48 additions & 0 deletions docker/openshift/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash

cd /var/www/html/public

function get_deploy_id {
if [ ! -f "sites/default/files/deploy.id" ];
touch sites/default/files/deploy.id
fi
echo $(cat sites/default/files/deploy.id)
}

function set_deploy_id {
echo ${1} > sites/default/files/deploy.id
}

function output_error_message {
echo ${1}
php ../docker/openshift/notify.php "${1}" || true
}

function deployment_in_progress {
if [ "$(get_deploy_id)" != "$OPENSHIFT_BUILD_NAME" ]; then
return 1
fi

if [ "$(drush state:get system.maintenance_mode)" = "1" ]; then
return 1
fi

return 0
}

if [ ! -d "sites/default/files" ]; then
output_error_message "Container start error: Public file folder does not exist. Exiting early."
exit 1
fi

# Make sure we have active Drupal configuration.
if [ ! -f "../conf/cmi/system.site.yml" ]; then
output_error_message "Container start error: Codebase is not deployed properly. Exiting early."
exit 1
fi

if [ ! -n "$OPENSHIFT_BUILD_NAME" ]; then
output_error_message "Container start error: OPENSHIFT_BUILD_NAME is not defined. Exiting early."
exit 1
fi

0 comments on commit 49829c3

Please sign in to comment.