mongo-charms-single-kernel 1.8.6__py3-none-any.whl → 1.8.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of mongo-charms-single-kernel might be problematic. Click here for more details.
- {mongo_charms_single_kernel-1.8.6.dist-info → mongo_charms_single_kernel-1.8.8.dist-info}/METADATA +2 -1
- {mongo_charms_single_kernel-1.8.6.dist-info → mongo_charms_single_kernel-1.8.8.dist-info}/RECORD +41 -40
- single_kernel_mongo/abstract_charm.py +8 -0
- single_kernel_mongo/config/literals.py +2 -23
- single_kernel_mongo/config/models.py +12 -0
- single_kernel_mongo/config/relations.py +0 -1
- single_kernel_mongo/config/statuses.py +10 -57
- single_kernel_mongo/core/abstract_upgrades_v3.py +149 -0
- single_kernel_mongo/core/k8s_workload.py +2 -2
- single_kernel_mongo/core/kubernetes_upgrades_v3.py +17 -0
- single_kernel_mongo/core/machine_upgrades_v3.py +54 -0
- single_kernel_mongo/core/operator.py +86 -5
- single_kernel_mongo/core/version_checker.py +7 -6
- single_kernel_mongo/core/vm_workload.py +30 -13
- single_kernel_mongo/core/workload.py +17 -19
- single_kernel_mongo/events/backups.py +3 -3
- single_kernel_mongo/events/cluster.py +1 -1
- single_kernel_mongo/events/database.py +1 -1
- single_kernel_mongo/events/lifecycle.py +5 -4
- single_kernel_mongo/events/tls.py +7 -4
- single_kernel_mongo/exceptions.py +4 -24
- single_kernel_mongo/lib/charms/operator_libs_linux/v1/systemd.py +288 -0
- single_kernel_mongo/managers/cluster.py +8 -8
- single_kernel_mongo/managers/config.py +5 -3
- single_kernel_mongo/managers/ldap.py +2 -1
- single_kernel_mongo/managers/mongo.py +48 -9
- single_kernel_mongo/managers/mongodb_operator.py +199 -96
- single_kernel_mongo/managers/mongos_operator.py +97 -35
- single_kernel_mongo/managers/sharding.py +4 -4
- single_kernel_mongo/managers/tls.py +54 -27
- single_kernel_mongo/managers/upgrade_v3.py +452 -0
- single_kernel_mongo/managers/upgrade_v3_status.py +133 -0
- single_kernel_mongo/state/app_peer_state.py +12 -2
- single_kernel_mongo/state/charm_state.py +31 -141
- single_kernel_mongo/state/config_server_state.py +0 -33
- single_kernel_mongo/state/unit_peer_state.py +10 -0
- single_kernel_mongo/templates/enable-transparent-huge-pages.service.j2 +14 -0
- single_kernel_mongo/utils/helpers.py +0 -6
- single_kernel_mongo/utils/mongo_config.py +32 -8
- single_kernel_mongo/core/abstract_upgrades.py +0 -890
- single_kernel_mongo/core/kubernetes_upgrades.py +0 -194
- single_kernel_mongo/core/machine_upgrades.py +0 -188
- single_kernel_mongo/events/upgrades.py +0 -157
- single_kernel_mongo/managers/upgrade.py +0 -334
- single_kernel_mongo/state/upgrade_state.py +0 -134
- {mongo_charms_single_kernel-1.8.6.dist-info → mongo_charms_single_kernel-1.8.8.dist-info}/WHEEL +0 -0
- {mongo_charms_single_kernel-1.8.6.dist-info → mongo_charms_single_kernel-1.8.8.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
# Copyright 2021 Canonical Ltd.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
"""Abstractions for stopping, starting and managing system services via systemd.
|
|
17
|
+
|
|
18
|
+
This library assumes that your charm is running on a platform that uses systemd. E.g.,
|
|
19
|
+
Centos 7 or later, Ubuntu Xenial (16.04) or later.
|
|
20
|
+
|
|
21
|
+
For the most part, we transparently provide an interface to a commonly used selection of
|
|
22
|
+
systemd commands, with a few shortcuts baked in. For example, service_pause and
|
|
23
|
+
service_resume with run the mask/unmask and enable/disable invocations.
|
|
24
|
+
|
|
25
|
+
Example usage:
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
from charms.operator_libs_linux.v0.systemd import service_running, service_reload
|
|
29
|
+
|
|
30
|
+
# Start a service
|
|
31
|
+
if not service_running("mysql"):
|
|
32
|
+
success = service_start("mysql")
|
|
33
|
+
|
|
34
|
+
# Attempt to reload a service, restarting if necessary
|
|
35
|
+
success = service_reload("nginx", restart_on_failure=True)
|
|
36
|
+
```
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
__all__ = [ # Don't export `_systemctl`. (It's not the intended way of using this lib.)
|
|
40
|
+
"SystemdError",
|
|
41
|
+
"daemon_reload",
|
|
42
|
+
"service_disable",
|
|
43
|
+
"service_enable",
|
|
44
|
+
"service_failed",
|
|
45
|
+
"service_pause",
|
|
46
|
+
"service_reload",
|
|
47
|
+
"service_restart",
|
|
48
|
+
"service_resume",
|
|
49
|
+
"service_running",
|
|
50
|
+
"service_start",
|
|
51
|
+
"service_stop",
|
|
52
|
+
]
|
|
53
|
+
|
|
54
|
+
import logging
|
|
55
|
+
import subprocess
|
|
56
|
+
|
|
57
|
+
logger = logging.getLogger(__name__)
|
|
58
|
+
|
|
59
|
+
# The unique Charmhub library identifier, never change it
|
|
60
|
+
LIBID = "045b0d179f6b4514a8bb9b48aee9ebaf"
|
|
61
|
+
|
|
62
|
+
# Increment this major API version when introducing breaking changes
|
|
63
|
+
LIBAPI = 1
|
|
64
|
+
|
|
65
|
+
# Increment this PATCH version before using `charmcraft publish-lib` or reset
|
|
66
|
+
# to 0 if you are raising the major API version
|
|
67
|
+
LIBPATCH = 4
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class SystemdError(Exception):
|
|
71
|
+
"""Custom exception for SystemD related errors."""
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _systemctl(*args: str, check: bool = False) -> int:
|
|
75
|
+
"""Control a system service using systemctl.
|
|
76
|
+
|
|
77
|
+
Args:
|
|
78
|
+
*args: Arguments to pass to systemctl.
|
|
79
|
+
check: Check the output of the systemctl command. Default: False.
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
Returncode of systemctl command execution.
|
|
83
|
+
|
|
84
|
+
Raises:
|
|
85
|
+
SystemdError: Raised if calling systemctl returns a non-zero returncode and check is True.
|
|
86
|
+
"""
|
|
87
|
+
cmd = ["systemctl", *args]
|
|
88
|
+
logger.debug(f"Executing command: {cmd}")
|
|
89
|
+
try:
|
|
90
|
+
proc = subprocess.run(
|
|
91
|
+
cmd,
|
|
92
|
+
stdout=subprocess.PIPE,
|
|
93
|
+
stderr=subprocess.STDOUT,
|
|
94
|
+
text=True,
|
|
95
|
+
bufsize=1,
|
|
96
|
+
encoding="utf-8",
|
|
97
|
+
check=check,
|
|
98
|
+
)
|
|
99
|
+
logger.debug(
|
|
100
|
+
f"Command {cmd} exit code: {proc.returncode}. systemctl output:\n{proc.stdout}"
|
|
101
|
+
)
|
|
102
|
+
return proc.returncode
|
|
103
|
+
except subprocess.CalledProcessError as e:
|
|
104
|
+
raise SystemdError(
|
|
105
|
+
f"Command {cmd} failed with returncode {e.returncode}. systemctl output:\n{e.stdout}"
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def service_running(service_name: str) -> bool:
|
|
110
|
+
"""Report whether a system service is running.
|
|
111
|
+
|
|
112
|
+
Args:
|
|
113
|
+
service_name: The name of the service to check.
|
|
114
|
+
|
|
115
|
+
Return:
|
|
116
|
+
True if service is running/active; False if not.
|
|
117
|
+
"""
|
|
118
|
+
# If returncode is 0, this means that is service is active.
|
|
119
|
+
return _systemctl("--quiet", "is-active", service_name) == 0
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def service_failed(service_name: str) -> bool:
|
|
123
|
+
"""Report whether a system service has failed.
|
|
124
|
+
|
|
125
|
+
Args:
|
|
126
|
+
service_name: The name of the service to check.
|
|
127
|
+
|
|
128
|
+
Returns:
|
|
129
|
+
True if service is marked as failed; False if not.
|
|
130
|
+
"""
|
|
131
|
+
# If returncode is 0, this means that the service has failed.
|
|
132
|
+
return _systemctl("--quiet", "is-failed", service_name) == 0
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def service_start(*args: str) -> bool:
|
|
136
|
+
"""Start a system service.
|
|
137
|
+
|
|
138
|
+
Args:
|
|
139
|
+
*args: Arguments to pass to `systemctl start` (normally the service name).
|
|
140
|
+
|
|
141
|
+
Returns:
|
|
142
|
+
On success, this function returns True for historical reasons.
|
|
143
|
+
|
|
144
|
+
Raises:
|
|
145
|
+
SystemdError: Raised if `systemctl start ...` returns a non-zero returncode.
|
|
146
|
+
"""
|
|
147
|
+
return _systemctl("start", *args, check=True) == 0
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def service_stop(*args: str) -> bool:
|
|
151
|
+
"""Stop a system service.
|
|
152
|
+
|
|
153
|
+
Args:
|
|
154
|
+
*args: Arguments to pass to `systemctl stop` (normally the service name).
|
|
155
|
+
|
|
156
|
+
Returns:
|
|
157
|
+
On success, this function returns True for historical reasons.
|
|
158
|
+
|
|
159
|
+
Raises:
|
|
160
|
+
SystemdError: Raised if `systemctl stop ...` returns a non-zero returncode.
|
|
161
|
+
"""
|
|
162
|
+
return _systemctl("stop", *args, check=True) == 0
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def service_restart(*args: str) -> bool:
|
|
166
|
+
"""Restart a system service.
|
|
167
|
+
|
|
168
|
+
Args:
|
|
169
|
+
*args: Arguments to pass to `systemctl restart` (normally the service name).
|
|
170
|
+
|
|
171
|
+
Returns:
|
|
172
|
+
On success, this function returns True for historical reasons.
|
|
173
|
+
|
|
174
|
+
Raises:
|
|
175
|
+
SystemdError: Raised if `systemctl restart ...` returns a non-zero returncode.
|
|
176
|
+
"""
|
|
177
|
+
return _systemctl("restart", *args, check=True) == 0
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def service_enable(*args: str) -> bool:
|
|
181
|
+
"""Enable a system service.
|
|
182
|
+
|
|
183
|
+
Args:
|
|
184
|
+
*args: Arguments to pass to `systemctl enable` (normally the service name).
|
|
185
|
+
|
|
186
|
+
Returns:
|
|
187
|
+
On success, this function returns True for historical reasons.
|
|
188
|
+
|
|
189
|
+
Raises:
|
|
190
|
+
SystemdError: Raised if `systemctl enable ...` returns a non-zero returncode.
|
|
191
|
+
"""
|
|
192
|
+
return _systemctl("enable", *args, check=True) == 0
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def service_disable(*args: str) -> bool:
|
|
196
|
+
"""Disable a system service.
|
|
197
|
+
|
|
198
|
+
Args:
|
|
199
|
+
*args: Arguments to pass to `systemctl disable` (normally the service name).
|
|
200
|
+
|
|
201
|
+
Returns:
|
|
202
|
+
On success, this function returns True for historical reasons.
|
|
203
|
+
|
|
204
|
+
Raises:
|
|
205
|
+
SystemdError: Raised if `systemctl disable ...` returns a non-zero returncode.
|
|
206
|
+
"""
|
|
207
|
+
return _systemctl("disable", *args, check=True) == 0
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def service_reload(service_name: str, restart_on_failure: bool = False) -> bool:
|
|
211
|
+
"""Reload a system service, optionally falling back to restart if reload fails.
|
|
212
|
+
|
|
213
|
+
Args:
|
|
214
|
+
service_name: The name of the service to reload.
|
|
215
|
+
restart_on_failure:
|
|
216
|
+
Boolean indicating whether to fall back to a restart if the reload fails.
|
|
217
|
+
|
|
218
|
+
Returns:
|
|
219
|
+
On success, this function returns True for historical reasons.
|
|
220
|
+
|
|
221
|
+
Raises:
|
|
222
|
+
SystemdError: Raised if `systemctl reload|restart ...` returns a non-zero returncode.
|
|
223
|
+
"""
|
|
224
|
+
try:
|
|
225
|
+
return _systemctl("reload", service_name, check=True) == 0
|
|
226
|
+
except SystemdError:
|
|
227
|
+
if restart_on_failure:
|
|
228
|
+
return service_restart(service_name)
|
|
229
|
+
else:
|
|
230
|
+
raise
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def service_pause(service_name: str) -> bool:
|
|
234
|
+
"""Pause a system service.
|
|
235
|
+
|
|
236
|
+
Stops the service and prevents the service from starting again at boot.
|
|
237
|
+
|
|
238
|
+
Args:
|
|
239
|
+
service_name: The name of the service to pause.
|
|
240
|
+
|
|
241
|
+
Returns:
|
|
242
|
+
On success, this function returns True for historical reasons.
|
|
243
|
+
|
|
244
|
+
Raises:
|
|
245
|
+
SystemdError: Raised if service is still running after being paused by systemctl.
|
|
246
|
+
"""
|
|
247
|
+
_systemctl("disable", "--now", service_name)
|
|
248
|
+
_systemctl("mask", service_name)
|
|
249
|
+
|
|
250
|
+
if service_running(service_name):
|
|
251
|
+
raise SystemdError(f"Attempted to pause {service_name!r}, but it is still running.")
|
|
252
|
+
|
|
253
|
+
return True
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def service_resume(service_name: str) -> bool:
|
|
257
|
+
"""Resume a system service.
|
|
258
|
+
|
|
259
|
+
Re-enable starting the service again at boot. Start the service.
|
|
260
|
+
|
|
261
|
+
Args:
|
|
262
|
+
service_name: The name of the service to resume.
|
|
263
|
+
|
|
264
|
+
Returns:
|
|
265
|
+
On success, this function returns True for historical reasons.
|
|
266
|
+
|
|
267
|
+
Raises:
|
|
268
|
+
SystemdError: Raised if service is not running after being resumed by systemctl.
|
|
269
|
+
"""
|
|
270
|
+
_systemctl("unmask", service_name)
|
|
271
|
+
_systemctl("enable", "--now", service_name)
|
|
272
|
+
|
|
273
|
+
if not service_running(service_name):
|
|
274
|
+
raise SystemdError(f"Attempted to resume {service_name!r}, but it is not running.")
|
|
275
|
+
|
|
276
|
+
return True
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
def daemon_reload() -> bool:
|
|
280
|
+
"""Reload systemd manager configuration.
|
|
281
|
+
|
|
282
|
+
Returns:
|
|
283
|
+
On success, this function returns True for historical reasons.
|
|
284
|
+
|
|
285
|
+
Raises:
|
|
286
|
+
SystemdError: Raised if `systemctl daemon-reload` returns a non-zero returncode.
|
|
287
|
+
"""
|
|
288
|
+
return _systemctl("daemon-reload", check=True) == 0
|
|
@@ -59,7 +59,7 @@ class ClusterProvider(Object):
|
|
|
59
59
|
self.relation_name = relation_name
|
|
60
60
|
self.data_interface = self.state.cluster_provider_data_interface
|
|
61
61
|
|
|
62
|
-
def assert_pass_hook_checks(self) -> None:
|
|
62
|
+
def assert_pass_hook_checks(self, initial_event: bool = False) -> None:
|
|
63
63
|
"""Runs the pre hook checks, raises if it fails."""
|
|
64
64
|
if not self.state.db_initialised:
|
|
65
65
|
raise DeferrableFailedHookChecksError("DB is not initialised")
|
|
@@ -77,7 +77,7 @@ class ClusterProvider(Object):
|
|
|
77
77
|
if not self.charm.unit.is_leader():
|
|
78
78
|
raise NonDeferrableFailedHookChecksError("Not leader")
|
|
79
79
|
|
|
80
|
-
if self.
|
|
80
|
+
if self.dependent.refresh_in_progress and initial_event:
|
|
81
81
|
raise DeferrableFailedHookChecksError(
|
|
82
82
|
"Processing mongos applications is not supported during an upgrade. The charm may be in a broken, unrecoverable state."
|
|
83
83
|
)
|
|
@@ -88,12 +88,12 @@ class ClusterProvider(Object):
|
|
|
88
88
|
# we don't have any cluster relation.
|
|
89
89
|
return self.state.is_role(MongoDBRoles.CONFIG_SERVER) or not self.state.cluster_relations
|
|
90
90
|
|
|
91
|
-
def share_secret_to_mongos(self, relation: Relation) -> None:
|
|
91
|
+
def share_secret_to_mongos(self, relation: Relation, initial_event: bool = False) -> None:
|
|
92
92
|
"""Handles the database requested event.
|
|
93
93
|
|
|
94
94
|
The first time secrets are written to relations should be on this event.
|
|
95
95
|
"""
|
|
96
|
-
self.assert_pass_hook_checks()
|
|
96
|
+
self.assert_pass_hook_checks(initial_event=initial_event)
|
|
97
97
|
|
|
98
98
|
config_server_db = self.state.generate_config_server_db()
|
|
99
99
|
self.dependent.mongo_manager.reconcile_mongo_users_and_dbs(relation)
|
|
@@ -133,7 +133,7 @@ class ClusterProvider(Object):
|
|
|
133
133
|
If it has departed, we run some checks and if we are a VM charm, we
|
|
134
134
|
proceed to reconcile the users and DB and cleanup mongoDB.
|
|
135
135
|
"""
|
|
136
|
-
if self.
|
|
136
|
+
if self.dependent.refresh_in_progress:
|
|
137
137
|
logger.warning(
|
|
138
138
|
"Removing integration to mongos is not supported during an upgrade. The charm may be in a broken, unrecoverable state."
|
|
139
139
|
)
|
|
@@ -269,8 +269,8 @@ class ClusterRequirer(Object):
|
|
|
269
269
|
raise DeferrableFailedHookChecksError(
|
|
270
270
|
"Mongos was waiting for config-server to enable TLS. Wait for TLS to be enabled until starting mongos."
|
|
271
271
|
)
|
|
272
|
-
if self.
|
|
273
|
-
|
|
272
|
+
if self.dependent.refresh_in_progress:
|
|
273
|
+
logger.warning(
|
|
274
274
|
"Processing client applications is not supported during an upgrade. The charm may be in a broken, unrecoverable state."
|
|
275
275
|
)
|
|
276
276
|
|
|
@@ -290,7 +290,7 @@ class ClusterRequirer(Object):
|
|
|
290
290
|
"""
|
|
291
291
|
if not username or not password:
|
|
292
292
|
raise WaitingForSecretsError
|
|
293
|
-
if self.
|
|
293
|
+
if self.dependent.refresh_in_progress:
|
|
294
294
|
logger.warning(
|
|
295
295
|
"Processing client applications is not supported during an upgrade. The charm may be in a broken, unrecoverable state."
|
|
296
296
|
)
|
|
@@ -18,7 +18,6 @@ from typing_extensions import override
|
|
|
18
18
|
from yaml import safe_dump, safe_load
|
|
19
19
|
|
|
20
20
|
from single_kernel_mongo.config.literals import (
|
|
21
|
-
LOCALHOST,
|
|
22
21
|
PBM_RESTART_DELAY,
|
|
23
22
|
CharmKind,
|
|
24
23
|
MongoPorts,
|
|
@@ -343,6 +342,9 @@ class MongoConfigManager(FileBasedConfigManager, ABC):
|
|
|
343
342
|
"allowInvalidCertificates": True,
|
|
344
343
|
"clusterCAFile": f"{self.workload.paths.int_ca_file}",
|
|
345
344
|
"clusterFile": f"{self.workload.paths.int_pem_file}",
|
|
345
|
+
"clusterAuthX509": {
|
|
346
|
+
"attributes": f"O={self.state.get_subject_name()}",
|
|
347
|
+
},
|
|
346
348
|
}
|
|
347
349
|
},
|
|
348
350
|
},
|
|
@@ -366,7 +368,7 @@ class MongoConfigManager(FileBasedConfigManager, ABC):
|
|
|
366
368
|
"tls": {
|
|
367
369
|
"CAFile": f"{self.workload.paths.ext_ca_file}",
|
|
368
370
|
"certificateKeyFile": f"{self.workload.paths.ext_pem_file}",
|
|
369
|
-
"mode": "
|
|
371
|
+
"mode": "requireTLS",
|
|
370
372
|
"disabledProtocols": "TLS1_0,TLS1_1",
|
|
371
373
|
}
|
|
372
374
|
},
|
|
@@ -494,7 +496,7 @@ class MongosConfigManager(MongoConfigManager):
|
|
|
494
496
|
return {"sharding": {"configDB": uri}}
|
|
495
497
|
return {
|
|
496
498
|
"sharding": {
|
|
497
|
-
"configDB": f"{self.state.app_peer_data.replica_set}/{
|
|
499
|
+
"configDB": f"{self.state.app_peer_data.replica_set}/{self.state.unit_peer_data.internal_address}:{MongoPorts.MONGODB_PORT.value}"
|
|
498
500
|
}
|
|
499
501
|
}
|
|
500
502
|
|
|
@@ -76,7 +76,8 @@ class LDAPManager(Object, ManagerStatusProtocol):
|
|
|
76
76
|
raise DeferrableFailedHookChecksError("DB is not initialised")
|
|
77
77
|
if self.state.is_role(MongoDBRoles.SHARD):
|
|
78
78
|
raise InvalidLdapWithShardError("Cannot integrate LDAP with shard.")
|
|
79
|
-
|
|
79
|
+
# Defer upon regular integration, but let's continue on an update.
|
|
80
|
+
if self.dependent.refresh_in_progress and not self.state.ldap.is_ready():
|
|
80
81
|
raise DeferrableFailedHookChecksError(
|
|
81
82
|
"Adding LDAP is not supported during an upgrade. The charm may be in a broken, unrecoverable state."
|
|
82
83
|
)
|
|
@@ -14,6 +14,7 @@ from __future__ import annotations
|
|
|
14
14
|
import json
|
|
15
15
|
import logging
|
|
16
16
|
from typing import TYPE_CHECKING
|
|
17
|
+
from urllib.parse import urlencode
|
|
17
18
|
|
|
18
19
|
from dacite import from_dict
|
|
19
20
|
from data_platform_helpers.advanced_statuses.models import StatusObject
|
|
@@ -27,6 +28,7 @@ from pymongo.errors import (
|
|
|
27
28
|
PyMongoError,
|
|
28
29
|
ServerSelectionTimeoutError,
|
|
29
30
|
)
|
|
31
|
+
from tenacity import Retrying, stop_after_attempt, wait_fixed
|
|
30
32
|
|
|
31
33
|
from single_kernel_mongo.config.literals import MongoPorts, Substrates
|
|
32
34
|
from single_kernel_mongo.config.statuses import CharmStatuses, MongodStatuses
|
|
@@ -34,6 +36,7 @@ from single_kernel_mongo.core.structured_config import MongoDBRoles
|
|
|
34
36
|
from single_kernel_mongo.exceptions import (
|
|
35
37
|
DatabaseRequestedHasNotRunYetError,
|
|
36
38
|
DeployedWithoutTrustError,
|
|
39
|
+
FailedToElectNewPrimaryError,
|
|
37
40
|
MissingCredentialsError,
|
|
38
41
|
SetPasswordError,
|
|
39
42
|
)
|
|
@@ -97,9 +100,15 @@ class MongoManager(Object, ManagerStatusProtocol):
|
|
|
97
100
|
Pass direct=True, when checking if a *single replica* is ready.
|
|
98
101
|
Pass direct=False, when checking if the entire replica set is ready
|
|
99
102
|
"""
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
+
port = (
|
|
104
|
+
MongoPorts.MONGOS_PORT.value
|
|
105
|
+
if self.state.is_role(MongoDBRoles.MONGOS)
|
|
106
|
+
else MongoPorts.MONGODB_PORT.value
|
|
107
|
+
)
|
|
108
|
+
params = self.state.operator_config.tls_config
|
|
109
|
+
|
|
110
|
+
actual_uri = uri or f"mongodb://localhost:{port}"
|
|
111
|
+
actual_uri = f"{actual_uri}/?{urlencode(params)}"
|
|
103
112
|
with MongoConnection(EMPTY_CONFIGURATION, actual_uri, direct=direct) as direct_mongo:
|
|
104
113
|
return direct_mongo.is_ready
|
|
105
114
|
|
|
@@ -265,7 +274,7 @@ class MongoManager(Object, ManagerStatusProtocol):
|
|
|
265
274
|
return
|
|
266
275
|
|
|
267
276
|
data_interface.set_endpoints(relation.id, ",".join(sorted(config.hosts)))
|
|
268
|
-
data_interface.set_uris(relation.id, config.
|
|
277
|
+
data_interface.set_uris(relation.id, config.uri_without_tls)
|
|
269
278
|
|
|
270
279
|
if not self.state.is_role(MongoDBRoles.MONGOS):
|
|
271
280
|
data_interface.set_replset(
|
|
@@ -390,10 +399,10 @@ class MongoManager(Object, ManagerStatusProtocol):
|
|
|
390
399
|
relation.id,
|
|
391
400
|
",".join(sorted(config.hosts)),
|
|
392
401
|
)
|
|
393
|
-
if config.
|
|
402
|
+
if config.uri_without_tls != uris:
|
|
394
403
|
data_interface.set_uris(
|
|
395
404
|
relation.id,
|
|
396
|
-
config.
|
|
405
|
+
config.uri_without_tls,
|
|
397
406
|
)
|
|
398
407
|
if config.database != database:
|
|
399
408
|
data_interface.set_database(
|
|
@@ -421,8 +430,7 @@ class MongoManager(Object, ManagerStatusProtocol):
|
|
|
421
430
|
"password": password,
|
|
422
431
|
"hosts": self.state.app_hosts,
|
|
423
432
|
"roles": set(roles.split(",")),
|
|
424
|
-
"
|
|
425
|
-
"tls_internal": False,
|
|
433
|
+
"tls_enabled": False,
|
|
426
434
|
"port": self.state.host_port,
|
|
427
435
|
}
|
|
428
436
|
if not self.state.is_role(MongoDBRoles.MONGOS):
|
|
@@ -466,7 +474,7 @@ class MongoManager(Object, ManagerStatusProtocol):
|
|
|
466
474
|
|
|
467
475
|
for member in config_hosts - replset_members:
|
|
468
476
|
logger.debug("Adding %s to replica set", member)
|
|
469
|
-
if not self.mongod_ready(uri=member):
|
|
477
|
+
if not self.mongod_ready(uri=f"mongodb://{member}"):
|
|
470
478
|
logger.debug("not reconfiguring: %s is not ready yet.", member)
|
|
471
479
|
raise NotReadyError
|
|
472
480
|
mongo.add_replset_member(member)
|
|
@@ -555,3 +563,34 @@ class MongoManager(Object, ManagerStatusProtocol):
|
|
|
555
563
|
return [MongodStatuses.WAITING_RECONFIG.value]
|
|
556
564
|
|
|
557
565
|
return charm_statuses
|
|
566
|
+
|
|
567
|
+
def set_feature_compatibility_version(self, feature_version: str) -> None:
|
|
568
|
+
"""Sets the mongos feature compatibility version."""
|
|
569
|
+
if self.state.is_role(MongoDBRoles.REPLICATION):
|
|
570
|
+
config = self.state.mongo_config
|
|
571
|
+
elif self.state.is_role(MongoDBRoles.CONFIG_SERVER):
|
|
572
|
+
config = self.state.mongos_config
|
|
573
|
+
else:
|
|
574
|
+
return
|
|
575
|
+
with MongoConnection(config) as mongos:
|
|
576
|
+
mongos.client.admin.command(
|
|
577
|
+
"setFeatureCompatibilityVersion", value=feature_version, confirm=True
|
|
578
|
+
)
|
|
579
|
+
|
|
580
|
+
def step_down_primary_and_wait_reelection(self):
|
|
581
|
+
"""Steps down the current primary and waits for a new one to be elected."""
|
|
582
|
+
if len(self.state.internal_hosts) < 2:
|
|
583
|
+
logger.warning(
|
|
584
|
+
"No secondaries to become primary - upgrading primary without electing a new one, expect downtime."
|
|
585
|
+
)
|
|
586
|
+
return
|
|
587
|
+
|
|
588
|
+
old_primary = self.dependent.primary_unit_name # type: ignore
|
|
589
|
+
with MongoConnection(self.state.mongo_config) as mongod:
|
|
590
|
+
mongod.step_down_primary()
|
|
591
|
+
|
|
592
|
+
for attempt in Retrying(stop=stop_after_attempt(30), wait=wait_fixed(1), reraise=True):
|
|
593
|
+
with attempt:
|
|
594
|
+
new_primary = self.dependent.primary_unit_name # type: ignore
|
|
595
|
+
if new_primary == old_primary:
|
|
596
|
+
raise FailedToElectNewPrimaryError()
|