Added core plugin approach. Changes not yet documented

This commit is contained in:
Eric Renfro 2017-08-19 18:25:29 -04:00
parent abd1c8fc71
commit 52546805ad
5 changed files with 161 additions and 116 deletions

View file

@ -14,17 +14,30 @@ trim() {
echo -n "$var" echo -n "$var"
} }
valid_ip() { detect_ip_version() {
local ip=$1 local ip=$1
if [[ "${prog_name,,}" == "update-ipv4" ]]; then if valid_ipv4 "$ip"; then
valid_ipv4 "$ip" return 4;
return $? elif valid_ipv6 "ip"; then
elif [[ "${prog_name,,}" == "update-ipv6" ]]; then return 6
valid_ipv6 "$ip" else
return $? return 0;
fi fi
return 89 }
valid_ip() {
local ip=$1
local iptype
detect_ip_version "$ip"
iptype=$?
case $iptype in
4) return 0;;
6) return 0;;
*) return 1;;
esac
} }
valid_ipv4() { valid_ipv4() {
@ -144,9 +157,9 @@ getDnsNS() {
fi fi
} }
getDnsIP() { getDnsRecord() {
local rec=$1 local rec=$1
local rectype local rectype=$2
local result local result
local err local err
@ -154,14 +167,6 @@ getDnsIP() {
do do
[[ "$i" = "ERROR" ]] && return 1 [[ "$i" = "ERROR" ]] && return 1
if [[ "${prog_name,,}" == "update-ipv4" ]]; then
rectype=A
elif [[ "${prog_name,,}" == "update-ipv6" ]]; then
rectype=AAAA
else
return 88
fi
result=$(dig +short @${i} ${rectype} ${rec} | head -n1) result=$(dig +short @${i} ${rectype} ${rec} | head -n1)
err=$? err=$?
@ -344,20 +349,21 @@ run-update() {
# Caches current local IP so it doesn't have to every time. # Caches current local IP so it doesn't have to every time.
getCurrentLocalIP() { getCurrentLocalIP() {
local iptype=$1
local cip local cip
if [[ -z "$currentip" ]]; then if [[ -z "$currentip" ]]; then
case "${prog_name,,}" in case "$iptype" in
update-ipv4) cip=$(getIPv4) || return 1;; 4) cip=$(getIPv4) || return 1;;
update-ipv6) cip=$(getIPv6) || return 1;; 6) cip=$(getIPv6) || return 1;;
*) return 3;; *) return 3;;
esac esac
echo "$cip" echo "$cip"
else else
case "${prog_name,,}" in case "$iptype" in
update-ipv4) valid_ipv4 "$currentip" || return 1;; 4) valid_ipv4 "$currentip" || return 1;;
update-ipv6) valid_ipv6 "$currentip" || return 1;; 6) valid_ipv6 "$currentip" || return 1;;
*) return 3;; *) return 3;;
esac esac
@ -366,42 +372,42 @@ getCurrentLocalIP() {
} }
getCurrentIP() { getCurrentIP() {
local i=$1 local iptype=$1
local record=$2
if [[ -z "$i" ]]; then [[ -z "$iptype" ]] && return 1
return 1 [[ -z "$record" ]] && return 1
else
case "${prog_name,,}" in case "$iptype" in
update-ipv4) 4) log "Checking if internet IP has changed for $record"
log "Checking if internet IP has changed for $i" currentip=$(getCurrentLocalIP 4) || return $?
currentip=$(getCurrentLocalIP) || return $? externalip=$(getDnsRecord "$record" "A") || return 2
externalip=$(getDnsIP $i) || return 2
return 0 return 0
;; ;;
update-ipv6) 6) log "Checking if internet IPv6 has changed for $record"
log "Checking if internet IPv6 has changed for $i" currentip=$(getCurrentLocalIP 6) || return $?
currentip=$(getCurrentLocalIP) || return $? externalip=$(getDnsRecord "$record" "AAAA") || return 2
externalip=$(getDnsIP $i) || return 2
return 0 return 0
;; ;;
*) logerr "FATAL"; return 3 ;; *) logerr "FATAL"; return 3 ;;
esac esac
fi
} }
check-update() { check-update() {
local arg=$1 local iptype=$1
local d local record=$2
if [[ -n "$arg" ]]; then [[ -z "$iptype" ]] && return 1
getCurrentIP $arg [[ -z "$record" ]] && return 1
getCurrentIP $iptype $record
err=$? err=$?
case $err in case $err in
0) if [[ "$currentip" != "$externalip" ]]; then 0) if [[ "$currentip" != "$externalip" ]]; then
log "Updates found: $externalip is not $currentip" log "Updates found: $externalip is not $currentip"
log "Running Agents for $arg" log "Running Agents for $record"
run-update "$currentip" "$externalip" "$arg" run-update "$currentip" "$externalip" "$record"
err=$? err=$?
if [[ $err -ne 0 ]]; then if [[ $err -ne 0 ]]; then
@ -425,40 +431,9 @@ check-update() {
exit $err exit $err
;; ;;
esac esac
else
if [[ -n "$record" ]]; then
check-update "$record"
elif [[ -n "$record_file" ]]; then
for d in $(getRecords "$record_file"); do
check-update "$d"
done
else
return 99
fi
fi
} }
prog_lock() {
# This section doesn't run if this script was sourced.
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
# Internal Initialization
prog_name=$(basename "$0")
script_dir=$(dirname "$(readlink -f "$0")")
if [[ -z "$1" ]]; then
logerr "ERROR: Need to provide a DNS record or file to look-up"
exit 1
else
if [[ -r "$1" ]]; then
record_file=$1
else
record=$1
fi
fi
## Locking ## Locking
if [[ "$USER" = "root" ]]; then if [[ "$USER" = "root" ]]; then
@ -490,13 +465,36 @@ if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
# Remember! Lock file is removed when one of the scripts exits and it is # Remember! Lock file is removed when one of the scripts exits and it is
# the only script holding the lock or lock is not acquired at all. # the only script holding the lock or lock is not acquired at all.
}
# This section doesn't run if this script was sourced.
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
# Internal Initialization
prog_name=$(basename "$0")
script_dir=$(dirname "$(readlink -f "$0")")
if [[ -z "$1" ]]; then
logerr "ERROR: Need to provide a DNS record or file to look-up"
exit 1
else
if [[ -r "$1" ]]; then
record_file=$1
else
record=$1
fi
fi
prog_lock
# Main # Main
if [[ -r "${script_dir}/plugins/${1}" ]]; then
case "${prog_name,,}" in source "${script_dir}/plugins/${1}"
update-ipv4) check-update; exit $? ;; plugin_name=${1,,}
update-ipv6) check-update; exit $? ;; else
*) logerr "ERROR: Unknown update method"; exit 1 ;; logerr "ERROR: Unknown plugin '${1}'"
esac fi
fi fi

