From 082a49ff3ff62481a2f208c16fa30b5900e119ba Mon Sep 17 00:00:00 2001 From: Tobias Schwarz Date: Sat, 17 Aug 2024 12:25:36 +0000 Subject: [PATCH] workflows: add mac_overrides check --- .github/checks/check-mac-overrides.sh | 78 +++++++++++++++++++++++ .github/workflows/check-mac-overrides.yml | 17 +++++ locations/k12.yml | 1 + 3 files changed, 96 insertions(+) create mode 100755 .github/checks/check-mac-overrides.sh create mode 100644 .github/workflows/check-mac-overrides.yml diff --git a/.github/checks/check-mac-overrides.sh b/.github/checks/check-mac-overrides.sh new file mode 100755 index 000000000..95b112c37 --- /dev/null +++ b/.github/checks/check-mac-overrides.sh @@ -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." diff --git a/.github/workflows/check-mac-overrides.yml b/.github/workflows/check-mac-overrides.yml new file mode 100644 index 000000000..06780d5f7 --- /dev/null +++ b/.github/workflows/check-mac-overrides.yml @@ -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 diff --git a/locations/k12.yml b/locations/k12.yml index 0caeeaccb..fa51098fc 100644 --- a/locations/k12.yml +++ b/locations/k12.yml @@ -8,6 +8,7 @@ contact_nickname: 'zander' contacts: - 'alexanderjabs@gmx.de' + hosts: - hostname: k12-core role: corerouter