]> Frank Brehm's Git Trees - pixelpark/create-terraform.git/commitdiff
Adding changing NetworkManager options for DNS to postinstall-scripts/conf-resolver
authorFrank Brehm <frank.brehm@pixelpark.com>
Mon, 16 Dec 2019 16:17:25 +0000 (17:17 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Mon, 16 Dec 2019 16:17:25 +0000 (17:17 +0100)
postinstall-scripts/conf-resolver

index da8992cf405c6364cc443001934cd3dfa0a5e643..189460f2e87c3890f5665a2a363ca7c91dd9f0d2 100755 (executable)
@@ -23,7 +23,7 @@ NORMAL=""
 HAS_TTY='y'
 HAS_COLORS="n"
 
-VERSION="0.7"
+VERSION="0.8"
 
 BASENAME="$(basename ${0})"
 BASE_DIR="$(dirname ${0})"
@@ -360,21 +360,27 @@ empty_line() {
 }
 
 #------------------------------------------------------------------------------
-CP() {
-    local cmd="cp"
-    if [[ "${VERBOSE}" == "y" ]] ; then
-        cmd+=" --verbose"
-    fi
+exec_cmd() {
+
     local msg="Executing"
     if [[ "${SIMULATE}" == "y" ]] ; then
         msg="Simulating"
     fi
     if [[ "${SIMULATE}" == "y" || "${VERBOSE}" == "y" ]] ; then
-        echo "${msg}: ${cmd} $*"
+        echo "${msg}: $*"
     fi
     if [[ "${SIMULATE}" != "y" ]] ; then
-        eval ${cmd} "$@"
+        eval "$@"
+    fi
+}
+
+#------------------------------------------------------------------------------
+CP() {
+    local cmd="cp"
+    if [[ "${VERBOSE}" == "y" ]] ; then
+        cmd+=" --verbose"
     fi
+    exec_cmd ${cmd} "$@"
 }
 
 #------------------------------------------------------------------------------
@@ -383,16 +389,7 @@ MV() {
     if [[ "${VERBOSE}" == "y" ]] ; then
         cmd+=" --verbose"
     fi
-    local msg="Executing"
-    if [[ "${SIMULATE}" == "y" ]] ; then
-        msg="Simulating"
-    fi
-    if [[ "${SIMULATE}" == "y" || "${VERBOSE}" == "y" ]] ; then
-        echo "${msg}: ${cmd} $*"
-    fi
-    if [[ "${SIMULATE}" != "y" ]] ; then
-        eval ${cmd} "$@"
-    fi
+    exec_cmd ${cmd} "$@"
 }
 
 #------------------------------------------------------------------------------
@@ -484,6 +481,82 @@ generate() {
 
 }
 
+#------------------------------------------------------------------------------
+do_nm() {
+
+    empty_line
+    info "Executing ${GREEN}NetworkManager settings${NORMAL} ..."
+
+    if [[ $( type -t ip ) != "file" ]] ; then
+        warn "Command '${YELLOW}ip${NORMAL}' not found!"
+        return
+    fi
+
+    if [[ $( type -t nmcli ) != "file" ]] ; then
+        warn "Command '${YELLOW}nmcli${NORMAL}' not found!"
+        return
+    fi
+
+    local first_if=$( ip -oneline address | \
+                        grep -w 'inet' | \
+                        grep -v -E '^[1-9][0-9]*:[     ][      ]*lo[   ]' | \
+                        awk '{ print $2 }' | \
+                        head -n 1 )
+
+    local cur_nm_ns=$( nmcli connection show "${first_if}" | \
+                        grep 'ipv4.dns:' | \
+                        awk '{ print $2 }' )
+    local cur_nm_search=$( nmcli connection show "${first_if}" | \
+                        grep 'ipv4.dns-search:' | \
+                        awk '{ print $2 }' )
+    local cur_nm_options=$( nmcli connection show "${first_if}" | \
+                        grep 'ipv4.dns-options:' | \
+                        awk '{ print $2 }' | \
+                        sed -e 's/^[   ]*"//' -e 's/"[         ]*$//' )
+
+    local ns=
+    local i=0
+    local exp_nm_ns=''
+    local nr="${#NAME_SERVERS[*]}"
+    for ns in "${NAME_SERVERS[@]}" ; do
+        i=$(( $i + 1 ))
+        exp_nm_ns+="${ns}"
+        if [[ "${i}" != "${nr}" ]] ; then
+            exp_nm_ns+=","
+        fi
+    done
+    debug "NM Nameservers: expected: '${exp_nm_ns}', configured: '${cur_nm_ns}'."
+    if [[ "${exp_nm_ns}" != "${cur_nm_ns}" ]] ; then
+        info "Setting ${GREEN}ipv4.dns${NORMAL} of '${first_if}' to '${GREEN}${exp_nm_ns}${NORMAL}'"
+        exec_cmd nmcli connection modify "${first_if}" ipv4.dns "${exp_nm_ns}"
+    fi
+
+    local dom=
+    local exp_nm_search=""
+    i=0
+    nr="${#SEARCH_DOMAINS[*]}"
+    for dom in "${SEARCH_DOMAINS[@]}" ; do
+        i=$(( $i + 1 ))
+        exp_nm_search+="${dom}"
+        if [[ "${i}" != "${nr}" ]] ; then
+            exp_nm_search+=","
+        fi
+    done
+    debug "NM search domains: expected: '${exp_nm_search}', configured: '${cur_nm_search}'."
+    if [[ "${exp_nm_search}" != "${cur_nm_search}" ]] ; then
+        info "Setting ${GREEN}ipv4.dns-search${NORMAL} of '${first_if}' to '${GREEN}${exp_nm_search}${NORMAL}'"
+        exec_cmd nmcli connection modify "${first_if}" ipv4.dns-search "${exp_nm_search}"
+    fi
+
+    local exp_nm_options=$( echo "${RESOLV_OPTIONS}" | sed -e 's/[     ][      ]*/,/g' )
+    debug "NM DNS options: expected: '${exp_nm_options}', configured: '${cur_nm_options}'."
+    if [[ "${exp_nm_options}" != "${cur_nm_options}" ]] ; then
+        info "Setting ${GREEN}ipv4.dns-options${NORMAL} of '${first_if}' to '${GREEN}${exp_nm_options}${NORMAL}'"
+        exec_cmd nmcli connection modify "${first_if}" ipv4.dns-options "${exp_nm_options}"
+    fi
+
+}
+
 
 ################################################################################
 ##
@@ -498,6 +571,7 @@ main() {
     check_for_root
     do_backup
     generate
+    do_nm
 
 }