From b383499dfad1a0235ded4ada40e61dc6104b956a Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Wed, 14 Sep 2016 11:05:43 +0200 Subject: [PATCH] DHCP: Allow configuring lease times in human-readable format Signed-off-by: Michael Tremer --- src/functions/functions.dhcpd | 25 ++++++++++++++--------- src/functions/functions.util | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/src/functions/functions.dhcpd b/src/functions/functions.dhcpd index 0c8f7056..e7826914 100644 --- a/src/functions/functions.dhcpd +++ b/src/functions/functions.dhcpd @@ -188,27 +188,30 @@ _dhcpd_edit_ipv4() { fi ;; --default-lease-time=*) - DEFAULT_LEASE_TIME=$(cli_get_val ${1}) + local val=$(cli_get_val ${1}) + DEFAULT_LEASE_TIME=$(parse_time ${val}) if ! isinteger DEFAULT_LEASE_TIME; then - error "Invalid value for --default-lease-time." + error "Invalid value for --default-lease-time: ${val}" return ${EXIT_ERROR} fi ;; --max-lease-time=*) - MAX_LEASE_TIME=$(cli_get_val ${1}) + local val=$(cli_get_val ${1}) + MAX_LEASE_TIME=$(parse_time ${val}) if ! isinteger MAX_LEASE_TIME; then - error "Invalid value for --max-lease-time." + error "Invalid value for --max-lease-time: ${val}" return ${EXIT_ERROR} fi ;; --min-lease-time=*) - MIN_LEASE_TIME=$(cli_get_val ${1}) + local val=$(cli_get_val ${1}) + MIN_LEASE_TIME=$(parse_time ${val}) if isset MIN_LEASE_TIME; then if ! isinteger MIN_LEASE_TIME; then - error "Invalid value for --min-lease-time." + error "Invalid value for --min-lease-time: ${val}" return ${EXIT_ERROR} fi fi @@ -231,18 +234,20 @@ _dhcpd_edit_ipv6() { while [ $# -gt 0 ]; do case "${1}" in --preferred-lifetime=*) - PREFERRED_LIFETIME=$(cli_get_val ${1}) + local val=$(cli_get_val ${1}) + PREFERRED_LIFETIME=$(parse_time ${val}) if ! isinteger PREFERRED_LIFETIME; then - error "Invalid value for --preferred-lifetime." + error "Invalid value for --preferred-lifetime: ${val}" return ${EXIT_ERROR} fi ;; --valid-lifetime=*) - VALID_LIFETIME=$(cli_get_val ${1}) + local val=$(cli_get_val ${1}) + VALID_LIFETIME=$(parse_time ${val}) if ! isinteger VALID_LIFETIME; then - error "Invalid value for --valid-lifetime." + error "Invalid value for --valid-lifetime: ${val}" return ${EXIT_ERROR} fi ;; diff --git a/src/functions/functions.util b/src/functions/functions.util index 9c3601b1..8ae38ee6 100644 --- a/src/functions/functions.util +++ b/src/functions/functions.util @@ -133,6 +133,44 @@ format_time() { echo ${ret} } +parse_time() { + local ret=0 + + local arg + for arg in $@; do + local unit + + case "${arg}" in + *h|*m|*s) + # Store unit + unit="${arg: -1}" + + # Remove unit + arg="${arg:0:-1}" + ;; + esac + + if ! isinteger arg; then + return ${EXIT_ERROR} + fi + + # Convert hours and minutes into seconds + case "${unit}" in + h) + arg=$(( ${arg} * 3600 )) + ;; + m) + arg=$(( ${arg} * 60 )) + ;; + esac + + # Add up everything + ret=$(( ${ret} + ${arg} )) + done + + print "${ret}" +} + assign() { local key=${1} assert isset key -- 2.39.2