filesystem: "root"
mode: 0755
path: "/etc/NetworkManager/dispatcher.d/90-long-hostname"
contents:
  inline: |
    #!/bin/bash
    #
    # 90-long-hostname is a wrapper around /usr/local/sbin/set-valid-hostname.sh,
    # which ensures that a node has a valid hostname.
    IF=$1
    STATUS=$2

    log() { logger --tag "network-manager/$(basename $0)" "${@}"; }

    if [[ ! "$STATUS" =~ (up|hostname|dhcp4-change|dhcp6-change) ]]; then
        exit 0
    fi

    if [[ ! "$(< /proc/sys/kernel/hostname)" =~ (localhost|localhost.localdomain) ]]; then
        log "hostname is already set"
        exit 0
    fi

    # source the script since NetworkManager execution rules do
    # allow sourcing from /usr/local. RHCOS has an read-only rootfs
    # which limits where this can be stashed.
    source /usr/local/sbin/set-valid-hostname.sh
    host_name="${DHCP4_HOST_NAME:-$DHCP6_HOST_NAME}"

    if [ -n "${host_name}" ]; then
        set_valid_hostname "${host_name}"
    fi

