From 2c27e6ade7ddfc6cef5069aba25335ce0199467f Mon Sep 17 00:00:00 2001 From: dvirlabs <114520947+dvirlabs@users.noreply.github.com> Date: Tue, 9 Jun 2026 16:46:33 +0300 Subject: [PATCH] add Dockerfile and gitops status script --- .gitignore | 1 + Dockerfile | 12 ++++ update-gitops-status.sh | 121 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 update-gitops-status.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..97ab241 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +custom-ansible.tar diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ce10300 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM alpine/ansible:latest + +# Install required dependencies for GitOps status check +# - git: for detecting deployed files and git operations +# - curl: for HTTP requests to gitops-status-server +# - jq: for JSON formatting and parsing +# - bash: for script execution +RUN apk add --no-cache \ + git \ + curl \ + jq \ + bash diff --git a/update-gitops-status.sh b/update-gitops-status.sh new file mode 100644 index 0000000..11fa1a6 --- /dev/null +++ b/update-gitops-status.sh @@ -0,0 +1,121 @@ +#!/bin/bash +# ============================================================================= +# GitOps Status Update Script +# Checks Ansible sync status and sends JSON update to gitops-status-server API +# Usage: ./update-gitops-status.sh +# Environment variables: +# - GITOPS_STATUS_SERVER_URL: API endpoint URL +# - REPO_NAME: Repository name +# - SERVER_NAME: Server name +# - MODE: post-deploy or cron (optional, for logging) +# - ANSIBLE_CONFIG: Path to ansible.cfg +# ============================================================================= + +set -e + +# Get configuration from environment variables +API_URL="${GITOPS_STATUS_SERVER_URL}/api/status" +REPO_NAME="${REPO_NAME:-unknown}" +SERVER_NAME="${SERVER_NAME:-unknown}" +MODE="${MODE:-check}" +ANSIBLE_CONFIG="${ANSIBLE_CONFIG:-ansible.cfg}" + +echo "==> GitOps Status Update: $REPO_NAME / $SERVER_NAME" +echo " API URL: $API_URL" +echo " Mode: $MODE" + +# Verify required environment variables +if [[ -z "$GITOPS_STATUS_SERVER_URL" || -z "$REPO_NAME" || -z "$SERVER_NAME" ]]; then + echo "ERROR: Missing required environment variables (GITOPS_STATUS_SERVER_URL, REPO_NAME, SERVER_NAME)" + exit 1 +fi + +# Run Ansible validation to check sync status +echo "==> Running Ansible sync check..." +ANSIBLE_OUTPUT=$(mktemp) +SYNC_STATUS="UNKNOWN" +DRIFT_COUNT=0 +DEPLOYED_FILES="[]" +DRIFTED_FILES="[]" + +# Try to run the validate playbook and capture output +if ansible-playbook -i ansible/inventory/hosts.yml \ + -c local \ + ansible/playbooks/validate.yml \ + -vv > "$ANSIBLE_OUTPUT" 2>&1; then + + SYNC_STATUS="SYNCED" + DRIFT_COUNT=0 + echo " ✓ Server is SYNCED with Git" +else + # If playbook fails, it means there's drift/differences + SYNC_STATUS="OUT_OF_SYNC" + + # Parse output to extract changed files + # Look for "CHANGED" or "failed" tasks + CHANGED_TASKS=$(grep -E "CHANGED|changed:|failed:" "$ANSIBLE_OUTPUT" | wc -l || true) + DRIFT_COUNT=$((CHANGED_TASKS > 0 ? CHANGED_TASKS : 1)) + + # Try to extract file information from Ansible output + # This is a best-effort attempt based on common Ansible patterns + CHANGED_FILES=$(grep -oE "path=([^ ]+)|src=([^ ]+)" "$ANSIBLE_OUTPUT" | cut -d= -f2 | sort -u | head -20) + + if [[ -n "$CHANGED_FILES" ]]; then + # Format changed files as JSON array + DRIFTED_FILES=$(echo "$CHANGED_FILES" | jq -R -s 'split("\n") | map(select(length > 0) | {name: .})') + else + DRIFTED_FILES="[]" + fi + + echo " ✗ Server is OUT_OF_SYNC with Git (drift count: $DRIFT_COUNT)" +fi + +# Get list of all managed files (best effort) +if [[ -f "ansible/playbooks/apply.yml" ]]; then + # Extract file paths from the apply playbook + MANAGED_FILES=$(grep -E "src:|path:|name:" ansible/playbooks/apply.yml | \ + grep -oE "'[^']+'" | tr -d "'" | sort -u | head -50) + + if [[ -n "$MANAGED_FILES" ]]; then + DEPLOYED_FILES=$(echo "$MANAGED_FILES" | jq -R -s 'split("\n") | map(select(length > 0) | {name: .})') + fi +fi + +# Get current timestamp in ISO 8601 format +TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ") + +# Build JSON payload +JSON_PAYLOAD=$(cat < Sending status update to API..." +echo "$JSON_PAYLOAD" | jq . + +# Send to API using curl +HTTP_CODE=$(curl -s -o /tmp/api_response.json -w "%{http_code}" \ + -X POST "$API_URL" \ + -H "Content-Type: application/json" \ + -d "$JSON_PAYLOAD") + +if [[ "$HTTP_CODE" == "200" ]]; then + echo " ✓ Status update sent successfully (HTTP $HTTP_CODE)" + cat /tmp/api_response.json | jq . +else + echo " ✗ Failed to send status update (HTTP $HTTP_CODE)" + cat /tmp/api_response.json +fi + +# Cleanup +rm -f "$ANSIBLE_OUTPUT" /tmp/api_response.json + +exit 0