2012-01-13 07:27:35 +00:00
|
|
|
require 'log4r'
|
|
|
|
|
2011-08-28 06:39:02 +00:00
|
|
|
require 'vagrant/util/platform'
|
|
|
|
|
2012-04-19 05:20:45 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module HostLinux
|
2010-07-17 16:09:06 +00:00
|
|
|
# Represents a Linux based host, such as Ubuntu.
|
2012-04-19 05:20:45 +00:00
|
|
|
class Host < Vagrant::Hosts::Base
|
|
|
|
include Vagrant::Util
|
|
|
|
include Vagrant::Util::Retryable
|
2010-07-17 16:09:06 +00:00
|
|
|
|
2011-12-12 07:22:44 +00:00
|
|
|
def self.match?
|
2012-04-19 05:20:45 +00:00
|
|
|
Vagrant::Util::Platform.linux?
|
2011-12-12 07:22:44 +00:00
|
|
|
end
|
2011-08-28 06:47:13 +00:00
|
|
|
|
2011-12-12 07:22:44 +00:00
|
|
|
def self.precedence
|
|
|
|
# Set a lower precedence because this is a generic OS. We
|
|
|
|
# want specific distros to match first.
|
|
|
|
2
|
2011-08-28 06:39:02 +00:00
|
|
|
end
|
|
|
|
|
2011-08-28 06:47:13 +00:00
|
|
|
def initialize(*args)
|
|
|
|
super
|
|
|
|
|
2012-01-13 07:27:35 +00:00
|
|
|
@logger = Log4r::Logger.new("vagrant::hosts::linux")
|
2011-08-28 06:47:13 +00:00
|
|
|
@nfs_server_binary = "/etc/init.d/nfs-kernel-server"
|
|
|
|
end
|
|
|
|
|
2010-07-17 16:09:06 +00:00
|
|
|
def nfs?
|
2010-09-09 07:37:54 +00:00
|
|
|
retryable(:tries => 10, :on => TypeError) do
|
2010-07-17 16:09:06 +00:00
|
|
|
# Check procfs to see if NFSd is a supported filesystem
|
2010-09-30 06:38:07 +00:00
|
|
|
system("cat /proc/filesystems | grep nfsd > /dev/null 2>&1")
|
2010-07-17 16:09:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-12 07:22:44 +00:00
|
|
|
def nfs_export(id, ip, folders)
|
2010-07-17 16:09:06 +00:00
|
|
|
output = TemplateRenderer.render('nfs/exports_linux',
|
2011-12-12 07:22:44 +00:00
|
|
|
:uuid => id,
|
2010-07-17 16:09:06 +00:00
|
|
|
:ip => ip,
|
|
|
|
:folders => folders)
|
|
|
|
|
2012-01-13 07:33:17 +00:00
|
|
|
@ui.info I18n.t("vagrant.hosts.linux.nfs_export")
|
2010-07-24 07:29:46 +00:00
|
|
|
sleep 0.5
|
|
|
|
|
2012-01-13 07:27:35 +00:00
|
|
|
nfs_cleanup(id)
|
|
|
|
|
2010-07-17 16:09:06 +00:00
|
|
|
output.split("\n").each do |line|
|
|
|
|
# This should only ask for administrative permission once, even
|
|
|
|
# though its executed in multiple subshells.
|
2010-09-30 06:38:07 +00:00
|
|
|
system(%Q[sudo su root -c "echo '#{line}' >> /etc/exports"])
|
2010-07-17 16:09:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# We run restart here instead of "update" just in case nfsd
|
|
|
|
# is not starting
|
2011-08-28 06:47:13 +00:00
|
|
|
system("sudo #{@nfs_server_binary} restart")
|
2010-07-17 16:09:06 +00:00
|
|
|
end
|
|
|
|
|
2012-01-13 07:27:35 +00:00
|
|
|
def nfs_prune(valid_ids)
|
2012-01-14 04:38:20 +00:00
|
|
|
return if !File.exist?("/etc/exports")
|
|
|
|
|
2012-01-13 07:27:35 +00:00
|
|
|
@logger.info("Pruning invalid NFS entries...")
|
|
|
|
|
2012-01-13 07:33:17 +00:00
|
|
|
output = false
|
|
|
|
|
2012-01-13 07:27:35 +00:00
|
|
|
File.read("/etc/exports").lines.each do |line|
|
2012-03-14 04:29:38 +00:00
|
|
|
if id = line[/^# VAGRANT-BEGIN: (.+?)$/, 1]
|
|
|
|
if valid_ids.include?(id)
|
|
|
|
@logger.debug("Valid ID: #{id}")
|
2012-01-13 07:27:35 +00:00
|
|
|
else
|
2012-01-13 07:33:17 +00:00
|
|
|
if !output
|
|
|
|
# We want to warn the user but we only want to output once
|
|
|
|
@ui.info I18n.t("vagrant.hosts.linux.nfs_prune")
|
|
|
|
output = true
|
|
|
|
end
|
|
|
|
|
2012-03-14 04:29:38 +00:00
|
|
|
@logger.info("Invalid ID, pruning: #{id}")
|
|
|
|
nfs_cleanup(id)
|
2012-01-13 07:27:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2011-12-12 07:22:44 +00:00
|
|
|
def nfs_cleanup(id)
|
2010-07-30 16:38:45 +00:00
|
|
|
return if !File.exist?("/etc/exports")
|
2010-07-17 16:09:06 +00:00
|
|
|
|
2012-01-13 02:48:47 +00:00
|
|
|
# Use sed to just strip out the block of code which was inserted
|
|
|
|
# by Vagrant
|
|
|
|
system("sudo sed -e '/^# VAGRANT-BEGIN: #{id}/,/^# VAGRANT-END: #{id}/ d' -ibak /etc/exports")
|
2010-07-17 16:09:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|