Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added minio migration script #277

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
22 changes: 18 additions & 4 deletions scripts/minio-update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ function _minio_check {
echo "MinIO appears to be up and running"
else
echo "MinIO doesn't seem to be accessible at $MINIO_ENDPOINT"
exit
exit 4
fi
local output=$(aws ${opts[*]} s3 ls)
if [[ $output =~ $MINIO_BUCKET ]]; then
echo "MinIO credentials valid"
else
echo "MinIO credentials invalid"
exit
exit 4
fi

}
Expand Down Expand Up @@ -212,6 +212,19 @@ function upload_bucket_objects () {
aws ${opts[*]} s3 sync $waypoint $s3_url
}

function minio_migration_rollback () {
latest_backup=$(ls -d /hab/svc/builder-minio/data-bkp-* | sort -r | head -n 1)

if [ -n "$latest_backup" ]; then
echo "Copying data from latest backup: $latest_backup"
sudo cp -a "$latest_backup/." /hab/svc/builder-minio/data/
else
echo "No backup folder found. Exiting."
exit 1
fi
sudo hab svc load "${BLDR_ORIGIN}/builder-minio" --channel "stable" --force
}

function preflight_checks () {
_prerequisites_check
_minio_check
Expand All @@ -220,7 +233,7 @@ function preflight_checks () {
echo ""
}

function downloand_bucket_objects () {
function download_bucket_objects () {
aws ${opts[*]} s3 sync $s3_url $waypoint
}

Expand Down Expand Up @@ -264,10 +277,11 @@ case "${1}" in
usage ) usage ;;
print_env ) print_env ;;
preflight_checks ) preflight_checks ;;
download ) downloand_bucket_objects ;;
download ) download_bucket_objects ;;
upgrade ) upgrade_minio ;;
downgrade ) downgrade_minio ;;
upload ) upload_bucket_objects ;;
minio_rollback ) minio_migration_rollback ;;
* ) usage ;;
esac

80 changes: 79 additions & 1 deletion scripts/provision.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ umask 0022

# Defaults
BLDR_ORIGIN=${BLDR_ORIGIN:="habitat"}
BREAKING_MINIO_VERSION="2023-11-01T01-57-10Z"

sudo () {
[[ $EUID = 0 ]] || set -- command sudo -E "$@"
Expand Down Expand Up @@ -200,7 +201,70 @@ start_datastore() {
}

start_minio() {
sudo hab svc load "${BLDR_ORIGIN}/builder-minio" --channel "${BLDR_MINIO_CHANNEL:=$BLDR_CHANNEL}" --force
set +e
is_minio_migration_needed
migration_needed=$?
set -e
sougata-progress marked this conversation as resolved.
Show resolved Hide resolved

if [ "$migration_needed" -eq 1 ]; then
echo MinIO migration required

if [ -d "/hab/svc/builder-minio/data/" ]; then
backup_minio_data
fi

sudo hab svc load "${BLDR_ORIGIN}/builder-minio" --channel "stable" --force
sleep 10
bash ./minio-update.sh preflight_checks
sougata-progress marked this conversation as resolved.
Show resolved Hide resolved
if [ $? != 0 ]; then
echo Minio not running.
exit 1
fi

sleep 10
echo starting minio download

bash ./minio-update.sh download
sudo hab svc unload "${BLDR_ORIGIN}/builder-minio"
sleep 10
sudo rm -rf /hab/svc/builder-minio/data/
sudo rm -rf /hab/pkgs/habitat/builder-minio/
sudo hab svc load "${BLDR_ORIGIN}/builder-minio" --channel $BLDR_CHANNEL --force
sleep 10
bash ./minio-update.sh upload
cleanup_migration
else
sudo hab svc load "${BLDR_ORIGIN}/builder-minio" --channel $BLDR_CHANNEL --force
fi

}

sougata-progress marked this conversation as resolved.
Show resolved Hide resolved
cleanup_migration() {
local leftover_file="minio-update-bldr-bucket-objects.txt"
local leftover_dir="minio-update-bldr-bucket-objects"

if [ -f "$leftover_file" ]; then
sudo rm "$leftover_file"
fi

if [ -d "$leftover_dir" ]; then
sudo rm -r "$leftover_dir"
fi
}

backup_minio_data() {
echo "Starting MinIO data backup"
current_timestamp=$(date +%s)

sudo mkdir -p /hab/svc/builder-minio/data-bkp-$current_timestamp/

if [ "$(ls -A /hab/svc/builder-minio/data/)" ]; then
sudo cp -rf /hab/svc/builder-minio/data/* /hab/svc/builder-minio/data-bkp-$current_timestamp/
echo "Old MinIO data has been backed up to /hab/svc/builder-minio/data-bkp-$current_timestamp/"
else
echo "No files to copy from /hab/svc/builder-minio/data/"
fi

}

start_memcached() {
Expand All @@ -222,6 +286,20 @@ generate_bldr_keys() {
hab file upload "builder-api.default" "$(date +%s)" "/hab/cache/keys/${KEY_NAME}.box.key"
}

is_minio_migration_needed() {
format_value=$(jq -r '.format' /hab/svc/builder-minio/data/.minio.sys/format.json)

response=$(curl -s "https://bldr.habitat.sh/v1/depot/channels/$BLDR_ORIGIN/$BLDR_CHANNEL/pkgs/builder-minio/latest")
minio_version_lookup=$(echo "$response" | jq -r '.deps[] | select(.origin == "core" and .name == "minio") | .version')

echo minio_version_lookup $minio_version_lookup
if [[ "${format_value}" == 'fs' && ! "${BREAKING_MINIO_VERSION}" > "${minio_version_lookup}" ]]; then
return 1
else
return 0
fi
sougata-progress marked this conversation as resolved.
Show resolved Hide resolved
}

upload_ssl_certificate() {
if [ "${APP_SSL_ENABLED}" = "true" ]; then
echo "SSL enabled - uploading certificate files"
Expand Down