Skip to content

Commit

Permalink
Fix cache and improve logging (#1)
Browse files Browse the repository at this point in the history
* 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
hoshiyosan authored May 23, 2022
1 parent 258b8e6 commit 0e5401b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ services:
HOSTNAME: "<host>.<domain>"
IDENTIFIER: "<domain>-<suffix>"
PASSWORD: "<password>"
LOG_LEVEL: "debug"
```
### Using kubernetes
Expand All @@ -40,12 +41,15 @@ spec:
value: "<domain>-<suffix>"
- name: PASSWORD
value: "<password>"
- name: LOG_LEVEL
value: "debug"
```
### Environment variables
*Following environment variables are mandatory* :
- **HOSTNAME**: Subdomain on which DNS record must be updated dynamically.
- **IDENTIFIER**: DynHost management username.
- **PASSWORD**: DynHost management password.
|Variable|Description|Is required?|Default|
|-|-|-|-|
|HOSTNAME|Subdomain on which DNS record must be updated dynamically.|**Yes**|-|
|IDENTIFIER|DynHost management username.|**Yes**|-|
|PASSWORD|DynHost management password.|**Yes**|-|
|LOG_LEVEL|String used to configure verbosity (must be one of: 'debug', 'info', 'error')|No|info|
48 changes: 42 additions & 6 deletions update-record
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

0 comments on commit 0e5401b

Please sign in to comment.