Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add workload version to juju status #18

Merged
merged 19 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ description: |
A charm for the matrix synapse chat server.
Synapse is a drop in replacement for other chat servers like Mattermost and Slack.
This charm is useful if you want to spin up your own chat instance.
docs: https://discourse.charmhub.io/t/synapse-documentation-overview/11579
docs: https://discourse.charmhub.io/t/synapse-documentation-overview/11358
issues: https://github.com/canonical/synapse-operator/issues
maintainers:
- https://launchpad.net/~canonical-is-devops
Expand Down
15 changes: 15 additions & 0 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from ops.main import main

import actions
import synapse
from charm_state import CharmConfigInvalidError, CharmState
from constants import SYNAPSE_CONTAINER_NAME, SYNAPSE_PORT
from database_observer import DatabaseObserver
Expand Down Expand Up @@ -79,13 +80,27 @@ def change_config(self, _: ops.HookEvent) -> None:
return
self.model.unit.status = ops.ActiveStatus()

def _set_workload_version(self) -> None:
"""Set workload version with Synapse version."""
container = self.unit.get_container(SYNAPSE_CONTAINER_NAME)
if not container.can_connect():
self.unit.status = ops.MaintenanceStatus("Waiting for pebble")
return
try:
synapse_version = synapse.get_version()
self.unit.set_workload_version(synapse_version)
except synapse.APIError as exc:
logger.debug("Cannot set workload version at this time: %s", exc)

def _on_config_changed(self, event: ops.HookEvent) -> None:
"""Handle changed configuration.

Args:
event: Event triggering after config is changed.
"""
self.change_config(event)
logger.debug("Setting workload in config-changed event")
self._set_workload_version()

def _on_pebble_ready(self, event: ops.HookEvent) -> None:
"""Handle pebble ready event.
Expand Down
1 change: 1 addition & 0 deletions src/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""This module defines constants used throughout the Synapse application."""

CHECK_READY_NAME = "synapse-ready"
CHECK_ALIVE_NAME = "synapse-alive"
COMMAND_MIGRATE_CONFIG = "migrate_config"
PROMETHEUS_TARGET_PORT = "9000"
SYNAPSE_CONFIG_DIR = "/data"
Expand Down
4 changes: 2 additions & 2 deletions src/database_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def prepare(self) -> None:
).format(sql.Literal(self._database_name))
)
except psycopg2.Error as exc:
logger.error("Failed to prepare database: %s", str(exc))
logger.exception("Failed to prepare database: %r", exc)
raise
finally:
self._close()
Expand All @@ -111,7 +111,7 @@ def erase(self) -> None:
).format(sql.Identifier(self._database_name))
)
except psycopg2.Error as exc:
logger.error("Failed to erase database: %s", str(exc))
logger.exception("Failed to erase database: %r", exc)
raise
finally:
self._close()
2 changes: 2 additions & 0 deletions src/pebble.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import synapse
from charm_state import CharmState
from constants import (
CHECK_ALIVE_NAME,
CHECK_READY_NAME,
SYNAPSE_COMMAND_PATH,
SYNAPSE_CONTAINER_NAME,
Expand Down Expand Up @@ -115,6 +116,7 @@ def _pebble_layer(self) -> ops.pebble.LayerDict:
},
"checks": {
CHECK_READY_NAME: synapse.check_ready(),
CHECK_ALIVE_NAME: synapse.check_alive(),
},
}
return typing.cast(ops.pebble.LayerDict, layer)
Expand Down
3 changes: 2 additions & 1 deletion src/synapse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
"""Synapse package is used to interact with Synapse instance."""

