Skip to content

Commit

Permalink
workflows: check for missing mac_overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
Noki committed Aug 18, 2024
1 parent 7ea7801 commit 2ed3594
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 18 deletions.
48 changes: 30 additions & 18 deletions .github/checks/check-mac-override-missing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,41 @@
# Initialize a variable to track if any errors are found
error_found=0

# Define patterns for location files and model files
location_files='locations/*.yml'
model_files='group_vars/model_*.yml'

# Loop through all location YAML files
# Find all models that require a mac_override
declare -A mac_override_required_models

for model_file_path in $model_files; do
# Extract model name from file path
model_file=$(basename "$model_file_path" .yml)
model_name=${model_file#model_}

# Check if the model requires mac_override
requires_mac_override=$(yq '.requires_mac_override' "$model_file_path" | tr -d '"')

# Store the result in the associative array
mac_override_required_models["$model_name"]=$requires_mac_override
done

# Find all missing mac_overrides
for location_file in $location_files; do
# Extract host information using yq
host_count=$(yq '.hosts | length' "$location_file")
# Get hosts as a single YAML block to minimize calls to yq
hosts=$(yq '.hosts' "$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, otherwise continue
if [ ! -f "$model_file_path" ]; then
continue
fi
for i in $(seq 0 $(($(echo "$hosts" | yq '. | length') - 1))); do
hostname=$(echo "$hosts" | yq ".[$i].hostname" | tr -d '"')
model=$(echo "$hosts" | yq ".[$i].model" | tr -d '"')
mac_override=$(echo "$hosts" | yq ".[$i].mac_override" | tr -d '"')

# Check if the model requires mac_override
requires_mac_override=$(yq '.requires_mac_override' "$model_file_path")
# Convert model name to match the model file format (underscore instead of hyphen)
model_name=${model//-/_}

# Check if the model requires mac_override using the associative array
requires_mac_override=${mac_override_required_models["$model_name"]}

if [ "$requires_mac_override" = "true" ]; then
if [ "$mac_override" == "null" ]; then
Expand All @@ -44,3 +55,4 @@ if [ "$error_found" -eq 1 ]; then
else
echo "No MAC override issues found."
fi

17 changes: 17 additions & 0 deletions .github/workflows/check-mac-override-missing.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2ed3594

Please sign in to comment.