Capistrano.configuration(:must_exist).load do
desc "Create the nfs share"
task :setup_nfs_share do
local_run "sudo nicl . -create #{export_dir}"
local_run "sudo nicl . -append #{export_dir} clients \"#{client_ips(" ")}\""
local_run "sudo nicl . -append #{export_dir} opts #{nfs_opts}"
restart_mountd
end
desc "remove the nfs share"
task :teardown_nfs_share do
local_run "sudo nicl . -delete #{export_dir}"
restart_mountd
end
desc "Restart the mountd process"
task :restart_mountd do
local_run "sudo kill -1 `cat /var/run/mountd.pid`"
end
desc "Mount nfs share"
task :mount_nfs_share do
sudo "mount -t nfs #{mount_options} #{interface_ip}:#{share_dir} #{mount_dir}"
end
desc "Unmount nfs share"
task :unmount_nfs_share do
sudo "umount #{mount_dir}"
end
desc "Opens nfs on the firewall for the client machine"
task :open_firewall do
local_run "sudo ipfw add 5001 allow tcp from #{client_ips} to any dst-port 600-1000,111,2049 setup in via #{interface} keep-state"
local_run "sudo ipfw add 5002 allow udp from #{client_ips} to any dst-port 600-1000,111,2049 in via #{interface} keep-state"
end
desc "Closes the firewall"
task :close_firewall do
local_run "sudo ipfw delete 5001"
local_run "sudo ipfw delete 5002"
end
desc "The main task to setup the share"
task :share do
#make sure the share isn't there
teardown_nfs_share
# set everything up
setup_nfs_share
open_firewall
mount_nfs_share
end
desc "The main task to teardown the share"
task :unshare do
unmount_nfs_share
close_firewall
teardown_nfs_share
end
# helper methods
def interface_ip
`ifconfig #{interface}`.match(/inet ([0-9\.]*) /)[1]
end
def client_ips(separator = ",")
roles[:client].map { |client| client.host }.join(separator)
end
def local_run(cmd)
puts "local cmd: #{cmd}"
puts `#{cmd}`
end
def escape_nicl_path(path)
path.gsub(/\//, "\\\\\\/")
end
def export_dir
"/exports/#{escape_nicl_path(share_dir)}"
end
end
File 2: named after the share and put in the same dir as the file above
require "capistrano-nfs-tasks"
role :client, "192.168.2.2"
set :user, "chris"
set :share_dir, '/Users/chris/dev'
set :mount_dir, '/home/chris/dev'
set :nfs_opts, "mapall=chris"
set :interface, "en0"
set :mount_options, ""