vaguerent/plugins/guests/netbsd/cap/configure_networks.rb
Seth Vargo 5a4f345363
Use Util::Tempfile when configuring networks
This fixes a fairly large tempfile leak. Vagrant uses a template
renderer to write network configuration files locally to disk. Then,
that temporarily file is uploaded to the remote host and moved into
place. Since Vagrant is such a short-lived process, GC never came along
and cleaned up those tempfiles, resulting in many temporary files being
created through regular Vagrant usage.

The Util::Tempfile class uses a block to ensure the temporary file is
deleted when the block finishes. This API required small tweaks to the
usage, but provides more safety to ensure the files are deleted.
2016-05-28 23:22:34 -04:00

52 lines
1.9 KiB
Ruby

require_relative "../../../../lib/vagrant/util/template_renderer"
require_relative "../../../../lib/vagrant/util/tempfile"
module VagrantPlugins
module GuestNetBSD
module Cap
class ConfigureNetworks
include Vagrant::Util
def self.configure_networks(machine, networks)
# setup a new rc.conf file
newrcconf = "/tmp/rc.conf.vagrant_configurenetworks"
machine.communicate.sudo("sed -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/rc.conf > #{newrcconf}")
networks.each do |network|
# create an interface configuration file fragment
entry = TemplateRenderer.render("guests/netbsd/network_#{network[:type]}",
options: network)
Tempfile.create("netbsd-configure-networks") do |f|
f.write(entry)
f.fsync
f.close
machine.communicate.upload(f.path, "/tmp/vagrant-network-entry")
end
machine.communicate.sudo("cat /tmp/vagrant-network-entry >> #{newrcconf}")
machine.communicate.sudo("rm -f /tmp/vagrant-network-entry")
ifname = "wm#{network[:interface]}"
# remove old configuration
machine.communicate.sudo("/sbin/dhcpcd -x #{ifname}", { error_check: false })
machine.communicate.sudo("/sbin/ifconfig #{ifname} inet delete", { error_check: false })
# live new configuration
if network[:type].to_sym == :static
machine.communicate.sudo("/sbin/ifconfig #{ifname} media autoselect up;/sbin/ifconfig #{ifname} inet #{network[:ip]} netmask #{network[:netmask]}")
elsif network[:type].to_sym == :dhcp
machine.communicate.sudo("/sbin/dhcpcd -n -q #{ifname}")
end
end
# install new rc.conf
machine.communicate.sudo("install -c -o 0 -g 0 -m 644 #{newrcconf} /etc/rc.conf")
end
end
end
end
end