add Dockerfile and gitops status script
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
This commit is contained in:
parent
c83725a027
commit
2c27e6ade7
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
custom-ansible.tar
|
||||
12
Dockerfile
Normal file
12
Dockerfile
Normal file
@ -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
|
||||
121
update-gitops-status.sh
Normal file
121
update-gitops-status.sh
Normal file
@ -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 <<EOF
|
||||
{
|
||||
"repo": "$REPO_NAME",
|
||||
"server": "$SERVER_NAME",
|
||||
"sync_status": "$SYNC_STATUS",
|
||||
"drift_count": $DRIFT_COUNT,
|
||||
"deployed_files": $DEPLOYED_FILES,
|
||||
"drifted_files": $DRIFTED_FILES,
|
||||
"last_check": "$TIMESTAMP"
|
||||
}
|
||||
EOF
|
||||
)
|
||||
|
||||
echo "==> 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
|
||||
Loading…
x
Reference in New Issue
Block a user