Class: Inspec::Plugin::V2::Installer

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
GemSpecHelper, Singleton
Defined in:
lib/inspec/plugin/v2/installer.rb

Overview

Handles all actions modifying the user’s plugin set:

  • Modifying the plugins.json file

  • Installing, updating, and removing gem-based plugins

Loading plugins is handled by Loader. Listing plugins is handled by Loader. Searching for plugins is handled by ???

Defined Under Namespace

Classes: InstalledVendorSet

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from GemSpecHelper

#loaded_recent_most_version_of?

Constructor Details

#initializeInstaller

Returns a new instance of Installer.



39
40
41
42
# File 'lib/inspec/plugin/v2/installer.rb', line 39

def initialize
  @loader = Inspec::Plugin::V2::Loader.new
  @registry = Inspec::Plugin::V2::Registry.instance
end

Instance Attribute Details

#conf_fileObject (readonly)

Returns the value of attribute conf_file.



33
34
35
# File 'lib/inspec/plugin/v2/installer.rb', line 33

def conf_file
  @conf_file
end

#loaderObject (readonly)

Returns the value of attribute loader.



33
34
35
# File 'lib/inspec/plugin/v2/installer.rb', line 33

def loader
  @loader
end

#registryObject (readonly)

Returns the value of attribute registry.



33
34
35
# File 'lib/inspec/plugin/v2/installer.rb', line 33

def registry
  @registry
end

Instance Method Details

#__resetObject

Testing API. Performs a hard reset on the installer and registry, and reloads the loader. Not for public use. TODO: bad timing coupling in tests



176
177
178
# File 'lib/inspec/plugin/v2/installer.rb', line 176

def __reset
  registry.__reset
end

#__reset_loaderObject



180
181
182
# File 'lib/inspec/plugin/v2/installer.rb', line 180

def __reset_loader
  @loader = Loader.new
end

#ensure_installed(name) ⇒ Object



52
53
54
# File 'lib/inspec/plugin/v2/installer.rb', line 52

def ensure_installed(name)
  plugin_installed?(name) || install(name)
end

#fetch_plugin_specs(fetcher, gem_name) ⇒ Object



168
169
170
171
# File 'lib/inspec/plugin/v2/installer.rb', line 168

def fetch_plugin_specs(fetcher, gem_name)
  plugin_dependency = Gem::Dependency.new(gem_name)
  fetcher.spec_for_dependency(plugin_dependency).flatten.first
end

#install(plugin_name, opts = {}) ⇒ Object

Installs a plugin. Defaults to assuming the plugin provided is a gem, and will try to install from whatever gemsources ‘rubygems` thinks it should use. If it’s a gem, installs it and its dependencies to the ‘gem_path`. The gem is not activated. If it’s a path, leaves it in place. Finally, updates the plugins.json file with the new information. No attempt is made to load the plugin.

Parameters:

  • plugin_name (String)
  • opts (Hash) (defaults to: {})

    The installation options

Options Hash (opts):

  • :gem_file (String)

    Path to a local gem file to install from

  • :path (String)

    Path to a file to be used as the entry point for a path-based plugin

  • :version (String)

    Version constraint for remote gem installs

  • :source (String)

    Alternate URL to use instead of rubygems.org



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/inspec/plugin/v2/installer.rb', line 69

def install(plugin_name, opts = {})
  # TODO: - check plugins.json for validity before trying anything that needs to modify it.
  validate_installation_opts(plugin_name, opts)

  # TODO: return installed thingy
  if opts[:path]
    install_from_path(plugin_name, opts)
  elsif opts[:gem_file]
    gem_version = install_from_gem_file(plugin_name, opts)
    opts[:version] = gem_version.to_s
  else
    gem_version = install_from_remote_gems(plugin_name, opts)
    opts[:version] = gem_version.to_s
  end

  update_plugin_config_file(plugin_name, opts.merge({ action: :install }))
end

#plugin_installed?(name) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/inspec/plugin/v2/installer.rb', line 44

def plugin_installed?(name)
  list_installed_plugin_gems.detect { |spec| spec.name == name }
end

#plugin_version_installed?(name, version) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/inspec/plugin/v2/installer.rb', line 48

