Example #1
0
 def get_description(id_):
     key = "diagnosis_description_" + id_
     # If no description available, fallback to id
     return m18n.n(key) if m18n.key_exists(key) else id_
Example #2
0
def _get_and_format_service_status(service, infos):

    systemd_service = infos.get("actual_systemd_service", service)
    raw_status, raw_service = _get_service_information_from_systemd(systemd_service)

    if raw_status is None:
        logger.error(
            "Failed to get status information via dbus for service %s, systemctl didn't recognize this service ('NoSuchUnit')."
            % systemd_service
        )
        return {
            "status": "unknown",
            "start_on_boot": "unknown",
            "last_state_change": "unknown",
            "description": "Error: failed to get information for this service, it doesn't exists for systemd",
            "configuration": "unknown",
        }

    # Try to get description directly from services.yml
    description = infos.get("description")

    # If no description was there, try to get it from the .json locales
    if not description:

        translation_key = "service_description_%s" % service
        if m18n.key_exists(translation_key):
            description = m18n.n(translation_key)
        else:
            description = str(raw_status.get("Description", ""))

    output = {
        "status": str(raw_status.get("SubState", "unknown")),
        "start_on_boot": str(raw_status.get("UnitFileState", "unknown")),
        "last_state_change": "unknown",
        "description": description,
        "configuration": "unknown",
    }

    # Fun stuff™ : to obtain the enabled/disabled status for sysv services,
    # gotta do this ... cf code of /lib/systemd/systemd-sysv-install
    if output["start_on_boot"] == "generated":
        output["start_on_boot"] = (
            "enabled" if glob("/etc/rc[S5].d/S??" + service) else "disabled"
        )
    elif os.path.exists(
        "/etc/systemd/system/multi-user.target.wants/%s.service" % service
    ):
        output["start_on_boot"] = "enabled"

    if "StateChangeTimestamp" in raw_status:
        output["last_state_change"] = datetime.utcfromtimestamp(
            raw_status["StateChangeTimestamp"] / 1000000
        )

    # 'test_status' is an optional field to test the status of the service using a custom command
    if "test_status" in infos:
        p = subprocess.Popen(
            infos["test_status"],
            shell=True,
            executable="/bin/bash",
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
        )

        p.communicate()

        output["status"] = "running" if p.returncode == 0 else "failed"
    elif (
        raw_service.get("Type", "").lower() == "oneshot"
        and output["status"] == "exited"
    ):
        # These are services like yunohost-firewall, hotspot, vpnclient,
        # ... they will be "exited" why doesn't provide any info about
        # the real state of the service (unless they did provide a
        # test_status, c.f. previous condition)
        output["status"] = "unknown"

    # 'test_status' is an optional field to test the status of the service using a custom command
    if "test_conf" in infos:
        p = subprocess.Popen(
            infos["test_conf"],
            shell=True,
            executable="/bin/bash",
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
        )

        out, _ = p.communicate()
        if p.returncode == 0:
            output["configuration"] = "valid"
        else:
            out = out.decode()
            output["configuration"] = "broken"
            output["configuration-details"] = out.strip().split("\n")

    return output