update/software/scripts/software-deploy-start
Heitor Matsui f88638a374 Include missing data migration steps on deploy start
There are additional steps needed to enable "deploy host"
operation for major release upgrades. This commit adds these
additional steps, which are:

1. Create TO release platform config directory, done during
   upgrade-start on legacy upgrade procedure
2. Create TO release rabbitmq directory, done by upgrade
   manifest after controller-1 reinstall on legacy upgrade

The commit also fixes some issues:

1. shell-utils logging functions logging nowhere after
   being sourced by other scripts
2. sync-controllers-feed script was only syncing the
   ostree_repo directory instead of the full feed content
3. major release deployment scripts ran from different
   places, now all scripts are executed from the TO release
   feed directory, or from checked out TO release ostree
   repo in case of chroot
4. change umount command from chroot_mounts to lazy umount

Note: Since the "deploy host" endpoint for major release
deployment is not yet implemented, the test plan will have
test cases that simulate the "deploy host" operation.

Test Plan
PASS: simulate "deploy host" successfully for AIO-SX
PASS: simulate "deploy host" successfully for AIO-DX

Depends-on: https://review.opendev.org/c/starlingx/config/+/913715

Story: 2010676
Task: 49703

Change-Id: Ib6ae49b3590a1e50acb305ac7482e28bcc4de403
Signed-off-by: Heitor Matsui <heitorvieira.matsui@windriver.com>
2024-03-22 15:12:14 -03:00

163 lines
5.2 KiB
Plaintext
Executable File

# !/bin/bash
#
# Copyright (c) 2023-2024 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
# This script is to start a major release deployment. It does the following:
# 1. collect data from running system for migration,
# 2. create bind mounts to deployment directory
# 3. copy necessary data to deployment directory
# 4. in chroot, start 2nd instance of PostgreSQL database service
# 5. perform data migration
#
# Used by shell-utils as the log file path
LOG_FILE="/var/log/software.log"
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
usage()
{
echo "usage: $0 from_ver to_ver k8s_ver postgresql_port feed [commit_id|latest_commit]"
}
if [ $# -lt 5 ]; then
usage
exit 1
fi
from_ver="$1"
to_ver="$2"
k8s_ver="$3"
port="$4"
feed="$5"
feed_url="file://${feed}"
if [ $# -eq 6 ]; then
commit_id="$6"
else
commit_id=''
fi
k8s_ver=$(echo ${k8s_ver} | sed 's/^v//')
staging_dir="/sysroot/upgrade"
rootdir=${staging_dir}"/sysroot"
repo=${staging_dir}"/ostree_repo"
instbr="starlingx"
report_agent="deploy-start"
deploy_cleanup() {
sudo ${script_dir}/deploy-cleanup ${repo} ${rootdir} all
}
deploy_update_state() {
local state="$1"
# update deploy state to start-done
/usr/bin/software-deploy-update -s ${state} ${report_agent}
}
handle_error() {
local exit_code="$1"
local error_message="$2"
local state="start-failed"
error "${error_message}"
error "Please check the error details and take appropriate action for recovery."
error "Update deploy state ${state}."
deploy_update_state ${state}
# cleanup before exiting
deploy_cleanup
exit ${exit_code}
}
for dir in $rootdir $repo; do
if [ -e ${dir} ]; then
error "${dir} already exists. Please ensure to clean up environment to continue."
exit 1
fi
done
sudo mkdir ${repo} -p
info "Initializing ostree repo..."
sudo ostree --repo=${repo} init --mode=archive || handle_error $? "Failed to init repo"
sudo ostree --repo=${repo} remote add ${instbr} ${feed_url} --no-gpg-verify || handle_error $? "Failed to remote add repo"
sudo ostree --repo=${repo} pull --depth=-1 --mirror ${instbr}:${instbr} || handle_error $? "Failed to pull repo"
# TODO(bqian) make commit_id mandatory once the commit-id is built to metadata.xml for major releases
if [ -z ${commit_id} ]; then
# get commit id, only latest for now
commit_id=$(ostree rev-parse --repo=${repo} ${instbr})
info "Latest commit id ${commit_id}"
if [ -z "${commit_id}" ]; then
handle_error 1 "Failed to retrieve commit id"
fi
fi
info "Checkout commit id ${commit_id}"
info "Checking out ostree repo..."
sudo ostree --repo=${repo} checkout ${commit_id} ${rootdir} || handle_error $? "Failed to checkout ${commit_id}"
# create proper mounts on deploy file system
info "Creating mount points..."
sudo ${script_dir}/chroot_mounts.sh ${rootdir} || handle_error $? "Failed to mount required mount points"
sudo mount --bind ${rootdir}/usr/local/kubernetes/${k8s_ver} ${rootdir}/usr/local/kubernetes/current
sudo cp /etc/kubernetes/admin.conf ${rootdir}/etc/kubernetes/
info "Mount points created successfully"
info "Preparing for data migration..."
DATA_PREP_SCRIPT="${script_dir}/prep-data-migration"
# OS_AUTH_URL, OS_USERNAME, OS_PASSWORD, OS_PROJECT_NAME, OS_USER_DOMAIN_NAME,
# OS_PROJECT_DOMAIN_NAME, OS_REGION_NAME are in env variables.
cmd_line=" --rootdir=${rootdir} --from_release=${from_ver} --to_release=${to_ver}"
cmd_line+=" --auth_url=${OS_AUTH_URL} --username=${OS_USERNAME} --password=${OS_PASSWORD}"
cmd_line+=" --project_name=${OS_PROJECT_NAME} --user_domain_name=${OS_USER_DOMAIN_NAME}"
cmd_line+=" --project_domain_name=${OS_PROJECT_DOMAIN_NAME} --region_name=${OS_REGION_NAME}"
prep_cmd="${DATA_PREP_SCRIPT} ${cmd_line}"
${prep_cmd} || handle_error $? "Failed to extract data for migration"
info "Data migration preparations complete."
info "Creating temporary database..."
sudo chroot ${rootdir} /usr/sbin/software-deploy/create_postgresql_database.sh ${port} || handle_error $? "Failed to start 2nd instance of postgresql"
info "Database creation complete."
info "Starting data migration..."
sudo chroot ${rootdir} /usr/bin/software-migrate ${from_ver} ${to_ver} ${port} || handle_error $? "Failed to migrate data"
info "Data migration completed."
info "Syncing feed between controllers..."
SYNC_CONTROLLERS_SCRIPT="${script_dir}/sync-controllers-feed"
sync_controllers_cmd="${SYNC_CONTROLLERS_SCRIPT} ${cmd_line} --feed=$(dirname $feed)"
${sync_controllers_cmd} || handle_error $? "Failed to sync feeds"
info "Feed sync complete."
# TODO(heitormatsui) remove once sysinv upgrade tables are deprecated
info "Creating ${to_ver} load entry in legacy upgrade table..."
sudo -u postgres psql -d sysinv -c "insert into loads(id, uuid, state, software_version) values (nextval('loads_id_seq'), 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', 'available', '${to_ver}');"
info "Load created successfully."
state="start-done"
deploy_update_state $state
info "Update deploy state ${state}."
# cleanup after successful data migration
info "Starting cleanup..."
deploy_cleanup
info "Cleanup complete."