]> Frank Brehm's Git Trees - pixelpark/create-terraform.git/commitdiff
Refactoring postinstall-scripts/conf-resolver
authorFrank Brehm <frank.brehm@pixelpark.com>
Mon, 16 Dec 2019 15:08:23 +0000 (16:08 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Mon, 16 Dec 2019 15:08:23 +0000 (16:08 +0100)
postinstall-scripts/conf-resolver

index 666c2d6c98901089875b1e3f5aa91a60016a63f2..da8992cf405c6364cc443001934cd3dfa0a5e643 100755 (executable)
@@ -10,6 +10,7 @@ VERBOSE="n"
 DEBUG="n"
 QUIET='n'
 DO_BACKUP="n"
+SIMULATE='n'
 
 # console colors:
 RED=""
@@ -22,25 +23,30 @@ NORMAL=""
 HAS_TTY='y'
 HAS_COLORS="n"
 
-VERSION="0.6"
+VERSION="0.7"
 
 BASENAME="$(basename ${0})"
 BASE_DIR="$(dirname ${0})"
 
 RESOLV_CONF="/etc/resolv.conf"
-DOMAIN=
 DATE_TPL="%Y-%m-%d_%H:%M:%S"
 
-declare -a SEARCH_DOMAINS=(
+declare -a DEFAULT_SEARCH_DOMAINS=(
     'pixelpark.net'
     'pixelpark.com'
 )
+declare -a SEARCH_DOMAINS=()
 
-declare -a NAME_SERVERS=(
+declare -a DEFAULT_NAME_SERVERS=(
     '217.66.52.10'
     '93.188.109.13'
     '212.91.225.75'
 )
+declare -a NAME_SERVERS=()
+
+DEFAULT_RESOLV_OPTIONS="timeout:1 attempts:2"
+UNSET_MARK="<<<unset>>>"
+RESOLV_OPTIONS="${UNSET_MARK}"
 
 #-------------------------------------------------------------------
 detect_color() {
@@ -104,13 +110,17 @@ description() {
     local i=
 
     echo "Generates a sane version of '${RESOLV_CONF}'."
+    cat <<-EOF
+               If the NetworkManager is used on the host, it generates the resolving options,
+               which are used by the NetworkManager after reboot to generate the '${RESOLV_CONF}'.
+               EOF
     echo
 
-    printf "Included are always the search domains: "
+    printf "If not given, the following search domains are used: "
     local domain=
     i=0
-    nr="${#SEARCH_DOMAINS[*]}"
-    for domain in "${SEARCH_DOMAINS[@]}" ; do
+    nr="${#DEFAULT_SEARCH_DOMAINS[*]}"
+    for domain in "${DEFAULT_SEARCH_DOMAINS[@]}" ; do
         i=$(( $i + 1 ))
         printf "'${domain}'"
         if [[ "${i}" != "${nr}" ]] ; then
@@ -119,11 +129,11 @@ description() {
     done
     echo
 
-    printf "As nameservers are always used: "
+    printf "If not given, the following nameservers are used: "
     local ns=
     i=0
-    nr="${#NAME_SERVERS[*]}"
-    for ns in "${NAME_SERVERS[@]}" ; do
+    nr="${#DEFAULT_NAME_SERVERS[*]}"
+    for ns in "${DEFAULT_NAME_SERVERS[@]}" ; do
         i=$(( $i + 1 ))
         printf "'${ns}'"
         if [[ "${i}" != "${nr}" ]] ; then
@@ -132,20 +142,30 @@ description() {
     done
     echo
 
+    printf "If not given, the following options for the resolv.conf are used: "
+    echo "${DEFAULT_RESOLV_OPTIONS}"
+
 }
 
 #------------------------------------------------------------------------------
 usage() {
     cat <<-EOF
-       Usage: ${BASENAME} [-d|--debug] [[-v|--verbose] | [-q|--quiet]]] [-b|--backup] [-D|--domain DOMAIN] [--nocolor]
+       Usage: ${BASENAME} [-d] [[-v] | [-q]]] [-b] [-s] [-S DOMAIN [-S DOMAIN ...]] [-N IP [-N IP ...]] [-O OPTIONS] [--nocolor]
               ${BASENAME} [-h|--help]
               ${BASENAME} [-V|--version]
 
            Options:
                -b|--backup     Creates a backup of an existing file
                                of the form '${RESOLV_CONF}.${DATE_TPL}.bak'.
-               -D|--domain DOMAIN
-                               Including the given domain at the first position in the search domains.
+               -S|--search DOMAIN [-S|--search DOMAIN ...]
+                               Use this domain as a search domain. Multiple search
+                               domains are allowed (and desired).
+               -N|--ns|--nameserver IPADRESS [-N|--ns|--nameserver IPADRESS ...]
+                               Use this IP address as an address of a responsible nameserver.
+                               Multiple addresses are allowed (and desired).
+               -O|--options OPTIONS
+                               Options for internal resolver variables, like timeout, rotate a.s.o.
+               -s|--simulate   Simulation mode, nothing is really done.
                -d|--debug      Debug output (bash -x).
                -v|--verbose    Set verbosity on. Mutually exclusive to '--quiet'.
                -q|--quiet      Quiet execution, only errors and warnings are shown.
@@ -160,11 +180,12 @@ get_options() {
 
     local tmp=
     local base_dir=
+    local short_options='bS:N:O:sdvqhV'
+    local long_options='backup,search:,ns:,nameserver:,options:,simulate,'
+    long_options+='debug,verbose,quiet,nocolor,help,version'
 
     set +e
-    tmp=$( getopt -o D:bdvqhV \
-                    --long domain:,backup,debug,verbose,quiet,nocolor,help,version \
-                    -n "${BASENAME}" -- "$@" )
+    tmp=$( getopt -o "${short_options}" --long "${long_options}" -n "${BASENAME}" -- "$@" )
     if [[ $? != 0 ]] ; then
         echo "" >&2
         usage >&2
@@ -176,22 +197,40 @@ get_options() {
     eval set -- "${tmp}"
 
     local p=
+    local dom=
+    local ns=
 
     while true ; do
         case "$1" in
-            -D|--domain)
-                DOMAIN=$( echo "$2" | tr '[:upper:]' '[:lower:]' )
+            -b|--backup)
+                DO_BACKUP="y"
+                shift
+                ;;
+            -S|--search)
+                dom=$( echo "$2" | tr '[:upper:]' '[:lower:]' )
+                SEARCH_DOMAINS+=("${dom}")
                 shift
                 shift
                 ;;
-            -b|--backup)
-                DO_BACKUP="y"
+            -N|--ns|--nameserver)
+                ns=$( echo "$2" | tr '[:upper:]' '[:lower:]' )
+                NAME_SERVERS+=("${ns}")
+                shift
+                shift
+                ;;
+            -O|--options)
+                RESOLV_OPTIONS=$( echo "$2" | sed -e 's/^[     ]*//' -e 's/[   ]*$//' )
+                shift
                 shift
                 ;;
             -d|--debug)
                 DEBUG="y"
                 shift
                 ;;
