Skip to content

Instantly share code, notes, and snippets.

@mgsisk
Last active January 12, 2023 20:06
Show Gist options
  • Save mgsisk/50956eeef7c56a0ca0378e2637d6ea28 to your computer and use it in GitHub Desktop.
Save mgsisk/50956eeef7c56a0ca0378e2637d6ea28 to your computer and use it in GitHub Desktop.
Vagrant triggers for updating host hosts
Vagrant.configure('2') do |config|
ENV['ZONE'] ||= 'America/Detroit'
config.vm.box = 'debian/contrib-buster64'
config.vm.hostname = File.basename(Dir.pwd) + '.test'
config.vm.network 'private_network', type: 'dhcp'
config.vm.provision 'shell', path: 'https://github.com/mgsisk/providence/releases/download/v0.1.6/provisioner.sh'
conf.trigger.before :destroy, :halt, :reload, :suspend do |t|
t.info = 'Updating system hosts...'
t.ruby do |env, vm|
command = "sudo sed -i '' '/ # vagrant-#{vm.id}$/d' /etc/hosts"
command = "type %WINDIR%\\system32\\drivers\\etc\\hosts | findstr /e /v /c:vagrant-#{vm.id}>prov-hosts && move /Y prov-hosts %WINDIR%\\system32\\drivers\\etc\\hosts" if Vagrant::Util::Platform.windows?
system(command)
end
end
conf.trigger.after :reload, :resume, :up do |t|
t.info = 'Updating sytstem hosts...'
t.ruby do |env, vm|
hostname = `vagrant ssh #{vm.name} -c 'hostname -f' -- -q`.chomp
ip_address = `vagrant ssh #{vm.name} -c 'hostname -I | cut -d" " -f2' -- -q`.chomp
command = "echo '#{ip_address} #{hostname} sys.#{hostname} # vagrant-#{vm.id}' | sudo tee -a /etc/hosts >/dev/null"
command = "echo #{ip_address} #{hostname} sys.#{hostname} # vagrant-#{vm.id}>> %WINDIR%\\system32\\drivers\\etc\\hosts" if Vagrant::Util::Platform.windows?
system(command)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment