Newly published systemd library for Machine Operators

Hi All,

I just published a systemd library. It is intended for use in machine charms that use the Operator Framework.

To use it, run the following from within the directory of a charm in which you wish to use the lib:

charmcraft fetch-lib charms.operator_libs_linux.v0.systemd

It then works like so:

from systemd import service_running, service_start, service_reload

# Start a service
if not service_running("mysql"):
    success = service_start("mysql")

# Attempt to reload a service's config, restarting if necessary
success = service_reload("nginx", restart_on_failure=True)

The library current supports the following interactions:

On the fly control of a service via service_start, service_stop, service_restart and service_reload.

Pause (stop and mask) a service until you explicitly resume it via service_pause and service_resume.

This is largely based on the legacy code in charmhelpers, so most of the library should be familiar to you if you have used that library in the past. Specifically, the new library retains the prior behavior of returning a boolean value for success or failure, rather than raising an exception. This makes it easy to run checks such as is_running or service_restart with minimal exception handling.

To file bugs, request enhancements, and submit patches (patches are always welcome!), go here: https://github.com/canonical/operator-libs-linux/, or ping me (@pengale) in this thread.

~ Penny

2 Likes

WOW!

Can you perhaps create a charm in this repo to help people get started with this awesome library ?

https://github.com/erik78se/juju-operators-examples

I can help you test and make it tidy. We can also showcase it in some community workshop in a week or two?

1 Like

Check this out @hallback @tmihoc @jamesbeedy @heitor

1 Like

Hey Erik!

Glad you like the library. We’ll have a bunch of charms that will use this soon, and I’m hoping that the integration tests and docstring up the top should be a good example, too :slight_smile:

1 Like

@jnsgruk sounds cool.

A friend of mine asked a valid question why one should use this lib, adding in more external sources as opposed to just

os.system('systemctl xxxxxx myservice')

What is the gain and loss of using the lib?

@erik-lonroth we’ve got a test charm for these things that lives at github.com/canonical/operator-libs-linux, but I can definitely look at sharing a test charm to the repo that you linked!

To answer your second question, os.system('some systemctl command') is just as valid a strategy as using the library. The library centralizes error handling strategies and best practices for doing things like stopping and masking a service, which might save you some boilerplate and some trips to the systemd docs. It’s there to use if you’d like, and there to ignore if you don’t :slight_smile:

1 Like

I can’t find a charm there or what am I missing?

Hrm. I may be mistaken. I thought that we had published a charm to hang the libs off of, but you are correct that its source cannot be found in the operator-libs-linux repo …

There was a test charm for a while, but then we moved to a more charm-independent test infra using LXD in this case. I’ll add a TODO to my list for next week to get the hello-juju charm updated to use the apt and systemd libraries, though :slight_smile: