Skip to content

Commit

Permalink
Workaround issue with mDNS where reboots effect hostname and broadcast.
Browse files Browse the repository at this point in the history
  • Loading branch information
craighurley committed Feb 24, 2019
1 parent 401457f commit 5017bba
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 27 deletions.
22 changes: 11 additions & 11 deletions Vagrantfile
Expand Up @@ -8,15 +8,15 @@ boxes = YAML.load_file('./boxes.yaml')

# API version
VAGRANTFILE_API_VERSION = 2
PROJECT_NAME = '/vagrant/' + File.basename(Dir.getwd)
DEFAULT_BASE_BOX = 'centos/7'

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
boxes.each do |boxes|
config.vm.define boxes['name'] do |srv|
# OS and hostname
srv.vm.box = boxes['box']
if boxes['box_version']
srv.vm.box_version = boxes['box_version']
end
srv.vm.box = boxes['box'] ||= DEFAULT_BASE_BOX
srv.vm.box_url = boxes['box_url'] if boxes.key? 'box_url'
srv.vm.hostname = boxes['name']

# Networking. By default a NAT interface is added.
Expand All @@ -39,19 +39,19 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
srv.vm.synced_folder '.', '/vagrant', disabled: true

srv.vm.provider 'virtualbox' do |vb|
vb.customize ['modifyvm', :id, '--cpus', boxes['cpus']]
vb.customize ['modifyvm', :id, '--cpuexecutioncap', boxes['cpu_execution_cap']]
vb.customize ['modifyvm', :id, '--memory', boxes['ram']]
vb.customize ['modifyvm', :id, '--name', boxes['name']]
vb.customize ['modifyvm', :id, '--description', boxes['description']]
vb.customize ['modifyvm', :id, '--groups', '/vagrant']
vb.customize ['modifyvm', :id, '--cpus', boxes['cpus']] if boxes.key? 'cpus'
vb.customize ['modifyvm', :id, '--cpuexecutioncap', boxes['cpu_execution_cap']] if boxes.key? 'cpuexecutioncap'
vb.customize ['modifyvm', :id, '--memory', boxes['ram']] if boxes.key? 'memory'
vb.customize ['modifyvm', :id, '--name', boxes['name']] if boxes.key? 'name'
vb.customize ['modifyvm', :id, '--description', boxes['description']] if boxes.key? 'description'
vb.customize ['modifyvm', :id, '--groups', PROJECT_NAME]
end

# Copy cloud-init files to tmp and provision
if boxes['provision']
srv.vm.provision :file, :source => boxes['provision']['meta-data'], :destination => '/tmp/vagrant/cloud-init/nocloud-net/meta-data'
srv.vm.provision :file, :source => boxes['provision']['user-data'], :destination => '/tmp/vagrant/cloud-init/nocloud-net/user-data'
srv.vm.provision :shell, :path => boxes['provision']['cloud-init']
srv.vm.provision :shell, :path => boxes['provision']['cloud-init'], :args => boxes['name']
end
end
end
Expand Down
8 changes: 2 additions & 6 deletions boxes.yaml
Expand Up @@ -2,10 +2,8 @@
- name: haproxy.local
description: haproxy node
box: centos/7
cpus: 2
cpu_execution_cap: 75
cpus: 1
ram: 1024
ssh_port: 2201
provision:
meta-data: ./cloud-init/nocloud-net/meta-data.yaml
user-data: ./cloud-init/nocloud-net/user-data-haproxy.yaml
Expand All @@ -14,10 +12,8 @@
- name: frontend.local
description: frontend node
box: centos/7
cpus: 2
cpu_execution_cap: 75
cpus: 1
ram: 1024
ssh_port: 2202
provision:
meta-data: ./cloud-init/nocloud-net/meta-data.yaml
user-data: ./cloud-init/nocloud-net/user-data-frontend.yaml
Expand Down
4 changes: 0 additions & 4 deletions cloud-init/nocloud-net/user-data-frontend.yaml
@@ -1,9 +1,5 @@
#cloud-config

hostname: frontend
fqdn: frontend.local
manage_etc_hosts: true

# set the locale
locale: en_NZ.UTF-8

Expand Down
4 changes: 0 additions & 4 deletions cloud-init/nocloud-net/user-data-haproxy.yaml
@@ -1,9 +1,5 @@
#cloud-config

hostname: haproxy
fqdn: haproxy.local
manage_etc_hosts: true

# set the locale
locale: en_NZ.UTF-8

Expand Down
23 changes: 21 additions & 2 deletions scripts/cloud-init.sh
Expand Up @@ -13,15 +13,34 @@ USER_DATA=/tmp/vagrant/cloud-init/nocloud-net/user-data
# check if vagrant_provision has run before
[[ -f $SUCCESS_INDICATOR ]] && exit 0

# install required packages, including cloud-init
yum install -y epel-release
yum install -y cloud-init avahi nss-mdns

# HACK: mDNS has an issue where other clients cannot resolve this host after vagrant halt/suspend
hostname "$1"

# enable Multicast DNS
sed -i.bak -e 's/^hosts:.*/hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4/g' /etc/nsswitch.conf
systemctl start avahi-daemon
systemctl restart avahi-daemon
systemctl enable avahi-daemon

# HACK: mDNS has an issue where other clients cannot resolve this host after vagrant halt/suspend
cat << EOF > /etc/NetworkManager/dispatcher.d/ifup-local
#!/bin/sh
case "\$1" in
eth*)
# Record event in /var/log/messages
logger "\$1 has come up... resetting hostname to $1 and restarting avahi-daemon.service - this is a hack"
hostname "$1"
systemctl restart avahi-daemon.service
;;
esac
exit 0
EOF
chmod 700 /etc/NetworkManager/dispatcher.d/ifup-local

# write cloud-init files
mkdir -p $DATA_SOURCE
[[ -f $META_DATA ]] && cp $META_DATA $DATA_SOURCE/ || exit 1
Expand Down

0 comments on commit 5017bba

Please sign in to comment.