diff --git a/plugins/guests/darwin/cap/insert_public_key.rb b/plugins/guests/bsd/cap/insert_public_key.rb similarity index 77% rename from plugins/guests/darwin/cap/insert_public_key.rb rename to plugins/guests/bsd/cap/insert_public_key.rb index 909a8a3c2..714904b35 100644 --- a/plugins/guests/darwin/cap/insert_public_key.rb +++ b/plugins/guests/bsd/cap/insert_public_key.rb @@ -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 diff --git a/plugins/guests/bsd/guest.rb b/plugins/guests/bsd/guest.rb new file mode 100644 index 000000000..1a5586586 --- /dev/null +++ b/plugins/guests/bsd/guest.rb @@ -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 diff --git a/plugins/guests/bsd/plugin.rb b/plugins/guests/bsd/plugin.rb new file mode 100644 index 000000000..a6d82742f --- /dev/null +++ b/plugins/guests/bsd/plugin.rb @@ -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 diff --git a/plugins/guests/darwin/plugin.rb b/plugins/guests/darwin/plugin.rb index 157cdd7e9..5f1ce453c 100644 --- a/plugins/guests/darwin/plugin.rb +++ b/plugins/guests/darwin/plugin.rb @@ -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 diff --git a/plugins/guests/freebsd/cap/insert_public_key.rb b/plugins/guests/freebsd/cap/insert_public_key.rb deleted file mode 100644 index cf1fea35b..000000000 --- a/plugins/guests/freebsd/cap/insert_public_key.rb +++ /dev/null @@ -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 diff --git a/plugins/guests/freebsd/plugin.rb b/plugins/guests/freebsd/plugin.rb index 158513dbb..17d2f30b2 100644 --- a/plugins/guests/freebsd/plugin.rb +++ b/plugins/guests/freebsd/plugin.rb @@ -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 diff --git a/plugins/guests/netbsd/cap/insert_public_key.rb b/plugins/guests/netbsd/cap/insert_public_key.rb deleted file mode 100644 index dadce0f56..000000000 --- a/plugins/guests/netbsd/cap/insert_public_key.rb +++ /dev/null @@ -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 diff --git a/plugins/guests/netbsd/plugin.rb b/plugins/guests/netbsd/plugin.rb index 1af36eab0..ea5c9b3dd 100644 --- a/plugins/guests/netbsd/plugin.rb +++ b/plugins/guests/netbsd/plugin.rb @@ -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 diff --git a/plugins/guests/openbsd/cap/insert_public_key.rb b/plugins/guests/openbsd/cap/insert_public_key.rb deleted file mode 100644 index 42437283c..000000000 --- a/plugins/guests/openbsd/cap/insert_public_key.rb +++ /dev/null @@ -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 diff --git a/plugins/guests/openbsd/plugin.rb b/plugins/guests/openbsd/plugin.rb index 6ea442cd1..b63383fa9 100644 --- a/plugins/guests/openbsd/plugin.rb +++ b/plugins/guests/openbsd/plugin.rb @@ -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 diff --git a/test/unit/plugins/guests/freebsd/cap/insert_public_key_test.rb b/test/unit/plugins/guests/bsd/cap/insert_public_key_test.rb similarity index 73% rename from test/unit/plugins/guests/freebsd/cap/insert_public_key_test.rb rename to test/unit/plugins/guests/bsd/cap/insert_public_key_test.rb index 8f4c45da8..62b0ea7dc 100644 --- a/test/unit/plugins/guests/freebsd/cap/insert_public_key_test.rb +++ b/test/unit/plugins/guests/bsd/cap/insert_public_key_test.rb @@ -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/)