25
src/plugins/update-ipv4 Normal file
View file

@ -0,0 +1,25 @@
plugin_name=$1
check_record=$2
updateerrors=0
if [[ -z "$check_record" ]]; then
logerr "ERROR: Need to provide a DNS record or file to look-up"
exit 1
else
if [[ -r "$check_record" ]]; then
for d in $(getRecords "$check_record"); do
check-update 4 "$check_record"
if [[ $? -ne 0 ]]; then
let updateerrors++
fi
done
else
check-update 4 "$check_record"
if [[ $? -ne 0 ]]; then
let updateerrors++
fi
fi
fi
exit $updateerrors

24
src/plugins/update-ipv6 Normal file
View file

@ -0,0 +1,24 @@
plugin_name=$1
check_record=$2
updateerrors=0
if [[ -z "$check_record" ]]; then
logerr "ERROR: Need to provide a DNS record or file to look-up"
exit 1
else
if [[ -r "$check_record" ]]; then
for d in $(getRecords "$check_record"); do
check-update 6 "$check_record"
if [[ $? -ne 0 ]]; then
let updateerrors++
fi
done
else
check-update 6 "$check_record"
if [[ $? -ne 0 ]]; then
let updateerrors++
fi
fi
fi
exit $updateerrors

View file

@ -1 +0,0 @@
dynamic-ip

View file

@ -1 +0,0 @@
dynamic-ip