-
Notifications
You must be signed in to change notification settings - Fork 7
/
elasticsearch_helper.install
212 lines (180 loc) · 6.42 KB
/
elasticsearch_helper.install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
/**
* @file
* Installation functions for the Elasticsearch Helper module.
*/
use Elastic\Elasticsearch\Exception\ElasticsearchException;
use Elastic\Transport\Exception\NoNodeAvailableException;
use Drupal\user\Entity\Role;
/**
* Implements hook_requirements.
*/
function elasticsearch_helper_requirements($phase) {
$requirements = [];
if ($phase == 'install') {
// Check that the Elasticsearch PHP library is available.
if (!class_exists('\Elastic\Elasticsearch\Client')) {
$requirements['elasticsearch_library'] = [
'description' => t('Elasticsearch Helper requires Elasticsearch PHP library to be installed. See README.md for further details.'),
'severity' => REQUIREMENT_ERROR,
];
}
}
if ($phase == 'runtime') {
// Check that the Elasticsearch PHP library is available.
if (!class_exists('\Elastic\Elasticsearch\Client')) {
$requirements['elasticsearch_library'] = [
'title' => t('Elasticsearch PHP library missing'),
'description' => t("Elasticsearch Helper requires the Elasticsearch PHP library. See README.md for further details."),
'severity' => REQUIREMENT_ERROR,
];
}
else {
// Check Elasticsearch status.
/** @var \Elastic\Elasticsearch\Client $client */
$client = \Drupal::service('elasticsearch_helper.elasticsearch_client');
try {
$health = $client->cluster()->health();
$color_states = [
'green' => REQUIREMENT_OK,
'yellow' => REQUIREMENT_WARNING,
'red' => REQUIREMENT_ERROR,
];
$requirements['elasticsearch_health'] = [
'title' => t('Elasticsearch status'),
'description' => t('Elasticsearch cluster status is @status.', ['@status' => $health['status']]),
'severity' => $color_states[$health['status']],
];
}
catch (NoNodeAvailableException $e) {
$requirements['elasticsearch_health'] = [
'title' => t('Elasticsearch status'),
'description' => t('No nodes available.'),
'severity' => REQUIREMENT_ERROR,
];
}
catch (ElasticsearchException $e) {
$requirements['elasticsearch_health'] = [
'title' => t('Elasticsearch status'),
'description' => t('Could not connect to Elasticsearch. Return code %code.', ['%code' => $e->getCode()]),
'severity' => REQUIREMENT_ERROR,
];
}
}
}
return $requirements;
}
/**
* Implements hook_install().
*/
function elasticsearch_helper_install() {
// Assign a low weight to ensure our implementation of
// hook_module_implements_alter() is run among the last ones.
module_set_weight('elasticsearch_helper', 10);
}
/**
* Set a low module weight to ensure elasticsearch_helper runs last.
*/
function elasticsearch_helper_update_8001() {
// Doing this in an update hook for installations that didn't have the weight
// set on installation.
module_set_weight('elasticsearch_helper', 10);
}
/**
* Explicitly set the default scheme to HTTP if it wasn't set before.
*/
function elasticsearch_helper_update_8002() {
$config = \Drupal::service('config.factory')->getEditable('elasticsearch_helper.settings');
if ($config->get('elasticsearch_helper.scheme')) {
$config->set('elasticsearch_helper.scheme', 'http');
}
}
/**
* Change module's configuration structure to allow defining multiple hosts.
*/
function elasticsearch_helper_update_8003() {
// Update module configuration.
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('elasticsearch_helper.settings');
$hosts = [
[
'scheme' => $config->get('elasticsearch_helper.scheme'),
'host' => $config->get('elasticsearch_helper.host'),
'port' => $config->get('elasticsearch_helper.port'),
'authentication' => [
'enabled' => (bool) $config->get('elasticsearch_helper.authentication'),
'user' => $config->get('elasticsearch_helper.user'),
'password' => $config->get('elasticsearch_helper.password'),
],
]
];
$defer_indexing = $config->get('elasticsearch_helper.defer_indexing');
// Delete the configuration object altogether.
$config->delete();
// Reinstate the configuration.
$config->set('hosts', $hosts);
$config->set('defer_indexing', (bool) $defer_indexing);
$config->save();
// Update permission name.
$mistyped_permission = 'configured elasticsearch helper';
$permission = 'configure elasticsearch helper';
/** @var \Drupal\user\RoleInterface $role */
foreach (Role::loadMultiple() as $role) {
if ($role->hasPermission($mistyped_permission)) {
$role->revokePermission($mistyped_permission);
$role->grantPermission($permission);
$role->trustData()->save();
}
}
}
/**
* Change configuration to enable configurable authentication, SSL settings.
*
* NOTE: This update hook changes the 7.x development version config structure
* to the new 7.0 release version config structure.
*/
function elasticsearch_helper_update_8004() {
// Get module configuration.
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('elasticsearch_helper.settings');
// Get hosts.
$hosts = $config->get('hosts') ?: [];
// Prepare hosts.
$updated_hosts = [];
foreach ($hosts as $host) {
if (!empty($host['host'])) {
$updated_hosts[] = [
'host' => $host['host'],
'port' => $host['port'] ?? '',
];
}
}
// Set hosts.
$config->set('hosts', $updated_hosts);
// Set scheme.
$existing_scheme = $hosts[0]['scheme'] ?? 'http';
$config->set('scheme', $existing_scheme);
// Set SSL configuration.
$config->set('ssl', [
'certificate' => '',
// Skip verification by default if scheme is https.
'skip_verification' => ($existing_scheme == 'https'),
]);
// Prepare authentication.
$existing_auth = $hosts[0]['authentication'] ?? [];
if (!empty($existing_auth['enabled']) && !empty($existing_auth['user'])) {
$authentication['method'] = 'basic_auth';
$authentication['configuration']['basic_auth']['user'] = $existing_auth['user'];
if (isset($hosts[0]['authentication']['password'])) {
$authentication['configuration']['basic_auth']['password'] = $existing_auth['password'];
}
}
else {
$authentication['method'] = '';
$authentication['configuration'] = [];
}
// Set authentication.
$config->set('authentication', $authentication);
// Save the configuration.
$config->save();
}