author | ymh <ymh.work@gmail.com> |
Tue, 20 Mar 2018 15:02:40 +0100 | |
changeset 573 | 25f3d28f51b2 |
parent 406 | cf0f23803a53 |
permissions | -rwxr-xr-x |
28 | 1 |
# == Define: elasticsearch::service::init |
2 |
# |
|
3 |
# This class 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::init( |
|
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 |
||
100 |
# set params: removal |
|
101 |
} else { |
|
102 |
||
103 |
# make sure the service is stopped and disabled (the removal itself will be |
|
104 |
# done by package.pp) |
|
105 |
$service_ensure = 'stopped' |
|
106 |
$service_enable = false |
|
107 |
||
108 |
} |
|
109 |
||
406
cf0f23803a53
upgrade elasticsearch to 5.0, upgrade ember
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
110 |
$notify_service = $elasticsearch::restart_config_change ? { |
28 | 111 |
true => Service["elasticsearch-instance-${name}"], |
112 |
false => undef, |
|
113 |
} |
|
114 |
||
115 |
||
406
cf0f23803a53
upgrade elasticsearch to 5.0, upgrade ember
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
116 |
if ( $ensure == 'present' ) { |
28 | 117 |
|
118 |
# defaults file content. Either from a hash or file |
|
119 |
if ($init_defaults_file != undef) { |
|
120 |
file { "${elasticsearch::params::defaults_location}/elasticsearch-${name}": |
|
121 |
ensure => $ensure, |
|
122 |
source => $init_defaults_file, |
|
123 |
owner => 'root', |
|
406
cf0f23803a53
upgrade elasticsearch to 5.0, upgrade ember
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
124 |
group => '0', |
28 | 125 |
mode => '0644', |
126 |
before => Service["elasticsearch-instance-${name}"], |
|
127 |
notify => $notify_service, |
|
128 |
} |
|
129 |
||
406
cf0f23803a53
upgrade elasticsearch to 5.0, upgrade ember
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
130 |
} else { |
28 | 131 |
|
406
cf0f23803a53
upgrade elasticsearch to 5.0, upgrade ember
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
132 |
if ($init_defaults != undef and is_hash($init_defaults) ) { |
cf0f23803a53
upgrade elasticsearch to 5.0, upgrade ember
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
133 |
if(has_key($init_defaults, 'ES_USER')) { |
cf0f23803a53
upgrade elasticsearch to 5.0, upgrade ember
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
134 |
if($init_defaults['ES_USER'] != $elasticsearch::elasticsearch_user) { |
cf0f23803a53
upgrade elasticsearch to 5.0, upgrade ember
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
135 |
fail('Found ES_USER setting for init_defaults but is not same as elasticsearch_user setting. Please use elasticsearch_user setting.') |
cf0f23803a53
upgrade elasticsearch to 5.0, upgrade ember
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
136 |
} |
28 | 137 |
} |
138 |
} |
|
139 |
||
140 |
$init_defaults_pre_hash = { 'ES_USER' => $elasticsearch::elasticsearch_user, 'ES_GROUP' => $elasticsearch::elasticsearch_group, 'MAX_OPEN_FILES' => '65535' } |
|
141 |
$new_init_defaults = merge($init_defaults_pre_hash, $init_defaults) |
|
142 |
||
143 |
augeas { "defaults_${name}": |
|
144 |
incl => "${elasticsearch::params::defaults_location}/elasticsearch-${name}", |
|
145 |
lens => 'Shellvars.lns', |
|
146 |
changes => template("${module_name}/etc/sysconfig/defaults.erb"), |
|
147 |
before => Service["elasticsearch-instance-${name}"], |
|
148 |
notify => $notify_service, |
|
149 |
} |
|
150 |
||
151 |
} |
|
152 |
||
153 |
# init file from template |
|
154 |
if ($init_template != undef) { |
|
155 |
||
156 |
file { "/etc/init.d/elasticsearch-${name}": |
|
157 |
ensure => $ensure, |
|
158 |
content => template($init_template), |
|
159 |
owner => 'root', |
|
406
cf0f23803a53
upgrade elasticsearch to 5.0, upgrade ember
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
160 |
group => '0', |
28 | 161 |
mode => '0755', |
162 |
before => Service["elasticsearch-instance-${name}"], |
|
163 |
notify => $notify_service, |
|
164 |
} |
|
165 |
||
166 |
} |
|
167 |
||
406
cf0f23803a53
upgrade elasticsearch to 5.0, upgrade ember
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
168 |
} else { |
28 | 169 |
|
170 |
file { "/etc/init.d/elasticsearch-${name}": |
|
171 |
ensure => 'absent', |
|
172 |
subscribe => Service["elasticsearch-instance-${name}"], |
|
173 |
} |
|
174 |
||
175 |
file { "${elasticsearch::params::defaults_location}/elasticsearch-${name}": |
|
176 |
ensure => 'absent', |
|
177 |
subscribe => Service["elasticsearch-${$name}"], |
|
178 |
} |
|
179 |
||
180 |
} |
|
181 |
||
406
cf0f23803a53
upgrade elasticsearch to 5.0, upgrade ember
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
182 |
# action |
cf0f23803a53
upgrade elasticsearch to 5.0, upgrade ember
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
183 |
service { "elasticsearch-instance-${name}": |
cf0f23803a53
upgrade elasticsearch to 5.0, upgrade ember
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
184 |
ensure => $service_ensure, |
cf0f23803a53
upgrade elasticsearch to 5.0, upgrade ember
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
185 |
enable => $service_enable, |
cf0f23803a53
upgrade elasticsearch to 5.0, upgrade ember
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
186 |
name => "elasticsearch-${name}", |
cf0f23803a53
upgrade elasticsearch to 5.0, upgrade ember
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
187 |
hasstatus => $elasticsearch::params::service_hasstatus, |
cf0f23803a53
upgrade elasticsearch to 5.0, upgrade ember
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
188 |
hasrestart => $elasticsearch::params::service_hasrestart, |
cf0f23803a53
upgrade elasticsearch to 5.0, upgrade ember
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
189 |
pattern => $elasticsearch::params::service_pattern, |
28 | 190 |
} |
191 |
||
406
cf0f23803a53
upgrade elasticsearch to 5.0, upgrade ember
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
192 |
|
28 | 193 |
} |