]> Frank Brehm's Git Trees - pixelpark/create-terraform.git/commitdiff
Adding management of hostname to postinstall-scripts/fix-etc-hosts
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 15 Aug 2024 12:11:51 +0000 (14:11 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 15 Aug 2024 12:11:51 +0000 (14:11 +0200)
postinstall-scripts/fix-etc-hosts

index 293b216d7fbbd3ccf91809f59e2abb8f8afa2588..5fc9003c9d9accc037e7c6435d3e0445fcd3a69d 100755 (executable)
@@ -20,13 +20,12 @@ detect_color
 IP=
 HAS_IPV6=
 NO_IPV6=
+LOCATION=
 
 REAL_HOSTSFILE='/etc/hosts'
 FAKE_HOSTSFILE='/tmp/hosts'
 USED_HOSTSFILE="${REAL_HOSTSFILE}"
 
-SIMULATE="y"
-
 #------------------------------------------------------------------------------
 get_ip() {
     local iface=
@@ -51,7 +50,18 @@ has_ipv6() {
     fi
 }
 
+#------------------------------------------------------------------------------
+get_os_major_rerlease() {
+
+    (
+        source /etc/os-release
+        echo "${VERSION_ID}" | sed -e 's/\..*//'
+    )
+
+}
+
 
+OS_MAJOR_RELEASE=$( get_os_major_rerlease )
 NODENAME=$( hostname --short )
 FQDN=$( hostname --fqdn )
 get_ip
@@ -67,7 +77,7 @@ DESCRIPTION=$( cat <<-EOF
 #------------------------------------------------------------------------------
 usage() {
     cat <<-EOF
-       Usage:  ${BASE_NAME} ${STD_USAGE} [-N|--no-ipv6]
+       Usage:  ${BASE_NAME} ${STD_USAGE} [-N|--no-ipv6] [-L|--location LOCATION]
                ${BASE_NAME} [-h|--help]
                ${BASE_NAME} [-V|--version]
 
@@ -82,8 +92,8 @@ usage() {
 get_options() {
 
     local tmp=
-    local short_options="N${STD_SHORT_OPTIONS}"
-    local long_options="no-ipv6,${STD_LONG_OPTIONS}"
+    local short_options="NL:${STD_SHORT_OPTIONS}"
+    local long_options="no-ipv6,location;,${STD_LONG_OPTIONS}"
 
     set +e
     tmp=$( getopt -o "${short_options}" --long "${long_options}" -n "${BASE_NAME}" -- "$@" )
@@ -115,6 +125,11 @@ get_options() {
                 NO_IPV6='y'
                 i=$(( i + 1 ))
                 ;;
+            -L|--location)
+                j=$(( $i + 1 ))
+                LOCATION="${REMAINING_OPTS[$j]}"
+                i=$(( $i + 2 ))
+                ;;
             *)  echo -e "Internal error - option '${RED}${arg}${NORMAL} was wrong!"
                 exit 1
                 ;;
@@ -174,6 +189,47 @@ generate() {
 
 }
 
+#------------------------------------------------------------------------------
+manage_hostname() {
+
+    empty_line
+    info "Setting hostname to '${CYAN}${NODENAME}${NORMAL}' ..."
+
+    local cmd
+
+    cmd="hostnamectl hostname \"${NODENAME}\""
+    if [[ "${SIMULATE}" == "y" ]] ; then
+        info "Simulated excuting: ${cmd}"
+    else
+        debug "Excuting: ${cmd}"
+        eval ${cmd}
+    fi
+
+    if [[ -n "${LOCATION}" ]] ; then
+
+        empty_line
+        info "Setting location to '${CYAN}${LOCATION}${NORMAL}' ..."
+
+        cmd="hostnamectl location \"${LOCATION}\""
+        if [[ "${SIMULATE}" == "y" ]] ; then
+            info "Simulated excuting: ${cmd}"
+        else
+            debug "Excuting: ${cmd}"
+            eval ${cmd}
+        fi
+
+    fi
+
+    if [[ "${QUIET}" != 'y' ]] ; then
+        empty_line
+        info "Current status of hostnamectl:"
+        cmd="hostnamectl status"
+        debug "Excuting: ${cmd}"
+        eval ${cmd}
+    fi
+
+}
+
 ################################################################################
 ##
 ## Main
@@ -190,6 +246,10 @@ main() {
 
     generate
 
+    if [[ "${OS_MAJOR_RELEASE}" -ge 9 ]] ; then
+        manage_hostname
+    fi
+
 }
 
 main "$@"