+            -s|--simulate)
+                SIMULATE="y"
+                shift
+                ;;
             -v|--verbose)
                 VERBOSE="y"
                 shift
@@ -236,6 +275,19 @@ get_options() {
         esac
     done
 
+    if [[ "${SIMULATE}" == "y" ]] ; then
+        local msg=$( cat <<-EOF
+
+                           ${CYAN}###############################${NORMAL}
+                           ${CYAN}#${NORMAL}       ${YELLOW}Simulation mode${NORMAL}       ${CYAN}#${NORMAL}
+                           ${CYAN}#${NORMAL} Nothing will be really done ${CYAN}#${NORMAL}
+                           ${CYAN}###############################${NORMAL}
+
+                       EOF
+        )
+        echo -e "${msg}"
+    fi
+
     if [[ "${DEBUG}" = "y" ]] ; then
         set -x
     fi
@@ -245,6 +297,24 @@ get_options() {
         exit 1
     fi
 
+    local dom=
+    if [[ "${#SEARCH_DOMAINS[*]}" == 0 ]] ; then
+        for dom in "${DEFAULT_SEARCH_DOMAINS[@]}" ; do
+            SEARCH_DOMAINS+=( "${dom}" )
+        done
+    fi
+
+    local ns=
+    if [[ "${#NAME_SERVERS[*]}" == 0 ]] ; then
+        for ns in "${DEFAULT_NAME_SERVERS[@]}" ; do
+            NAME_SERVERS+=( "${ns}" )
+        done
+    fi
+
+    if [[ "${RESOLV_OPTIONS}" == "${UNSET_MARK}" ]] ; then
+        RESOLV_OPTIONS="${DEFAULT_RESOLV_OPTIONS}"
+    fi
+
 }
 
 #########################################
@@ -295,7 +365,16 @@ CP() {
     if [[ "${VERBOSE}" == "y" ]] ; then
         cmd+=" --verbose"
     fi
-    eval ${cmd} "$@"
+    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
 }
 
 #------------------------------------------------------------------------------
@@ -304,7 +383,32 @@ MV() {
     if [[ "${VERBOSE}" == "y" ]] ; then
         cmd+=" --verbose"
     fi
-    eval ${cmd} "$@"
+    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
+}
+
+#------------------------------------------------------------------------------
+check_for_root() {
+
+    debug "Checking for execution as root ..."
+    local my_uid=$( id -u )
+    if [[ "${my_uid}" != 0 ]] ; then
+        empty_line
+        error "You must be ${RED}root${NORMAL} to execute this script!"
+        empty_line
+        if [[ "${SIMULATE}" != "y" ]] ; then
+            exit 5
+        fi
+    fi
+
 }
 
 #------------------------------------------------------------------------------
@@ -331,22 +435,13 @@ generate() {
 
     local content="# Generated by '${BASENAME}' version '${VERSION}' at: $( date --rfc-3339=seconds )"
     content+="
-"
 
-    content+="search "
-
-    if [[ -n "${DOMAIN}" ]] ; then
-        if [[ "${DOMAIN}" != 'pixelpark.com' && "${DOMAIN}" != 'pixelpark.com' ]] ; then
-            info "Including '${GREEN}${DOMAIN}${NORMAL}' as a search domain."
-            content+=$( printf "${DOMAIN} " )
-        else
-            warn "Domain '${YELLOW}${DOMAIN}${NORMAL}' is already a search domain."
-        fi
-    fi
+"
 
     local domain=
     local i=0
     local nr="${#SEARCH_DOMAINS[*]}"
+    content+="search "
     for domain in "${SEARCH_DOMAINS[@]}" ; do
         i=$(( $i + 1 ))
         content+=$( printf "${domain}" )
@@ -354,9 +449,9 @@ generate() {
             content+=$( printf " " )
         fi
     done
-    echo
     content+="
 "
+    echo
 
     local ns=
     i=0
@@ -365,6 +460,13 @@ generate() {
         content+="
 "
     done
+
+    if [[ -n "${RESOLV_OPTIONS}" ]] ; then
+        content+=$( echo "options ${RESOLV_OPTIONS}" )
+        content+="
+"
+    fi
+
     content+="
 "
 
@@ -374,7 +476,11 @@ generate() {
         printf "${content}"
     fi
 
-    printf "${content}" >"${RESOLV_CONF}"
+    if [[ "${SIMULATE}" == "y" ]] ; then
+        info "Simulating writing into '${RESOLV_CONF}'"
+    else
+        printf "${content}" >"${RESOLV_CONF}"
+    fi
 
 }
 
@@ -389,6 +495,7 @@ generate() {
 main() {
 
     get_options "$@"
+    check_for_root
     do_backup
     generate