|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +NSS_MDNS=$(dpkg -s libnss-mdns | grep Version: \ |
| 4 | + | cut -d: -f2 | cut -d- -f1 | tr -d ' ') |
| 5 | + |
| 6 | +if [ "${NSS_MDNS}" != '0.10' ]; then |
| 7 | + # After nss-mdns >0.10 we need to reconfigure the allowed hosts to support |
| 8 | + # multiple sub-domain resolution |
| 9 | + cat > /etc/mdns.allow <<EOF |
| 10 | +.local. |
| 11 | +.local |
| 12 | +EOF |
| 13 | + |
| 14 | + # And we need to make use of the +mdns+ nss module, not the minimal one so |
| 15 | + # that the above configuration will be used (see: |
| 16 | + # https://github.com/lathiat/nss-mdns) |
| 17 | + sed -i 's/mdns4_minimal/mdns4/' /etc/nsswitch.conf |
| 18 | +fi |
| 19 | + |
| 20 | +# Configure the mDNS hostname on avahi |
| 21 | +if [ -n "${MDNS_HOSTNAME}" ]; then |
| 22 | + |
| 23 | + # MDNS_HOSTNAME could be node.local or node.sub.local |
| 24 | + IFS='.' read -ra MDNS_HOSTNAME_PARTS <<< "${MDNS_HOSTNAME}" |
| 25 | + |
| 26 | + # Save the first part as host part |
| 27 | + HOST_PART="${MDNS_HOSTNAME_PARTS[0]}" |
| 28 | + |
| 29 | + # Shift the first part |
| 30 | + MDNS_HOSTNAME_PARTS=("${MDNS_HOSTNAME_PARTS[@]:1}") |
| 31 | + |
| 32 | + # Join the rest to the domain part |
| 33 | + DOMAIN_PART=$(IFS='.'; echo "${MDNS_HOSTNAME_PARTS[*]}") |
| 34 | + |
| 35 | + # Set the host and domain part on the avahi config |
| 36 | + sed \ |
| 37 | + -e "s/.*\(host-name=\).*/\1${HOST_PART}/g" \ |
| 38 | + -e "s/.*\(domain-name=\).*/\1${DOMAIN_PART}/g" \ |
| 39 | + -e "s/.*\(enable-dbus=\).*/\1yes/g" \ |
| 40 | + -i /etc/avahi/avahi-daemon.conf |
| 41 | + |
| 42 | + echo "Configured mDNS hostname to ${MDNS_HOSTNAME}" |
| 43 | +fi |
| 44 | + |
| 45 | +# Configure all mDNS CNAMEs on avahi |
| 46 | +if [ -n "${MDNS_CNAMES}" ]; then |
| 47 | + |
| 48 | + # MDNS_CNAMES could be a single domain, or a comma-separated list |
| 49 | + IFS=',' read -ra CNAMES <<< "${MDNS_CNAMES}" |
| 50 | + |
| 51 | + for CNAME in "${CNAMES[@]}"; do |
| 52 | + # Construct the command |
| 53 | + COMMAND='/usr/bin/avahi-publish -f -a -R' |
| 54 | + COMMAND+=" \"${CNAME}\" \`hostname -i\`" |
| 55 | + |
| 56 | + # Write a new supervisord unit file |
| 57 | + cat > "/etc/supervisor/conf.d/${CNAME}.conf" <<EOF |
| 58 | +[program:${CNAME}] |
| 59 | +priority=20 |
| 60 | +directory=/tmp |
| 61 | +command=/bin/sh -c '${COMMAND}' |
| 62 | +user=root |
| 63 | +autostart=false |
| 64 | +autorestart=true |
| 65 | +stopsignal=KILL |
| 66 | +stopwaitsecs=1 |
| 67 | +EOF |
| 68 | + |
| 69 | + # Reload the supervisord config files and start |
| 70 | + # the current publish service |
| 71 | + supervisorctl update |
| 72 | + supervisorctl start "${CNAME}" |
| 73 | + done |
| 74 | +fi |
| 75 | + |
| 76 | +# Disable the rlimits from default debian |
| 77 | +sed \ |
| 78 | + -e 's/^\(rlimit\)/#\1/g' \ |
| 79 | + -i /etc/avahi/avahi-daemon.conf |
| 80 | + |
| 81 | +# If a avahi daemon is running, kill it |
| 82 | +avahi-daemon -c && avahi-daemon -k |
| 83 | + |
| 84 | +# Clean up orphans |
| 85 | +rm -rf /run/avahi-daemon/{pid,socket} |
| 86 | + |
| 87 | +# Start avahi |
| 88 | +exec avahi-daemon --no-rlimits |
0 commit comments