Class: Puppet::Provider::ElasticUserCommand

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

Overview

Parent provider for Elasticsearch Shield/X-Pack file-based user management tools.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = {}) ⇒ ElasticUserCommand

Returns a new instance of ElasticUserCommand.



65
66
67
68
# File 'lib/puppet/provider/elastic_user_command.rb', line 65

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

Instance Attribute Details

#homedirObject

Returns the value of attribute homedir.



5
6
7
# File 'lib/puppet/provider/elastic_user_command.rb', line 5

def homedir
  @homedir
end

Class Method Details

.command_with_path(args) ⇒ Object

Run the user management command with specified tool arguments.



20
21
22
# File 'lib/puppet/provider/elastic_user_command.rb', line 20

def self.command_with_path(args)
  users_cli(args.is_a?(Array) ? args : [args])
end

.fetch_usersObject

Gather local file-based users into an array of Hash objects.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/puppet/provider/elastic_user_command.rb', line 25

def self.fetch_users
  begin
    output = command_with_path('list')
  rescue Puppet::ExecutionFailure => e
    debug("#fetch_users had an error: #{e.inspect}")
    return nil
  end

  debug("Raw command output: #{output}")
  output.split("\n").select { |u|
    # Keep only expected "user : role1,role2" formatted lines
    u[/^[^:]+:\s+\S+$/]
  }.map { |u|
    # Break into ["user ", " role1,role2"]
    u.split(':').first.strip
  }.map do |user|
    {
      :name => user,
      :ensure => :present,
      :provider => name,
    }
  end
end

.homedirObject

Elasticsearch’s home directory.

Returns:

  • String



10
11
12
13
14
15
16
17
# File 'lib/puppet/provider/elastic_user_command.rb', line 10

def self.homedir
  @homedir ||= case Facter.value('osfamily')
               when 'OpenBSD'
                 '/usr/local/elasticsearch'
               else
                 '/usr/share/elasticsearch'
               end
end

.instancesObject

Fetch an array of provider objects from the the list of local users.



50
51
52
53
54
# File 'lib/puppet/provider/elastic_user_command.rb', line 50

def self.instances
  fetch_users.map do |user|
    new user
  end
end

.prefetch(resources) ⇒ Object

Generic prefetch boilerplate.



57
58
59
60
61
62
63
# File 'lib/puppet/provider/elastic_user_command.rb', line 57

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

Instance Method Details

#createObject

Set this provider’s ‘:ensure` property to `:present`.



91
92
93
# File 'lib/puppet/provider/elastic_user_command.rb', line 91

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

#destroyObject

Set this provider’s ‘:ensure` property to `:absent`.



100
101
102
# File 'lib/puppet/provider/elastic_user_command.rb', line 100

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

#exists?Boolean

Returns:

  • (Boolean)


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

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

#flushObject

Enforce the desired state for this user on-disk.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/puppet/provider/elastic_user_command.rb', line 71

def flush
  arguments = []

  case @property_flush[:ensure]
  when :absent
    arguments << 'userdel'
    arguments << resource[:name]
  else
    arguments << 'useradd'
    arguments << resource[:name]
    arguments << '-p' << resource[:password]
  end

  self.class.command_with_path(arguments)
  @property_hash = self.class.fetch_users.detect do |u|
    u[:name] == resource[:name]
  end
end

#passwdObject

Manually set this user’s password.



105
106
107
108
109
110
111
# File 'lib/puppet/provider/elastic_user_command.rb', line 105

def passwd
  self.class.command_with_path([
    'passwd',
    resource[:name],
    '-p', resource[:password]
  ])
end