From 5adff0288919954c3446036025e72c704bbd3e6f Mon Sep 17 00:00:00 2001 From: uu59 Date: Tue, 20 May 2014 13:34:20 +0900 Subject: [PATCH] Actually uninstall plugin --- app/controllers/plugins_controller.rb | 3 +-- app/models/plugin.rb | 23 ++++++++++++++++------- app/workers/gem_installer.rb | 9 ++++++--- app/workers/gem_uninstaller.rb | 20 ++++++++++++++++++++ 4 files changed, 43 insertions(+), 12 deletions(-) create mode 100644 app/workers/gem_uninstaller.rb diff --git a/app/controllers/plugins_controller.rb b/app/controllers/plugins_controller.rb index 09455ca..5fd2351 100644 --- a/app/controllers/plugins_controller.rb +++ b/app/controllers/plugins_controller.rb @@ -24,8 +24,7 @@ class PluginsController < ApplicationController def uninstall params[:plugins].each do |gem_name| - pl = Plugin.new(gem_name: gem_name) - pl.uninstall! + GemUninstaller.new.async.perform(gem_name) end redirect_to plugins_path end diff --git a/app/models/plugin.rb b/app/models/plugin.rb index 0b08b8c..a975f5a 100644 --- a/app/models/plugin.rb +++ b/app/models/plugin.rb @@ -31,12 +31,14 @@ class Plugin # NOTE: do not uninstall gem actually for now. because it is not necessary, and slow job # NOTE: should uninstall that situation: installed verions is A, self.version is NOT A. only check gem_name. - new_gemfile = "" - File.open(gemfile_path).each_line do |line| - next if line.include?(%Q|gem "#{gem_name}"|) - new_gemfile << line + if gem_uninstall + new_gemfile = "" + File.open(gemfile_path).each_line do |line| + next if line.include?(%Q|gem "#{gem_name}"|) + new_gemfile << line + end + File.open(gemfile_path, "w"){|f| f.write new_gemfile } end - File.open(gemfile_path, "w"){|f| f.write new_gemfile } end def upgrade!(new_version) @@ -121,10 +123,17 @@ class Plugin fluent_gem("install", gem_name, "-v", version) end + def gem_uninstall + fluent_gem("uninstall", gem_name, "-x", "-a") + end + def fluent_gem(*commands) # NOTE: use `fluent-gem` instead of `gem` - unless system(*%W(bundle exec fluent-gem) + commands) # TODO: should grab stdout/stderr - raise GemError, "failed command #{commands.join(" ")}" + Bundler.with_clean_env do + # NOTE: this app is under the Bundler, so call `system` in with_clean_env is Bundler jail breaking + unless system(*%W(fluent-gem) + commands) # TODO: should grab stdout/stderr + raise GemError, "failed command #{commands.join(" ")}" + end end true end diff --git a/app/workers/gem_installer.rb b/app/workers/gem_installer.rb index 143973a..48e8ed7 100644 --- a/app/workers/gem_installer.rb +++ b/app/workers/gem_installer.rb @@ -9,9 +9,12 @@ class GemInstaller pl = Plugin.new(gem_name: gem_name, version: version) unless WORKING.find{|p| p.gem_name == pl.gem_name} WORKING.push(pl) - pl.uninstall! if pl.installed? - pl.install! - WORKING.delete(pl) + begin + pl.uninstall! if pl.installed? + pl.install! + ensure + WORKING.delete(pl) + end end SuckerPunch.logger.info "installed #{gem_name} #{version}" end diff --git a/app/workers/gem_uninstaller.rb b/app/workers/gem_uninstaller.rb new file mode 100644 index 0000000..353a418 --- /dev/null +++ b/app/workers/gem_uninstaller.rb @@ -0,0 +1,20 @@ +class GemUninstaller + include SuckerPunch::Job + workers 16 + + WORKING = [] + + def perform(gem_name) + SuckerPunch.logger.info "uninstall #{gem_name}" + pl = Plugin.new(gem_name: gem_name) + unless WORKING.find{|p| p.gem_name == pl.gem_name} + WORKING.push(pl) + begin + pl.uninstall! + ensure + WORKING.delete(pl) + end + end + SuckerPunch.logger.info "uninstalled #{gem_name}" + end +end