mongo-charms-single-kernel 1.8.7__py3-none-any.whl → 1.8.9__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.

Files changed (31) hide show
  1. {mongo_charms_single_kernel-1.8.7.dist-info → mongo_charms_single_kernel-1.8.9.dist-info}/METADATA +1 -1
  2. {mongo_charms_single_kernel-1.8.7.dist-info → mongo_charms_single_kernel-1.8.9.dist-info}/RECORD +30 -28
  3. single_kernel_mongo/config/literals.py +8 -3
  4. single_kernel_mongo/config/models.py +12 -0
  5. single_kernel_mongo/config/relations.py +2 -1
  6. single_kernel_mongo/config/statuses.py +127 -20
  7. single_kernel_mongo/core/operator.py +68 -1
  8. single_kernel_mongo/core/structured_config.py +2 -0
  9. single_kernel_mongo/core/workload.py +10 -4
  10. single_kernel_mongo/events/cluster.py +5 -0
  11. single_kernel_mongo/events/sharding.py +3 -1
  12. single_kernel_mongo/events/tls.py +183 -157
  13. single_kernel_mongo/exceptions.py +0 -8
  14. single_kernel_mongo/lib/charms/operator_libs_linux/v1/systemd.py +288 -0
  15. single_kernel_mongo/lib/charms/tls_certificates_interface/v4/tls_certificates.py +1995 -0
  16. single_kernel_mongo/managers/cluster.py +70 -28
  17. single_kernel_mongo/managers/config.py +14 -8
  18. single_kernel_mongo/managers/mongo.py +1 -1
  19. single_kernel_mongo/managers/mongodb_operator.py +53 -56
  20. single_kernel_mongo/managers/mongos_operator.py +18 -20
  21. single_kernel_mongo/managers/sharding.py +154 -127
  22. single_kernel_mongo/managers/tls.py +223 -206
  23. single_kernel_mongo/state/charm_state.py +39 -16
  24. single_kernel_mongo/state/cluster_state.py +8 -0
  25. single_kernel_mongo/state/config_server_state.py +9 -0
  26. single_kernel_mongo/state/tls_state.py +39 -12
  27. single_kernel_mongo/templates/enable-transparent-huge-pages.service.j2 +14 -0
  28. single_kernel_mongo/utils/helpers.py +4 -19
  29. single_kernel_mongo/lib/charms/tls_certificates_interface/v3/tls_certificates.py +0 -2123
  30. {mongo_charms_single_kernel-1.8.7.dist-info → mongo_charms_single_kernel-1.8.9.dist-info}/WHEEL +0 -0
  31. {mongo_charms_single_kernel-1.8.7.dist-info → mongo_charms_single_kernel-1.8.9.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