]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/elasticsearch/manifests/service/systemd.pp
upgrade to elasticsearch/elasticsearch 0.9.6
[dsa-puppet.git] / 3rdparty / modules / elasticsearch / manifests / service / systemd.pp
1 # == Define: elasticsearch::service::systemd
2 #
3 # This define exists to coordinate all service management related actions,
4 # functionality and logical units in a central place.
5 #
6 # <b>Note:</b> "service" is the Puppet term and type for background processes
7 # in general and is used in a platform-independent way. E.g. "service" means
8 # "daemon" in relation to Unix-like systems.
9 #
10 #
11 # === Parameters
12 #
13 # [*ensure*]
14 #   String. Controls if the managed resources shall be <tt>present</tt> or
15 #   <tt>absent</tt>. If set to <tt>absent</tt>:
16 #   * The managed software packages are being uninstalled.
17 #   * Any traces of the packages will be purged as good as possible. This may
18 #     include existing configuration files. The exact behavior is provider
19 #     dependent. Q.v.:
20 #     * Puppet type reference: {package, "purgeable"}[http://j.mp/xbxmNP]
21 #     * {Puppet's package provider source code}[http://j.mp/wtVCaL]
22 #   * System modifications (if any) will be reverted as good as possible
23 #     (e.g. removal of created users, services, changed log settings, ...).
24 #   * This is thus destructive and should be used with care.
25 #   Defaults to <tt>present</tt>.
26 #
27 # [*status*]
28 #   String to define the status of the service. Possible values:
29 #   * <tt>enabled</tt>: Service is running and will be started at boot time.
30 #   * <tt>disabled</tt>: Service is stopped and will not be started at boot
31 #     time.
32 #   * <tt>running</tt>: Service is running but will not be started at boot time.
33 #     You can use this to start a service on the first Puppet run instead of
34 #     the system startup.
35 #   * <tt>unmanaged</tt>: Service will not be started at boot time and Puppet
36 #     does not care whether the service is running or not. For example, this may
37 #     be useful if a cluster management software is used to decide when to start
38 #     the service plus assuring it is running on the desired node.
39 #   Defaults to <tt>enabled</tt>. The singular form ("service") is used for the
40 #   sake of convenience. Of course, the defined status affects all services if
41 #   more than one is managed (see <tt>service.pp</tt> to check if this is the
42 #   case).
43 #
44 # [*init_defaults*]
45 #   Defaults file content in hash representation
46 #
47 # [*init_defaults_file*]
48 #   Defaults file as puppet resource
49 #
50 # [*init_template*]
51 #   Service file as a template
52 #
53 # === Authors
54 #
55 # * Richard Pijnenburg <mailto:richard.pijnenburg@elasticsearch.com>
56 #
57 define elasticsearch::service::systemd(
58   $ensure             = $elasticsearch::ensure,
59   $status             = $elasticsearch::status,
60   $init_defaults_file = undef,
61   $init_defaults      = undef,
62   $init_template      = undef,
63 ) {
64
65   #### Service management
66
67   # set params: in operation
68   if $ensure == 'present' {
69
70     case $status {
71       # make sure service is currently running, start it on boot
72       'enabled': {
73         $service_ensure = 'running'
74         $service_enable = true
75       }
76       # make sure service is currently stopped, do not start it on boot
77       'disabled': {
78         $service_ensure = 'stopped'
79         $service_enable = false
80       }
81       # make sure service is currently running, do not start it on boot
82       'running': {
83         $service_ensure = 'running'
84         $service_enable = false
85       }
86       # do not start service on boot, do not care whether currently running
87       # or not
88       'unmanaged': {
89         $service_ensure = undef
90         $service_enable = false
91       }
92       # unknown status
93       # note: don't forget to update the parameter check in init.pp if you
94       #       add a new or change an existing status.
95       default: {
96         fail("\"${status}\" is an unknown service status value")
97       }
98     }
99   } else {
100     # make sure the service is stopped and disabled (the removal itself will be
101     # done by package.pp)
102     $service_ensure = 'stopped'
103     $service_enable = false
104   }
105
106   $notify_service = $elasticsearch::restart_on_change ? {
107     true  => [ Exec["systemd_reload_${name}"], Service["elasticsearch-instance-${name}"] ],
108     false => Exec["systemd_reload_${name}"]
109   }
110
111   if ( $status != 'unmanaged' and $ensure == 'present' ) {
112
113     # defaults file content. Either from a hash or file
114     if ($init_defaults_file != undef) {
115       file { "${elasticsearch::params::defaults_location}/elasticsearch-${name}":
116         ensure => $ensure,
117         source => $init_defaults_file,
118         owner  => 'root',
119         group  => 'root',
120         mode   => '0644',
121         before => Service["elasticsearch-instance-${name}"],
122         notify => $notify_service,
123       }
124
125     } else {
126       if ($init_defaults != undef and is_hash($init_defaults) ) {
127
128         if(has_key($init_defaults, 'ES_USER')) {
129           if($init_defaults['ES_USER'] != $elasticsearch::elasticsearch_user) {
130             fail('Found ES_USER setting for init_defaults but is not same as elasticsearch_user setting. Please use elasticsearch_user setting.')
131           }
132         }
133       }
134       $init_defaults_pre_hash = { 'ES_USER' => $elasticsearch::elasticsearch_user, 'ES_GROUP' => $elasticsearch::elasticsearch_group, 'MAX_OPEN_FILES' => '65535' }
135       $new_init_defaults = merge($init_defaults_pre_hash, $init_defaults)
136
137       augeas { "defaults_${name}":
138         incl    => "${elasticsearch::params::defaults_location}/elasticsearch-${name}",
139         lens    => 'Shellvars.lns',
140         changes => template("${module_name}/etc/sysconfig/defaults.erb"),
141         before  => Service["elasticsearch-instance-${name}"],
142         notify  => $notify_service,
143       }
144     }
145
146     # init file from template
147     if ($init_template != undef) {
148
149       $user              = $elasticsearch::elasticsearch_user
150       $group             = $elasticsearch::elasticsearch_group
151       $pid_dir           = $elasticsearch::pid_dir
152       $defaults_location = $elasticsearch::defaults_location
153
154       if ($new_init_defaults != undef and is_hash($new_init_defaults) and has_key($new_init_defaults, 'MAX_OPEN_FILES')) {
155         $nofile = $new_init_defaults['MAX_OPEN_FILES']
156       }else{
157         $nofile = '65535'
158       }
159
160       if ($new_init_defaults != undef and is_hash($new_init_defaults) and has_key($new_init_defaults, 'MAX_LOCKED_MEMORY')) {
161         $memlock = $new_init_defaults['MAX_LOCKED_MEMORY']
162       }else{
163         $memlock = undef
164       }
165
166       file { "/lib/systemd/system/elasticsearch-${name}.service":
167         ensure  => $ensure,
168         content => template($init_template),
169         before  => Service["elasticsearch-instance-${name}"],
170         notify  => $notify_service,
171       }
172
173     }
174
175   $service_require = Exec["systemd_reload_${name}"]
176
177   } elsif($status != 'unmanaged') {
178
179     file { "/lib/systemd/system/elasticsearch-${name}.service":
180       ensure    => 'absent',
181       subscribe => Service["elasticsearch-instance-${name}"],
182       notify    => Exec["systemd_reload_${name}"],
183     }
184
185     file { "${elasticsearch::params::defaults_location}/elasticsearch-${name}":
186       ensure    => 'absent',
187       subscribe => Service["elasticsearch-instance-${name}"],
188       notify    => Exec["systemd_reload_${name}"],
189     }
190
191     $service_require = undef
192
193   }
194
195   exec { "systemd_reload_${name}":
196     command     => '/bin/systemctl daemon-reload',
197     refreshonly => true,
198   }
199
200   if ($status != 'unmanaged') {
201
202     # action
203     service { "elasticsearch-instance-${name}":
204       ensure     => $service_ensure,
205       enable     => $service_enable,
206       name       => "elasticsearch-${name}.service",
207       hasstatus  => $elasticsearch::params::service_hasstatus,
208       hasrestart => $elasticsearch::params::service_hasrestart,
209       pattern    => $elasticsearch::params::service_pattern,
210       provider   => 'systemd',
211       require    => $service_require,
212     }
213
214   }
215
216 }