[PATCH v3] tbot_contrib/utils.py: collect some usefull linux utils

Harald Seiler hws at denx.de
Wed Apr 22 09:07:29 CEST 2020


Hello Heiko,

On Wed, 2020-04-22 at 07:01 +0200, Heiko Schocher wrote:
> collect usefull linux utils functions. Start with
> 
> ensure_sd_unit(lnx: linux.LinuxShell, services: typing.List[str])
> 
> check if all systemd services in list services run on linux
> machine lnx. If not, try to start them.
> 
> Signed-off-by: Heiko Schocher <hs at denx.de>
> ---
> 
> Changes in v3:
> - fix commit message
>   rename check_systemd_services_running() to ensure_sd_unit()
> 
> Changes in v2:
> - added comments from Harald
>   - rename check_systemd_services_running() to ensure_sd_unit()
>   - fix Copyright
>   - correct type-annotation for services
>   - use lnx.test()
>   - use "is-active" instead "status" for getting state of service
> - added a services cache, so we only check once per linux
>   machine, if systemd services run.

The cache is a great idea, thanks for adding that!  On that note, I've
been playing with the idea of adding a new decorator for this kind of
purpose.  It seems to happen quite often that some action needs to happen
once for each machine it is called on.

>  tbot_contrib/utils.py | 42 ++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 42 insertions(+)
>  create mode 100644 tbot_contrib/utils.py
> 
> diff --git a/tbot_contrib/utils.py b/tbot_contrib/utils.py
> new file mode 100644
> index 0000000..ee91d00
> --- /dev/null
> +++ b/tbot_contrib/utils.py
> @@ -0,0 +1,42 @@
> +# tbot, Embedded Automation Tool
> +# Copyright (C) 2020  Heiko Schocher
> +#
> +# This program is free software: you can redistribute it and/or modify
> +# it under the terms of the GNU General Public License as published by
> +# the Free Software Foundation, either version 3 of the License, or
> +# (at your option) any later version.
> +#
> +# This program is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with this program.  If not, see <https://www.gnu.org/licenses/>;;.
> +
> +from tbot.machine import linux
> +import typing
> +
> +
> +_SERVICES_CACHE: typing.Dict[str, typing.Dict[str, bool]] = {}
> +
> +
> +def ensure_sd_unit(lnx: linux.LinuxShell, services: typing.List[str]) -> None:
> +    """
> +    check if all systemd services in list services run on linux machine lnx.
> +    If not, try to start them.
> +
> +    :param lnx: linux shell
> +    :param services: list of systemd services
> +    """
> +    if lnx.name not in _SERVICES_CACHE:
> +        _SERVICES_CACHE[lnx.name] = {}

Small nitpick:  Using the name as the key for your cache is not ideal
because there is no guarantee that we won't see multiple machines with the
same name.  Instead, you can use the machine itself as the key like I have
done in check_for_tool [1].  This is possible because machines implement
the hash() protocol (and cloned machines all yield the same hash).

> +    for s in services:
> +        if s in _SERVICES_CACHE[lnx.name]:
> +            continue
> +
> +        if not lnx.test("systemctl", "is-active", s, "--no-pager"):

You don't need `--no-pager` for is-active.

> +            lnx.exec0("sudo", "systemctl", "start", s)
> +
> +        _SERVICES_CACHE[lnx.name][s] = True

[1]: https://github.com/Rahix/tbot/blob/master/tbot/tc/shell.py#L173
-- 
Harald



More information about the tbot mailing list