From a09cce0202a40fb0b0dd0ec3cae82af0a7b6f2eb Mon Sep 17 00:00:00 2001 From: uu59 Date: Thu, 15 May 2014 15:23:51 +0900 Subject: [PATCH] Add latest_version method for use Rubygems API, allow Plugin#version is empty for install --- Gemfile.lock | 9 ++----- app/controllers/plugins_controller.rb | 3 ++- app/models/plugin.rb | 36 +++++++++++++++++++++------ fluentd-ui.gemspec | 1 + 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 06aa5fe..0d0e7ca 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -7,6 +7,7 @@ PATH coffee-rails (~> 4.0.0) fluentd (= 0.10.46) haml-rails (~> 0.5.3) + httpclient i18n_generators (= 1.2.1) jbuilder (~> 2.0) jquery-rails (~> 3.1.0) @@ -46,7 +47,6 @@ GEM tzinfo (~> 1.1) arel (5.0.1.20140414130214) bcrypt (3.1.7) - bson (1.10.0) builder (3.2.2) capybara (2.2.1) mime-types (>= 1.16) @@ -74,9 +74,6 @@ GEM factory_girl_rails (4.4.1) factory_girl (~> 4.4.0) railties (>= 3.0.0) - fluent-plugin-mongo (0.7.3) - fluentd (~> 0.10.9) - mongo (>= 1.8.0) fluentd (0.10.46) cool.io (>= 1.1.1, < 2.0.0, != 1.2.0) http_parser.rb (>= 0.5.1, < 0.7.0) @@ -95,6 +92,7 @@ GEM http-cookie (1.0.2) domain_name (~> 0.5) http_parser.rb (0.6.0) + httpclient (2.3.4.1) i18n (0.6.9) i18n_generators (1.2.1) mechanize @@ -122,8 +120,6 @@ GEM mime-types (1.25.1) mini_portile (0.5.3) minitest (5.3.3) - mongo (1.10.0) - bson (~> 1.10.0) msgpack (0.5.8) multi_json (1.10.0) net-http-digest_auth (1.4) @@ -215,7 +211,6 @@ DEPENDENCIES capybara (~> 2.2.1) database_cleaner (~> 1.2.0) factory_girl_rails - fluent-plugin-mongo (= 0.7.3) fluentd-ui! pry rake diff --git a/app/controllers/plugins_controller.rb b/app/controllers/plugins_controller.rb index 53f9b0d..d3a149b 100644 --- a/app/controllers/plugins_controller.rb +++ b/app/controllers/plugins_controller.rb @@ -20,7 +20,8 @@ class PluginsController < ApplicationController def plugins # TODO [ - Plugin.new(gem_name: "fluent-plugin-mongo", version: "0.7.3") + Plugin.new(gem_name: "fluent-plugin-mongo", version: "0.7.3"), + Plugin.new(gem_name: "fluent-plugin-s3"), ] end diff --git a/app/models/plugin.rb b/app/models/plugin.rb index 951bc38..2855b12 100644 --- a/app/models/plugin.rb +++ b/app/models/plugin.rb @@ -1,4 +1,6 @@ require "fileutils" +require "json" +require "httpclient" class Plugin class GemError < StandardError; end @@ -14,21 +16,25 @@ class Plugin end def install! - if valid? && !installed? - if fluent_gem("install", gem_name, "-v", version) - File.open(gemfile_path, "a") do |f| - f.puts format_gemfile + unless installed? + self.version = latest_version unless version + if valid? + if fluent_gem("install", gem_name, "-v", version) + File.open(gemfile_path, "a") do |f| + f.puts format_gemfile + end end end end end def uninstall! - if valid? && installed? + if installed? # 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. new_gemfile = "" File.open(gemfile_path).each_line do |line| - next if line.strip == format_gemfile + next if line.include?(%Q|gem "#{gem_name}"|) new_gemfile << line end File.open(gemfile_path, "w"){|f| f.write new_gemfile } @@ -46,18 +52,34 @@ class Plugin end def installed? - File.read(gemfile_path).lines.map(&:strip).grep(format_gemfile).present? + self.class.installed.find do |plugin| + plugin.gem_name == gem_name + end end def format_gemfile + self.version = latest_version unless version %Q|gem "#{gem_name}", "#{version}"| end + def latest_version + res = HTTPClient.get("https://rubygems.org/api/v1/versions/#{gem_name}.json") + if res.code == 200 + JSON.parse(res.body).map {|ver| Gem::Version.new ver["number"] }.max.to_s + end + end + def self.gemfile_changed? # if true, rails server needs to restart } @initial_gemfile_content != File.read(gemfile_path) end + def self.installed + File.read(gemfile_path).scan(/"(.*?)", "(.*?)"/).map do |plugin| + new(gem_name: plugin[0], version: plugin[1]) + end + end + def self.gemfile_path if Rails.env == "test" gemfile_path = "/tmp/fluentd-ui-test-Gemfile.plugins" # can't create a file under Rails.root directory on Circle CI diff --git a/fluentd-ui.gemspec b/fluentd-ui.gemspec index 62d8966..84a2660 100644 --- a/fluentd-ui.gemspec +++ b/fluentd-ui.gemspec @@ -30,5 +30,6 @@ Gem::Specification.new do |spec| spec.add_dependency 'jquery-rails', "~> 3.1.0" spec.add_dependency 'jbuilder', '~> 2.0' spec.add_dependency "bundler", "~> 1.5" + spec.add_dependency "httpclient" end