adding Cumulus as guest plugin

This commit is contained in:
Scott Suehle 2017-01-12 11:28:40 -05:00
parent 159fca9d13
commit e2908b0396
7 changed files with 214 additions and 0 deletions

View File

@ -0,0 +1,44 @@
module VagrantPlugins
module GuestDebian
module Cap
class ChangeHostName
def self.change_host_name(machine, name)
comm = machine.communicate
if !comm.test("hostname -f | grep '^#{name}$'", sudo: false)
basename = name.split(".", 2)[0]
comm.sudo <<-EOH.gsub(/^ {14}/, '')
# Set the hostname
echo '#{basename}' > /etc/hostname
hostname -F /etc/hostname
if command -v hostnamectl; then
hostnamectl set-hostname '#{basename}'
fi
# Remove comments and blank lines from /etc/hosts
sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts
# Prepend ourselves to /etc/hosts
grep -w '#{name}' /etc/hosts || {
sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts
}
# Update mailname
echo '#{name}' > /etc/mailname
# Restart hostname services
if test -f /etc/init.d/hostname; then
/etc/init.d/hostname start || true
fi
if test -f /etc/init.d/hostname.sh; then
/etc/init.d/hostname.sh start || true
fi
EOH
end
end
end
end
end
end

View File

@ -0,0 +1,72 @@
require "tempfile"
require_relative "../../../../lib/vagrant/util/template_renderer"
module VagrantPlugins
module GuestCumulus
module Cap
class ConfigureNetworks
include Vagrant::Util
def self.configure_networks(machine, networks)
comm = machine.communicate
commands = []
entries = []
interfaces = machine.guest.capability(:network_interfaces)
networks.each do |network|
network[:device] = interfaces[network[:interface]]
entry = TemplateRenderer.render("guests/cumulus/network_#{network[:type]}",
options: network,
)
entries << entry
end
Tempfile.open("vagrant-cumulus-configure-networks") do |f|
f.binmode
f.write(entries.join("\n"))
f.fsync
f.close
comm.upload(f.path, "/tmp/vagrant-network-entry")
end
networks.each do |network|
# Ubuntu 16.04+ returns an error when downing an interface that
# does not exist. The `|| true` preserves the behavior that older
# Ubuntu versions exhibit and Vagrant expects (GH-7155)
commands << "/sbin/ifdown '#{network[:device]}' || true"
commands << "/sbin/ip addr flush dev '#{network[:device]}'"
end
# Reconfigure /etc/network/interfaces.
commands << <<-EOH.gsub(/^ {12}/, "")
# Remove any previous network modifications from the interfaces file
sed -e '/^#VAGRANT-BEGIN/,$ d' /etc/network/interfaces > /tmp/vagrant-network-interfaces.pre
sed -ne '/^#VAGRANT-END/,$ p' /etc/network/interfaces | tac | sed -e '/^#VAGRANT-END/,$ d' | tac > /tmp/vagrant-network-interfaces.post
cat \\
/tmp/vagrant-network-interfaces.pre \\
/tmp/vagrant-network-entry \\
/tmp/vagrant-network-interfaces.post \\
> /etc/network/interfaces
rm -f /tmp/vagrant-network-interfaces.pre
rm -f /tmp/vagrant-network-entry
rm -f /tmp/vagrant-network-interfaces.post
EOH
# Bring back up each network interface, reconfigured.
networks.each do |network|
commands << "/sbin/ifup '#{network[:device]}'"
end
# Run all the commands in one session to prevent partial configuration
# due to a severed network.
comm.sudo(commands.join("\n"))
end
end
end
end
end

View File

@ -0,0 +1,16 @@
module VagrantPlugins
module GuestDebian
module Cap
class NFS
def self.nfs_client_install(machine)
comm = machine.communicate
comm.sudo <<-EOH.gsub(/^ {12}/, '')
apt-get -yqq update
apt-get -yqq install nfs-common portmap
exit $?
EOH
end
end
end
end
end

View File

@ -0,0 +1,15 @@
module VagrantPlugins
module GuestDebian
module Cap
class RSync
def self.rsync_install(machine)
comm = machine.communicate
comm.sudo <<-EOH.gsub(/^ {14}/, '')
apt-get -yqq update
apt-get -yqq install rsync
EOH
end
end
end
end
end

View File

@ -0,0 +1,17 @@
module VagrantPlugins
module GuestDebian
module Cap
class SMB
def self.smb_install(machine)
comm = machine.communicate
if !comm.test("test -f /sbin/mount.cifs")
comm.sudo <<-EOH.gsub(/^ {14}/, '')
apt-get -yqq update
apt-get -yqq install cifs-utils
EOH
end
end
end
end
end
end

View File

@ -0,0 +1,10 @@
require_relative '../linux/guest'
module VagrantPlugins
module GuestCumulus
class Guest < VagrantPlugins::GuestLinux::Guest
# Name used for guest detection
GUEST_DETECTION_NAME = "cumulus".freeze
end
end
end

View File

@ -0,0 +1,40 @@
require "vagrant"
module VagrantPlugins
module GuestCumulus
class Plugin < Vagrant.plugin("2")
name "Cumulus guest"
description "Cumulus guest support."
guest(:cumulus, :linux) do
require_relative "guest"
Guest
end
guest_capability(:cumulus, :configure_networks) do
require_relative "cap/configure_networks"
Cap::ConfigureNetworks
end
guest_capability(:cumulus, :change_host_name) do
require_relative "cap/change_host_name"
Cap::ChangeHostName
end
guest_capability(:cumulus, :nfs_client_install) do
require_relative "cap/nfs"
Cap::NFS
end
guest_capability(:cumulus, :rsync_install) do
require_relative "cap/rsync"
Cap::RSync
end
guest_capability(:cumulus, :smb_install) do
require_relative "cap/smb"
Cap::SMB
end
end
end
end