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
64 # [*init_defaults_file*]
65 # Defaults file as puppet resource
69 # * Richard Pijnenburg <mailto:richard.pijnenburg@elasticsearch.com>
71 define elasticsearch::instance(
72 $ensure = $elasticsearch::ensure,
73 $status = $elasticsearch::status,
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
85 require elasticsearch::params
88 owner => $elasticsearch::elasticsearch_user,
89 group => $elasticsearch::elasticsearch_group,
93 path => [ '/bin', '/usr/bin', '/usr/local/bin' ],
98 if ! ($ensure in [ 'present', 'absent' ]) {
99 fail("\"${ensure}\" is not a valid ensure parameter value")
102 $notify_service = $elasticsearch::restart_on_change ? {
103 true => Elasticsearch::Service[$name],
107 # Instance config directory
108 if ($configdir == undef) {
109 $instance_configdir = "${elasticsearch::configdir}/${name}"
111 $instance_configdir = $configdir
114 if ($ensure == 'present') {
117 if ($config == undef) {
118 $instance_config = {}
120 validate_hash($config)
121 $instance_config = $config
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 = {}
130 $instance_node_name = { 'node.name' => "${::hostname}-${name}" }
133 $instance_node_name = { 'node.name' => "${::hostname}-${name}" }
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}")
141 $instance_datadir = "${elasticsearch::datadir}/${name}"
144 $instance_datadir = $datadir
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
156 if(is_hash($elasticsearch::logging_config)) {
157 $main_logging_config = $elasticsearch::logging_config
159 $main_logging_config = { }
162 if(is_hash($logging_config)) {
163 $instance_logging_config = $logging_config
165 $instance_logging_config = { }
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)
173 $logging_content = template("${module_name}/etc/elasticsearch/logging.yml.erb")
175 $logging_source = undef
178 if ($elasticsearch::config != undef) {
179 $main_config = $elasticsearch::config
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 } }
190 $instance_datadir_config = { 'path.data' => $instance_datadir }
193 $instance_datadir_config = { 'path.data' => $instance_datadir }
196 if(is_array($instance_datadir)) {
197 $dirs = join($instance_datadir, ' ')
199 $dirs = $instance_datadir
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],
209 file { $instance_datadir:
210 ensure => 'directory',
211 owner => $elasticsearch::elasticsearch_user,
214 require => [ Exec["mkdir_datadir_elasticsearch_${name}"], Class['elasticsearch::package'] ],
215 before => Elasticsearch::Service[$name],
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],
225 file { $instance_configdir:
226 ensure => 'directory',
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],
234 file { "${instance_configdir}/logging.yml":
236 content => $logging_content,
237 source => $logging_source,
239 notify => $notify_service,
240 require => Class['elasticsearch::package'],
241 before => Elasticsearch::Service[$name],
244 file { "${instance_configdir}/scripts":
246 target => "${elasticsearch::configdir}/scripts",
249 # build up new config
250 $instance_conf = merge($main_config, $instance_node_name, $instance_config, $instance_datadir_config)
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')
258 if (is_hash($elasticsearch::init_defaults)) {
259 $global_init_defaults = $elasticsearch::init_defaults
261 $global_init_defaults = { }
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' }
266 if (is_hash($init_defaults)) {
267 $instance_init_defaults = $init_defaults
269 $instance_init_defaults = { }
271 $init_defaults_new = merge($global_init_defaults, $instance_init_defaults_main, $instance_init_defaults )
273 $user = $elasticsearch::elasticsearch_user
274 $group = $elasticsearch::elasticsearch_group
276 file { "${instance_configdir}/elasticsearch.yml":
278 content => template("${module_name}/etc/elasticsearch/elasticsearch.yml.erb"),
280 notify => $notify_service,
281 require => Class['elasticsearch::package'],
284 $require_service = Class['elasticsearch::package']
285 $before_service = undef
289 file { $instance_configdir:
295 $require_service = undef
296 $before_service = File[$instance_configdir]
298 $init_defaults_new = {}
301 elasticsearch::service { $name:
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,