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.



73
74
75
76
# File 'lib/puppet/provider/elastic_user_command.rb', line 73

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

Instance Attribute Details

#homedirObject

Returns the value of attribute homedir.



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

def homedir
  @homedir
end

Class Method Details

.command_with_path(args, configdir = nil) ⇒ Object

Run the user management command with specified tool arguments.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/puppet/provider/elastic_user_command.rb', line 19

def self.command_with_path(args, configdir = nil)
  options = {
    :custom_environment => {
      'ES_PATH_CONF' => configdir || '/etc/elasticsearch'
    }
  }

  execute(
    [command(:users_cli)] + (args.is_a?(Array) ? args : [args]),
    options
  )
end

.fetch_usersObject

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/puppet/provider/elastic_user_command.rb', line 33

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



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

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.



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

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

.prefetch(resources) ⇒ Object

Generic prefetch boilerplate.



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

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`.



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

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

#destroyObject

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



108
109
110
# File 'lib/puppet/provider/elastic_user_command.rb', line 108

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

#exists?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/puppet/provider/elastic_user_command.rb', line 103

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

#flushObject

Enforce the desired state for this user on-disk.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/puppet/provider/elastic_user_command.rb', line 79

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, resource[:configdir])
  @property_hash = self.class.fetch_users.detect do |u|
    u[:name] == resource[:name]
  end
end

#passwdObject

Manually set this user’s password.



113
114
115
116
117
118
119
120
121
122
# File 'lib/puppet/provider/elastic_user_command.rb', line 113

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