-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix wrong cache filename causing cache not working at all * Make logging verbosity configurable * Use JSON logs instead of text logs * Set default LOG_LEVEL to info to avoid too many logs
- Loading branch information
1 parent
258b8e6
commit 0e5401b
Showing
2 changed files
with
51 additions
and
11 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,60 @@ | ||
#!/bin/sh | ||
exec 3>&1 | ||
|
||
# get int value for given log level debug=>10, info=>20, error=>40 | ||
get_log_level_value(){ | ||
level=20 | ||
case $1 in | ||
debug) | ||
level=10 | ||
;; | ||
info) | ||
level=20 | ||
;; | ||
error) | ||
level=40 | ||
;; | ||
esac | ||
echo $level | ||
} | ||
|
||
# functions used to display logs | ||
log(){ | ||
log_level_value=$(get_log_level_value $1) | ||
if [ $log_level_value -ge $current_level_value ] | ||
then | ||
echo "{\"date\": \"$(date -u +"%Y-%m-%dT%H:%M:%SZ")\", \"level\": \"$1\", $levelvalue \"message\": \"$2\"}" 1>&3 | ||
fi | ||
} | ||
debug(){ log debug "$1"; } | ||
info(){ log info "$1"; } | ||
error(){ log error "$1"; } | ||
|
||
# validate inputs and set defaults | ||
|
||
# check that required input are properly set | ||
[ -z $HOSTNAME ] && echo You need to set environment variable HOSTNAME && exit | ||
[ -z $IDENTIFIER ] && echo You need to set environment variable IDENTIFIER && exit | ||
[ -z $PASSWORD ] && echo You need to set environment variable PASSWORD && exit | ||
[ -z $LOG_LEVEL ] && LOG_LEVEL=debug | ||
|
||
# set current log level, all logs with a lower level are filtered | ||
current_level_value=$(get_log_level_value $LOG_LEVEL) | ||
|
||
# retrieve last known (cached) address and current public address | ||
CACHED_IP_ADDRESS=$(cat /dynhost/cached_address 2>/dev/null) | ||
CACHED_IP_ADDRESS=$(cat /dynhost/.cache/ip_address 2>/dev/null) | ||
PUBLIC_IP_ADDRESS=$(dig @resolver4.opendns.com myip.opendns.com +short) | ||
|
||
# compare current address to cached one to avoid useless updates | ||
if [ "$CACHED_IP_ADDRESS" != "$PUBLIC_IP_ADDRESS" ] | ||
then | ||
echo "$(date +"%Y-%m-%d/%T") - INFO - Cached address changed from '$CACHED_IP_ADDRESS' to '$PUBLIC_IP_ADDRESS'" | ||
info "Cached address changed from '$CACHED_IP_ADDRESS' to '$PUBLIC_IP_ADDRESS'" | ||
# update DynHost record using OVH API | ||
ovh_response=$(curl -sS "http://www.ovh.com/nic/update?system=dyndns&hostname=${HOSTNAME}&myip=${PUBLIC_IP_ADDRESS}" \ | ||
-H "Authorization: Basic $(echo -n $IDENTIFIER:$PASSWORD | base64)") | ||
echo "$(date +"%Y-%m-%d/%T") - INFO - OVH response: ${ovh_response}" | ||
debug "OVH response: ${ovh_response}" | ||
# cache address for comparison, to avoid useless updates | ||
echo $PUBLIC_IP_ADDRESS > cached_address | ||
mkdir -p .cache/ | ||
echo $PUBLIC_IP_ADDRESS > /dynhost/.cache/ip_address | ||
else | ||
echo "$(date +"%Y-%m-%d/%T") - DEBUG - No changes detected" | ||
debug "No changes detected" | ||
fi |