guests/freebsd: Insert public key in one command
This commit is contained in:
parent
7806223020
commit
bf51f6a71d
|
@ -1,19 +1,32 @@
|
|||
require "vagrant/util/shell_quote"
|
||||
require "tempfile"
|
||||
|
||||
module VagrantPlugins
|
||||
module GuestFreeBSD
|
||||
module Cap
|
||||
class InsertPublicKey
|
||||
def self.insert_public_key(machine, contents)
|
||||
contents = Vagrant::Util::ShellQuote.escape(contents, "'")
|
||||
contents = contents.gsub("\n", "\\n")
|
||||
comm = machine.communicate
|
||||
contents = contents.chomp
|
||||
|
||||
machine.communicate.tap do |comm|
|
||||
comm.execute("mkdir -p ~/.ssh", shell: "sh")
|
||||
comm.execute("chmod 0700 ~/.ssh", shell: "sh")
|
||||
comm.execute("printf '#{contents}' >> ~/.ssh/authorized_keys", shell: "sh")
|
||||
comm.execute("chmod 0600 ~/.ssh/authorized_keys", shell: "sh")
|
||||
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
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
require_relative "../../../../base"
|
||||
|
||||
describe "VagrantPlugins::GuestFreeBSD::Cap::InsertPublicKey" do
|
||||
let(:described_class) do
|
||||
VagrantPlugins::GuestFreeBSD::Plugin
|
||||
.components
|
||||
.guest_capabilities[:freebsd]
|
||||
.get(:insert_public_key)
|
||||
end
|
||||
|
||||
let(:machine) { double("machine") }
|
||||
let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
|
||||
|
||||
before do
|
||||
allow(machine).to receive(:communicate).and_return(comm)
|
||||
end
|
||||
|
||||
after do
|
||||
comm.verify_expectations!
|
||||
end
|
||||
|
||||
describe ".insert_public_key" do
|
||||
it "inserts the public key" do
|
||||
described_class.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/)
|
||||
expect(comm.received_commands[0]).to match(/chmod 0600 ~\/.ssh\/authorized_keys/)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue