28
|
1 |
# == Define: elasticsearch::template |
|
2 |
# |
|
3 |
# This define allows you to insert, update or delete templates that are used within Elasticsearch for the indexes |
|
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 |
# [*file*] |
|
22 |
# File path of the template ( json file ) |
|
23 |
# Value type is string |
|
24 |
# Default value: undef |
|
25 |
# This variable is optional |
|
26 |
# |
|
27 |
# [*content*] |
|
28 |
# Contents of the template ( json ) |
|
29 |
# Value type is string |
|
30 |
# Default value: undef |
|
31 |
# This variable is optional |
|
32 |
# |
|
33 |
# [*host*] |
|
34 |
# Host name or IP address of the ES instance to connect to |
|
35 |
# Value type is string |
|
36 |
# Default value: localhost |
|
37 |
# This variable is optional |
|
38 |
# |
|
39 |
# [*port*] |
|
40 |
# Port number of the ES instance to connect to |
|
41 |
# Value type is number |
|
42 |
# Default value: 9200 |
|
43 |
# This variable is optional |
|
44 |
# |
|
45 |
# === Authors |
|
46 |
# |
|
47 |
# * Richard Pijnenburg <mailto:richard.pijnenburg@elasticsearch.com> |
|
48 |
# |
|
49 |
define elasticsearch::template( |
|
50 |
$ensure = 'present', |
|
51 |
$file = undef, |
|
52 |
$content = undef, |
|
53 |
$host = 'localhost', |
|
54 |
$port = 9200 |
|
55 |
) { |
|
56 |
|
|
57 |
require elasticsearch |
|
58 |
|
|
59 |
# ensure |
|
60 |
if ! ($ensure in [ 'present', 'absent' ]) { |
|
61 |
fail("\"${ensure}\" is not a valid ensure parameter value") |
|
62 |
} |
|
63 |
|
|
64 |
if ! is_integer($port) { |
|
65 |
fail("\"${port}\" is not an integer") |
|
66 |
} |
|
67 |
|
|
68 |
Exec { |
|
69 |
path => [ '/bin', '/usr/bin', '/usr/local/bin' ], |
|
70 |
cwd => '/', |
|
71 |
tries => 6, |
|
72 |
try_sleep => 10, |
|
73 |
} |
|
74 |
|
|
75 |
# Build up the url |
|
76 |
$es_url = "http://${host}:${port}/_template/${name}" |
|
77 |
|
|
78 |
# Can't do a replace and delete at the same time |
|
79 |
|
|
80 |
if ($ensure == 'present') { |
|
81 |
|
|
82 |
# Fail when no file or content is supplied |
|
83 |
if $file == undef and $content == undef { |
|
84 |
fail('The variables "file" and "content" cannot be empty when inserting or updating a template.') |
|
85 |
} elsif $file != undef and $content != undef { |
|
86 |
fail('The variables "file" and "content" cannot be used together when inserting or updating a template.') |
|
87 |
} else { # we are good to go. notify to insert in case we deleted |
|
88 |
$insert_notify = Exec[ "insert_template_${name}" ] |
|
89 |
} |
|
90 |
|
|
91 |
} else { |
|
92 |
|
|
93 |
$insert_notify = undef |
|
94 |
|
|
95 |
} |
|
96 |
|
|
97 |
# Delete the existing template |
|
98 |
# First check if it exists of course |
|
99 |
exec { "delete_template_${name}": |
|
100 |
command => "curl -s -XDELETE ${es_url}", |
|
101 |
onlyif => "test $(curl -s '${es_url}?pretty=true' | wc -l) -gt 1", |
|
102 |
notify => $insert_notify, |
|
103 |
refreshonly => true, |
|
104 |
} |
|
105 |
|
|
106 |
if ($ensure == 'absent') { |
|
107 |
|
|
108 |
# delete the template file on disk and then on the server |
|
109 |
file { "${elasticsearch::params::homedir}/templates_import/elasticsearch-template-${name}.json": |
|
110 |
ensure => 'absent', |
|
111 |
notify => Exec[ "delete_template_${name}" ], |
|
112 |
require => File[ "${elasticsearch::params::homedir}/templates_import" ], |
|
113 |
} |
|
114 |
} |
|
115 |
|
|
116 |
if ($ensure == 'present') { |
|
117 |
|
|
118 |
if $content == undef { |
|
119 |
# place the template file using the file source |
|
120 |
file { "${elasticsearch::params::homedir}/templates_import/elasticsearch-template-${name}.json": |
|
121 |
ensure => file, |
|
122 |
source => $file, |
|
123 |
notify => Exec[ "delete_template_${name}" ], |
|
124 |
require => File[ "${elasticsearch::params::homedir}/templates_import" ], |
|
125 |
} |
|
126 |
} else { |
|
127 |
# place the template file using content |
|
128 |
file { "${elasticsearch::params::homedir}/templates_import/elasticsearch-template-${name}.json": |
|
129 |
ensure => file, |
|
130 |
content => $content, |
|
131 |
notify => Exec[ "delete_template_${name}" ], |
|
132 |
require => File[ "${elasticsearch::params::homedir}/templates_import" ], |
|
133 |
} |
|
134 |
} |
|
135 |
|
|
136 |
exec { "insert_template_${name}": |
|
137 |
command => "curl -sL -w \"%{http_code}\\n\" -XPUT ${es_url} -d @${elasticsearch::params::homedir}/templates_import/elasticsearch-template-${name}.json -o /dev/null | egrep \"(200|201)\" > /dev/null", |
|
138 |
unless => "test $(curl -s '${es_url}?pretty=true' | wc -l) -gt 1", |
|
139 |
refreshonly => true, |
|
140 |
loglevel => 'debug', |
|
141 |
} |
|
142 |
|
|
143 |
} |
|
144 |
|
|
145 |
} |