Skip to content

Commit

Permalink
scripts: improve mass-update.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
Noki committed Dec 24, 2024
1 parent 1729799 commit 4c492be
Showing 1 changed file with 42 additions and 19 deletions.
61 changes: 42 additions & 19 deletions mass-update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,52 @@

# Define file directory and endings
WORK_DIR="tmp"
FILE_ENDINGS=".itb .bin"
FILE_ENDINGS=".itb .bin .gz"

# Extract hosts from YAML files
echo "Extracting host information from YAML files..."
mapfile -t COREROUTERS < <(yq '.hosts[] | select(.role == "corerouter" or .role == "gateway") | .hostname' locations/*.yml | tr -d '"')
mapfile -t APS < <(yq '.hosts[] | select(.role == "ap") | .hostname' locations/*.yml | tr -d '"')

# Find files matching the specified endings
FILES=""
FILES=()
for ENDING in $FILE_ENDINGS; do
FILES="$FILES $(find "$WORK_DIR/images" -type f -name "*$ENDING")"
while IFS= read -r FILE_PATH; do
FILES+=("$FILE_PATH")
done < <(find "$WORK_DIR/images" -type f -name "*$ENDING")
done

# Sort files based on whether filename contains "core" or not
CORE_FILES=""
OTHER_FILES=""
for FILE_PATH in $FILES; do
if [[ "$FILE_PATH" == *"core"* ]]; then
CORE_FILES="$CORE_FILES $FILE_PATH"
else
OTHER_FILES="$OTHER_FILES $FILE_PATH"
# Separate files for APs and corerouters
AP_FILES=()
COREROUTER_FILES=()
for FILE_PATH in "${FILES[@]}"; do
FILENAME=$(basename "$FILE_PATH")
NODENAME="${FILENAME%.*}"

# Check if the node belongs to APS or COREROUTERS
if [[ " ${APS[*]} " == *" $NODENAME "* ]]; then
AP_FILES+=("$FILE_PATH")
elif [[ " ${COREROUTERS[*]} " == *" $NODENAME "* ]]; then
COREROUTER_FILES+=("$FILE_PATH")
fi
done
SORTED_FILES="$OTHER_FILES $CORE_FILES"

# Combine APs first, then corerouters
SORTED_FILES=("${AP_FILES[@]}" "${COREROUTER_FILES[@]}")

# Print information and prompt for confirmation
echo ""
echo "This script will do the following:"
echo ""
echo "- flash all the following hosts with the corresponding firmware files currently present in $WORK_DIR/images"
echo "- first flash APs, than core routers based on the naming convention"
echo "- first flash APs, then corerouters an gateways based on the role derived from host within the YAML files"
echo "- check the availability of the hosts before and after flashing"
echo "- ignore keychecking"
echo "- make sure that at least 'image size + 1 MB' of RAM is available before starting a firmware upgrade"
echo "- delete the local firmware file, build log, build and config files from disk after flashing"
echo ""
echo "The following firmware files will be flashed:"
for FILE_PATH in $SORTED_FILES; do
for FILE_PATH in "${SORTED_FILES[@]}"; do
echo "- $(basename "$FILE_PATH")"
done
echo ""
Expand All @@ -45,8 +58,18 @@ if [[ ! "$choice" =~ ^[Yy]$ ]]; then
exit 0
fi

# Function to check reachability
check_reachability() {
local hostname="$1"
if ping -4 -c 1 "$hostname" >/dev/null 2>&1 || ping -6 -c 1 "$hostname" >/dev/null 2>&1; then
return 0
else
return 1
fi
}

# Loop through each file
for FILE_PATH in $SORTED_FILES; do
for FILE_PATH in "${SORTED_FILES[@]}"; do
# Horizontal line to separate iterations
echo "----------------------------------------"

Expand All @@ -64,7 +87,7 @@ for FILE_PATH in $SORTED_FILES; do

# Check if hostname is reachable
echo "Checking if $HOSTNAME is reachable..."
if ping -c 1 "$HOSTNAME" >/dev/null 2>&1; then
if check_reachability "$HOSTNAME"; then
echo "Hostname $HOSTNAME is reachable"

# Check memory on remote host
Expand All @@ -82,12 +105,12 @@ for FILE_PATH in $SORTED_FILES; do

# Wait for hostname to become unreachable
echo "Waiting for $HOSTNAME to become unreachable..."
while ping -c 1 "$HOSTNAME" >/dev/null 2>&1; do sleep 1; done
while check_reachability "$HOSTNAME"; do sleep 1; done

# Wait 20 seconds and than wait for hostname to become reachable again
# Wait 20 seconds and then wait for hostname to become reachable again
echo "Waiting for $HOSTNAME to become reachable again..."
sleep 20
while ! ping -c 1 "$HOSTNAME" >/dev/null 2>&1; do sleep 1; done
while ! check_reachability "$HOSTNAME"; do sleep 1; done

# Remove local files
echo "Removing local files for $NODENAME from $WORK_DIR"
Expand Down

0 comments on commit 4c492be

Please sign in to comment.