
If during the software deploy delete the chroot_mounts file do not exist in the rootdir the execution will fail. This commit add a check to get the chroot_mounts from feed in case it do not exist in rootdir. The absense of the file may occur in deploy start during a reboot while ostree checkout is running. Test Plan: PASS: deploy start -> deploy delete -> deploy start PASS: deploy start -> reboot during ostree checkout -> software-deploy-set-failed -> deploy delete -> deploy start Story: 2010676 Task: 51304 Change-Id: I51c0128d8f73768bf2b7ffb79181ea7ceeef7737 Signed-off-by: Luis Eduardo Bonatti <luizeduardo.bonatti@windriver.com>
163 lines
4.7 KiB
Bash
163 lines
4.7 KiB
Bash
#!/bin/bash
|
|
#
|
|
# Copyright (c) 2024 Wind River Systems, Inc.
|
|
#
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
#
|
|
# This script removes artifacts created by the deploy start script:
|
|
# 1. Stop temporary database created for data migrations
|
|
# 2. Unmount the bind mounts used by the data migration process
|
|
# 3. Remove the staging deployment directory created by checking out TO release ostree branch
|
|
#
|
|
# It can be used either by another script (e.g. software-deploy-start) to automatically
|
|
# cleanup the environment after both success/failure paths of the data migration process,
|
|
# or can be used by a system administrator to manually cleanup the environment if the
|
|
# automatic cleanup process fails.
|
|
#
|
|
|
|
script_dir=$(dirname $0)
|
|
shell_utils=${script_dir}/shell-utils
|
|
if [ -f $shell_utils ]; then
|
|
source $shell_utils
|
|
else
|
|
echo "ERROR: ${shell_utils} module not found."
|
|
exit 1
|
|
fi
|
|
|
|
stop_database() {
|
|
local rootdir=$1
|
|
local to_ver=$2
|
|
local rc=0
|
|
|
|
# attempt to stop temporary database if still up
|
|
tmp_db_dir=${rootdir}/var/lib/postgresql/${to_ver}
|
|
info "Attempting to stop the temporary database in ${tmp_db_dir}..."
|
|
if [ -d $tmp_db_dir ]; then
|
|
lsof $tmp_db_dir
|
|
if [ $? -eq 0 ]; then
|
|
tmp_db_bin_dir=$(${rootdir}/usr/bin/pg_config --bindir)
|
|
sudo -u postgres ${tmp_db_bin_dir}/pg_ctl -D ${tmp_db_dir} stop
|
|
if [ $? -ne 0 ]; then
|
|
rc=1
|
|
error "Error stopping database."
|
|
else
|
|
info "Success stopping database."
|
|
fi
|
|
else
|
|
info "Database is not running."
|
|
fi
|
|
else
|
|
warning "No database found in the specified directory."
|
|
fi
|
|
return $rc
|
|
}
|
|
|
|
unmount_filesystems() {
|
|
local rootdir=$1
|
|
local rc=0
|
|
local filepath="${rootdir}/usr/sbin/software-deploy/chroot_mounts.sh"
|
|
|
|
if [ ! -f $filepath ]; then
|
|
filepath=/var/www/pages/feed/rel-${to_ver}/upgrades/software-deploy/chroot_mounts.sh
|
|
fi
|
|
|
|
info "Attempting to unmount filesystems under ${rootdir}..."
|
|
sudo $filepath ${rootdir} -u
|
|
if [ $? -ne 0 ]; then
|
|
rc=1
|
|
error "Error unmounting filesystems."
|
|
else
|
|
info "Success unmounting filesystems."
|
|
fi
|
|
return $rc
|
|
}
|
|
|
|
remove_temp_directories() {
|
|
local repo=$1
|
|
local rootdir=$2
|
|
local rc=0
|
|
local filepath="${rootdir}/usr/sbin/software-deploy/chroot_mounts.sh"
|
|
|
|
if [ ! -f $filepath ]; then
|
|
filepath=/var/www/pages/feed/rel-${to_ver}/upgrades/software-deploy/chroot_mounts.sh
|
|
fi
|
|
|
|
info "Attempting to remove temporary deployment directories [${repo}, ${rootdir}]..."
|
|
sudo $filepath ${rootdir} -c
|
|
if [ $? -ne 0 ]; then
|
|
rc=1
|
|
error "Some mount points are still mounted, cannot proceed with the cleanup."
|
|
else
|
|
rm -rf $repo $rootdir
|
|
info "Temporary deployment directories removed successfully."
|
|
fi
|
|
return $rc
|
|
}
|
|
|
|
# script usage
|
|
if [ $# -ne 3 ]; then
|
|
echo
|
|
echo "usage: deploy-cleanup <tmp_ostree_repo_dir> <tmp_root_dir> <action>"
|
|
echo -e "\nParameters"
|
|
echo "=========="
|
|
echo "tmp_ostree_repo_dir: temporary ostree repo directory (example: /sysroot/upgrade/ostree_repo)"
|
|
echo "tmp_root_dir: temporary ostree checkout root directory (example: /sysroot/upgrade/sysroot)"
|
|
echo "action: action executed by the script (db|umount|remove|all)"
|
|
echo "- db: stop temporary database created for target release"
|
|
echo "- umount: unmount the bind mounts"
|
|
echo "- remove: delete the target release temporary directory"
|
|
echo "- all: all of the above, in top-down order"
|
|
echo
|
|
exit 1
|
|
fi
|
|
|
|
repo=$1
|
|
rootdir=$2
|
|
action=$3
|
|
|
|
# basic checks
|
|
for dir in $repo $rootdir; do
|
|
if [ ! -d $dir ]; then
|
|
error "Specified directory $dir does not exist, cannot proceed with cleanup."
|
|
exit 1
|
|
fi
|
|
done
|
|
|
|
target_build_info=${rootdir}/usr/etc/build.info
|
|
if [ ! -f $target_build_info ]; then
|
|
error "Cannot get target release build-info information, cannot proceed with cleanup."
|
|
exit 1
|
|
fi
|
|
|
|
# set to_ver based on build-info inside rootdir
|
|
to_ver=$(cat $target_build_info | grep SW_VERSION | cut -d'"' -f2)
|
|
|
|
# main
|
|
info "Starting cleanup for staging directories [${repo}, ${rootdir}]..."
|
|
case $action in
|
|
"db")
|
|
stop_database $rootdir $to_ver
|
|
;;
|
|
"umount")
|
|
unmount_filesystems $rootdir
|
|
;;
|
|
"remove")
|
|
remove_temp_directories $repo $rootdir
|
|
;;
|
|
"all")
|
|
stop_database $rootdir $to_ver && \
|
|
unmount_filesystems $rootdir && \
|
|
remove_temp_directories $repo $rootdir
|
|
;;
|
|
*)
|
|
error "Invalid action specified: ${action}"
|
|
;;
|
|
esac
|
|
rc=$?
|
|
if [ $rc -ne 0 ]; then
|
|
error "Error cleaning up [${repo}, ${rootdir}], please check the logs, take manual actions and retry the script."
|
|
fi
|
|
info "Cleanup script ended."
|
|
|
|
exit $rc
|