convert more hosts: arch and linux

This commit is contained in:
Mitchell Hashimoto 2014-01-07 20:53:39 -08:00
parent bdb88da743
commit a6c7cc481e
7 changed files with 221 additions and 183 deletions

View File

@ -13,32 +13,6 @@ module Vagrant
def detect?
false
end
# Returns true of false denoting whether or not this host supports
# NFS shared folder setup. This method ideally should verify that
# NFS is installed.
#
# @return [Boolean]
def nfs?
false
end
# Exports the given hash of folders via NFS.
#
# @param [String] id A unique ID that is guaranteed to be unique to
# match these sets of folders.
# @param [String] ip IP of the guest machine.
# @param [Hash] folders Shared folders to sync.
def nfs_export(id, ip, folders)
end
# Prunes any NFS exports made by Vagrant which aren't in the set
# of valid ids given.
#
# @param [Array<String>] valid_ids Valid IDs that should not be
# pruned.
def nfs_prune(valid_ids)
end
end
end
end

View File

@ -0,0 +1,35 @@
module VagrantPlugins
module HostArch
module Cap
class NFS
def self.nfs_check_command(env)
if systemd?
return "/usr/sbin/systemctl status nfsd"
else
return "/etc/rc.d/nfs-server status"
end
end
def self.nfs_start_command(env)
if systemd?
return "/usr/sbin/systemctl start nfsd rpc-idmapd rpc-mountd rpcbind"
else
return "sh -c 'for s in {rpcbind,nfs-common,nfs-server}; do /etc/rc.d/$s start; done'"
end
end
def self.nfs_installed(environment)
Kernel.system("grep -Fq nfs /proc/filesystems")
end
protected
# This tests to see if systemd is used on the system. This is used
# in newer versions of Arch, and requires a change in behavior.
def self.systemd?
`ps -o comm= 1`.chomp == 'systemd'
end
end
end
end
end

View File

@ -1,46 +1,11 @@
require "vagrant"
require Vagrant.source_root.join("plugins/hosts/linux/host")
module VagrantPlugins
module HostArch
class Host < VagrantPlugins::HostLinux::Host
def self.match?
class Host < Vagrant.plugin("2", :host)
def detect?(env)
File.exist?("/etc/arch-release")
end
def self.nfs?
# HostLinux checks for nfsd which returns false unless the
# services are actively started. This leads to a misleading
# error message. Checking for nfs (no d) seems to work
# regardless. Also fixes useless use of cat, regex, and
# redirection.
Kernel.system("grep -Fq nfs /proc/filesystems")
end
# Normal, mid-range precedence.
def self.precedence
5
end
def initialize(*args)
super
if systemd?
@nfs_check_command = "/usr/sbin/systemctl status nfsd"
@nfs_start_command = "/usr/sbin/systemctl start nfsd rpc-idmapd rpc-mountd rpcbind"
else
@nfs_check_command = "/etc/rc.d/nfs-server status"
@nfs_start_command = "sh -c 'for s in {rpcbind,nfs-common,nfs-server}; do /etc/rc.d/$s start; done'"
end
end
protected
# This tests to see if systemd is used on the system. This is used
# in newer versions of Arch, and requires a change in behavior.
def systemd?
`ps -o comm= 1`.chomp == 'systemd'
end
end
end
end

View File

@ -6,10 +6,32 @@ module VagrantPlugins
name "Arch host"
description "Arch host support."
host("arch") do
require File.expand_path("../host", __FILE__)
host("arch", "linux") do
require_relative "host"
Host
end
host_capability("arch", "nfs_installed") do
require_relative "cap/nfs"
Cap::NFS
end
# Linux-specific helpers we need to determine paths that can
# be overriden.
host_capability("arch", "nfs_apply_command") do
require_relative "cap/nfs"
Cap::NFS
end
host_capability("arch", "nfs_check_command") do
require_relative "cap/nfs"
Cap::NFS
end
host_capability("arch", "nfs_start_command") do
require_relative "cap/nfs"
Cap::NFS
end
end
end
end

View File

