Class: JenkinsApi::Client::PluginManager
- Inherits:
-
Object
- Object
- JenkinsApi::Client::PluginManager
- Defined in:
- lib/improved_jenkins_client/plugin_manager.rb
Overview
This classes communicates with the /pluginManager API for listing installed plugins, installing new plgins through hacks, and performing a lot of operations on installed plugins. It also gives the ability to obtain the details about available plugins in Jenkins update center by communicating with /updateCenter API.
Class Method Summary collapse
-
.plugin_action_method(action, post_endpoint) ⇒ Object
Defines a method to perform the given action on plugin(s).
Instance Method Summary collapse
-
#check_for_updates ⇒ Object
Requests the Jenkins plugin manager to check for updates by connecting to the update site.
-
#disable(plugins) ⇒ Object
Disables the specified plugin or list of plugins.
-
#downgrade(plugins) ⇒ Object
Downgrades the specified plugin or list of plugins.
-
#enable(plugins) ⇒ Object
Enables the specified plugin or list of plugins.
-
#get_available_info(plugin) ⇒ Hash
Obtains the information about a plugin that is available in the Jenkins update center.
-
#get_installed_info(plugin) ⇒ Hash
Obtains the details of a single installed plugin.
-
#initialize(client) ⇒ PluginManager
constructor
Initializes a new PluginManager object.
-
#install(plugins) ⇒ String
(also: #update)
Installs a specific plugin or list of plugins.
-
#list_available(filters = {}) ⇒ Hash<String, String>
List the available plugins from jenkins update center along with their version numbers.
-
#list_installed(filters = {}) ⇒ Hash<String, String>
Obtains the list of installed plugins from Jenkins along with their version numbers with optional filters.
-
#list_updates ⇒ Hash<String, String>
List the available updates for plugins from jenkins update center along with their version numbers.
-
#restart_required? ⇒ Boolean
Whether restart required for the completion of plugin installations/uninstallations.
-
#to_s ⇒ Object
Returns a string representation of PluginManager class.
-
#uninstall(plugins) ⇒ Object
Uninstalls the specified plugin or list of plugins.
-
#unpin(plugins) ⇒ Object
Unpins the specified plugin or list of plugins.
Constructor Details
#initialize(client) ⇒ PluginManager
Initializes a new PluginManager object.
37 38 39 40 |
# File 'lib/improved_jenkins_client/plugin_manager.rb', line 37 def initialize(client) @client = client @logger = @client.logger end |
Class Method Details
.plugin_action_method(action, post_endpoint) ⇒ Object
Defines a method to perform the given action on plugin(s)
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/improved_jenkins_client/plugin_manager.rb', line 54 def self.plugin_action_method(action, post_endpoint) define_method(action) do |plugins| plugins = [plugins] unless plugins.is_a?(Array) @logger.info "Performing '#{action}' on plugins: #{plugins.inspect}" plugins.each do |plugin| @client.api_post_request( "/pluginManager/plugin/#{plugin}/#{post_endpoint}" ) end end end |
Instance Method Details
#check_for_updates ⇒ Object
Requests the Jenkins plugin manager to check for updates by connecting to the update site.
438 439 440 |
# File 'lib/improved_jenkins_client/plugin_manager.rb', line 438 def check_for_updates @client.api_post_request("/pluginManager/checkUpdates") end |
#disable(plugins) ⇒ Object
Disables the specified plugin or list of plugins. This method makes a POST request for every plugin specified - so it might lead to some delay if a big list is provided.
431 |
# File 'lib/improved_jenkins_client/plugin_manager.rb', line 431 plugin_action_method :disable, :makeDisabled |
#downgrade(plugins) ⇒ Object
Downgrades the specified plugin or list of plugins. This method makes s POST request for every plugin specified - so it might lead to some delay if a big list is provided.
384 |
# File 'lib/improved_jenkins_client/plugin_manager.rb', line 384 plugin_action_method :downgrade, :downgrade |
#enable(plugins) ⇒ Object
Enables the specified plugin or list of plugins. This method makes a POST request for every plugin specified - so it might lead to some delay if a big list is provided.
415 |
# File 'lib/improved_jenkins_client/plugin_manager.rb', line 415 plugin_action_method :enable, :makeEnabled |
#get_available_info(plugin) ⇒ Hash
Obtains the information about a plugin that is available in the Jenkins update center
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/improved_jenkins_client/plugin_manager.rb', line 272 def get_available_info(plugin) plugins = @client.api_get_request( "/updateCenter/coreSource", "depth=1" )["availables"] matched_plugin = plugins.select do |a_plugin| a_plugin["name"] == plugin end if matched_plugin.empty? raise Exceptions::PluginNotFound.new( @logger, "Plugin '#{plugin}' is not found" ) else matched_plugin.first end end |
#get_installed_info(plugin) ⇒ Hash
Obtains the details of a single installed plugin
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/improved_jenkins_client/plugin_manager.rb', line 155 def get_installed_info(plugin) @logger.info "Obtaining the details of plugin: #{plugin}" plugins = @client.api_get_request( "/pluginManager", "depth=1" )["plugins"] matched_plugin = plugins.select do |a_plugin| a_plugin["shortName"] == plugin end if matched_plugin.empty? raise Exceptions::PluginNotFound.new( @logger, "Plugin '#{plugin}' is not found" ) else matched_plugin.first end end |
#install(plugins) ⇒ String Also known as: update
Installs a specific plugin or list of plugins. This method will install the latest available plugins that jenkins reports. The installation might not take place right away for some plugins and they might require restart of jenkins instances. This method makes a single POST request for the installation of multiple plugins. Updating plugins can be done the same way. When the install action is issued, it gets the latest version of the plugin if the plugin is outdated.
338 339 340 341 342 343 344 345 346 347 |
# File 'lib/improved_jenkins_client/plugin_manager.rb', line 338 def install(plugins) # Convert the input argument to an array if it is not already an array plugins = [plugins] unless plugins.is_a?(Array) @logger.info "Installing plugins: #{plugins.inspect}" # Build the form data to post to jenkins form_data = {} plugins.each { |plugin| form_data["plugin.#{plugin}.default"] = "on" } @client.api_post_request("/pluginManager/install", form_data) end |
#list_available(filters = {}) ⇒ Hash<String, String>
List the available plugins from jenkins update center along with their version numbers
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/improved_jenkins_client/plugin_manager.rb', line 215 def list_available(filters = {}) supported_filters = [:category, :dependency] filter_plural_map = { :dependency => "dependencies", :category => "categories" } unless filters.keys.all? { |filter| supported_filters.include?(filter) } raise ArgumentError, "Unsupported filters specified." + " Supported filters: #{supported_filters.inspect}" end # Compute the filters to be passed to the JSON tree parameter tree_filters = if filters.empty? "" else ",#{filters.keys.map{ |key| filter_plural_map[key] }.join(",")}" end availables = @client.api_get_request( "/updateCenter/coreSource", "tree=availables[name,version#{tree_filters}]" )["availables"] Hash[availables.map do |plugin| if filters.keys.all? do |key| !plugin[filter_plural_map[key]].nil? && plugin[filter_plural_map[key]].include?(filters[key]) end [plugin["name"], plugin["version"]] end end] end |
#list_installed(filters = {}) ⇒ Hash<String, String>
Obtains the list of installed plugins from Jenkins along with their version numbers with optional filters
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/improved_jenkins_client/plugin_manager.rb', line 108 def list_installed(filters = {}) supported_filters = [ :active, :bundled, :deleted, :downgradable, :enabled, :hasUpdate, :pinned ] unless filters.keys.all? { |filter| supported_filters.include?(filter) } raise ArgumentError, "Unsupported filters specified." + " Supported filters: #{supported_filters.inspect}" end tree_filters = filters.empty? ? "" : ",#{filters.keys.join(",")}" plugins = @client.api_get_request( "/pluginManager", "tree=plugins[shortName,version#{tree_filters}]" )["plugins"] installed = Hash[plugins.map do |plugin| if filters.keys.all? { |key| plugin[key.to_s] == filters[key] } [plugin["shortName"], plugin["version"]] end end.compact] installed end |
#list_updates ⇒ Hash<String, String>
List the available updates for plugins from jenkins update center along with their version numbers
304 305 306 307 308 309 310 |
# File 'lib/improved_jenkins_client/plugin_manager.rb', line 304 def list_updates updates = @client.api_get_request( "/updateCenter/coreSource", "tree=updates[name,version]" )["updates"] Hash[updates.map { |plugin| [plugin["name"], plugin["version"]] }] end |
#restart_required? ⇒ Boolean
Whether restart required for the completion of plugin installations/uninstallations
450 451 452 453 454 455 456 457 |
# File 'lib/improved_jenkins_client/plugin_manager.rb', line 450 def restart_required? response = @client.api_get_request( "/updateCenter", "tree=restartRequiredForCompletion" ) response["restartRequiredForCompletion"] || !list_installed(:deleted => true).empty? end |
#to_s ⇒ Object
Returns a string representation of PluginManager class.
44 45 46 |
# File 'lib/improved_jenkins_client/plugin_manager.rb', line 44 def to_s "#<JenkinsApi::Client::PluginManager>" end |
#uninstall(plugins) ⇒ Object
Uninstalls the specified plugin or list of plugins. Only the user installed plugins can be uninstalled. The plugins installed by default by jenkins (also known as bundled plugins) cannot be uninstalled. The call will succeed but the plugins wil still remain in jenkins installed. This method makes a POST request for every plugin requested - so it might lead to some delay if a big list is provided.
368 |
# File 'lib/improved_jenkins_client/plugin_manager.rb', line 368 plugin_action_method :uninstall, :doUninstall |
#unpin(plugins) ⇒ Object
Unpins the specified plugin or list of plugins. This method makes a POST request for every plugin specified - so it might lead to some delay if a big list is provided.
399 |
# File 'lib/improved_jenkins_client/plugin_manager.rb', line 399 plugin_action_method :unpin, :unpin |