1 # == Define: elasticsearch::template |
1 # == Define: elasticsearch::template |
2 # |
2 # |
3 # This define allows you to insert, update or delete templates that are used within Elasticsearch for the indexes |
3 # This define allows you to insert, update or delete Elasticsearch index |
|
4 # templates. |
|
5 # |
|
6 # Template content should be defined through either the `content` parameter |
|
7 # (when passing a hash or json string) or the `source` parameter (when passing |
|
8 # the puppet file URI to a template json file). |
4 # |
9 # |
5 # === Parameters |
10 # === Parameters |
6 # |
11 # |
7 # [*ensure*] |
12 # [*ensure*] |
8 # String. Controls if the managed resources shall be <tt>present</tt> or |
13 # Controls whether the named index template should be present or absent in |
9 # <tt>absent</tt>. If set to <tt>absent</tt>: |
14 # the cluster. |
10 # * The managed software packages are being uninstalled. |
15 # Value type is string |
11 # * Any traces of the packages will be purged as good as possible. This may |
16 # Default value: present |
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 # |
17 # |
21 # [*file*] |
18 # [*file*] |
22 # File path of the template ( json file ) |
19 # File path of the template (json file). This parameter is deprecated; |
|
20 # use `source` instead. |
|
21 # Value type is string |
|
22 # Default value: undef |
|
23 # This variable is deprecated |
|
24 # |
|
25 # [*source*] |
|
26 # Source path for the template file. Can be any value similar to `source` |
|
27 # values for `file` resources. |
23 # Value type is string |
28 # Value type is string |
24 # Default value: undef |
29 # Default value: undef |
25 # This variable is optional |
30 # This variable is optional |
26 # |
31 # |
27 # [*content*] |
32 # [*content*] |
28 # Contents of the template ( json ) |
33 # Contents of the template. Can be either a puppet hash or a string |
29 # Value type is string |
34 # containing JSON. |
|
35 # Value type is string or hash. |
30 # Default value: undef |
36 # Default value: undef |
31 # This variable is optional |
37 # This variable is optional |
32 # |
38 # |
33 # [*host*] |
39 # [*api_protocol*] |
|
40 # Protocol that should be used to connect to the Elasticsearch API. |
|
41 # Value type is string |
|
42 # Default value inherited from elasticsearch::api_protocol: http |
|
43 # This variable is optional |
|
44 # |
|
45 # [*api_host*] |
34 # Host name or IP address of the ES instance to connect to |
46 # Host name or IP address of the ES instance to connect to |
35 # Value type is string |
47 # Value type is string |
36 # Default value: localhost |
48 # Default value inherited from $elasticsearch::api_host: localhost |
37 # This variable is optional |
49 # This variable is optional |
38 # |
50 # |
39 # [*port*] |
51 # [*api_port*] |
40 # Port number of the ES instance to connect to |
52 # Port number of the ES instance to connect to |
41 # Value type is number |
53 # Value type is number |
42 # Default value: 9200 |
54 # Default value inherited from $elasticsearch::api_port: 9200 |
|
55 # This variable is optional |
|
56 # |
|
57 # [*api_timeout*] |
|
58 # Timeout period (in seconds) for the Elasticsearch API. |
|
59 # Value type is int |
|
60 # Default value inherited from elasticsearch::api_timeout: 10 |
|
61 # This variable is optional |
|
62 # |
|
63 # [*api_basic_auth_username*] |
|
64 # HTTP basic auth username to use when communicating over the Elasticsearch |
|
65 # API. |
|
66 # Value type is String |
|
67 # Default value inherited from elasticsearch::api_basic_auth_username: undef |
|
68 # This variable is optional |
|
69 # |
|
70 # [*api_basic_auth_password*] |
|
71 # HTTP basic auth password to use when communicating over the Elasticsearch |
|
72 # API. |
|
73 # Value type is String |
|
74 # Default value inherited from elasticsearch::api_basic_auth_password: undef |
|
75 # This variable is optional |
|
76 # |
|
77 # [*api_ca_file*] |
|
78 # Path to a CA file which will be used to validate server certs when |
|
79 # communicating with the Elasticsearch API over HTTPS. |
|
80 # Value type is String |
|
81 # Default value inherited from elasticsearch::api_ca_file: undef |
|
82 # This variable is optional |
|
83 # |
|
84 # [*api_ca_path*] |
|
85 # Path to a directory with CA files which will be used to validate server |
|
86 # certs when communicating with the Elasticsearch API over HTTPS. |
|
87 # Value type is String |
|
88 # Default value inherited from elasticsearch::api_ca_path: undef |
|
89 # This variable is optional |
|
90 # |
|
91 # [*validate_tls*] |
|
92 # Determines whether the validity of SSL/TLS certificates received from the |
|
93 # Elasticsearch API should be verified or ignored. |
|
94 # Value type is boolean |
|
95 # Default value inherited from elasticsearch::validate_tls: true |
43 # This variable is optional |
96 # This variable is optional |
44 # |
97 # |
45 # === Authors |
98 # === Authors |
46 # |
99 # |
47 # * Richard Pijnenburg <mailto:richard.pijnenburg@elasticsearch.com> |
100 # * Richard Pijnenburg <mailto:richard.pijnenburg@elasticsearch.com> |
|
101 # * Tyler Langlois <mailto:tyler@elastic.co> |
48 # |
102 # |
49 define elasticsearch::template( |
103 define elasticsearch::template ( |
50 $ensure = 'present', |
104 $ensure = 'present', |
51 $file = undef, |
105 $file = undef, |
52 $content = undef, |
106 $source = undef, |
53 $host = 'localhost', |
107 $content = undef, |
54 $port = 9200 |
108 $api_protocol = $elasticsearch::_api_protocol, |
|
109 $api_host = $elasticsearch::api_host, |
|
110 $api_port = $elasticsearch::api_port, |
|
111 $api_timeout = $elasticsearch::api_timeout, |
|
112 $api_basic_auth_username = $elasticsearch::_api_basic_auth_username, |
|
113 $api_basic_auth_password = $elasticsearch::_api_basic_auth_password, |
|
114 $api_ca_file = $elasticsearch::api_ca_file, |
|
115 $api_ca_path = $elasticsearch::api_ca_path, |
|
116 $validate_tls = $elasticsearch::_validate_tls, |
55 ) { |
117 ) { |
|
118 validate_string( |
|
119 $api_protocol, |
|
120 $api_host, |
|
121 $api_basic_auth_username, |
|
122 $api_basic_auth_password |
|
123 ) |
|
124 validate_bool($validate_tls) |
|
125 |
|
126 if ! ($ensure in ['present', 'absent']) { |
|
127 fail("'${ensure}' is not a valid 'ensure' parameter value") |
|
128 } |
|
129 if ! is_integer($api_port) { fail('"api_port" is not an integer') } |
|
130 if ! is_integer($api_timeout) { fail('"api_timeout" is not an integer') } |
|
131 if ($api_ca_file != undef) { validate_absolute_path($api_ca_file) } |
|
132 if ($api_ca_path != undef) { validate_absolute_path($api_ca_path) } |
|
133 |
|
134 if ($file != undef) { |
|
135 warning('"file" parameter is deprecated; use $source instead') |
|
136 $_source = $file |
|
137 } else { |
|
138 $_source = $source |
|
139 } |
|
140 |
|
141 if $_source != undef { validate_string($_source) } |
|
142 |
|
143 if $content != undef and is_string($content) { |
|
144 $_content = parsejson($content) |
|
145 } else { |
|
146 $_content = $content |
|
147 } |
|
148 |
|
149 if $ensure == 'present' and $_source == undef and $_content == undef { |
|
150 fail('one of "file" or "content" required.') |
|
151 } elsif $_source != undef and $_content != undef { |
|
152 fail('"file" and "content" cannot be simultaneously defined.') |
|
153 } |
56 |
154 |
57 require elasticsearch |
155 require elasticsearch |
58 |
156 |
59 # ensure |
157 es_instance_conn_validator { "${name}-template": |
60 if ! ($ensure in [ 'present', 'absent' ]) { |
158 server => $api_host, |
61 fail("\"${ensure}\" is not a valid ensure parameter value") |
159 port => $api_port, |
|
160 } -> |
|
161 elasticsearch_template { $name: |
|
162 ensure => $ensure, |
|
163 content => $_content, |
|
164 source => $_source, |
|
165 protocol => $api_protocol, |
|
166 host => $api_host, |
|
167 port => $api_port, |
|
168 timeout => $api_timeout, |
|
169 username => $api_basic_auth_username, |
|
170 password => $api_basic_auth_password, |
|
171 ca_file => $api_ca_file, |
|
172 ca_path => $api_ca_path, |
|
173 validate_tls => $validate_tls, |
62 } |
174 } |
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 } |
175 } |