github.com/hernad/nomad@v1.6.112/e2e/terraform/packer/ubuntu-jammy-amd64/dnsconfig.sh (about)

     1  #!/usr/bin/env bash
     2  # Copyright (c) HashiCorp, Inc.
     3  # SPDX-License-Identifier: MPL-2.0
     4  
     5  set -e
     6  
     7  # These tasks can't be executed during AMI builds because they rely on
     8  # instance-specific data.
     9  
    10  mkdir -p /var/run/dnsmasq
    11  mkdir -p /etc/dnsmasq.d
    12  
    13  # Add hostname to /etc/hosts
    14  echo "127.0.0.1 $(hostname)" | tee --append /etc/hosts
    15  
    16  # this script should run after docker.service but we can't guarantee
    17  # it's created docker0 yet, so wait to make sure
    18  while ! (ip link | grep -q docker0)
    19  do
    20      sleep 1
    21  done
    22  
    23  # Use dnsmasq first and then docker bridge network for DNS resolution
    24  DOCKER_BRIDGE_IP_ADDRESS=$(/usr/local/bin/sockaddr eval 'GetInterfaceIP "docker0"')
    25  cat <<EOF > /tmp/resolv.conf
    26  nameserver 127.0.0.1
    27  nameserver $DOCKER_BRIDGE_IP_ADDRESS
    28  EOF
    29  cp /tmp/resolv.conf /etc/resolv.conf
    30  
    31  # need to get the interface for dnsmasq config so that we can
    32  # accomodate both "predictable" and old-style interface names
    33  IFACE=$(/usr/local/bin/sockaddr eval 'GetDefaultInterfaces | attr "Name"')
    34  
    35  cat <<EOF > /tmp/dnsmasq
    36  port=53
    37  resolv-file=/var/run/dnsmasq/resolv.conf
    38  bind-interfaces
    39  interface=docker0
    40  interface=lo
    41  interface=$IFACE
    42  listen-address=127.0.0.1
    43  server=/consul/127.0.0.1#8600
    44  EOF
    45  cp /tmp/dnsmasq /etc/dnsmasq.d/default
    46  
    47  # need to get the AWS DNS address from the VPC...
    48  # this is pretty hacky but will work for any typical case
    49  MAC=$(curl -s --fail http://169.254.169.254/latest/meta-data/mac)
    50  CIDR_BLOCK=$(curl -s --fail "http://169.254.169.254/latest/meta-data/network/interfaces/macs/$MAC/vpc-ipv4-cidr-block")
    51  VPC_DNS_ROOT=$(echo "$CIDR_BLOCK" | cut -d'.' -f1-3)
    52  echo "nameserver ${VPC_DNS_ROOT}.2" > /tmp/dnsmasq-resolv.conf
    53  cp /tmp/dnsmasq-resolv.conf /var/run/dnsmasq/resolv.conf
    54  
    55  /usr/sbin/dnsmasq --test