mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
56 lines
2.2 KiB
Python
56 lines
2.2 KiB
Python
#!/usr/bin/env python
|
|
|
|
import os
|
|
import requests
|
|
|
|
from yunohost.diagnosis import Diagnoser
|
|
from yunohost.utils.error import YunohostError
|
|
from yunohost.service import _get_services
|
|
|
|
class PortsDiagnoser(Diagnoser):
|
|
|
|
id_ = os.path.splitext(os.path.basename(__file__))[0].split("-")[1]
|
|
cache_duration = 3600
|
|
dependencies = ["ip"]
|
|
|
|
def run(self):
|
|
|
|
# This dict is something like :
|
|
# { 80: "nginx",
|
|
# 25: "postfix",
|
|
# 443: "nginx"
|
|
# ... }
|
|
ports = {}
|
|
for service, infos in _get_services().items():
|
|
for port in infos.get("needs_exposed_ports", []):
|
|
ports[port] = service
|
|
|
|
try:
|
|
r = requests.post('https://diagnosis.yunohost.org/check-ports', json={'ports': ports.keys()}, timeout=30).json()
|
|
if "status" not in r.keys():
|
|
raise Exception("Bad syntax for response ? Raw json: %s" % str(r))
|
|
elif r["status"] == "error":
|
|
if "content" in r.keys():
|
|
raise Exception(r["content"])
|
|
else:
|
|
raise Exception("Bad syntax for response ? Raw json: %s" % str(r))
|
|
elif r["status"] != "ok" or "ports" not in r.keys() or not isinstance(r["ports"], dict):
|
|
raise Exception("Bad syntax for response ? Raw json: %s" % str(r))
|
|
except Exception as e:
|
|
raise YunohostError("diagnosis_ports_could_not_diagnose", error=e)
|
|
|
|
for port, service in ports.items():
|
|
if r["ports"].get(str(port), None) is not True:
|
|
yield dict(meta={"port": port, "needed_by": service},
|
|
status="ERROR",
|
|
summary=("diagnosis_ports_unreachable", {"port": port}),
|
|
details=[("diagnosis_ports_needed_by", (service,)), ("diagnosis_ports_forwarding_tip", ())])
|
|
else:
|
|
yield dict(meta={"port": port, "needed_by": service},
|
|
status="SUCCESS",
|
|
summary=("diagnosis_ports_ok", {"port": port}),
|
|
details=[("diagnosis_ports_needed_by", (service))])
|
|
|
|
|
|
def main(args, env, loggers):
|
|
return PortsDiagnoser(args, env, loggers).diagnose()
|