Upgrade to 3rdparty version 0.9.4 of elasticsearch/elasticsearch
[mirror/dsa-puppet.git] / 3rdparty / modules / elasticsearch / manifests / instance.pp
1 # == Define: elasticsearch::instance
2 #
3 #  This define allows you to create or remove an elasticsearch instance
4 #
5 # === Parameters
6 #
7 # [*ensure*]
8 #   String. Controls if the managed resources shall be <tt>present</tt> or
9 #   <tt>absent</tt>. If set to <tt>absent</tt>:
10 #   * The managed software packages are being uninstalled.
11 #   * Any traces of the packages will be purged as good as possible. This may
12 #     include existing configuration files. The exact behavior is provider
13 #     dependent. Q.v.:
14 #     * Puppet type reference: {package, "purgeable"}[http://j.mp/xbxmNP]
15 #     * {Puppet's package provider source code}[http://j.mp/wtVCaL]
16 #   * System modifications (if any) will be reverted as good as possible
17 #     (e.g. removal of created users, services, changed log settings, ...).
18 #   * This is thus destructive and should be used with care.
19 #   Defaults to <tt>present</tt>.
20 #
21 # [*status*]
22 #   String to define the status of the service. Possible values:
23 #   * <tt>enabled</tt>: Service is running and will be started at boot time.
24 #   * <tt>disabled</tt>: Service is stopped and will not be started at boot
25 #     time.
26 #   * <tt>running</tt>: Service is running but will not be started at boot time.
27 #     You can use this to start a service on the first Puppet run instead of
28 #     the system startup.
29 #   * <tt>unmanaged</tt>: Service will not be started at boot time and Puppet
30 #     does not care whether the service is running or not. For example, this may
31 #     be useful if a cluster management software is used to decide when to start
32 #     the service plus assuring it is running on the desired node.
33 #   Defaults to <tt>enabled</tt>. The singular form ("service") is used for the
34 #   sake of convenience. Of course, the defined status affects all services if
35 #   more than one is managed (see <tt>service.pp</tt> to check if this is the
36 #   case).
37 #
38 # [*config*]
39 #   Elasticsearch configuration hash
40 #
41 # [*configdir*]
42 #   Path to directory containing the elasticsearch configuration.
43 #   Use this setting if your packages deviate from the norm (/etc/elasticsearch)
44 #
45 # [*datadir*]
46 #   Allows you to set the data directory of Elasticsearch
47 #
48 # [*logging_file*]
49 #   Instead of a hash you can supply a puppet:// file source for the logging.yml file
50 #
51 # [*logging_config*]
52 #   Hash representation of information you want in the logging.yml file
53 #
54 # [*logging_template*]
55 #  Use a custom logging template - just supply the reative path ie ${module}/elasticsearch/logging.yml.erb
56 #
57 # [*logging_level*]
58 #   Default logging level for Elasticsearch.
59 #   Defaults to: INFO
60 #
61 # [*init_defaults*]
62 #   Defaults file content in hash representation
63 #
64 # [*init_defaults_file*]
65 #   Defaults file as puppet resource
66 #
67 # === Authors
68 #
69 # * Richard Pijnenburg <mailto:richard.pijnenburg@elasticsearch.com>
70 #
71 define elasticsearch::instance(
72   $ensure             = $elasticsearch::ensure,
73   $status             = $elasticsearch::status,
74   $config             = undef,
75   $configdir          = undef,
76   $datadir            = undef,
77   $logging_file       = undef,
78   $logging_config     = undef,
79   $logging_template   = undef,
80   $logging_level      = $elasticsearch::default_logging_level,
81   $init_defaults      = undef,
82   $init_defaults_file = undef
83 ) {
84
85   require elasticsearch::params
86
87   File {
88     owner => $elasticsearch::elasticsearch_user,
89     group => $elasticsearch::elasticsearch_group,
90   }
91
92   Exec {
93     path => [ '/bin', '/usr/bin', '/usr/local/bin' ],
94     cwd  => '/',
95   }
96
97   # ensure
98   if ! ($ensure in [ 'present', 'absent' ]) {
99     fail("\"${ensure}\" is not a valid ensure parameter value")
100   }
101
102   $notify_service = $elasticsearch::restart_on_change ? {
103     true  => Elasticsearch::Service[$name],
104     false => undef,
105   }
106
107   # Instance config directory
108   if ($configdir == undef) {
109     $instance_configdir = "${elasticsearch::configdir}/${name}"
110   } else {
111     $instance_configdir = $configdir
112   }
113
114   if ($ensure == 'present') {
115
116     # Configuration hash
117     if ($config == undef) {
118       $instance_config = {}
119     } else {
120       validate_hash($config)
121       $instance_config = $config
122     }
123
124     if(has_key($instance_config, 'node.name')) {
125       $instance_node_name = {}
126     } elsif(has_key($instance_config,'node')) {
127       if(has_key($instance_config['node'], 'name')) {
128         $instance_node_name = {}
129       } else {
130         $instance_node_name = { 'node.name' => "${::hostname}-${name}" }
131       }
132     } else {
133       $instance_node_name = { 'node.name' => "${::hostname}-${name}" }
134     }
135
136     # String or array for data dir(s)
137     if ($datadir == undef) {
138       if (is_array($elasticsearch::datadir)) {
139         $instance_datadir = array_suffix($elasticsearch::datadir, "/${name}")
140       } else {
141         $instance_datadir = "${elasticsearch::datadir}/${name}"
142       }
143     } else {
144       $instance_datadir = $datadir
145     }
146
147     # Logging file or hash
148     if ($logging_file != undef) {
149       $logging_source = $logging_file
150       $logging_content = undef
151     } elsif ($elasticsearch::logging_file != undef) {
152       $logging_source = $elasticsearch::logging_file
153       $logging_content = undef
154     } else {
155
156       if(is_hash($elasticsearch::logging_config)) {
157         $main_logging_config = $elasticsearch::logging_config
158       } else {
159         $main_logging_config = { }
160       }
161
162       if(is_hash($logging_config)) {
163         $instance_logging_config = $logging_config
164       } else {
165         $instance_logging_config = { }
166       }
167       $logging_hash = merge($elasticsearch::params::logging_defaults, $main_logging_config, $instance_logging_config)
168       if ($logging_template != undef ) {
169         $logging_content = template($logging_template)
170       } elsif ($elasticsearch::logging_template != undef) {
171         $logging_content = template($elasticsearch::logging_template)
172       } else {
173         $logging_content = template("${module_name}/etc/elasticsearch/logging.yml.erb")
174       }
175       $logging_source = undef
176     }
177
178     if ($elasticsearch::config != undef) {
179       $main_config = $elasticsearch::config
180     } else {
181       $main_config = { }
182     }
183
184     if(has_key($instance_config, 'path.data')) {
185       $instance_datadir_config = { 'path.data' => $instance_datadir }
186     } elsif(has_key($instance_config, 'path')) {
187       if(has_key($instance_config['path'], 'data')) {
188         $instance_datadir_config = { 'path' => { 'data' => $instance_datadir } }
189       } else {
190         $instance_datadir_config = { 'path.data' => $instance_datadir }
191       }
192     } else {
193       $instance_datadir_config = { 'path.data' => $instance_datadir }
194     }
195
196     if(is_array($instance_datadir)) {
197       $dirs = join($instance_datadir, ' ')
198     } else {
199       $dirs = $instance_datadir
200     }
201
202     exec { "mkdir_datadir_elasticsearch_${name}":
203       command => "mkdir -p ${dirs}",
204       creates => $instance_datadir,
205       require => Class['elasticsearch::package'],
206       before  => Elasticsearch::Service[$name],
207     }
208
209     file { $instance_datadir:
210       ensure  => 'directory',
211       owner   => $elasticsearch::elasticsearch_user,
212       group   => undef,
213       mode    => '0644',
214       require => [ Exec["mkdir_datadir_elasticsearch_${name}"], Class['elasticsearch::package'] ],
215       before  => Elasticsearch::Service[$name],
216     }
217
218     exec { "mkdir_configdir_elasticsearch_${name}":
219       command => "mkdir -p ${instance_configdir}",
220       creates => $elasticsearch::configdir,
221       require => Class['elasticsearch::package'],
222       before  => Elasticsearch::Service[$name],
223     }
224
225     file { $instance_configdir:
226       ensure  => 'directory',
227       mode    => '0644',
228       purge   => $elasticsearch::purge_configdir,
229       force   => $elasticsearch::purge_configdir,
230       require => [ Exec["mkdir_configdir_elasticsearch_${name}"], Class['elasticsearch::package'] ],
231       before  => Elasticsearch::Service[$name],
232     }
233
234     file { "${instance_configdir}/logging.yml":
235       ensure  => file,
236       content => $logging_content,
237       source  => $logging_source,
238       mode    => '0644',
239       notify  => $notify_service,
240       require => Class['elasticsearch::package'],
241       before  => Elasticsearch::Service[$name],
242     }
243
244     file { "${instance_configdir}/scripts":
245       ensure => 'link',
246       target => "${elasticsearch::configdir}/scripts",
247     }
248
249     # build up new config
250     $instance_conf = merge($main_config, $instance_node_name, $instance_config, $instance_datadir_config)
251
252     # defaults file content
253     # ensure user did not provide both init_defaults and init_defaults_file
254     if (($init_defaults != undef) and ($init_defaults_file != undef)) {
255       fail ('Only one of $init_defaults and $init_defaults_file should be defined')
256     }
257
258     if (is_hash($elasticsearch::init_defaults)) {
259       $global_init_defaults = $elasticsearch::init_defaults
260     } else {
261       $global_init_defaults = { }
262     }
263
264     $instance_init_defaults_main = { 'CONF_DIR' => $instance_configdir, 'CONF_FILE' => "${instance_configdir}/elasticsearch.yml", 'LOG_DIR' => "/var/log/elasticsearch/${name}", 'ES_HOME' => '/usr/share/elasticsearch' }
265
266     if (is_hash($init_defaults)) {
267       $instance_init_defaults = $init_defaults
268     } else {
269       $instance_init_defaults = { }
270     }
271     $init_defaults_new = merge($global_init_defaults, $instance_init_defaults_main, $instance_init_defaults )
272
273     $user = $elasticsearch::elasticsearch_user
274     $group = $elasticsearch::elasticsearch_group
275
276     file { "${instance_configdir}/elasticsearch.yml":
277       ensure  => file,
278       content => template("${module_name}/etc/elasticsearch/elasticsearch.yml.erb"),
279       mode    => '0644',
280       notify  => $notify_service,
281       require => Class['elasticsearch::package'],
282     }
283
284     $require_service = Class['elasticsearch::package']
285     $before_service  = undef
286
287   } else {
288
289     file { $instance_configdir:
290       ensure  => 'absent',
291       recurse => true,
292       force   => true,
293     }
294
295     $require_service = undef
296     $before_service  = File[$instance_configdir]
297
298     $init_defaults_new = {}
299   }
300
301   elasticsearch::service { $name:
302     ensure             => $ensure,
303     status             => $status,
304     init_defaults      => $init_defaults_new,
305     init_defaults_file => $init_defaults_file,
306     init_template      => "${module_name}/etc/init.d/${elasticsearch::params::init_template}",
307     require            => $require_service,
308     before             => $before_service,
309   }
310
311 }