]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/elasticsearch/manifests/instance.pp
Add new module elasticsearch for listsearch
[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 # === Authors
65 #
66 # * Richard Pijnenburg <mailto:richard.pijnenburg@elasticsearch.com>
67 #
68 define elasticsearch::instance(
69   $ensure           = $elasticsearch::ensure,
70   $status           = $elasticsearch::status,
71   $config           = undef,
72   $configdir        = undef,
73   $datadir          = undef,
74   $logging_file     = undef,
75   $logging_config   = undef,
76   $logging_template = undef,
77   $logging_level    = $elasticsearch::default_logging_level,
78   $init_defaults    = undef
79 ) {
80
81   require elasticsearch::params
82
83   File {
84     owner => $elasticsearch::elasticsearch_user,
85     group => $elasticsearch::elasticsearch_group
86   }
87
88   Exec {
89     path => [ '/bin', '/usr/bin', '/usr/local/bin' ],
90     cwd  => '/',
91   }
92
93   # ensure
94   if ! ($ensure in [ 'present', 'absent' ]) {
95     fail("\"${ensure}\" is not a valid ensure parameter value")
96   }
97
98   $notify_service = $elasticsearch::restart_on_change ? {
99     true  => Elasticsearch::Service[$name],
100     false => undef,
101   }
102
103   # Instance config directory
104   if ($configdir == undef) {
105     $instance_configdir = "${elasticsearch::configdir}/${name}"
106   } else {
107     $instance_configdir = $configdir
108   }
109
110   if ($ensure == 'present') {
111
112     # Configuration hash
113     if ($config == undef) {
114       $instance_config = {}
115     } else {
116       validate_hash($config)
117       $instance_config = $config
118     }
119
120     if(has_key($instance_config, 'node.name')) {
121       $instance_node_name = {}
122     } elsif(has_key($instance_config,'node')) {
123       if(has_key($instance_config['node'], 'name')) {
124         $instance_node_name = {}
125       } else {
126         $instance_node_name = { 'node.name' => "${::hostname}-${name}" }
127       }
128     } else {
129       $instance_node_name = { 'node.name' => "${::hostname}-${name}" }
130     }
131
132     # String or array for data dir(s)
133     if ($datadir == undef) {
134       if (is_array($elasticsearch::datadir)) {
135         $instance_datadir = array_suffix($elasticsearch::datadir, "/${name}")
136       } else {
137         $instance_datadir = "${elasticsearch::datadir}/${name}"
138       }
139     } else {
140       $instance_datadir = $datadir
141     }
142
143     # Logging file or hash
144     if ($logging_file != undef) {
145       $logging_source = $logging_file
146       $logging_content = undef
147     } elsif ($elasticsearch::logging_file != undef) {
148       $logging_source = $elasticsearch::logging_file
149       $logging_content = undef
150     } else {
151
152       if(is_hash($elasticsearch::logging_config)) {
153         $main_logging_config = $elasticsearch::logging_config
154       } else {
155         $main_logging_config = { }
156       }
157
158       if(is_hash($logging_config)) {
159         $instance_logging_config = $logging_config
160       } else {
161         $instance_logging_config = { }
162       }
163       $logging_hash = merge($elasticsearch::params::logging_defaults, $main_logging_config, $instance_logging_config)
164       if ($logging_template != undef ) {
165         $logging_content = template($logging_template)
166       } elsif ($elasticsearch::logging_template != undef) {
167         $logging_content = template($elasticsearch::logging_template)
168       } else {
169         $logging_content = template("${module_name}/etc/elasticsearch/logging.yml.erb")
170       }
171       $logging_source = undef
172     }
173
174     if ($elasticsearch::config != undef) {
175       $main_config = $elasticsearch::config
176     } else {
177       $main_config = { }
178     }
179
180     if(has_key($instance_config, 'path.data')) {
181       $instance_datadir_config = { 'path.data' => $instance_datadir }
182     } elsif(has_key($instance_config, 'path')) {
183       if(has_key($instance_config['path'], 'data')) {
184         $instance_datadir_config = { 'path' => { 'data' => $instance_datadir } }
185       } else {
186         $instance_datadir_config = { 'path.data' => $instance_datadir }
187       }
188     } else {
189       $instance_datadir_config = { 'path.data' => $instance_datadir }
190     }
191
192     if(is_array($instance_datadir)) {
193       $dirs = join($instance_datadir, ' ')
194     } else {
195       $dirs = $instance_datadir
196     }
197
198     exec { "mkdir_datadir_elasticsearch_${name}":
199       command => "mkdir -p ${dirs}",
200       creates => $instance_datadir,
201       require => Class['elasticsearch::package'],
202       before  => Elasticsearch::Service[$name]
203     }
204
205     file { $instance_datadir:
206       ensure  => 'directory',
207       owner   => $elasticsearch::elasticsearch_user,
208       group   => undef,
209       mode    => '0644',
210       require => [ Exec["mkdir_datadir_elasticsearch_${name}"], Class['elasticsearch::package'] ],
211       before  => Elasticsearch::Service[$name]
212     }
213
214     exec { "mkdir_configdir_elasticsearch_${name}":
215       command => "mkdir -p ${instance_configdir}",
216       creates => $elasticsearch::configdir,
217       require => Class['elasticsearch::package'],
218       before  => Elasticsearch::Service[$name]
219     }
220
221     file { $instance_configdir:
222       ensure  => 'directory',
223       mode    => '0644',
224       purge   => $elasticsearch::purge_configdir,
225       force   => $elasticsearch::purge_configdir,
226       require => [ Exec["mkdir_configdir_elasticsearch_${name}"], Class['elasticsearch::package'] ],
227       before  => Elasticsearch::Service[$name]
228     }
229
230     file { "${instance_configdir}/logging.yml":
231       ensure  => file,
232       content => $logging_content,
233       source  => $logging_source,
234       mode    => '0644',
235       notify  => $notify_service,
236       require => Class['elasticsearch::package'],
237       before  => Elasticsearch::Service[$name]
238     }
239
240     # build up new config
241     $instance_conf = merge($main_config, $instance_node_name, $instance_config, $instance_datadir_config)
242
243     # defaults file content
244     if (is_hash($elasticsearch::init_defaults)) {
245       $global_init_defaults = $elasticsearch::init_defaults
246     } else {
247       $global_init_defaults = { }
248     }
249
250     $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' }
251
252     if (is_hash($init_defaults)) {
253       $instance_init_defaults = $init_defaults
254     } else {
255       $instance_init_defaults = { }
256     }
257     $init_defaults_new = merge($global_init_defaults, $instance_init_defaults_main, $instance_init_defaults )
258
259     $user = $elasticsearch::elasticsearch_user
260     $group = $elasticsearch::elasticsearch_group
261
262     file { "${instance_configdir}/elasticsearch.yml":
263       ensure  => file,
264       content => template("${module_name}/etc/elasticsearch/elasticsearch.yml.erb"),
265       mode    => '0644',
266       notify  => $notify_service,
267       require => Class['elasticsearch::package']
268     }
269
270     $require_service = Class['elasticsearch::package']
271     $before_service  = undef
272
273   } else {
274
275     file { $instance_configdir:
276       ensure  => 'absent',
277       recurse => true,
278       force   => true,
279     }
280
281     $require_service = undef
282     $before_service  = File[$instance_configdir]
283
284     $init_defaults_new = {}
285   }
286
287   elasticsearch::service { $name:
288     ensure        => $ensure,
289     status        => $status,
290     init_defaults => $init_defaults_new,
291     init_template => "${module_name}/etc/init.d/${elasticsearch::params::init_template}",
292     require       => $require_service,
293     before        => $before_service
294   }
295
296 }