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 13 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,11 +8,11 @@ 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: ""
amandahla marked this conversation as resolved.
Show resolved Hide resolved
issues: https://github.com/canonical/synapse-operator/issues
maintainers:
- launchpad.net/~canonical-is-devops
source: https://github.com/canonical/synapse-operator
docs: "https://discourse.charmhub.io/t/synapse-documentation-overview/11358"
assumes:
- k8s-api

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
173 changes: 134 additions & 39 deletions src/synapse/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@
import hashlib
import hmac
import logging
import re
import typing

import requests
from requests import Session
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 +36,38 @@ 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 NonceNotFoundError(GetNonceError):
"""Exception raised when nonce is not found."""


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


class VersionNotFoundError(GetVersionError):
"""Exception raised when version is not found."""


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 @@ -55,36 +77,39 @@ def register_user(registration_shared_secret: str, user: User) -> None:

Raises:
NetworkError: if there was an error registering the user.
GetNonceError: if there was an error while getting nonce.
"""
# get nonce
nonce = _get_nonce()

# generate mac
hex_mac = _generate_mac(
shared_secret=registration_shared_secret,
nonce=nonce,
user=user.username,
password=user.password,
admin=user.admin,
)
data = {
"nonce": nonce,
"username": user.username,
"password": user.password,
"mac": hex_mac,
"admin": user.admin,
}
# finally register user
try:
# get nonce
nonce = _get_nonce()

# generate mac
hex_mac = _generate_mac(
shared_secret=registration_shared_secret,
nonce=nonce,
user=user.username,
password=user.password,
admin=user.admin,
)
data = {
"nonce": nonce,
"username": user.username,
"password": user.password,
"mac": hex_mac,
"admin": user.admin,
}
# finally register user
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
except GetNonceError as exc:
logger.exception("Failed to get nonce: %r", exc)
raise GetNonceError(str(exc)) from exc
amandahla marked this conversation as resolved.
Show resolved Hide resolved


def _generate_mac(
Expand Down Expand Up @@ -137,15 +162,85 @@ def _get_nonce() -> str:

Raises:
NetworkError: if there was an error fetching the nonce.
GetNonceError: if there was an error while getting nonce.
"""
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
res_json = res.json()
if not isinstance(res_json, dict):
# Exception not in docstring because is captured.
raise GetNonceError(f"Response has unexpected encode: {res_json}") # noqa: DCO053
nonce = res_json.get("nonce", None)
if nonce is None:
# Exception not in docstring because is captured.
raise NonceNotFoundError( # noqa: DCO053
f"There is no nonce in JSON output: {res_json}"
)
return nonce
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
except (GetNonceError, requests.exceptions.JSONDecodeError) as exc:
logger.exception("Failed to get nonce: %r", exc)
raise GetNonceError(str(exc)) from exc


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:
NetworkError: if there was an error fetching the version.
GetVersionError: if there was an error while reading version.
"""
try:
session = Session()
retries = Retry(
total=3,
backoff_factor=3,
)
session.mount("http://", HTTPAdapter(max_retries=retries))
res = session.get(VERSION_URL, timeout=10)
res.raise_for_status()
res_json = res.json()
if not isinstance(res_json, dict):
amandahla marked this conversation as resolved.
Show resolved Hide resolved
# Exception not in docstring because is captured.
raise VersionUnexpectedContentError( # noqa: DCO053
f"Response has unexpected encode: {res_json}"
)
server_version = res_json.get("server_version", None)
amandahla marked this conversation as resolved.
Show resolved Hide resolved
if server_version is None:
# Exception not in docstring because is captured.
amandahla marked this conversation as resolved.
Show resolved Hide resolved
raise VersionNotFoundError( # noqa: DCO053
f"There is no server_version in JSON output: {res_json}"
)
version_match = re.search(SYNAPSE_VERSION_REGEX, server_version)
if not version_match:
# Exception not in docstring because is captured.
amandahla marked this conversation as resolved.
Show resolved Hide resolved
raise VersionUnexpectedContentError( # noqa: DCO053
f"server_version has unexpected content: {server_version}"
)
return version_match.group(1)
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as exc:
logger.exception("Failed to connect to %s: %r", VERSION_URL, exc)
raise NetworkError(f"Failed to connect to {VERSION_URL}.") from exc
except requests.exceptions.HTTPError as exc:
logger.exception("HTTP error from %s: %r", VERSION_URL, exc)
raise NetworkError(f"HTTP error from {VERSION_URL}.") from exc
except (GetVersionError, requests.exceptions.JSONDecodeError) as exc:
amandahla marked this conversation as resolved.
Show resolved Hide resolved
logger.exception("Failed to get version: %r", exc)
raise GetVersionError(str(exc)) from exc
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