From bb2ebd54b66b4cd94abb59d34fd9b55f41b2dbde Mon Sep 17 00:00:00 2001 From: Tobias Schwarz Date: Sat, 17 Aug 2024 14:01:19 +0000 Subject: [PATCH] workflows: check for missing and duplicate mac_overrides --- .../checks/check-mac-override-duplicates.sh | 12 +++ .github/checks/check-mac-override-missing.sh | 79 +++++++++++++++++++ .../check-mac-override-duplicates.yml | 17 ++++ .../workflows/check-mac-override-missing.yml | 17 ++++ 4 files changed, 125 insertions(+) create mode 100755 .github/checks/check-mac-override-duplicates.sh create mode 100755 .github/checks/check-mac-override-missing.sh create mode 100644 .github/workflows/check-mac-override-duplicates.yml create mode 100644 .github/workflows/check-mac-override-missing.yml diff --git a/.github/checks/check-mac-override-duplicates.sh b/.github/checks/check-mac-override-duplicates.sh new file mode 100755 index 000000000..a0024b06d --- /dev/null +++ b/.github/checks/check-mac-override-duplicates.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +# Check for address duplicates +duplicates=$(yq 'select(.hosts != null) | .hosts[].mac_override | select(. != null) | to_entries[] | .value' locations/*.yml | tr '[:upper:]' '[:lower:]' | sed 's/["'\'']//g' | sort | uniq -cd) + +if [ -n "$duplicates" ]; then + echo "Duplicate mac_overrides found:" + echo "$duplicates" + exit 1 +else + echo "No duplicate mac_overrides found." +fi diff --git a/.github/checks/check-mac-override-missing.sh b/.github/checks/check-mac-override-missing.sh new file mode 100755 index 000000000..5be14b018 --- /dev/null +++ b/.github/checks/check-mac-override-missing.sh @@ -0,0 +1,79 @@ +#!/bin/bash + +# Initialize a variable to track if any errors are found +error_found=0 + +# Get the list of changed .yml files in the locations directory compared to the main branch +changed_files='locations/*.yml' + +# Check if there are no changes in .yml files in the locations directory +if [ -z "$changed_files" ]; then + echo "No changes in .yml files within the locations directory." + exit 0 +fi + +# Loop through all changed location YAML files +for location_file in $changed_files; do + # Extract host information using yq + host_count=$(yq '.hosts | length' "$location_file") + + # Loop through each host entry + for ((i=0; i<$host_count; i++)); do + hostname=$(yq ".hosts[$i].hostname" "$location_file") + model=$(yq ".hosts[$i].model" "$location_file") + mac_override=$(yq ".hosts[$i].mac_override" "$location_file") + + # Convert model name to match the model file format + model_file=$(echo "$model" | sed 's/-/_/g' | sed 's/"//g') + model_file_path="group_vars/model_${model_file}.yml" + + # Check if the model file exists + if [ ! -f "$model_file_path" ]; then + echo "Model file $model_file_path not found for host $hostname with model $model" + error_found=1 + continue + fi + + # Check if the model requires mac_override + requires_mac_override=$(yq '.requires_mac_override' "$model_file_path") + + if [ "$requires_mac_override" = "true" ]; then + if [ "$mac_override" == "null" ]; then + # Output the missing mac_override details immediately + echo "Host $hostname (model: $model) in $location_file is missing mac_override." + error_found=1 + fi + fi + done +done + +# Check for duplicates + +# Variable to accumulate duplicate findings +all_duplicates="" + +# Iterate over each file in the directory +for file in $changed_files; do + # Check if it is a file + if [ -f "$file" ]; then + # Extract VIDs / VLAN names, sort, and find duplicates + duplicates=$(yq 'select(.hosts != null) | .hosts[].mac_override | select(. != null) | to_entries[] | .value' "$file" | grep -v 'null' | sed 's/["'\'']//g' | sort | uniq -cd) + # Accumulate duplicates if found + if [ -n "$duplicates" ]; then + all_duplicates+="\nDuplicate mac_overrides found in $file:\n$duplicates" + fi + fi +done + +# Check if there were any duplicates found +if [ -n "$all_duplicates" ]; then + echo -e "Duplicates mac_overrides found:$all_duplicates" + error_found=1 +fi + +# Exit with a non-zero status code if any errors were found +if [ "$error_found" -eq 1 ]; then + exit 1 +else + echo "MAC override check completed successfully." +fi diff --git a/.github/workflows/check-mac-override-duplicates.yml b/.github/workflows/check-mac-override-duplicates.yml new file mode 100644 index 000000000..9c02c5f7b --- /dev/null +++ b/.github/workflows/check-mac-override-duplicates.yml @@ -0,0 +1,17 @@ +--- +name: Check for duplicate mac_overrides + +on: [push, pull_request] # yamllint disable-line rule:truthy + +jobs: + check-mac-override-duplicates: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Run mac_override duplicate check + run: | + ./.github/checks/check-mac-override-duplicates.sh diff --git a/.github/workflows/check-mac-override-missing.yml b/.github/workflows/check-mac-override-missing.yml new file mode 100644 index 000000000..42211da5d --- /dev/null +++ b/.github/workflows/check-mac-override-missing.yml @@ -0,0 +1,17 @@ +--- +name: Check missing mac_overrides + +on: [push, pull_request] # yamllint disable-line rule:truthy + +jobs: + check-mac-override-missing: + runs-on: ubuntu-latest + steps: + - name: Checkout branch + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Run mac_override missing check + run: | + ./.github/checks/check-mac-override-missing.sh