Skip to content

Commit

Permalink
scripts: improved image generation
Browse files Browse the repository at this point in the history
- query locations and hosts with yq
- allow passing locations as comma separated list
- skip invalid locations
  • Loading branch information
Noki committed Dec 24, 2024
1 parent 89abe33 commit c9a0a42
Showing 1 changed file with 73 additions and 34 deletions.
107 changes: 73 additions & 34 deletions generate-images.sh
Original file line number Diff line number Diff line change
@@ -1,32 +1,68 @@
#!/bin/bash
# prepare options list by searching through group_vars/ and locations/

# Prepare options list by searching through locations/
unset locations i
for location in $({
while IFS= read -r -d $'\0' file; do
prefix="./locations/"
l=${file/#$prefix}
echo "${l%.yml}"
done < <(find ./locations/ -name "*.yml" -print0)
while IFS= read -r -d $'\0' directory; do
prefix="./group_vars/location_"
echo "${directory/#$prefix}"
done < <(find ./group_vars/ -maxdepth 1 -type d -name "location_*" -print0)
} | sort); do
locations[i++]="$location"
done

# show menu

# Using yq to extract the 'location' field from all .yml files in the locations/ directory
mapfile -t locations < <(yq '.location' locations/*.yml | sed 's/\"//g' | sort)

# Function to extract hostnames from a location file
get_hosts_for_location() {
local location=$1
yq '.hosts[].hostname' "locations/${location}.yml" | sed 's/\"//g' | tr '\n' ',' | sed 's/,$//'
}

# Function to run Ansible playbook with a list of hostnames
generate_images_for_hosts() {
local host_list=$1
echo "Generating images for hosts: $host_list"
ansible-playbook play.yml --limit "$host_list" --tags image && echo "Location of generated images: ./tmp/images"
}

# Check if an argument is passed
if [[ $# -gt 0 ]]; then
# Parse the argument as a comma-separated list of locations
IFS="," read -ra input_locations <<< "$1"

# Validate the input locations
valid_hosts=()
for loc in "${input_locations[@]}"; do
if [[ " ${locations[*]} " == *" $loc "* ]]; then
hosts=$(get_hosts_for_location "$loc")
if [[ -n $hosts ]]; then
valid_hosts+=("$hosts")
fi
else
echo "Warning: Invalid location '$loc' ignored."
fi
done

if [[ ${#valid_hosts[@]} -gt 0 ]]; then
# Combine valid hosts into a single list and run the playbook
combined_hosts=$(IFS=,; echo "${valid_hosts[*]}")
generate_images_for_hosts "$combined_hosts"
else
echo "No valid hosts provided. Exiting."
fi
exit
fi

# Show menu
echo "Usage:
This helper script allows you to perform bbb-config related tasks via an easy menu.
Either select a location by typing the corresponding number or one of the following
actions by just typing them.
This helper script allows you to build multiple locations by passing them as a
parameter e.g.
./generate-images.sh sama,rhnk,segen
or perform bbb-config related tasks via an easy menu. Either select a location by
typing the corresponding number or one of the following actions by just typing them.
Actions:
abort
return to the command line
all
generage images for all nodes and locations and return to the command line
generate images for all nodes and locations and return to the command line
clean
delete all temporary files generated by bbb-configs and wait for additional
commands
Expand All @@ -39,58 +75,61 @@ Actions:

echo "The following locations were found:
" >&2
PS3="
Use a location number to generate images for that location or type an action: "
PS3=$'\nUse a location number to generate images for that location or type an action: '

# allow the user to choose a location
# Allow the user to choose a location
unset location
select location in "${locations[@]}"
do
# abort if selected
# Abort if selected
if [[ "$REPLY" == abort ]]; then break; fi

# generate all images if selected
# Generate all images if selected
if [[ "$REPLY" == all ]]
then
ansible-playbook play.yml --tags image && echo "location of generated images: ./tmp/images"
break
fi

# delete old directories and get rid of artifacts
# Delete old directories and get rid of artifacts
if [[ "$REPLY" == clean ]]
then
[ -d "./tmp/" ] && rm -r ./tmp/
echo "tmp directory was deleted..."
continue
fi

# check all configurations files with ansible-lint
# Check all configuration files with ansible-lint
if [[ "$REPLY" == lint ]]
then
yamllint -d .github/linters/.yaml-lint.yml .
ansible-lint -c .github/linters/.ansible-lint.yml
break
fi

# install or update requirements
# Install or update requirements
if [[ "$REPLY" == requirements ]]
then
pip3 install -r requirements.txt
continue
fi

# complain if no location was selected, and loop to ask again
# Complain if no location was selected, and loop to ask again
if [[ "$location" == "" ]]
then
echo "'$REPLY' is not a valid selection"
continue
fi

# generate images
echo "firmwares for the following location will be generated: $location"
echo 'executing: ansible-playbook play.yml --limit "'"$location"'-*" --tags image'
ansible-playbook play.yml --limit "$location-*" --tags image && echo "location of generated images: ./tmp/images"
# Generate images for a specific location
echo "Firmwares for the following location will be generated: $location"
hosts=$(get_hosts_for_location "$location")
if [[ -n $hosts ]]; then
generate_images_for_hosts "$hosts"
else
echo "No hosts found for location $location. Exiting."
fi

# break the loop
# Break the loop
break
done

0 comments on commit c9a0a42

Please sign in to comment.