1 # == Define: elasticsearch::instance
3 # This define allows you to create or remove an elasticsearch instance
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
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>.
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
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
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
39 # Elasticsearch configuration hash
42 # Path to directory containing the elasticsearch configuration.
43 # Use this setting if your packages deviate from the norm (/etc/elasticsearch)
46 # Allows you to set the data directory of Elasticsearch
49 # Instead of a hash you can supply a puppet:// file source for the logging.yml file
52 # Hash representation of information you want in the logging.yml file
54 # [*logging_template*]
55 # Use a custom logging template - just supply the reative path ie ${module}/elasticsearch/logging.yml.erb
58 # Default logging level for Elasticsearch.
62 # Defaults file content in hash representation
66 # * Richard Pijnenburg <mailto:richard.pijnenburg@elasticsearch.com>
68 define elasticsearch::instance(
69 $ensure = $elasticsearch::ensure,
70 $status = $elasticsearch::status,
74 $logging_file = undef,
75 $logging_config = undef,
76 $logging_template = undef,
77 $logging_level = $elasticsearch::default_logging_level,
78 $init_defaults = undef
81 require elasticsearch::params
84 owner => $elasticsearch::elasticsearch_user,
85 group => $elasticsearch::elasticsearch_group
89 path => [ '/bin', '/usr/bin', '/usr/local/bin' ],
94 if ! ($ensure in [ 'present', 'absent' ]) {
95 fail("\"${ensure}\" is not a valid ensure parameter value")
98 $notify_service = $elasticsearch::restart_on_change ? {
99 true => Elasticsearch::Service[$name],
103 # Instance config directory
104 if ($configdir == undef) {
105 $instance_configdir = "${elasticsearch::configdir}/${name}"
107 $instance_configdir = $configdir
110 if ($ensure == 'present') {
113 if ($config == undef) {
114 $instance_config = {}
116 validate_hash($config)
117 $instance_config = $config
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 = {}
126 $instance_node_name = { 'node.name' => "${::hostname}-${name}" }
129 $instance_node_name = { 'node.name' => "${::hostname}-${name}" }
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}")
137 $instance_datadir = "${elasticsearch::datadir}/${name}"
140 $instance_datadir = $datadir
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
152 if(is_hash($elasticsearch::logging_config)) {
153 $main_logging_config = $elasticsearch::logging_config
155 $main_logging_config = { }
158 if(is_hash($logging_config)) {
159 $instance_logging_config = $logging_config
161 $instance_logging_config = { }
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)
169 $logging_content = template("${module_name}/etc/elasticsearch/logging.yml.erb")
171 $logging_source = undef
174 if ($elasticsearch::config != undef) {
175 $main_config = $elasticsearch::config
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 } }
186 $instance_datadir_config = { 'path.data' => $instance_datadir }
189 $instance_datadir_config = { 'path.data' => $instance_datadir }
192 if(is_array($instance_datadir)) {
193 $dirs = join($instance_datadir, ' ')
195 $dirs = $instance_datadir
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]
205 file { $instance_datadir:
206 ensure => 'directory',
207 owner => $elasticsearch::elasticsearch_user,
210 require => [ Exec["mkdir_datadir_elasticsearch_${name}"], Class['elasticsearch::package'] ],
211 before => Elasticsearch::Service[$name]
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]
221 file { $instance_configdir:
222 ensure => 'directory',
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]
230 file { "${instance_configdir}/logging.yml":
232 content => $logging_content,
233 source => $logging_source,
235 notify => $notify_service,
236 require => Class['elasticsearch::package'],
237 before => Elasticsearch::Service[$name]
240 # build up new config
241 $instance_conf = merge($main_config, $instance_node_name, $instance_config, $instance_datadir_config)
243 # defaults file content
244 if (is_hash($elasticsearch::init_defaults)) {
245 $global_init_defaults = $elasticsearch::init_defaults
247 $global_init_defaults = { }
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' }
252 if (is_hash($init_defaults)) {
253 $instance_init_defaults = $init_defaults
255 $instance_init_defaults = { }
257 $init_defaults_new = merge($global_init_defaults, $instance_init_defaults_main, $instance_init_defaults )
259 $user = $elasticsearch::elasticsearch_user
260 $group = $elasticsearch::elasticsearch_group
262 file { "${instance_configdir}/elasticsearch.yml":
264 content => template("${module_name}/etc/elasticsearch/elasticsearch.yml.erb"),
266 notify => $notify_service,
267 require => Class['elasticsearch::package']
270 $require_service = Class['elasticsearch::package']
271 $before_service = undef
275 file { $instance_configdir:
281 $require_service = undef
282 $before_service = File[$instance_configdir]
284 $init_defaults_new = {}
287 elasticsearch::service { $name:
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