Merge pull request #7478 from mitchellh/sethvargo/bsd_cap

guests/bsd: Add shared BSD guest for common behavior
This commit is contained in:
Seth Vargo 2016-06-17 20:26:33 -04:00 committed by GitHub
commit ce22a3e90d
11 changed files with 45 additions and 108 deletions

View File

@ -1,7 +1,7 @@
require "tempfile"
module VagrantPlugins
module GuestDarwin
module GuestBSD
module Cap
class InsertPublicKey
def self.insert_public_key(machine, contents)
@ -9,7 +9,7 @@ module VagrantPlugins
contents = contents.strip << "\n"
remote_path = "/tmp/vagrant-authorized-keys-#{Time.now.to_i}"
Tempfile.open("vagrant-darwin-insert-public-key") do |f|
Tempfile.open("vagrant-bsd-insert-public-key") do |f|
f.binmode
f.write(contents)
f.fsync
@ -17,7 +17,10 @@ module VagrantPlugins
comm.upload(f.path, remote_path)
end
# Use execute (not sudo) because we want to execute this as the SSH
# user (which is "vagrant" by default).
comm.execute <<-EOH.gsub(/^ {12}/, '')
set -e
mkdir -p ~/.ssh
chmod 0700 ~/.ssh
cat '#{remote_path}' >> ~/.ssh/authorized_keys

View File

@ -0,0 +1,9 @@
module VagrantPlugins
module GuestBSD
class Guest < Vagrant.plugin("2", :guest)
def detect?(machine)
machine.communicate.test("uname -s | grep -i 'Darwin|BSD'")
end
end
end
end

View File

@ -0,0 +1,20 @@
require "vagrant"
module VagrantPlugins
module GuestBSD
class Plugin < Vagrant.plugin("2")
name "BSD-based guest"
description "BSD-based guest support."
guest(:bsd) do
require_relative "guest"
Guest
end
guest_capability(:bsd, :insert_public_key) do
require_relative "cap/insert_public_key"
Cap::InsertPublicKey
end
end
end
end

View File

@ -6,7 +6,7 @@ module VagrantPlugins
name "Darwin guest"
description "Darwin guest support."
guest(:darwin) do
guest(:darwin, :bsd) do
require_relative "guest"
Guest
end
@ -31,11 +31,6 @@ module VagrantPlugins
Cap::Halt
end
guest_capability(:darwin, :insert_public_key) do
require_relative "cap/insert_public_key"
Cap::InsertPublicKey
end
guest_capability(:darwin, :mount_nfs_folder) do
require_relative "cap/mount_nfs_folder"
Cap::MountNFSFolder

View File

@ -1,34 +0,0 @@
require "tempfile"
module VagrantPlugins
module GuestFreeBSD
module Cap
class InsertPublicKey
def self.insert_public_key(machine, contents)
comm = machine.communicate
contents = contents.strip << "\n"
remote_path = "/tmp/vagrant-authorized-keys-#{Time.now.to_i}"
Tempfile.open("vagrant-freebsd-insert-public-key") do |f|
f.binmode
f.write(contents)
f.fsync
f.close
comm.upload(f.path, remote_path)
end
command = <<-EOH.gsub(/^ {12}/, '')
mkdir -p ~/.ssh
chmod 0700 ~/.ssh
cat '#{remote_path}' >> ~/.ssh/authorized_keys
chmod 0600 ~/.ssh/authorized_keys
# Remove the temporary file
rm -f '#{remote_path}'
EOH
comm.execute(command, { shell: "sh" })
end
end
end
end
end

View File

@ -6,7 +6,7 @@ module VagrantPlugins
name "FreeBSD guest"
description "FreeBSD guest support."
guest(:freebsd) do
guest(:freebsd, :bsd) do
require_relative "guest"
Guest
end
@ -26,11 +26,6 @@ module VagrantPlugins
Cap::Halt
end
guest_capability(:freebsd, :insert_public_key) do
require_relative "cap/insert_public_key"
Cap::InsertPublicKey
end
guest_capability(:freebsd, :mount_nfs_folder) do
require_relative "cap/mount_nfs_folder"
Cap::MountNFSFolder

