Class: Plugin
- Inherits:
-
Object
- Object
- Plugin
- Includes:
- ActiveModel::Model
- Defined in:
- app/models/plugin.rb
Defined Under Namespace
Classes: GemError
Constant Summary collapse
- WORKING =
[]
Instance Attribute Summary collapse
-
#category ⇒ Object
Returns the value of attribute category.
-
#gem_name ⇒ Object
Returns the value of attribute gem_name.
-
#version ⇒ Object
Returns the value of attribute version.
Class Method Summary collapse
- .fluent_gem_path ⇒ Object
- .installed ⇒ Object
- .installing ⇒ Object
- .processing ⇒ Object
- .recommended ⇒ Object
- .uninstalling ⇒ Object
Instance Method Summary collapse
- #authors ⇒ Object
- #gem_json_url ⇒ Object
- #gem_versions ⇒ Object
- #gem_versions! ⇒ Object
- #inspect ⇒ Object
- #install! ⇒ Object
- #installed? ⇒ Boolean
- #installed_version ⇒ Object
- #latest_version ⇒ Object
- #latest_version? ⇒ Boolean
- #processing? ⇒ Boolean
- #released_versions ⇒ Object
- #rubygems_org_page ⇒ Object
- #summary ⇒ Object
- #to_param ⇒ Object
- #uninstall! ⇒ Object
- #upgrade!(new_version) ⇒ Object
Instance Attribute Details
#category ⇒ Object
Returns the value of attribute category.
12 13 14 |
# File 'app/models/plugin.rb', line 12 def category @category end |
#gem_name ⇒ Object
Returns the value of attribute gem_name.
12 13 14 |
# File 'app/models/plugin.rb', line 12 def gem_name @gem_name end |
#version ⇒ Object
Returns the value of attribute version.
12 13 14 |
# File 'app/models/plugin.rb', line 12 def version @version end |
Class Method Details
.fluent_gem_path ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'app/models/plugin.rb', line 141 def self.fluent_gem_path # On installed both td-agent and fluentd system, decide which fluent-gem command should be used depend on setup(Fluentd.instance) if Fluentd.instance && Fluentd.instance.fluentd? return "fluent-gem" # maybe `fluent-gem` command is in the $PATH end # NOTE: td-agent has a command under the /usr/lib{,64}, td-agent2 has under /opt/td-agent %W( /opt/td-agent/embedded/bin/fluent-gem /usr/lib/fluent/ruby/bin/fluent-gem /usr/lib64/fluent/ruby/bin/fluent-gem ).find do |path| system("which #{path}", out: File::NULL, err: File::NULL) end end |
.installed ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'app/models/plugin.rb', line 88 def self.installed Rails.cache.fetch("installed_gems", expires_in: 3.seconds) do Bundler.with_clean_env do gems = `#{fluent_gem_path} list`.try(:lines) return [] unless gems gems.grep(/fluent-plugin/).map do |gem| name, versions_str = gem.strip.split(" ") version = versions_str[/[^(), ]+/] new(gem_name: name, version: version) end end end end |
.installing ⇒ Object
114 115 116 |
# File 'app/models/plugin.rb', line 114 def self.installing processing.find_all{|data| data[:type] == :install }.map{|data| data[:plugin] } end |
.processing ⇒ Object
108 109 110 111 112 |
# File 'app/models/plugin.rb', line 108 def self.processing WORKING.find_all do |data| data[:state] == :running end end |
.recommended ⇒ Object
102 103 104 105 106 |
# File 'app/models/plugin.rb', line 102 def self.recommended Settings.recommended_plugins.map do |data| new(category: data["category"], gem_name: "fluent-plugin-#{data["name"]}") end end |
.uninstalling ⇒ Object
118 119 120 |
# File 'app/models/plugin.rb', line 118 def self.uninstalling processing.find_all{|data| data[:type] == :uninstall }.map{|data| data[:plugin] } end |
Instance Method Details
#authors ⇒ Object
74 75 76 77 |
# File 'app/models/plugin.rb', line 74 def target_version = self.version || latest_version JSON.parse(gem_versions).find {|ver| ver["number"] == target_version }.try(:[], "authors") end |
#gem_json_url ⇒ Object
137 138 139 |
# File 'app/models/plugin.rb', line 137 def gem_json_url "https://rubygems.org/api/v1/versions/#{gem_name}.json" end |
#gem_versions ⇒ Object
122 123 124 125 126 |
# File 'app/models/plugin.rb', line 122 def gem_versions Rails.cache.fetch(gem_json_url, expires_in: 60.minutes) do # NOTE: 60.minutes could be changed if it doesn't fit gem_versions! end end |
#gem_versions! ⇒ Object
128 129 130 131 132 133 134 135 |
# File 'app/models/plugin.rb', line 128 def gem_versions! res = HTTPClient.get(gem_json_url) if res.code == 200 json = res.body Rails.cache.write(gem_json_url, json, expires_in: 60.minutes) # NOTE: 60.minutes could be changed if it doesn't fit json end end |
#inspect ⇒ Object
79 80 81 82 |
# File 'app/models/plugin.rb', line 79 def inspect self.version ||= latest_version %Q|<#{gem_name}, "#{version}">| end |
#install! ⇒ Object
20 21 22 23 24 |
# File 'app/models/plugin.rb', line 20 def install! return if installed? self.version ||= latest_version valid? && gem_install end |
#installed? ⇒ Boolean
42 43 44 45 46 |
# File 'app/models/plugin.rb', line 42 def installed? self.class.installed.find do |plugin| plugin.gem_name == gem_name end end |
#installed_version ⇒ Object
52 53 54 55 |
# File 'app/models/plugin.rb', line 52 def installed_version return unless inst = installed? inst.version end |
#latest_version ⇒ Object
61 62 63 |
# File 'app/models/plugin.rb', line 61 def latest_version @latest_version ||= JSON.parse(gem_versions).map {|ver| Gem::Version.new ver["number"] }.max.to_s end |
#latest_version? ⇒ Boolean
57 58 59 |
# File 'app/models/plugin.rb', line 57 def latest_version? installed_version == latest_version end |
#processing? ⇒ Boolean
48 49 50 |
# File 'app/models/plugin.rb', line 48 def processing? !!WORKING.find{|data| data[:plugin].gem_name == gem_name} end |
#released_versions ⇒ Object
65 66 67 |
# File 'app/models/plugin.rb', line 65 def released_versions @released_versions ||= JSON.parse(gem_versions).map {|ver| ver["number"]}.sort_by{|ver| Gem::Version.new ver}.reverse end |
#rubygems_org_page ⇒ Object
84 85 86 |
# File 'app/models/plugin.rb', line 84 def rubygems_org_page "https://rubygems.org/gems/#{gem_name}" end |
#summary ⇒ Object
69 70 71 72 |
# File 'app/models/plugin.rb', line 69 def summary target_version = self.version || latest_version JSON.parse(gem_versions).find {|ver| ver["number"] == target_version }.try(:[], "summary") end |
#to_param ⇒ Object
16 17 18 |
# File 'app/models/plugin.rb', line 16 def to_param gem_name end |
#uninstall! ⇒ Object
26 27 28 29 |
# File 'app/models/plugin.rb', line 26 def uninstall! return unless installed? gem_uninstall # NOTE: not validate end |
#upgrade!(new_version) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'app/models/plugin.rb', line 31 def upgrade!(new_version) return unless installed? upgrade = self.class.new(gem_name: self.gem_name, version: new_version) if self.valid? && upgrade.valid? self.uninstall! upgrade.install! self.version = upgrade.version end end |