Skip to content

Commit

Permalink
Addresses [email protected] complaint (currernt is v0.10.0) and shfmt…
Browse files Browse the repository at this point in the history
… was run

Signed-off-by: Jason Heath <[email protected]>
  • Loading branch information
jasonheath committed Dec 10, 2024
1 parent 910ed88 commit 943d9e5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 79 deletions.
83 changes: 38 additions & 45 deletions components/builder-minio/habitat/config_install/minio-migration.sh
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
#!/bin/bash

# JAH: Consider this function more closely and maybe eliminated it
function sudo () {
function sudo() {
[[ $EUID = 0 ]] || set -- command sudo -E "$@"
"$@"
}

function _hab_exec () {
pkg=$1; shift
function _hab_exec() {
pkg=$1
shift
hab pkg exec "$pkg" -- "$@"
}

function _aws () {
function _aws() {
_hab_exec 'core/aws-cli' aws --endpoint-url "{{cfg.env.MINIO_ENDPOINT}}" "$@"
}

function _curl () {
function _curl() {
_hab_exec 'core/curl' curl "$@"
}

function _jq () {
function _jq() {
_hab_exec 'core/jq-static' jq "$@"
}


function _hab_pkg_install () {
pkg=$1; shift
function _hab_pkg_install() {
pkg=$1
shift
echo "$pkg not installed, installing"
if ! sudo hab pkg install "$pkg" -- "$@"
then
if ! sudo hab pkg install "$pkg" -- "$@"; then
echo "ERROR: install of $pkg FAILED"
exit 4
fi
return 0
}

function _are_dependencies_installed () {
function _are_dependencies_installed() {
echo "CHECKING dependencies"
declare -a deps
deps=( 'core/aws-cli' 'core/curl' 'core/jq-static' )
deps=('core/aws-cli' 'core/curl' 'core/jq-static')
for d in "${deps[@]}"; do
if hab pkg env "$d" &> /dev/null; then
if hab pkg env "$d" &>/dev/null; then
if ! _hab_pkg_install "$d"; then
return 5
fi
Expand All @@ -51,23 +51,22 @@ function _are_dependencies_installed () {
function _minio_check {
echo "CHECKING MinIO"
local output
output=$( _aws s3 ls )
output=$(_aws s3 ls)
if [[ ! $output =~ $MINIO_BUCKET ]]; then
echo "ERROR: Invalid MinIO credentials"
return 6
fi
return 0
}

function preflight_checks () {
if ! _are_dependencies_installed
then
function preflight_checks() {
if ! _are_dependencies_installed; then
echo "ERROR: one or more preflight checks FAILED"
exit 7
fi
}

function download_bucket_objects () {
function download_bucket_objects() {
echo "DOWNLOADING objects from the MinIO that we are migrating from"
if ! _aws s3 sync "$s3_url" "$WAYPOINT"; then
echo "ERROR: download of objects FAILED"
Expand All @@ -76,13 +75,13 @@ function download_bucket_objects () {
return 0
}

function _ensure_bucket_exists () {
if ! _aws s3 ls "$MINIO_BUCKET" &> /dev/null; then
function _ensure_bucket_exists() {
if ! _aws s3 ls "$MINIO_BUCKET" &>/dev/null; then
_aws s3 mb "$s3_url"
fi
}

function upload_bucket_objects () {
function upload_bucket_objects() {
_ensure_bucket_exists
echo "UPLOADING objects to the MinIO that we are migrating to"
if ! _aws s3 sync "$WAYPOINT" "$s3_url"; then
Expand All @@ -94,46 +93,40 @@ function upload_bucket_objects () {

function is_migration_from_removed_fs_backend_needed() {
echo "CHECKING if migration from removed fs backend is needed"
if [[ -f /hab/svc/builder-minio/data/.minio.sys/format.json ]]
then
if [[ -f /hab/svc/builder-minio/data/.minio.sys/format.json ]]; then
format_value=$(_jq -r '.format' /hab/svc/builder-minio/data/.minio.sys/format.json)
if [[ "${format_value}" == 'fs' ]]
then
if [[ "${format_value}" == 'fs' ]]; then
return 0
fi
fi
return 1
}

function minio_health_live_check () {
for (( n=0; n<20; n++ ))
do
curl_http_code_args=( -fs -o /dev/null -w "%{http_code}" --retry 4 --retry-delay 1 )
function minio_health_live_check() {
for ((n = 0; n < 20; n++)); do
curl_http_code_args=(-fs -o /dev/null -w "%{http_code}" --retry 4 --retry-delay 1)
code=$(_curl "${curl_http_code_args[@]}" "$MINIO_ENDPOINT/minio/health/live")
if [[ $code == 200 ]]
then
if [[ $code == 200 ]]; then
return 0
else
else
sleep .5
fi
done
return 1
}

function minio_stop () {
for (( n=0; n<20; n++ ))
do
if pgrep minio > /dev/null
then
sudo pkill minio > /dev/null
function minio_stop() {
for ((n = 0; n < 20; n++)); do
if pgrep minio >/dev/null; then
sudo pkill minio >/dev/null
sleep 1
else
return 0
fi
done
}

function config_environment_for_migration () {
function config_environment_for_migration() {
if [[ ! $1 =~ ^[0-9]+$ ]]; then
echo "ERROR: Invalid timestamp"
fi
Expand All @@ -142,15 +135,15 @@ function config_environment_for_migration () {
export AWS_SECRET_ACCESS_KEY="{{cfg.env.MINIO_SECRET_KEY}}"
export s3_url="s3://{{cfg.env.MINIO_BUCKET}}"
export MINIO_BUCKET="{{cfg.env.MINIO_BUCKET}}"
WAYPOINT=$( mktemp -d -t minio-waypoint-"$1"-XXXXXXXXXX )
WAYPOINT=$(mktemp -d -t minio-waypoint-"$1"-XXXXXXXXXX)
export WAYPOINT
}

function _enumerate_bucket_objects () {
_aws s3 ls "$s3_url" --recursive --summarize > "$1"
function _enumerate_bucket_objects() {
_aws s3 ls "$s3_url" --recursive --summarize >"$1"
}

function summarize_old_minio_bucket_objects () {
function summarize_old_minio_bucket_objects() {
if [[ ! $1 =~ ^[0-9]+$ ]]; then
echo "Invalid timestamp"
fi
Expand All @@ -160,7 +153,7 @@ function summarize_old_minio_bucket_objects () {
echo "$tempfile"
}

function summarize_new_minio_bucket_objects () {
function summarize_new_minio_bucket_objects() {
if [[ ! $1 =~ ^[0-9]+$ ]]; then
echo "ERROR: Invalid timestamp"
fi
Expand Down
62 changes: 28 additions & 34 deletions components/builder-minio/habitat/hooks/install
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,17 @@ set -euo pipefail
source "{{pkg.svc_config_install_path}}/minio-migration.sh"

timestamp=$EPOCHSECONDS # provides a shared element in naming items below
config_environment_for_migration $timestamp
preflight_checks
config_environment_for_migration "$timestamp"
preflight_checks

echo "Checking if MinIO migration is needed"
if is_migration_from_removed_fs_backend_needed
then
if is_migration_from_removed_fs_backend_needed; then

echo "BEGIN MinIO migration"

if pgrep minio &> /dev/null
then
if pgrep minio &>/dev/null; then

if ! minio_health_live_check
then
if ! minio_health_live_check; then
echo "There seems to be a MinIO process but it fails the health check"
exit 9
fi
Expand All @@ -29,58 +26,55 @@ then

minio_pkg_old="core/minio/2018-10-05T01-03-03Z/20181006010122"
# This pkg_id is the minio dependency used in the on-prem-stable version of
# https://bldr.habitat.sh/#/pkgs/habitat/builder-minio/7764/20181006010221
# which was on-prem-stable for 6+ years so this should be a safe choice
# https://bldr.habitat.sh/#/pkgs/habitat/builder-minio/7764/20181006010221
# which was on-prem-stable for 6+ years so this should be a safe choice

if ! hab pkg list $minio_pkg_old > /dev/null; then
hab pkg install $minio_pkg_old;
if ! hab pkg list $minio_pkg_old >/dev/null; then
hab pkg install $minio_pkg_old
fi
old_minio_stdout=$( mktemp -t "minio-old-stdout-$timestamp-XXXXXXXXXX" )

old_minio_stdout=$(mktemp -t "minio-old-stdout-$timestamp-XXXXXXXXXX")
# shellcheck disable=SC2024
sudo --background hab pkg exec $minio_pkg_old minio -- server /hab/svc/builder-minio/data > "$old_minio_stdout"
sudo --background hab pkg exec $minio_pkg_old minio -- server /hab/svc/builder-minio/data >"$old_minio_stdout"

if ! minio_health_live_check
then
if ! minio_health_live_check; then
echo "MinIO is not running so MinIO migration cannot begin"
exit 10
fi

fi
minio_old_summary=$( summarize_old_minio_bucket_objects "$timestamp" )

minio_old_summary=$(summarize_old_minio_bucket_objects "$timestamp")

download_bucket_objects

minio_stop

export MIGRATION_BACKUP_DIRECTORY="/hab/svc/builder-minio/data-backup-$EPOCHSECONDS"
sudo mv /hab/svc/builder-minio/data/ "$MIGRATION_BACKUP_DIRECTORY"
sudo mkdir /hab/svc/builder-minio/data/

sudo hab pkg install core/minio --channel stable
export MINIO_ROOT_USER="{{cfg.env.MINIO_ACCESS_KEY}}"
export MINIO_ROOT_PASSWORD="{{cfg.env.MINIO_SECRET_KEY}}"
new_minio_stdout=$( mktemp -t "minio-new-stdout-$timestamp-XXXXXXXXXX" )
new_minio_stdout=$(mktemp -t "minio-new-stdout-$timestamp-XXXXXXXXXX")
# shellcheck disable=SC2024
sudo --background hab pkg exec core/minio minio -- server /hab/svc/builder-minio/data > "$new_minio_stdout"

if ! minio_health_live_check
then
sudo --background hab pkg exec core/minio minio -- server /hab/svc/builder-minio/data >"$new_minio_stdout"

if ! minio_health_live_check; then
echo "MinIO did not come back up so we cannot upload the habitat artifacts into the new MinIO"
echo "EXITING"
exit 1
fi

upload_bucket_objects

minio_new_summary=$( summarize_new_minio_bucket_objects "$timestamp" )

if diff <(tail -n 2 "$minio_old_summary") <(tail -n 2 "$minio_new_summary")
then
minio_new_summary=$(summarize_new_minio_bucket_objects "$timestamp")

if diff <(tail -n 2 "$minio_old_summary") <(tail -n 2 "$minio_new_summary"); then
echo "Migration was successful"
echo "A copy of the original minio backend has been left in $MIGRATION_BACKUP_DIRECTORY and should be removed manually"
rm -rf "$WAYPOINT"
rm -rf "$WAYPOINT"
echo "Old Minio bucket summary: $minio_old_summary"
echo "New Minio bucket summary: $minio_new_summary"
else
Expand All @@ -92,7 +86,7 @@ then
echo "The migration waypoint is $WAYPOINT"
echo "A copy of the original minio backend data is also in $MIGRATION_BACKUP_DIRECTORY"
fi

minio_stop

sudo chown --recursive hab:hab /hab/svc/builder-minio/data/
Expand Down

0 comments on commit 943d9e5

Please sign in to comment.