Darryl L. Pierce
2009-Jun-29 14:40 UTC
[Ovirt-devel] [PATCH node] Change DNS setup to match NTP. rhbz#508677
Rather than expecting the DNS entries to be on one line, they are now entered as two separate entries. Also fixes a bug that caused the first DNS entry to be lost when a second entry was provided. Signed-off-by: Darryl L. Pierce <dpierce at redhat.com> --- scripts/ovirt-config-networking | 31 +++++++++++-------------------- 1 files changed, 11 insertions(+), 20 deletions(-) diff --git a/scripts/ovirt-config-networking b/scripts/ovirt-config-networking index d29bd12..901e48d 100755 --- a/scripts/ovirt-config-networking +++ b/scripts/ovirt-config-networking @@ -206,26 +206,18 @@ function configure_dns local IF_CONFIG if [ -z "$AUTO" ]; then - while true; do + for dns in first second; do printf "\n" - echo "Enter up to two DNS servers separated by commas:" - if [ -n "$OVIRT_DNS" ]; then - echo "Press Enter for defaults: ($OVIRT_DNS)" + read -ep "Please enter the ${dns} DNS server: " + if [[ -z "${DNS}" ]] && [[ -z "${REPLY}" ]]; then + printf "\nAborted...\n" + return fi - read -ep ": " - DNS=$REPLY - - if [ -z "$DNS" ]; then - DNS=$OVIRT_DNS + if [[ -z "${DNS}" ]]; then + DNS="${REPLY}" + elif [[ -n "${REPLY}" ]]; then + DNS="${DNS}, ${REPLY}" fi - - printf "\n" - read -ep "Is this correct (Y/N/A)? " - case $REPLY in - Y|y) break ;; - N|n) ;; - A|a) return ;; - esac done fi @@ -233,9 +225,8 @@ function configure_dns DNS1=$(echo "$DNS" | awk -F, '{print $1}') DNS2=$(echo "$DNS" | awk -F, '{print $2}') - test -n "$DNS1" && IF_CONFIG="set $IF_ROOT/DNS1 $DNS1" - test -n "$DNS2" && IF_CONFIG="set $IF_ROOT/DNS2 $DNS2" - printf "$IF_CONFIG\n" >> $IF_FILENAME + test -n "$DNS1" && printf "set $IF_ROOT/DNS1 $DNS1\n" >> $IF_FILENAME + test -n "$DNS2" && printf "set $IF_ROOT/DNS2 $DNS2\n" >> $IF_FILENAME fi } -- 1.6.0.6
The code will now build strings that are comma-delimited to keep consistency between the interactive and automated setups.
Darryl L. Pierce
2009-Jul-09 14:18 UTC
[Ovirt-devel] [PATCH node] Change DNS setup to match NTP. rhbz#508677
Rather than expecting the DNS entries to be on one line, they are now entered as two separate entries. Also fixes a bug that caused the first DNS entry to be lost when a second entry was provided. In both cases, the input is collected in a colon-delimited string that is then processed. This is to provide consistency between the interactive setup and the automated setup. Signed-off-by: Darryl L. Pierce <dpierce at redhat.com> --- scripts/ovirt-config-networking | 53 +++++++++++++++++++++++++------------- scripts/ovirt-functions | 18 +++++++++++++ 2 files changed, 53 insertions(+), 18 deletions(-) diff --git a/scripts/ovirt-config-networking b/scripts/ovirt-config-networking index aa04a7c..730381e 100755 --- a/scripts/ovirt-config-networking +++ b/scripts/ovirt-config-networking @@ -212,17 +212,30 @@ function configure_dns if [ -z "$AUTO" ]; then while true; do - printf "\n" - echo "Enter up to two DNS servers separated by commas:" - if [ -n "$OVIRT_DNS" ]; then - echo "Press Enter for defaults: ($OVIRT_DNS)" - fi - read -ep ": " - DNS=$REPLY - - if [ -z "$DNS" ]; then - DNS=$OVIRT_DNS - fi + for dns in first second; do + while true; do + printf "\n" + read -ep "Please enter the ${dns} DNS server (or ENTER to exit): " + if [[ -z "${REPLY}" ]]; then + if [[ -z "${DNS}" ]]; then + printf "\nAborted...\n" + return + else + break + fi + fi + if is_valid_ipv4 $REPLY; then + if [[ -z "${DNS}" ]]; then + DNS="${REPLY}" + elif [[ -n "${REPLY}" ]]; then + DNS="${DNS}:${REPLY}" + fi + break + else + printf "${REPLY} is an invalid address.\n" + fi + done + done printf "\n" ask_yes_or_no "Is this correct (y/n/a)?" true true @@ -235,12 +248,11 @@ function configure_dns fi if [ -n "$DNS" ]; then - DNS1=$(echo "$DNS" | awk -F, '{print $1}') - DNS2=$(echo "$DNS" | awk -F, '{print $2}') + DNS1=$(echo "$DNS" | awk -F\: '{print $1}') + DNS2=$(echo "$DNS" | awk -F\: '{print $2}') - test -n "$DNS1" && IF_CONFIG="set $IF_ROOT/DNS1 $DNS1" - test -n "$DNS2" && IF_CONFIG="set $IF_ROOT/DNS2 $DNS2" - printf "$IF_CONFIG\n" >> $IF_FILENAME + test -n "$DNS1" && printf "set $IF_ROOT/DNS1 $DNS1\n" >> $IF_FILENAME + test -n "$DNS2" && printf "set $IF_ROOT/DNS2 $DNS2\n" >> $IF_FILENAME fi } @@ -259,7 +271,11 @@ function configure_ntp if [ -z "$REPLY" ]; then break; fi - NTPSERVERS="$NTPSERVERS $REPLY" + if is_valid_ipv4 $REPLY; then + NTPSERVERS="${NTPSERVERS}:${REPLY}" + else + printf "${REPLY} is an invalid address.\n" + fi done fi } @@ -278,7 +294,8 @@ save\n" > $ntpconf if [ -n "$NTPSERVERS" ]; then offset=1 - for server in $NTPSERVERS; do + SERVERS=$(echo $NTPSERVERS | awk 'BEGIN{FS=":"}{for (i=1; i<=NF; i++) print $i}') + for server in $SERVERS; do printf "set /files/etc/ntp.conf/server[${offset}] ${server}\n" >> $ntpconf offset=$(echo "$offset+1" | bc) done diff --git a/scripts/ovirt-functions b/scripts/ovirt-functions index ecde762..81db36b 100755 --- a/scripts/ovirt-functions +++ b/scripts/ovirt-functions @@ -654,6 +654,24 @@ ask_yes_or_no () { done } +# Verifies the address entered is a valid IPv4 address. +is_valid_ipv4 () { + local address=${1} + local result=1 + + if [[ "$address" =~ "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" ]]; then + OIFS=$IFS + IFS='.' + ip=($address) + IFS=$OIFS + [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \ + && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]] + result=$? + fi + + return $result +} + # execute a function if called as a script, e.g. # ovirt-functions ovirt_store_config /etc/hosts -- 1.6.2.5
Reasonably Related Threads
- [PATCH node] add network.py script
- [PATCH node] Rerunning network config resets all network config. bz#507393
- [PATCH node] Make all yes/no prompts consistent. rhbz#508778
- [PATCH node] Adds vlan support to auto-installations for the node. bz#511056
- Obsoletes previous patch