def plugin_version_installed?(name, version)
  list_installed_plugin_gems.detect { |spec| spec.name == name && spec.version == Gem::Version.new(version) }
end

#search(plugin_query, opts = {}) ⇒ Hash of Arrays

Search rubygems.org for a plugin gem.

Parameters:

  • plugin_seach_term (String)
  • opts (Hash) (defaults to: {})

    Search options

Options Hash (opts):

  • :exact (TrueClass, FalseClass)

    If true, use plugin_search_term exactly. If false (default), append a wildcard.

  • :scope (Symbol)

    Which versions to search for. :released (default) - all released versions. :prerelease - Also include versioned marked prerelease. :latest - only return one version, the latest one.

Returns:

  • (Hash of Arrays)
    • Keys are String names of gems, arrays contain String versions.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/inspec/plugin/v2/installer.rb', line 136

def search(plugin_query, opts = {}) # rubocop: disable Metrics/AbcSize
  validate_search_opts(plugin_query, opts)

  fetcher = Gem::SpecFetcher.fetcher
  if opts[:source]
    source_list = Gem::SourceList.from([opts[:source]])
    fetcher = Gem::SpecFetcher.new(source_list)
  end

  matched_tuples = []
  if opts[:exact]
    matched_tuples = fetcher.detect(opts[:scope]) { |tuple| tuple.name == plugin_query }
  else
    regex = Regexp.new("^" + plugin_query + ".*")
    matched_tuples = fetcher.detect(opts[:scope]) do |tuple|
      tuple.name =~ regex && !Inspec::Plugin::V2::PluginFilter.exclude?(tuple.name)
    end
  end

  # sort tuples
  matched_tuples.sort! { |a, b| b.first.version <=> a.first.version }

  gem_info = {}
  matched_tuples.each do |tuple|
    gem_info[tuple.first.name] ||= {}
    gem_info[tuple.first.name]["versions"] ||= []
    gem_info[tuple.first.name]["versions"] << tuple.first.version.to_s
    gem_info[tuple.first.name]["description"] ||= fetch_plugin_specs(fetcher, tuple.first.name)&.summary
  end
  gem_info
end

#uninstall(plugin_name, opts = {}) ⇒ Object

Uninstalls (removes) a plugin. Refers to plugin.json to determine if it was a gem-based or path-based install. If it’s a gem, uninstalls it, and all other unused plugins. If it’s a path, removes the reference from the plugins.json, but does not tamper with the plugin source tree. Either way, the plugins.json file is updated with the new information.

Parameters:

  • plugin_name (String)
  • opts (Hash) (defaults to: {})

    The uninstallation options. Currently unused.



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/inspec/plugin/v2/installer.rb', line 116

def uninstall(plugin_name, opts = {})
  # TODO: - check plugins.json for validity before trying anything that needs to modify it.
  validate_uninstall_opts(plugin_name, opts)

  if registry.path_based_plugin?(plugin_name)
    uninstall_via_path(plugin_name, opts)
  else
    uninstall_via_gem(plugin_name, opts)
  end

  update_plugin_config_file(plugin_name, opts.merge({ action: :uninstall }))
end

#update(plugin_name, opts = {}) ⇒ Object

Updates a plugin. Most options same as install, but will not handle path installs. If no :version is provided, updates to the latest. If a version is provided, the plugin becomes pinned at that specified version.

Parameters:

  • plugin_name (String)
  • opts (Hash) (defaults to: {})

    The installation options

Options Hash (opts):

  • :gem_file (String)

    Reserved for future use. No effect.

  • :version (String)

    Version constraint for remote gem updates



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/inspec/plugin/v2/installer.rb', line 95

def update(plugin_name, opts = {})
  # TODO: - check plugins.json for validity before trying anything that needs to modify it.
  validate_update_opts(plugin_name, opts)
  opts[:update_mode] = true

  # TODO: Handle installing from a local file
  # TODO: Perform dependency checks to make sure the new solution is valid
  gem_version = install_from_remote_gems(plugin_name, opts)

  update_plugin_config_file(plugin_name, opts.merge({ action: :update, version: gem_version.to_s }))
end