]> Frank Brehm's Git Trees - pixelpark/create-terraform.git/commitdiff
Adding postinstall-scripts/fix-etc-hosts
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 21 Jun 2024 13:35:42 +0000 (15:35 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 21 Jun 2024 13:35:42 +0000 (15:35 +0200)
postinstall-scripts/create-motd
postinstall-scripts/fix-etc-hosts [new file with mode: 0755]

index de758c696bac73cba86253c098451ac2af8a582d..c63e9047e43642f87dbe0aa88dbf3b1c217d2972 100755 (executable)
@@ -35,10 +35,10 @@ get_ip() {
 
 }
 
-NODENAME=$(hostname -s)
+NODENAME=$( hostname --short )
 PURPOSE="Customer project"
 FQDN=$( hostname --fqdn )
-DOMAIN=$(hostname --fqdn | cut -d. -f 2- )
+DOMAIN=$( hostname --fqdn | cut -d. -f 2- )
 LOCATION="L105 / VMWare"
 get_ip
 HARDWARE=$( uname -p )
@@ -70,9 +70,9 @@ DESCRIPTION=$( cat <<-EOF
 #------------------------------------------------------------------------------
 usage() {
     cat <<-EOF
-       Usage: ${BASE_NAME} [Options ...]
-              ${BASE_NAME} [-h|--help]
-              ${BASE_NAME} [-V|--version]
+       Usage:  ${BASE_NAME} [Options ...]
+               ${BASE_NAME} [-h|--help]
+               ${BASE_NAME} [-V|--version]
 
            Options:
                -n|--node NODENAME
diff --git a/postinstall-scripts/fix-etc-hosts b/postinstall-scripts/fix-etc-hosts
new file mode 100755 (executable)
index 0000000..293b216
--- /dev/null
@@ -0,0 +1,199 @@
+#!/bin/bash
+
+set -e
+set -u
+
+BASE_NAME="$( basename ${0} )"
+MY_REAL_NAME=$( readlink -f $0 )
+BIN_DIR=$( dirname "${MY_REAL_NAME}" )
+BASE_DIR=$( dirname "${BIN_DIR}" )
+
+if [[ -f "${BIN_DIR}/functions.rc" ]] ; then
+    . "${BIN_DIR}/functions.rc"
+else
+    echo "Bash resource file '${BIN_DIR}/functions.rc' not found" >&2
+    exit 5
+fi
+
+detect_color
+
+IP=
+HAS_IPV6=
+NO_IPV6=
+
+REAL_HOSTSFILE='/etc/hosts'
+FAKE_HOSTSFILE='/tmp/hosts'
+USED_HOSTSFILE="${REAL_HOSTSFILE}"
+
+SIMULATE="y"
+
+#------------------------------------------------------------------------------
+get_ip() {
+    local iface=
+
+    IP=
+
+    for iface in $( ip -o link show | tail -n +2 | awk '{print $2}' | sed -e 's/:$//' ); do
+
+        IP=$( ip -o addr show dev ${iface} | grep -w inet | head -n 1 | awk '{print $4}' | sed -e 's|/.*||' )
+        if [[ -n "${IP}" ]] ; then
+            break
+        fi
+
+    done
+
+}
+
+#------------------------------------------------------------------------------
+has_ipv6() {
+    if ip -o addr show dev lo | grep -q -w inet6 ; then
+        HAS_IPV6='yes'
+    fi
+}
+
+
+NODENAME=$( hostname --short )
+FQDN=$( hostname --fqdn )
+get_ip
+has_ipv6
+
+#------------------------------------------------------------------------------
+DESCRIPTION=$( cat <<-EOF
+       Writes valid lines in /etc/hosts.
+
+       EOF
+)
+
+#------------------------------------------------------------------------------
+usage() {
+    cat <<-EOF
+       Usage:  ${BASE_NAME} ${STD_USAGE} [-N|--no-ipv6]
+               ${BASE_NAME} [-h|--help]
+               ${BASE_NAME} [-V|--version]
+
+           Options:
+               -N|--no-ipv6    Don't insert a line for '::1' in /etc/hosts, even if IPv6 seems to be enabled.
+       EOF
+
+       echo "${STD_USAGE_MSG}"
+}
+
+#------------------------------------------------------------------------------
+get_options() {
+
+    local tmp=
+    local short_options="N${STD_SHORT_OPTIONS}"
+    local long_options="no-ipv6,${STD_LONG_OPTIONS}"
+
+    set +e
+    tmp=$( getopt -o "${short_options}" --long "${long_options}" -n "${BASE_NAME}" -- "$@" )
+    if [[ $? != 0 ]] ; then
+        echo "" >&2
+        usage >&2
+        exit 1
+    fi
+    set -e
+
+    # Note the quotes around `$TEMP': they are essential!
+    eval set -- "${tmp}"
+    eval_common_options "$@"
+    if [[ "${DEBUG}" == 'y' ]] ; then
+        declare -p REMAINING_OPTS
+        declare -p REMAINING_ARGS
+    fi
+
+    local len="${#REMAINING_OPTS[*]}"
+    local i="0"
+    local j=
+    local arg=
+    while [[ "$i" -lt "${len}" ]] ; do
+
+        arg="${REMAINING_OPTS[$i]}"
+
+        case "${arg}" in
+            -N|--no-ipv6)
+                NO_IPV6='y'
+                i=$(( i + 1 ))
+                ;;
+            *)  echo -e "Internal error - option '${RED}${arg}${NORMAL} was wrong!"
+                exit 1
+                ;;
+        esac
+    done
+
+    if [[ "${SIMULATE}" == "y" ]] ; then
+        local msg=$( cat <<-EOF
+
+                       Using '${CYAN}${FAKE_HOSTSFILE}${NORMAL}' instead of '${CYAN}${REAL_HOSTSFILE}${NORMAL}'.
+                       EOF
+        )
+        echo -e "${msg}"
+        USED_HOSTSFILE="${FAKE_HOSTSFILE}"
+        empty_line
+    fi
+
+    check_for_root
+
+}
+
+#------------------------------------------------------------------------------
+
+generate() {
+
+    local tabs=
+    local len_ip=
+
+    empty_line
+    info "Generating new '${CYAN}${USED_HOSTSFILE}${NORMAL}' ..."
+
+    echo "127.0.0.1    localhost localhost.localdomain localhost4 localhost4.localdomain4" > "${USED_HOSTSFILE}"
+
+    if [[ "${HAS_IPV6}" && -z "${NO_IPV6}" ]] ; then
+        echo "::1              localhost localhost.localdomain localhost6 localhost6.localdomain6" >> "${USED_HOSTSFILE}"
+    fi
+
+    echo '' >> "${USED_HOSTSFILE}"
+
+    if [[ "${IP}" ]] ; then
+        tabs=' '
+        len_ip=$( printf "${IP}" | wc -c )
+        if [[ "${len_ip}" -lt 8 ]] ; then
+            tabs+="    "
+        fi
+
+        echo "${IP}${tabs}${FQDN} ${NODENAME}" >> "${USED_HOSTSFILE}"
+
+    fi
+
+    if [[ "${QUIET}" != 'y' ]] ; then
+        empty_line
+        info "Generated '${CYAN}${USED_HOSTSFILE}${NORMAL}':"
+        cat "${USED_HOSTSFILE}"
+        echo "<--------"
+    fi
+
+}
+
+################################################################################
+##
+## Main
+##
+################################################################################
+
+#------------------------------------------------------------------------------
+main() {
+
+    get_options "$@"
+    set_locale "en_US.utf8"
+
+    empty_line >&2
+
+    generate
+
+}
+
+main "$@"
+
+exit 0
+
+# vim: list