# Exporting methods to be used for another modules
from .api import APIError, register_user # noqa: F401
from .api import APIError, get_version, register_user # noqa: F401
from .workload import ( # noqa: F401
ExecResult,
WorkloadError,
check_alive,
check_ready,
enable_metrics,
execute_migrate_config,
Expand Down
122 changes: 98 additions & 24 deletions src/synapse/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@
import hashlib
import hmac
import logging
import re
import typing

import requests
from requests.adapters import HTTPAdapter
from urllib3.util import Retry

from user import User

logger = logging.getLogger(__name__)

SYNAPSE_URL = "http://localhost:8008"
REGISTER_URL = f"{SYNAPSE_URL}/_synapse/admin/v1/register"
VERSION_URL = f"{SYNAPSE_URL}/_synapse/admin/v1/server_version"
SYNAPSE_VERSION_REGEX = r"(\d+\.\d+\.\d+(?:\w+)?)\s?"


class APIError(Exception):
Expand All @@ -30,22 +35,30 @@ class APIError(Exception):
"""

def __init__(self, msg: str):
"""Initialize a new instance of the RegisterUserError exception.
"""Initialize a new instance of the APIError exception.

Args:
msg (str): Explanation of the error.
"""
self.msg = msg


class RegisterUserError(APIError):
"""Exception raised when registering user via API fails."""


class NetworkError(APIError):
"""Exception raised when requesting API fails due network issues."""


class GetNonceError(APIError):
"""Exception raised when getting nonce via API fails."""


class GetVersionError(APIError):
"""Exception raised when getting version via API fails."""


class VersionUnexpectedContentError(GetVersionError):
"""Exception raised when output of getting version is unexpected."""


def register_user(registration_shared_secret: str, user: User) -> None:
"""Register user.

Expand All @@ -58,7 +71,6 @@ def register_user(registration_shared_secret: str, user: User) -> None:
"""
# get nonce
nonce = _get_nonce()

# generate mac
hex_mac = _generate_mac(
shared_secret=registration_shared_secret,
Expand All @@ -67,6 +79,7 @@ def register_user(registration_shared_secret: str, user: User) -> None:
password=user.password,
admin=user.admin,
)
# build data
data = {
"nonce": nonce,
"username": user.username,
Expand All @@ -78,13 +91,12 @@ def register_user(registration_shared_secret: str, user: User) -> None:
try:
res = requests.post(REGISTER_URL, json=data, timeout=5)
res.raise_for_status()
except (
requests.exceptions.ConnectionError,
requests.exceptions.Timeout,
requests.exceptions.HTTPError,
) as exc:
logger.error("Failed to request %s : %s", REGISTER_URL, exc)
raise NetworkError(f"Failed to request {REGISTER_URL}.") from exc
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as exc:
logger.exception("Failed to connect to %s: %r", REGISTER_URL, exc)
raise NetworkError(f"Failed to connect to {REGISTER_URL}.") from exc
except requests.exceptions.HTTPError as exc:
logger.exception("HTTP error from %s: %r", REGISTER_URL, exc)
raise NetworkError(f"HTTP error from {REGISTER_URL}.") from exc
amandahla marked this conversation as resolved.
Show resolved Hide resolved


def _generate_mac(
Expand Down Expand Up @@ -129,23 +141,85 @@ def _generate_mac(
return mac.hexdigest()


def _send_get_request(api_url: str, retry: bool = False) -> requests.Response:
"""Call Synapse API using requests.get with retry and timeout.

Args:
api_url: URL to be requested.
retry: call URL with a retry. Default is False.

Raises:
NetworkError: if there was an error fetching the api_url.

Returns:
Response from calling the URL.
"""
try:
session = requests.Session()
retries = Retry(
total=3,
backoff_factor=3,
)
if retry:
session.mount("http://", HTTPAdapter(max_retries=retries))
res = session.get(api_url, timeout=10)
res.raise_for_status()
session.close()
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as exc:
logger.exception("Failed to connect to %s: %r", api_url, exc)
raise NetworkError(f"Failed to connect to {api_url}.") from exc
except requests.exceptions.HTTPError as exc:
logger.exception("HTTP error from %s: %r", api_url, exc)
raise NetworkError(f"HTTP error from {api_url}.") from exc
return res


def _get_nonce() -> str:
"""Get nonce.

Returns:
The nonce returned by Synapse API.

Raises:
NetworkError: if there was an error fetching the nonce.
GetNonceError: if there was an error while reading nonce.
"""
res = _send_get_request(REGISTER_URL)
try:
res = requests.get(REGISTER_URL, timeout=5)
res.raise_for_status()
return res.json()["nonce"]
except (
requests.exceptions.ConnectionError,
requests.exceptions.Timeout,
requests.exceptions.HTTPError,
) as exc:
logger.error("Failed to request %s : %s", REGISTER_URL, exc)
raise NetworkError(f"Failed to request {REGISTER_URL}.") from exc
nonce = res.json()["nonce"]
except (requests.exceptions.JSONDecodeError, TypeError, KeyError) as exc:
logger.exception("Failed to decode nonce: %r. Received: %s", exc, res.text)
raise GetNonceError(str(exc)) from exc

return nonce


def get_version() -> str:
"""Get version.

Expected API output:
{
"server_version": "0.99.2rc1 (b=develop, abcdef123)",
"python_version": "3.7.8"
}

We're using retry here because after the config change, Synapse is restarted.

Returns:
The version returned by Synapse API.

Raises:
GetVersionError: if there was an error while reading version.
VersionUnexpectedContentError: if the version has unexpected content.
"""
res = _send_get_request(VERSION_URL, retry=True)
try:
server_version = res.json()["server_version"]
amandahla marked this conversation as resolved.
Show resolved Hide resolved
except (requests.exceptions.JSONDecodeError, KeyError, TypeError) as exc:
logger.exception("Failed to decode version: %r. Received: %s", exc, res.text)
raise GetVersionError(str(exc)) from exc
version_match = re.search(SYNAPSE_VERSION_REGEX, server_version)
if not version_match:
raise VersionUnexpectedContentError(
f"server_version has unexpected content: {server_version}"
)
return version_match.group(1)
30 changes: 22 additions & 8 deletions src/synapse/workload.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
SYNAPSE_PORT,
)

from .api import VERSION_URL

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -68,18 +70,30 @@ class ExecResult(typing.NamedTuple):
stderr: str


def check_ready() -> typing.Dict:
"""Return the Synapse container check.
def check_ready() -> ops.pebble.CheckDict:
"""Return the Synapse container ready check.

Returns:
Dict: check object converted to its dict representation.
"""
check = Check(CHECK_READY_NAME)
check.override = "replace"
check.level = "ready"
check.http = {"url": VERSION_URL}
return check.to_dict()


def check_alive() -> ops.pebble.CheckDict:
"""Return the Synapse container alive check.

Returns:
Dict: check object converted to its dict representation.
"""
check = Check(CHECK_READY_NAME)
check.override = "replace"
check.level = "alive"
check.tcp = {"port": SYNAPSE_PORT}
# _CheckDict cannot be imported
return check.to_dict() # type: ignore
return check.to_dict()


def execute_migrate_config(container: ops.Container, charm_state: CharmState) -> None:
Expand Down Expand Up @@ -167,8 +181,8 @@ def reset_instance(container: ops.Container) -> None:
if "device or resource busy" in str(path_error):
pass
else:
logger.error(
"exception while erasing directory %s: %s", SYNAPSE_CONFIG_DIR, path_error
logger.exception(
"exception while erasing directory %s: %r", SYNAPSE_CONFIG_DIR, path_error
)
raise

Expand Down Expand Up @@ -275,8 +289,8 @@ def _get_configuration_field(container: ops.Container, fieldname: str) -> typing
SYNAPSE_CONFIG_PATH,
)
return None
logger.error(
"exception while reading configuration file %s: %s",
logger.exception(
"exception while reading configuration file %s: %r",
SYNAPSE_CONFIG_PATH,
path_error,
)
Expand Down
Loading
Loading