Skip to content

Commit

Permalink
workflows: add mac_overrides check
Browse files Browse the repository at this point in the history
  • Loading branch information
Noki committed Aug 17, 2024
1 parent 2e0b025 commit 082a49f
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
78 changes: 78 additions & 0 deletions .github/checks/check-mac-overrides.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/bin/bash

set -e # Exit immediately if a command exits with a non-zero status
set -o pipefail # If any command in a pipeline fails, return the exit status of the failed command

# Directory to hold all model files
MODEL_DIR="group_vars"

# Function to check if a model requires a mac_override
check_mac_override() {
local model=$1

# Convert hyphens to underscores to match the model file naming convention
local model_file="${MODEL_DIR}/model_${model//-/_}.yml"

# Check if the model file exists
if [[ ! -f "$model_file" ]]; then
echo "Model file for $model not found: $model_file"
exit 1
fi

# Check if the model requires a mac_override
yq eval '.requires_mac_override // false' "$model_file"
}

# Get a list of changed .yml files in the locations directory
changed_files=$(git diff --name-only -- '*.yml' | grep '^locations/')

# Exit if no .yml files have changed
if [[ -z "$changed_files" ]]; then
echo "No relevant .yml files changed."
exit 0
fi

# Flag to track if we should fail the script
should_fail=0

# Iterate over each changed .yml file
for file in $changed_files; do
echo "Processing file: $file"

# Get the list of hosts from the changed location file
hosts=$(yq eval '.hosts[]' "$file" 2>/dev/null)

# Check if the file has hosts section
if [[ -z "$hosts" ]]; then
echo "No hosts found in $file"
continue
fi

# Iterate over each host in the file
yq eval '.hosts[]' "$file" | while read -r host; do
hostname=$(echo "$host" | yq eval '.hostname' -)
model=$(echo "$host" | yq eval '.model' -)

# Check if model requires mac_override
requires_mac_override=$(check_mac_override "$model")

# If mac_override is required, check if it's present in the host's configuration
if [[ "$requires_mac_override" == "true" ]]; then
mac_override=$(echo "$host" | yq eval '.mac_override' -)

# If the mac_override is missing or null, mark as failure
if [[ -z "$mac_override" || "$mac_override" == "null" ]]; then
echo "Host $hostname is missing mac_override and the model $model requires it."
should_fail=1
fi
fi
done
done

# If any host is missing a required mac_override, fail the script
if [[ "$should_fail" -eq 1 ]]; then
echo "Some hosts are missing required mac_override. Failing the check."
exit 1
fi

echo "All checks passed."
17 changes: 17 additions & 0 deletions .github/workflows/check-mac-overrides.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
name: Check mac_overrides

on: [push, pull_request] # yamllint disable-line rule:truthy

jobs:
mac_override_check:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Run mac_override check
run: |
./.github/checks/check-mac-overrides.sh
1 change: 1 addition & 0 deletions locations/k12.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ contact_nickname: 'zander'
contacts:
- '[email protected]'


hosts:
- hostname: k12-core
role: corerouter
Expand Down

0 comments on commit 082a49f

Please sign in to comment.