FreeBSD capabilities

This commit is contained in:
Mitchell Hashimoto 2013-04-04 11:56:42 -07:00
parent 8bf9fb17b1
commit 2c362d4d28
6 changed files with 103 additions and 54 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -6,25 +6,11 @@ module VagrantPlugins
# #
# Contributed by Kenneth Vestergaard <kvs@binarysolutions.dk> # Contributed by Kenneth Vestergaard <kvs@binarysolutions.dk>
class Guest < Vagrant.plugin("2", :guest) 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) def detect?(machine)
# TODO: FreeBSD detection # TODO: FreeBSD detection
false false
end 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 # TODO: vboxsf is currently unsupported in FreeBSD, if you are able to
# help out with this project, please contact vbox@FreeBSD.org # 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 mount -t vboxfs v-root #{guestpath}")
# ssh.exec!("sudo chown #{vm.config.ssh.username} #{guestpath}") # ssh.exec!("sudo chown #{vm.config.ssh.username} #{guestpath}")
# end # 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 end
end end

View File

@ -10,6 +10,26 @@ module VagrantPlugins
require File.expand_path("../guest", __FILE__) require File.expand_path("../guest", __FILE__)
Guest Guest
end 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 end
end end