Files
sunbeam-charms/ops-sunbeam/ops_sunbeam/ovn/container_handlers.py
Guillaume Boutry 8c674de50e [ops-sunbeam] Implement mypy linter
Implement mypy in the most non-breaking way possible. There's still some
changes of behavior that crept in, merely due to incorrect edge case
handling.

Charm libraries are generally well typed, include py.typed marker for
all of the libraries, to allow mypy analyzing their usage.

Change-Id: I7bda1913fa08dd4954a606526272ac80b45197cc
Signed-off-by: Guillaume Boutry <guillaume.boutry@canonical.com>
2024-08-13 18:56:33 +02:00

120 lines
4.0 KiB
Python

# Copyright 2022 Canonical Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Base classes for defining OVN Pebble handlers."""
import ops
import ops_sunbeam.container_handlers as sunbeam_chandlers
import ops_sunbeam.core as sunbeam_core
import ops_sunbeam.tracing as sunbeam_tracing
@sunbeam_tracing.trace_type
class OVNPebbleHandler(sunbeam_chandlers.ServicePebbleHandler):
"""Common class for OVN services."""
@property
def wrapper_script(self) -> str:
"""Path to OVN service wrapper."""
raise NotImplementedError
@property
def status_command(self) -> str:
"""Command to check status of service."""
raise NotImplementedError
def init_service(self, context: sunbeam_core.OPSCharmContexts) -> None:
"""Initialise service ready for use.
Write configuration files to the container and record
that service is ready for us.
NOTE: Override default to services being automatically started
"""
self.setup_dirs()
changes = self.write_config(context)
self.files_changed(changes)
self.status.set(ops.ActiveStatus(""))
@property
def service_description(self) -> str:
"""Return a short description of service e.g. OVN Southbound DB."""
raise NotImplementedError
def get_layer(self) -> ops.pebble.LayerDict:
"""Pebble configuration layer for OVN service.
:returns: pebble layer configuration for service
:rtype: dict
"""
return {
"summary": f"{self.service_description} service",
"description": (
"Pebble config layer for " f"{self.service_description}"
),
"services": {
self.service_name: {
"override": "replace",
"summary": f"{self.service_description}",
"command": f"bash {self.wrapper_script}",
"startup": "disabled",
},
},
}
def get_healthcheck_layer(self) -> ops.pebble.LayerDict:
"""Health check pebble layer.
:returns: pebble health check layer configuration for OVN service
:rtype: dict
"""
return {
"checks": {
"online": {
"override": "replace",
"level": "ready",
"exec": {"command": f"{self.status_command}"},
},
}
}
@property
def directories(self) -> list[sunbeam_chandlers.ContainerDir]:
"""Directories to creete in container."""
return [
sunbeam_chandlers.ContainerDir("/etc/ovn", "root", "root"),
sunbeam_chandlers.ContainerDir("/run/ovn", "root", "root"),
sunbeam_chandlers.ContainerDir("/var/lib/ovn", "root", "root"),
sunbeam_chandlers.ContainerDir("/var/log/ovn", "root", "root"),
]
def default_container_configs(
self,
) -> list[sunbeam_core.ContainerConfigFile]:
"""Files to render into containers."""
return [
sunbeam_core.ContainerConfigFile(
self.wrapper_script, "root", "root"
),
sunbeam_core.ContainerConfigFile(
"/etc/ovn/key_host", "root", "root"
),
sunbeam_core.ContainerConfigFile(
"/etc/ovn/cert_host", "root", "root"
),
sunbeam_core.ContainerConfigFile(
"/etc/ovn/ovn-central.crt", "root", "root"
),
]