From 62fc30cac42e091b625a56cda8838bc2395351b4 Mon Sep 17 00:00:00 2001 From: Arif Ali Date: Fri, 22 Dec 2023 09:17:08 +0000 Subject: [PATCH] [pebble][init] Add pebble service manager Closes: #3207 Resolves: SET-86 Signed-off-by: Arif Ali --- sos/policies/init_systems/pebble.py | 53 +++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 sos/policies/init_systems/pebble.py diff --git a/sos/policies/init_systems/pebble.py b/sos/policies/init_systems/pebble.py new file mode 100644 index 0000000000..f1d04884c7 --- /dev/null +++ b/sos/policies/init_systems/pebble.py @@ -0,0 +1,53 @@ +# Copyright (C) 2024 Canonical Ltd., Arif Ali + +# This file is part of the sos project: https://github.com/sosreport/sos +# +# This copyrighted material is made available to anyone wishing to use, +# modify, copy, or redistribute it subject to the terms and conditions of +# version 2 of the GNU General Public License. +# +# See the LICENSE file in the source distribution for further information. + +from sos.policies.init_systems import InitSystem +from sos.utilities import shell_out + + +class PebbleInit(InitSystem): + """InitSystem abstraction for Pebble systems""" + + def __init__(self, chroot=None): + super(PebbleInit, self).__init__( + init_cmd='pebble', + list_cmd='services', + query_cmd=f'{init_cmd} {list_cmd}', + chroot=chroot + ) + self.load_all_services() + + def parse_query(self, output): + for line in output.splitlines()[1:]: + if line.split()[2] == 'active': + return 'active' + return 'unknown' + + def load_all_services(self): + svcs = shell_out(self.list_cmd, chroot=self.chroot).splitlines()[1:] + for line in svcs: + try: + name = line.split()[0] + config = line.split()[1] + self.services[name] = { + 'name': name, + 'config': config, + } + except IndexError: + pass + + def is_running(self, name, default=False): + try: + svc = self.get_service_status(name) + return svc['status'] == 'active' + except Exception: + return default + +# vim: set et ts=4 sw=4 :