2012-01-13 07:27:35 +00:00
|
|
|
require 'log4r'
|
|
|
|
|
2012-05-23 23:03:14 +00:00
|
|
|
require "vagrant"
|
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-11-07 05:20:22 +00:00
|
|
|
class Host < Vagrant.plugin("2", :host)
|
2012-04-19 05:20:45 +00:00
|
|
|
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")
|
2013-08-24 10:37:09 +00:00
|
|
|
@nfs_apply_command = "/usr/sbin/exportfs -r"
|
|
|
|
@nfs_check_command = "/etc/init.d/nfs-kernel-server status"
|
|
|
|
@nfs_start_command = "/etc/init.d/nfs-kernel-server start"
|
2011-08-28 06:47:13 +00:00
|
|
|
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
|
|
|
|
|
2013-09-10 15:39:41 +00:00
|
|
|
def nfs_opts_setup(folders)
|
2013-09-01 20:08:02 +00:00
|
|
|
folders.each do |k, opts|
|
|
|
|
if !opts[:linux__nfs_options]
|
2013-09-07 15:09:09 +00:00
|
|
|
opts[:linux__nfs_options] ||= ["rw", "no_subtree_check", "all_squash"]
|
2013-09-01 20:08:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Only automatically set anonuid/anongid if they weren't
|
|
|
|
# explicitly set by the user.
|
|
|
|
hasgid = false
|
|
|
|
hasuid = false
|
|
|
|
opts[:linux__nfs_options].each do |opt|
|
|
|
|
hasgid = !!(opt =~ /^anongid=/) if !hasgid
|
|
|
|
hasuid = !!(opt =~ /^anonuid=/) if !hasuid
|
|
|
|
end
|
|
|
|
|
|
|
|
opts[:linux__nfs_options] << "anonuid=#{opts[:map_uid]}" if !hasuid
|
|
|
|
opts[:linux__nfs_options] << "anongid=#{opts[:map_gid]}" if !hasgid
|
|
|
|
opts[:linux__nfs_options] << "fsid=#{opts[:uuid]}"
|
|
|
|
end
|
2013-09-10 15:39:41 +00:00
|
|
|
end
|
2013-09-01 20:08:02 +00:00
|
|
|
|
2013-09-10 15:39:41 +00:00
|
|
|
def nfs_export(id, ips, folders)
|
|
|
|
nfs_opts_setup(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,
|
2013-09-06 15:54:02 +00:00
|
|
|
:ips => ips,
|
2013-03-27 15:15:39 +00:00
|
|
|
:folders => folders,
|
|
|
|
:user => Process.uid)
|
2010-07-17 16:09:06 +00:00
|
|
|
|
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
|
|
|
|
|
2013-08-24 10:37:09 +00:00
|
|
|
if nfs_running?
|
|
|
|
system("sudo #{@nfs_apply_command}")
|
|
|
|
else
|
|
|
|
system("sudo #{@nfs_start_command}")
|
|
|
|
end
|
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
|
2013-03-27 15:15:39 +00:00
|
|
|
user = Process.uid
|
2012-01-13 07:33:17 +00:00
|
|
|
|
2012-01-13 07:27:35 +00:00
|
|
|
File.read("/etc/exports").lines.each do |line|
|
2013-03-27 15:15:39 +00:00
|
|
|
if id = line[/^# VAGRANT-BEGIN:( #{user})? ([A-Za-z0-9-]+?)$/, 2]
|
2012-03-14 04:29:38 +00:00
|
|
|
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
|
|
|
|
|
2013-08-24 10:37:09 +00:00
|
|
|
def nfs_running?
|
|
|
|
system("#{@nfs_check_command}")
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
2013-03-27 15:15:39 +00:00
|
|
|
user = Process.uid
|
2012-01-13 02:48:47 +00:00
|
|
|
# Use sed to just strip out the block of code which was inserted
|
|
|
|
# by Vagrant
|
2013-09-01 19:00:36 +00:00
|
|
|
system("sudo sed -r -e '/^# VAGRANT-BEGIN:( #{user})? #{id}/,/^# VAGRANT-END:( #{user})? #{id}/ d' -ibak /etc/exports")
|
2010-07-17 16:09:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|