View File

@ -1,21 +0,0 @@
require "vagrant/util/shell_quote"
module VagrantPlugins
module GuestNetBSD
module Cap
class InsertPublicKey
def self.insert_public_key(machine, contents)
contents = Vagrant::Util::ShellQuote.escape(contents, "'")
contents = contents.gsub("\n", "\\n")
machine.communicate.tap do |comm|
comm.execute("mkdir -p ~/.ssh")
comm.execute("chmod 0700 ~/.ssh")
comm.execute("printf '#{contents}' >> ~/.ssh/authorized_keys")
comm.execute("chmod 0600 ~/.ssh/authorized_keys")
end
end
end
end
end
end

View File

@ -6,7 +6,7 @@ module VagrantPlugins
name "NetBSD guest"
description "NetBSD guest support."
guest(:netbsd) do
guest(:netbsd, :bsd) do
require_relative "guest"
Guest
end
@ -26,11 +26,6 @@ module VagrantPlugins
Cap::Halt
end
guest_capability(:netbsd, :insert_public_key) do
require_relative "cap/insert_public_key"
Cap::InsertPublicKey
end
guest_capability(:netbsd, :mount_nfs_folder) do
require_relative "cap/mount_nfs_folder"
Cap::MountNFSFolder

View File

@ -1,21 +0,0 @@
require "vagrant/util/shell_quote"
module VagrantPlugins
module GuestOpenBSD
module Cap
class InsertPublicKey
def self.insert_public_key(machine, contents)
contents = Vagrant::Util::ShellQuote.escape(contents, "'")
contents = contents.gsub("\n", "\\n")
machine.communicate.tap do |comm|
comm.execute("mkdir -p ~/.ssh")
comm.execute("chmod 0700 ~/.ssh")
comm.execute("printf '#{contents}\\n' >> ~/.ssh/authorized_keys")
comm.execute("chmod 0600 ~/.ssh/authorized_keys")
end
end
end
end
end
end

View File

@ -6,7 +6,7 @@ module VagrantPlugins
name "OpenBSD guest"
description "OpenBSD guest support."
guest(:openbsd) do
guest(:openbsd, :bsd) do
require_relative "guest"
Guest
end
@ -26,11 +26,6 @@ module VagrantPlugins
Cap::Halt
end
guest_capability(:openbsd, :insert_public_key) do
require_relative "cap/insert_public_key"
Cap::InsertPublicKey
end
guest_capability(:openbsd, :mount_nfs_folder) do
require_relative "cap/mount_nfs_folder"
Cap::MountNFSFolder

View File

@ -1,11 +1,10 @@
require_relative "../../../../base"
describe "VagrantPlugins::GuestFreeBSD::Cap::InsertPublicKey" do
let(:described_class) do
VagrantPlugins::GuestFreeBSD::Plugin
describe "VagrantPlugins::GuestBSD::Cap::InsertPublicKey" do
let(:caps) do
VagrantPlugins::GuestBSD::Plugin
.components
.guest_capabilities[:freebsd]
.get(:insert_public_key)
.guest_capabilities[:bsd]
end
let(:machine) { double("machine") }
@ -20,8 +19,10 @@ describe "VagrantPlugins::GuestFreeBSD::Cap::InsertPublicKey" do
end
describe ".insert_public_key" do
let(:cap) { caps.get(:insert_public_key) }
it "inserts the public key" do
described_class.insert_public_key(machine, "ssh-rsa ...")
cap.insert_public_key(machine, "ssh-rsa ...")
expect(comm.received_commands[0]).to match(/mkdir -p ~\/.ssh/)
expect(comm.received_commands[0]).to match(/chmod 0700 ~\/.ssh/)
expect(comm.received_commands[0]).to match(/cat '\/tmp\/vagrant-(.+)' >> ~\/.ssh\/authorized_keys/)