diff --git a/README.md b/README.md index faa768e..22b58e2 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ services: HOSTNAME: "." IDENTIFIER: "-" PASSWORD: "" + LOG_LEVEL: "debug" ``` ### Using kubernetes @@ -40,12 +41,15 @@ spec: value: "-" - name: PASSWORD value: "" + - 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| diff --git a/update-record b/update-record index 8d58e88..dafaac2 100755 --- a/update-record +++ b/update-record @@ -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