diff --git a/lib/logstash/gemfile.rb b/lib/logstash/gemfile.rb index de4dc6ba8..675572e4c 100644 --- a/lib/logstash/gemfile.rb +++ b/lib/logstash/gemfile.rb @@ -15,6 +15,12 @@ module LogStash @gemset = nil end + def self.open(file_path) + gemfile = new(::File.new(file_path, "r+")) + gemfile.load + gemfile + end + def load @gemset ||= DSL.parse(@io.read) self @@ -51,6 +57,11 @@ module LogStash def remove(name) @gemset.remove_gem(name) end + + def close + save if @gemset + @io.close + end end class Gemset diff --git a/lib/logstash/pluginmanager/install.rb b/lib/logstash/pluginmanager/install.rb index 7fd94d2aa..55cf19578 100644 --- a/lib/logstash/pluginmanager/install.rb +++ b/lib/logstash/pluginmanager/install.rb @@ -32,7 +32,7 @@ class LogStash::PluginManager::Install < Clamp::Command end raise(LogStash::PluginManager::Error, "File #{LogStash::Environment::GEMFILE_PATH} does not exist or is not writable, aborting") unless File.writable?(LogStash::Environment::GEMFILE_PATH) - gemfile = LogStash::Gemfile.new(File.new(LogStash::Environment::GEMFILE_PATH, "r+")).load + gemfile = LogStash::Gemfile.open(LogStash::Environment::GEMFILE_PATH) # keep a copy of the gemset to revert on error original_gemset = gemfile.gemset.copy @@ -58,17 +58,8 @@ class LogStash::PluginManager::Install < Clamp::Command end if verify? # at this point we know that we either have a valid gem name & version or a valid .gem file path - - # if LogStash::PluginManager.plugin_file?(plugin) - # raise(LogStash::PluginManager::Error) unless cache_gem_file(plugin) - # spec = LogStash::PluginManager.plugin_file_spec(plugin) - # gemfile.update(spec.name, spec.version.to_s) - # else - # plugins.each{|tuple| gemfile.update(*tuple)} - # end end - install_list = LogStash::PluginManager.merge_duplicates(install_list) install_list.each{|plugin, version| gemfile.update(plugin, version)} gemfile.save @@ -94,6 +85,9 @@ class LogStash::PluginManager::Install < Clamp::Command end puts("Installation successful") + + ensure + gemfile.close if gemfile end # copy .gem file into bundler cache directory, log any error to $stderr diff --git a/lib/logstash/pluginmanager/uninstall.rb b/lib/logstash/pluginmanager/uninstall.rb index 85d5af235..8c576994f 100644 --- a/lib/logstash/pluginmanager/uninstall.rb +++ b/lib/logstash/pluginmanager/uninstall.rb @@ -15,7 +15,7 @@ class LogStash::PluginManager::Uninstall < Clamp::Command def execute raise(LogStash::PluginManager::Error, "File #{LogStash::Environment::GEMFILE_PATH} does not exist or is not writable, aborting") unless File.writable?(LogStash::Environment::GEMFILE_PATH) - gemfile = LogStash::Gemfile.new(File.new(LogStash::Environment::GEMFILE_PATH, "r+")).load + gemfile = LogStash::Gemfile.open(LogStash::Environment::GEMFILE_PATH) # keep a copy of the gemset to revert on error original_gemset = gemfile.gemset.copy @@ -45,5 +45,7 @@ class LogStash::PluginManager::Uninstall < Clamp::Command raise(LogStash::PluginManager::Error, "Uninstall aborted") end end + ensure + gemfile.close if gemfile end end diff --git a/lib/logstash/pluginmanager/update.rb b/lib/logstash/pluginmanager/update.rb index 62c18bbec..c0b17c318 100644 --- a/lib/logstash/pluginmanager/update.rb +++ b/lib/logstash/pluginmanager/update.rb @@ -12,7 +12,7 @@ class LogStash::PluginManager::Update < Clamp::Command parameter "[PLUGIN] ...", "Plugin name(s) to upgrade to latest version" def execute - gemfile = LogStash::Gemfile.new(File.new(LogStash::Environment::GEMFILE_PATH, "r+")).load + gemfile = LogStash::Gemfile.open(LogStash::Environment::GEMFILE_PATH) # keep a copy of the gemset to revert on error original_gemset = gemfile.gemset.copy @@ -60,6 +60,8 @@ class LogStash::PluginManager::Update < Clamp::Command end end puts("No plugin updated") if update_count.zero? + ensure + gemfile.close end private