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

feat: enable juju-systemd-notices to observe snap services #128

Merged
merged 15 commits into from
Jul 21, 2024

Conversation

NucciTheBoss
Copy link
Contributor

@NucciTheBoss NucciTheBoss commented Jul 16, 2024

Fixes #126

This pull request enables support for observing snap services using the juju-systemd-notices charm library.

The key change in this pull request is modifying how the notices daemon configures how it observes services on the machine. Rather than configure which services to observe by reading the hooks directory, the daemon instead reads a watch.yaml file in the charm directory. Here's what the watch.yaml file would look like in a deployed charm using the notices daemon:

---
snap.slurm.slurmd: slurmd
snap.slurm.munged: munged

Originally, juju-systemd-notices used a regex that parsed hook names to determine which services to observe with the notices daemon. This approach worked until we needed to start working with nested services - e.g. have . in the service name like snap.slurm.slurmd - so we had to find a new way to configure how to observe services, but also ensure that the correct hook would be fired by the charm. We decided to use a key value approach where the key is the service name to observe over DBus, and the value is the valid event name alias used to trigger the start/stop events.

Now, when the notices daemon captures the event, it uses the configured alias to determine which hook to fire, but can still watch DBus using the correct service name.

Misc.

  1. I tried using systemd unit aliases, but that didn't work. DBus will "follow the symlink" to the original service name - e.g. snap.slurm.slurmd - and not the unit alias slurmd.
  2. We had to update tox.ini because ruff now requires the subcommand check to perform linting actions.
  3. We likely need to bump the LIBAPI version since the SystemdNotices init constructor now takes a variable amount of Service objects rather than a List[str]. We used a Service dataclass since it made it easy to provide service name aliases and set a default alias if none was provided by the charm author.

Original `ruff $@` is no longer supported by latest versions of ruff

Signed-off-by: Jason C. Nucciarone <[email protected]>
Adds the `watch.yaml` file for configuring how the notices
daemon observes services. The daemon will get the service
alias from this file and use the alias to determine the
proper hook to fire when the observed service is started or stopped.

BREAKING CHANGES: __init__ for SystemdNotices now takes the Service
data class instead of a generic list. The Service dataclass was added
so that it would be easier to specify an alias for an observed service.

Signed-off-by: Jason C. Nucciarone <[email protected]>
* Consolidates metadata.yaml and actions.yaml into charmcraft.yaml
  for test charm. Less files to worry about.

Signed-off-by: Jason C. Nucciarone <[email protected]>
Signed-off-by: Jason C. Nucciarone <[email protected]>
Signed-off-by: Jason C. Nucciarone <[email protected]>
Signed-off-by: Jason C. Nucciarone <[email protected]>
`yaml.dump()` returns a string if steam is none, so it
is easier to just pass the content off to `write_text` instead
of opening the file with a context manager.

Signed-off-by: Jason C. Nucciarone <[email protected]>
Copy link
Collaborator

@benhoyt benhoyt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @NucciTheBoss -- nice work on this. A couple of comments about backwards compatibility, and a suggestion to simplify by using CLI arguments instead of watch.yaml.

While you're here, would you also have a few spare cycles to look at the test_snap_set_and_get_with_typed integration test failure? (In a separate PR.) If you can, that would be amazing.

lib/charms/operator_libs_linux/v0/juju_systemd_notices.py Outdated Show resolved Hide resolved
lib/charms/operator_libs_linux/v0/juju_systemd_notices.py Outdated Show resolved Hide resolved
lib/charms/operator_libs_linux/v0/juju_systemd_notices.py Outdated Show resolved Hide resolved
lib/charms/operator_libs_linux/v0/juju_systemd_notices.py Outdated Show resolved Hide resolved
lib/charms/operator_libs_linux/v0/juju_systemd_notices.py Outdated Show resolved Hide resolved
lib/charms/operator_libs_linux/v0/juju_systemd_notices.py Outdated Show resolved Hide resolved
- Switch to passing the service names and aliases
  via argparse rather than using the pyyaml external
  dependency to create the watch.yaml file.
- Use global map to track the observed service rather than
  caching the output of a watch.yaml read.

Signed-off-by: Jason C. Nucciarone <[email protected]>
- Reverted SystemdNotices in test charm back to the
  original constructor API.

Signed-off-by: Jason C. Nucciarone <[email protected]>
- Fixes unit tests failing on Python 3.10 due to incorrect patching.

Signed-off-by: Jason C. Nucciarone <[email protected]>
@NucciTheBoss NucciTheBoss changed the title feat!: enable juju-systemd-notices to observe snap services feat: enable juju-systemd-notices to observe snap services Jul 18, 2024
@NucciTheBoss
Copy link
Contributor Author

@benhoyt thank you for the feedback! I have applied the latest updates to this branch. The pull request is ready for review again 😃

Re. #123 - seems like an intermittent issue. I could potentially look at it if I have some extra time this pulse ⌚⌛

Copy link
Collaborator

@benhoyt benhoyt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! I do think it's more straight-forward without the watch.yaml file. Just one more iteration: 1) required: increment LIBPATCH, and 2) optional: can we remove the globals?

@NucciTheBoss
Copy link
Contributor Author

NucciTheBoss commented Jul 19, 2024

Addressed latest feedback comments and bumped LIBPATCH to 2. I think these updates look pretty good. I decided to keep the global variables for now, but we can refactor in the future if the lib becomes too complex and the global values are frequently updated by multiple methods.

Let me know if you have any more questions or comments!

Copy link
Collaborator

@benhoyt benhoyt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks for the tweaks and LIBPATCH bump. Good with me!

@benhoyt benhoyt merged commit 9c7f617 into canonical:main Jul 21, 2024
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

juju-systemd-notices does not support nested service names like snap.slurm.slurmd
2 participants