From 263a2c4d9b616ca9b2c35106d2f3accad832dde2 Mon Sep 17 00:00:00 2001 From: Alexander von Gluck IV Date: Tue, 5 Dec 2017 18:19:15 -0600 Subject: [PATCH] haiku: Improve capabilities. (public keys, halt) --- plugins/guests/haiku/cap/halt.rb | 16 ++++++++++++++ plugins/guests/haiku/cap/insert_public_key.rb | 21 +++++++++++++++++++ plugins/guests/haiku/cap/remove_public_key.rb | 21 +++++++++++++++++++ plugins/guests/haiku/plugin.rb | 15 +++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 plugins/guests/haiku/cap/halt.rb create mode 100644 plugins/guests/haiku/cap/insert_public_key.rb create mode 100644 plugins/guests/haiku/cap/remove_public_key.rb diff --git a/plugins/guests/haiku/cap/halt.rb b/plugins/guests/haiku/cap/halt.rb new file mode 100644 index 000000000..96ed01918 --- /dev/null +++ b/plugins/guests/haiku/cap/halt.rb @@ -0,0 +1,16 @@ +module VagrantPlugins + module GuestHaiku + module Cap + class Halt + def self.halt(machine) + begin + machine.communicate.execute("/bin/shutdown") + rescue IOError, Vagrant::Errors::SSHDisconnected + # Ignore, this probably means connection closed because it + # shut down. + end + end + end + end + end +end diff --git a/plugins/guests/haiku/cap/insert_public_key.rb b/plugins/guests/haiku/cap/insert_public_key.rb new file mode 100644 index 000000000..089da5e03 --- /dev/null +++ b/plugins/guests/haiku/cap/insert_public_key.rb @@ -0,0 +1,21 @@ +require "vagrant/util/shell_quote" + +module VagrantPlugins + module GuestHaiku + module Cap + class InsertPublicKey + def self.insert_public_key(machine, contents) + contents = contents.chomp + contents = Vagrant::Util::ShellQuote.escape(contents, "'") + + machine.communicate.tap do |comm| + comm.execute("mkdir -p $(finddir B_USER_SETTINGS_DIRECTORY)/ssh") + comm.execute("chmod 0700 $(finddir B_USER_SETTINGS_DIRECTORY)/ssh") + comm.execute("printf '#{contents}\\n' >> $(finddir B_USER_SETTINGS_DIRECTORY)/ssh/authorized_keys") + comm.execute("chmod 0600 $(finddir B_USER_SETTINGS_DIRECTORY)/ssh/authorized_keys") + end + end + end + end + end +end diff --git a/plugins/guests/haiku/cap/remove_public_key.rb b/plugins/guests/haiku/cap/remove_public_key.rb new file mode 100644 index 000000000..bb823dcc1 --- /dev/null +++ b/plugins/guests/haiku/cap/remove_public_key.rb @@ -0,0 +1,21 @@ +require "vagrant/util/shell_quote" + +module VagrantPlugins + module GuestHaiku + module Cap + class RemovePublicKey + def self.remove_public_key(machine, contents) + contents = contents.chomp + contents = Vagrant::Util::ShellQuote.escape(contents, "'") + + machine.communicate.tap do |comm| + if comm.test("test -f $(finddir B_USER_SETTINGS_DIRECTORY)/ssh/authorized_keys") + comm.execute( + "sed -i '/^.*#{contents}.*$/d' $(finddir B_USER_SETTINGS_DIRECTORY)/ssh/authorized_keys") + end + end + end + end + end + end +end diff --git a/plugins/guests/haiku/plugin.rb b/plugins/guests/haiku/plugin.rb index 63e7ce849..4654d3b75 100644 --- a/plugins/guests/haiku/plugin.rb +++ b/plugins/guests/haiku/plugin.rb @@ -11,10 +11,25 @@ module VagrantPlugins Guest end + guest_capability(:haiku, :halt) do + require_relative "cap/halt" + Cap::Halt + end + guest_capability(:haiku, :change_host_name) do require_relative "cap/change_host_name" Cap::ChangeHostName end + + guest_capability(:haiku, :insert_public_key) do + require_relative "cap/insert_public_key" + Cap::InsertPublicKey + end + + guest_capability(:haiku, :remove_public_key) do + require_relative "cap/remove_public_key" + Cap::RemovePublicKey + end end end end