Class: Kontena::PluginManager::Installer

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/kontena/plugin_manager/installer.rb

Constant Summary

Constants included from Common

Common::KRATES_PLUGIN

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

dummy_ui, gem_versions, install_dir, installed, installed?, plugin_debug?, plugins, prefix, rubygems_client, search_plugins, use_dummy_ui

Constructor Details

#initialize(plugin_name, pre: false, version: nil) ⇒ Installer

Create a new instance of plugin installer

Parameters:

  • plugin_name (String)
  • pre (Boolean) (defaults to: false)

    install a prerelease version if available

  • version (String) (defaults to: nil)

    install a specific version



17
18
19
20
21
# File 'lib/kontena/plugin_manager/installer.rb', line 17

def initialize(plugin_name, pre: false, version: nil)
  @plugin_name = plugin_name
  @pre = pre
  @version = version
end

Instance Attribute Details

#plugin_nameObject (readonly)

Returns the value of attribute plugin_name.



10
11
12
# File 'lib/kontena/plugin_manager/installer.rb', line 10

def plugin_name
  @plugin_name
end

#preObject (readonly)

Returns the value of attribute pre.



10
11
12
# File 'lib/kontena/plugin_manager/installer.rb', line 10

def pre
  @pre
end

#versionObject

Returns the value of attribute version.



11
12
13
# File 'lib/kontena/plugin_manager/installer.rb', line 11

def version
  @version
end

Instance Method Details

#available_upgradeObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/kontena/plugin_manager/installer.rb', line 57

def available_upgrade
  installed = installed(plugin_name)
  return false unless installed

  pre = installed.version.prerelease?
  latest = rubygems_client.latest_version(prefix(plugin_name), pre: pre? || pre)
  if latest > installed.version
    latest.to_s
  end
end

#commandObject



27
28
29
30
31
32
33
34
# File 'lib/kontena/plugin_manager/installer.rb', line 27

def command
  @command ||= Gem::DependencyInstaller.new(
    document: false,
    force: true,
    prerelease: pre,
    minimal_deps: true
  )
end

#installObject

Install a plugin



37
38
39
40
41
42
# File 'lib/kontena/plugin_manager/installer.rb', line 37

def install
  return install_uri if plugin_name.include?('://')
  plugin_version = version.nil? ? Gem::Requirement.default : Gem::Requirement.new(version)
  command.install(prefix(plugin_name), plugin_version)
  command.installed_gems
end

#install_uriObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/kontena/plugin_manager/installer.rb', line 44

def install_uri
  require 'tempfile'
  require 'open-uri'
  file = Tempfile.new(['kontena_plugin', '.gem'])
  open(plugin_name) do |input|
    file.write input.read
    file.close
  end
  self.class.new(file.path).install
ensure
  file.unlink
end

#pre?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/kontena/plugin_manager/installer.rb', line 23

def pre?
  !!@pre
end

#upgradeObject

Upgrade an installed plugin



69
70
71
72
73
74
75
# File 'lib/kontena/plugin_manager/installer.rb', line 69

def upgrade
  return install if version
  upgrade_to = available_upgrade
  if upgrade_to
    Installer.new(plugin_name, version: upgrade_to).install
  end
end