Last active
October 10, 2019 21:01
-
-
Save morozov/6f2e3f9eb82e0fbfabb600e603540625 to your computer and use it in GitHub Desktop.
Watch and Diff. A shell script for monitoring changes in configuration files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -euo pipefail | |
function __get_missing_keys { | |
php -- "$@" << 'EOF' | |
<?php | |
$args = $GLOBALS['argv']; | |
array_shift($args); | |
foreach ($args as $path) { | |
if (file_exists($path)) { | |
include $path; | |
} | |
} | |
$expected = [ | |
'allowedSAs' => null, | |
'clientId' => null, | |
'clientSecret' => null, | |
'cloudConsoleRoutes' => null, | |
'cloudConsoleUrl' => null, | |
'crmOAuthScope' => null, | |
'enabled' => null, | |
'idpUrl' => null, | |
'requestedOAuthScopes' => null, | |
'stsKeySetId' => null, | |
'stsUrl' => null, | |
'tid' => null, | |
]; | |
$actual = array_intersect_key($sugar_config['idm_mode'] ?? [], $expected); | |
$diff = array_diff_key($expected, $actual); | |
if ($diff) { | |
echo implode(PHP_EOL, array_keys($diff)), PHP_EOL; | |
exit(1); | |
} | |
EOF | |
} | |
ALERT_EMAIL="" | |
ALERT_SENT=0 | |
if [ "$1" == "-e" ]; then | |
ALERT_EMAIL="$2" | |
shift 2 | |
fi | |
if [ $# -lt 1 ]; then | |
echo "Usage: $(basename $0) [-e [email protected]] <file>..." >&2 | |
exit 1; | |
fi | |
declare -A CONTENTS | |
for FILE in "$@" | |
do | |
CONTENTS["$FILE"]=$(<"$FILE") | |
done | |
while FILE=`inotifywait -e close_write,move_self,delete_self --format "%w" "$@"` | |
do | |
diff -u <(echo -n "${CONTENTS["$FILE"]}") "$FILE" || true | |
CONTENTS["$FILE"]=$(<"$FILE") | |
echo | |
MISSING_KEYS=$(__get_missing_keys "$@") || { | |
echo "The following configuration keys are missing:" | |
echo "$MISSING_KEYS" | |
echo | |
if [ ! -z "$ALERT_EMAIL" -a $ALERT_SENT -ne 1 ]; then | |
echo "Sending alert to $ALERT_EMAIL" | |
{ | |
echo "Instance path: $(dirname $FILE)" | |
echo "Missing keys:" | |
echo "$MISSING_KEYS" | |
} | mail -s "Missing configuration keys" $ALERT_EMAIL && ALERT_SENT=1 | |
fi | |
} | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment