blob: 81c6fb80a3d5fd2a84e777991ba5cc0610c39bac (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
#!/bin/sh
# {{ ansible_managed }}
# Atomic Nginx configuration validator wrapper for Ansible
TEMP_FILE="$1"
DEST_FILE="$2"
if [ -z "$TEMP_FILE" ] || [ -z "$DEST_FILE" ]; then
echo "Usage: nginx-validate <temp_file> <dest_file>" >&2
exit 1
fi
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
BACKUP_FILE="${DEST_FILE}.${TIMESTAMP}.bak"
# Back up current destination if it exists
if [ -f "$DEST_FILE" ]; then
cp "$DEST_FILE" "$BACKUP_FILE"
fi
# Apply the rendered temporary file for testing
cp "$TEMP_FILE" "$DEST_FILE"
# Test master configuration
if nginx -t; then
rm -f "$BACKUP_FILE"
exit 0
else
# Restore previous config on failure and clean up
if [ -f "$BACKUP_FILE" ]; then
cp "$BACKUP_FILE" "$DEST_FILE"
rm -f "$BACKUP_FILE"
else
rm -f "$DEST_FILE"
fi
exit 1
fi
|