Skip to content

Commit

Permalink
Removes all sudo, pulls dependencies into plan file
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Heath <[email protected]>
  • Loading branch information
jasonheath committed Dec 11, 2024
1 parent 943d9e5 commit 7bfcd7c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
#!/bin/bash

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

function _hab_exec() {
pkg=$1
shift
Expand All @@ -24,48 +18,6 @@ function _jq() {
_hab_exec 'core/jq-static' jq "$@"
}

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

function _are_dependencies_installed() {
echo "CHECKING dependencies"
declare -a deps
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_install "$d"; then
return 5
fi
fi
done
}

function _minio_check {
echo "CHECKING MinIO"
local output
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
echo "ERROR: one or more preflight checks FAILED"
exit 7
fi
}

function download_bucket_objects() {
echo "DOWNLOADING objects from the MinIO that we are migrating from"
if ! _aws s3 sync "$s3_url" "$WAYPOINT"; then
Expand Down Expand Up @@ -118,7 +70,7 @@ function minio_health_live_check() {
function minio_stop() {
for ((n = 0; n < 20; n++)); do
if pgrep minio >/dev/null; then
sudo pkill minio >/dev/null
pkill minio >/dev/null
sleep 1
else
return 0
Expand Down
27 changes: 16 additions & 11 deletions components/builder-minio/habitat/hooks/install
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ 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

echo "Checking if MinIO migration is needed"
if is_migration_from_removed_fs_backend_needed; then
Expand All @@ -30,12 +29,15 @@ if is_migration_from_removed_fs_backend_needed; then
# 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
echo "ERROR: $minio_pkg_old is not installed and we require it for the migration."
exit 11
fi

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"
hab pkg exec $minio_pkg_old minio -- server \
--config-dir "{{pkg.svc_config_path}}" \
/hab/svc/builder-minio/data |
tee "$old_minio_stdout" &

if ! minio_health_live_check; then
echo "MinIO is not running so MinIO migration cannot begin"
Expand All @@ -50,16 +52,21 @@ if is_migration_from_removed_fs_backend_needed; then

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/
MIGRATION_BACKUP_DIRECTORY=$(mktemp -d -t minio-data-backup-"$timestamp"-XXXXXXXXXX)
cp -r /hab/svc/builder-minio/data "$MIGRATION_BACKUP_DIRECTORY"
GLOBIGNORE=".:.."
for x in /hab/svc/builder-minio/data/*; do
rm -rf "$x"
done

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")
# shellcheck disable=SC2024
sudo --background hab pkg exec core/minio minio -- server /hab/svc/builder-minio/data >"$new_minio_stdout"
hab pkg exec core/minio minio -- server \
/hab/svc/builder-minio/data \
--config-dir "{{pkg.svc_config_path}}" |
tee "$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"
Expand Down Expand Up @@ -89,7 +96,5 @@ if is_migration_from_removed_fs_backend_needed; then

minio_stop

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

echo "END MinIO migration"
fi
7 changes: 4 additions & 3 deletions components/builder-minio/habitat/plan.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# shellcheck shell=bash
# shellcheck shell=bash disable=SC2034

pkg_name=builder-minio
pkg_origin=habitat
pkg_maintainer="The Habitat Maintainers <[email protected]>"
pkg_license=('Apache-2.0')
pkg_deps=(core/minio core/cacerts core/openssl core/aws-cli core/bash)
pkg_deps=(core/aws-cli core/bash core/cacerts core/curl core/jq-static core/minio core/openssl)
pkg_build_deps=(core/git)

pkg_exports=(
Expand All @@ -17,7 +17,8 @@ pkg_exports=(
pkg_version() {
# TED: After migrating the builder repo we needed to add to
# the rev-count to keep version sorting working
echo "$(($(git rev-list HEAD --count) + 5000))"
# echo "$(($(git rev-list HEAD --count) + 5000))"
echo "$(($(git rev-list HEAD --count) + 5001))"
}

do_before() {
Expand Down

0 comments on commit 7bfcd7c

Please sign in to comment.