@ -0,0 +1,126 @@
require "vagrant/util"
require "vagrant/util/retryable"
module VagrantPlugins
module HostLinux
module Cap
class NFS
extend Vagrant::Util::Retryabe
def self.nfs_apply_command(env)
"/usr/bin/exportfs -r"
end
def self.nfs_check_command(env)
"/etc/init.d/nfs-kernel-server status"
end
def self.nfs_start_command(env)
"/etc/init.d/nfs-kernel-server start"
end
def self.nfs_export(env, ui, id, ips, folders)
# Get some values we need before we do anything
nfs_apply_command = env.host.capability(:nfs_apply_command)
nfs_check_command = env.host.capability(:nfs_check_command)
nfs_start_command = env.host.capability(:nfs_start_command)
nfs_opts_setup(folders)
output = Vagrant::Util::TemplateRenderer.render('nfs/exports_linux',
:uuid => id,
:ips => ips,
:folders => folders,
:user => Process.uid)
ui.info I18n.t("vagrant.hosts.linux.nfs_export")
sleep 0.5
nfs_cleanup(id)
output.split("\n").each do |line|
# This should only ask for administrative permission once, even
# though its executed in multiple subshells.
system(%Q[sudo su root -c "echo '#{line}' >> /etc/exports"])
end
if nfs_running?(nfs_check_command)
system("sudo #{nfs_apply_command}")
else
system("sudo #{nfs_start_command}")
end
end
def self.nfs_installed(environment)
retryable(:tries => 10, :on => TypeError) do
# Check procfs to see if NFSd is a supported filesystem
system("cat /proc/filesystems | grep nfsd > /dev/null 2>&1")
end
end
def self.nfs_prune(environment, ui, valid_ids)
return if !File.exist?("/etc/exports")
logger = Log4r::Logger.new("vagrant::hosts::linux")
logger.info("Pruning invalid NFS entries...")
output = false
user = Process.uid
File.read("/etc/exports").lines.each do |line|
if id = line[/^# VAGRANT-BEGIN:( #{user})? ([A-Za-z0-9-]+?)$/, 2]
if valid_ids.include?(id)
logger.debug("Valid ID: #{id}")
else
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
logger.info("Invalid ID, pruning: #{id}")
nfs_cleanup(id)
end
end
end
end
protected
def self.nfs_cleanup(id)
return if !File.exist?("/etc/exports")
user = Process.uid
# Use sed to just strip out the block of code which was inserted
# by Vagrant
system("sudo sed -r -e '/^# VAGRANT-BEGIN:( #{user})? #{id}/,/^# VAGRANT-END:( #{user})? #{id}/ d' -ibak /etc/exports")
end
def self.nfs_opts_setup(folders)
folders.each do |k, opts|
if !opts[:linux__nfs_options]
opts[:linux__nfs_options] ||= ["rw", "no_subtree_check", "all_squash"]
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
end
def self.nfs_running?(check_command)
system(check_command)
end
end
end
end
end

View File

@ -1,128 +1,12 @@
require 'log4r'
require "vagrant"
require 'vagrant/util/platform'
module VagrantPlugins
module HostLinux
# Represents a Linux based host, such as Ubuntu.
class Host < Vagrant.plugin("2", :host)
include Vagrant::Util
include Vagrant::Util::Retryable
def self.match?
def self.detect?(env)
Vagrant::Util::Platform.linux?
end
def self.precedence
# Set a lower precedence because this is a generic OS. We
# want specific distros to match first.
2
end
def initialize(*args)
super
@logger = Log4r::Logger.new("vagrant::hosts::linux")
@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"
end
def nfs?
retryable(:tries => 10, :on => TypeError) do
# Check procfs to see if NFSd is a supported filesystem
system("cat /proc/filesystems | grep nfsd > /dev/null 2>&1")
end
end
def nfs_opts_setup(folders)
folders.each do |k, opts|
if !opts[:linux__nfs_options]
opts[:linux__nfs_options] ||= ["rw", "no_subtree_check", "all_squash"]
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
end
def nfs_export(id, ips, folders)
nfs_opts_setup(folders)
output = TemplateRenderer.render('nfs/exports_linux',
:uuid => id,
:ips => ips,
:folders => folders,
:user => Process.uid)
@ui.info I18n.t("vagrant.hosts.linux.nfs_export")
sleep 0.5
nfs_cleanup(id)
output.split("\n").each do |line|
# This should only ask for administrative permission once, even
# though its executed in multiple subshells.
system(%Q[sudo su root -c "echo '#{line}' >> /etc/exports"])
end
if nfs_running?
system("sudo #{@nfs_apply_command}")
else
system("sudo #{@nfs_start_command}")
end
end
def nfs_prune(valid_ids)
return if !File.exist?("/etc/exports")
@logger.info("Pruning invalid NFS entries...")
output = false
user = Process.uid
File.read("/etc/exports").lines.each do |line|
if id = line[/^# VAGRANT-BEGIN:( #{user})? ([A-Za-z0-9-]+?)$/, 2]
if valid_ids.include?(id)
@logger.debug("Valid ID: #{id}")
else
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
@logger.info("Invalid ID, pruning: #{id}")
nfs_cleanup(id)
end
end
end
end
protected
def nfs_running?
system("#{@nfs_check_command}")
end
def nfs_cleanup(id)
return if !File.exist?("/etc/exports")
user = Process.uid
# Use sed to just strip out the block of code which was inserted
# by Vagrant
system("sudo sed -r -e '/^# VAGRANT-BEGIN:( #{user})? #{id}/,/^# VAGRANT-END:( #{user})? #{id}/ d' -ibak /etc/exports")
end
end
end
end

View File

@ -7,9 +7,41 @@ module VagrantPlugins
description "Linux host support."
host("linux") do
require File.expand_path("../host", __FILE__)
require_relative "host"
Host
end
host_capability("linux", "nfs_export") do
require_relative "cap/nfs"
Cap::NFS
end
host_capability("linux", "nfs_installed") do
require_relative "cap/nfs"
Cap::NFS
end
host_capability("linux", "nfs_prune") do
require_relative "cap/nfs"
Cap::NFS
end
# Linux-specific helpers we need to determine paths that can
# be overriden.
host_capability("linux", "nfs_apply_command") do
require_relative "cap/nfs"
Cap::NFS
end
host_capability("linux", "nfs_check_command") do
require_relative "cap/nfs"
Cap::NFS
end
host_capability("linux", "nfs_start_command") do
require_relative "cap/nfs"
Cap::NFS
end
end
end
end