Class: Puppet::Provider::ElasticKibana

Inherits:
Puppet::Provider
  • Object
show all
Defined in:
lib/puppet/provider/elastic_kibana.rb

Overview

Parent class for Kibana plugin providers.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = {}) ⇒ ElasticKibana

Provider constructor



148
149
150
151
# File 'lib/puppet/provider/elastic_kibana.rb', line 148

def initialize(value = {})
  super(value)
  @property_flush = {}
end

Class Attribute Details

.format_urlObject

Returns the value of attribute format_url.



8
9
10
# File 'lib/puppet/provider/elastic_kibana.rb', line 8

def format_url
  @format_url
end

.home_pathObject

Returns the value of attribute home_path.



8
9
10
# File 'lib/puppet/provider/elastic_kibana.rb', line 8

def home_path
  @home_path
end

.install_argsObject

Returns the value of attribute install_args.



8
9
10
# File 'lib/puppet/provider/elastic_kibana.rb', line 8

def install_args
  @install_args
end

.plugin_directoryObject

Returns the value of attribute plugin_directory.



8
9
10
# File 'lib/puppet/provider/elastic_kibana.rb', line 8

def plugin_directory
  @plugin_directory
end

.remove_argsObject

Returns the value of attribute remove_args.



8
9
10
# File 'lib/puppet/provider/elastic_kibana.rb', line 8

def remove_args
  @remove_args
end

Class Method Details

.instancesArray

Finds and returns all present resources on the host.

Returns:

  • (Array)

    array of providers



130
131
132
133
134
# File 'lib/puppet/provider/elastic_kibana.rb', line 130

def self.instances
  present_plugins.map do |plugin|
    new plugin
  end
end

.prefetch(resources) ⇒ Object

Puppet prefetch boilerplate.

Parameters:

  • resources (Hash)

    collection of resources extant on the system



139
140
141
142
143
144
145
# File 'lib/puppet/provider/elastic_kibana.rb', line 139

def self.prefetch(resources)
  instances.each do |prov|
    if (resource = resources[prov.name])
      resource.provider = prov
    end
  end
end

.present_pluginsArray<Hash>

Discovers plugins present on the system. This is essentially the same way that the node code does it, so we do it in native ruby to speed up the process and grab arbitrary metadata from the plugin json (which should always be present).

Returns:

  • (Array<Hash>)

    array of discovered providers on the host.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/puppet/provider/elastic_kibana.rb', line 27

def self.present_plugins
  plugins = Dir[File.join(home_path, plugin_directory, '*')].select do |directory|
    !File.basename(directory).start_with? '.' \
      and File.exist? File.join(directory, 'package.json')
  end
  plugins.map do |plugin|
    j = JSON.parse(File.read(File.join(plugin, 'package.json')))
    {
      name: File.basename(plugin),
      ensure: :present,
      provider: name,
      version: j['version']
    }
  end
end

Instance Method Details

#createSymbol

Sets the ensure property in the @property_flush hash.

Returns:

  • (Symbol)

    :present



102
103
104
# File 'lib/puppet/provider/elastic_kibana.rb', line 102

def create
  @property_flush[:ensure] = :present
end

#destroySymbol

Set flushed ensure property to absent.

Returns:

  • (Symbol)

    :absent



116
117
118
# File 'lib/puppet/provider/elastic_kibana.rb', line 116

def destroy
  @property_flush[:ensure] = :absent
end

#exists?Boolean

Determine whether this resource is present on the system.

Returns:

  • (Boolean)


109
110
111
# File 'lib/puppet/provider/elastic_kibana.rb', line 109

def exists?
  @property_hash[:ensure] == :present
end

#flushObject

Enforce the desired state dictated by the properties to flush from the provider.

Returns:

  • nil



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/puppet/provider/elastic_kibana.rb', line 47

def flush
  if @property_flush[:ensure] == :absent
    # Simply remove the plugin if it should be gone
    run_plugin self.class.remove_args + [resource[:name]]
  else
    run_plugin self.class.remove_args + [resource[:name]] unless @property_flush[:version].nil?
    run_plugin self.class.install_args + plugin_url
  end

  set_property_hash
end

#format_urlProc

Formats a url for the plugin command-line argument. Necessary since different versions of the Kibana plugin CLI tool accept URL arguments in differing ways.

Returns:

  • (Proc)

    a lambda that accepts the URL and scope binding and returns the formatted URL.



17
18
19
# File 'lib/puppet/provider/elastic_kibana.rb', line 17

def format_url
  self.class.format_url ||= ->(url, _) { [url] }
end

#plugin_urlArray<String>

Helps to format the plugin name for installation. That is, if we have a URL, pass it in correctly to the CLI tool.

Returns:

  • (Array<String>)

    array of name elements suitable for use in a Puppet::Provider#execute call.



73
74
75
76
77
78
79
80
81
# File 'lib/puppet/provider/elastic_kibana.rb', line 73

def plugin_url
  if !resource[:url].nil?
    format_url.call resource[:url], binding
  elsif !resource[:organization].nil?
    [[resource[:organization], resource[:name], resource[:version]].join('/')]
  else
    [resource[:name]]
  end
end

#run_plugin(args) ⇒ String

Wrap the plugin command in some helper functionality to set the right uid/gid.

Returns:

  • (String)

    debugging command output.



63
64
65
66
# File 'lib/puppet/provider/elastic_kibana.rb', line 63

def run_plugin(args)
  stdout = execute([command(:plugin)] + args, uid: 'kibana', gid: 'kibana')
  stdout.exitstatus.zero? ? debug(stdout) : raise(Puppet::Error, stdout)
end

#set_property_hashObject

Repopulates the @property_hash to the on-system state for the provider.



121
122
123
124
125
# File 'lib/puppet/provider/elastic_kibana.rb', line 121

def set_property_hash
  @property_hash = self.class.present_plugins.find do |p|
    p[:name] == resource[:name]
  end
end

#versionString

version property getter

Returns:

  • (String)

    version



95
96
97
# File 'lib/puppet/provider/elastic_kibana.rb', line 95

def version
  @property_hash[:version]
end

#version=(new_version) ⇒ String

version property setter

Returns:

  • (String)

    version



88
89
90
# File 'lib/puppet/provider/elastic_kibana.rb', line 88

def version=(new_version)
  @property_flush[:version] = new_version
end