From: Antonio Terceiro Date: Wed, 27 May 2015 09:36:17 -0300 Subject: Support system-installed plugins Plugins must be installed as regular Ruby libraries, and they must contain /usr/share/vagrant-plugins/plugins.d/$PLUGINNAME.json with the following content: { "${PLUGINNAME}": { "ruby_version":"$(ruby -e 'puts RUBY_VERSION')", "vagrant_version":"$(cat /usr/share/vagrant/version.txt)", "gem_version":"", "require":"", "sources":[] } } --- lib/vagrant/plugin/manager.rb | 4 ++-- lib/vagrant/plugin/state_file.rb | 28 +++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/vagrant/plugin/manager.rb b/lib/vagrant/plugin/manager.rb index dfaab2e..35122e0 100644 --- a/lib/vagrant/plugin/manager.rb +++ b/lib/vagrant/plugin/manager.rb @@ -18,7 +18,7 @@ module Vagrant # Returns the path to the [StateFile] for system plugins. def self.system_plugins_file - dir = Vagrant.installer_embedded_dir + dir = '/usr/share/vagrant-plugins' return nil if !dir Pathname.new(dir).join("plugins.json") end @@ -33,7 +33,7 @@ module Vagrant system_path = self.class.system_plugins_file @system_file = nil - @system_file = StateFile.new(system_path) if system_path && system_path.file? + @system_file = StateFile.new(system_path, true) if system_path && system_path.file? end # Installs another plugin into our gem directory. diff --git a/lib/vagrant/plugin/state_file.rb b/lib/vagrant/plugin/state_file.rb index f41da1b..717b353 100644 --- a/lib/vagrant/plugin/state_file.rb +++ b/lib/vagrant/plugin/state_file.rb @@ -5,8 +5,9 @@ module Vagrant # This is a helper to deal with the plugin state file that Vagrant # uses to track what plugins are installed and activated and such. class StateFile - def initialize(path) + def initialize(path, system = false) @path = path + @system = system @data = {} if @path.exist? @@ -22,6 +23,21 @@ module Vagrant @data["version"] ||= "1" @data["installed"] ||= {} + load_extra_plugins + end + + def load_extra_plugins + extra_plugins = Dir.glob(@path.dirname.join('plugins.d', '*.json')) + extra_plugins.each do |filename| + json = File.read(filename) + begin + plugin_data = JSON.parse(json) + @data["installed"].merge!(plugin_data) + rescue JSON::ParserError => e + raise Vagrant::Errors::PluginStateFileParseError, + path: filename, message: e.message + end + end end # Add a plugin that is installed to the state file. @@ -91,8 +107,14 @@ module Vagrant # This saves the state back into the state file. def save! - @path.open("w+") do |f| - f.write(JSON.dump(@data)) + begin + @path.open("w+") do |f| + f.write(JSON.dump(@data)) + end + rescue Errno::EACCES + # Ignore permission denied against system-installed plugins; regular + # users are not supposed to write there. + raise unless @system end end