# !/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 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."