From 2c362d4d2854aa89b7598f2d6d22582ba9392199 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 4 Apr 2013 11:56:42 -0700 Subject: [PATCH] FreeBSD capabilities --- .../guests/freebsd/cap/change_host_name.rb | 14 +++++ .../guests/freebsd/cap/configure_networks.rb | 39 ++++++++++++++ plugins/guests/freebsd/cap/halt.rb | 16 ++++++ .../guests/freebsd/cap/mount_nfs_folder.rb | 14 +++++ plugins/guests/freebsd/guest.rb | 54 ------------------- plugins/guests/freebsd/plugin.rb | 20 +++++++ 6 files changed, 103 insertions(+), 54 deletions(-) create mode 100644 plugins/guests/freebsd/cap/change_host_name.rb create mode 100644 plugins/guests/freebsd/cap/configure_networks.rb create mode 100644 plugins/guests/freebsd/cap/halt.rb create mode 100644 plugins/guests/freebsd/cap/mount_nfs_folder.rb diff --git a/plugins/guests/freebsd/cap/change_host_name.rb b/plugins/guests/freebsd/cap/change_host_name.rb new file mode 100644 index 000000000..513a450bd --- /dev/null +++ b/plugins/guests/freebsd/cap/change_host_name.rb @@ -0,0 +1,14 @@ +module VagrantPlugins + module GuestFreeBSD + module Cap + class ChangeHostName + def self.change_host_name(machine, name) + if !machine.communicate.test("hostname -f | grep '^#{name}$' || hostname -s | grep '^#{name}$'") + machine.communicate.sudo("sed -i '' 's/^hostname=.*$/hostname=#{name}/' /etc/rc.conf") + machine.communicate.sudo("hostname #{name}") + end + end + end + end + end +end diff --git a/plugins/guests/freebsd/cap/configure_networks.rb b/plugins/guests/freebsd/cap/configure_networks.rb new file mode 100644 index 000000000..daca21cfa --- /dev/null +++ b/plugins/guests/freebsd/cap/configure_networks.rb @@ -0,0 +1,39 @@ +require "tempfile" + +require "vagrant/util/template_renderer" + +module VagrantPlugins + module GuestFreeBSD + module Cap + class ConfigureNetworks + extend Vagrant::Util + + def self.configure_networks(machine, networks) + # Remove any previous network additions to the configuration file. + machine.communicate.sudo("sed -i '' -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/rc.conf") + + networks.each do |network| + entry = TemplateRenderer.render("guests/freebsd/network_#{network[:type]}", + :options => network) + + # Write the entry to a temporary location + temp = Tempfile.new("vagrant") + temp.binmode + temp.write(entry) + temp.close + + machine.communicate.upload(temp.path, "/tmp/vagrant-network-entry") + machine.communicate.sudo("su -m root -c 'cat /tmp/vagrant-network-entry >> /etc/rc.conf'") + machine.communicate.sudo("rm /tmp/vagrant-network-entry") + + if network[:type].to_sym == :static + machine.communicate.sudo("ifconfig em#{network[:interface]} inet #{network[:ip]} netmask #{network[:netmask]}") + elsif network[:type].to_sym == :dhcp + machine.communicate.sudo("dhclient em#{network[:interface]}") + end + end + end + end + end + end +end diff --git a/plugins/guests/freebsd/cap/halt.rb b/plugins/guests/freebsd/cap/halt.rb new file mode 100644 index 000000000..938add189 --- /dev/null +++ b/plugins/guests/freebsd/cap/halt.rb @@ -0,0 +1,16 @@ +module VagrantPlugins + module GuestFreeBSD + module Cap + class Halt + def self.halt(machine) + begin + machine.communicate.sudo("shutdown -p now") + rescue IOError + # Do nothing because SSH connection closed and it probably + # means the VM just shut down really fast. + end + end + end + end + end +end diff --git a/plugins/guests/freebsd/cap/mount_nfs_folder.rb b/plugins/guests/freebsd/cap/mount_nfs_folder.rb new file mode 100644 index 000000000..bcd675887 --- /dev/null +++ b/plugins/guests/freebsd/cap/mount_nfs_folder.rb @@ -0,0 +1,14 @@ +module VagrantPlugins + module GuestFreeBSD + module Cap + class MountNFSFolder + def self.mount_nfs_folder(machine, ip, folders) + folders.each do |name, opts| + machine.communicate.sudo("mkdir -p #{opts[:guestpath]}") + machine.communicate.sudo("mount '#{ip}:#{opts[:hostpath]}' '#{opts[:guestpath]}'") + end + end + end + end + end +end diff --git a/plugins/guests/freebsd/guest.rb b/plugins/guests/freebsd/guest.rb index c5ff14951..faaff332e 100644 --- a/plugins/guests/freebsd/guest.rb +++ b/plugins/guests/freebsd/guest.rb @@ -6,25 +6,11 @@ module VagrantPlugins # # Contributed by Kenneth Vestergaard class Guest < Vagrant.plugin("2", :guest) - # Here for whenever it may be used. - class FreeBSDError < Vagrant::Errors::VagrantError - error_namespace("vagrant.guest.freebsd") - end - def detect?(machine) # TODO: FreeBSD detection false end - def halt - begin - vm.communicate.sudo("shutdown -p now") - rescue IOError - # Do nothing because SSH connection closed and it probably - # means the VM just shut down really fast. - end - end - # TODO: vboxsf is currently unsupported in FreeBSD, if you are able to # help out with this project, please contact vbox@FreeBSD.org # @@ -35,46 +21,6 @@ module VagrantPlugins # ssh.exec!("sudo mount -t vboxfs v-root #{guestpath}") # ssh.exec!("sudo chown #{vm.config.ssh.username} #{guestpath}") # end - - def mount_nfs(ip, folders) - folders.each do |name, opts| - vm.communicate.sudo("mkdir -p #{opts[:guestpath]}") - vm.communicate.sudo("mount '#{ip}:#{opts[:hostpath]}' '#{opts[:guestpath]}'") - end - end - - def configure_networks(networks) - # Remove any previous network additions to the configuration file. - vm.communicate.sudo("sed -i '' -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/rc.conf") - - networks.each do |network| - entry = TemplateRenderer.render("guests/freebsd/network_#{network[:type]}", - :options => network) - - # Write the entry to a temporary location - temp = Tempfile.new("vagrant") - temp.binmode - temp.write(entry) - temp.close - - vm.communicate.upload(temp.path, "/tmp/vagrant-network-entry") - vm.communicate.sudo("su -m root -c 'cat /tmp/vagrant-network-entry >> /etc/rc.conf'") - vm.communicate.sudo("rm /tmp/vagrant-network-entry") - - if network[:type].to_sym == :static - vm.communicate.sudo("ifconfig em#{network[:interface]} inet #{network[:ip]} netmask #{network[:netmask]}") - elsif network[:type].to_sym == :dhcp - vm.communicate.sudo("dhclient em#{network[:interface]}") - end - end - end - - def change_host_name(name) - if !vm.communicate.test("hostname -f | grep '^#{name}$' || hostname -s | grep '^#{name}$'") - vm.communicate.sudo("sed -i '' 's/^hostname=.*$/hostname=#{name}/' /etc/rc.conf") - vm.communicate.sudo("hostname #{name}") - end - end end end end diff --git a/plugins/guests/freebsd/plugin.rb b/plugins/guests/freebsd/plugin.rb index e20cff5c6..8e147087b 100644 --- a/plugins/guests/freebsd/plugin.rb +++ b/plugins/guests/freebsd/plugin.rb @@ -10,6 +10,26 @@ module VagrantPlugins require File.expand_path("../guest", __FILE__) Guest end + + guest_capability("freebsd", "change_host_name") do + require_relative "cap/change_host_name" + Cap::ChangeHostName + end + + guest_capability("freebsd", "configure_networks") do + require_relative "cap/configure_networks" + Cap::ConfigureNetworks + end + + guest_capability("freebsd", "halt") do + require_relative "cap/halt" + Cap::Halt + end + + guest_capability("freebsd", "mount_nfs_folder") do + require_relative "cap/mount_nfs_folder" + Cap::MountNFSFolder + end end end end