2020-02-21 17:55:41 +01:00
|
|
|
"""Init file for Supervisor RESTful API."""
|
2017-03-30 16:01:52 +02:00
|
|
|
import logging
|
2017-05-13 17:41:46 +02:00
|
|
|
from pathlib import Path
|
2019-03-27 17:20:05 +01:00
|
|
|
from typing import Optional
|
2017-03-30 16:01:52 +02:00
|
|
|
|
|
|
|
from aiohttp import web
|
|
|
|
|
2019-03-27 17:20:05 +01:00
|
|
|
from ..coresys import CoreSys, CoreSysAttributes
|
2017-04-12 22:59:40 +02:00
|
|
|
from .addons import APIAddons
|
2020-03-27 17:37:48 +01:00
|
|
|
from .audio import APIAudio
|
2018-10-12 12:21:48 +02:00
|
|
|
from .auth import APIAuth
|
2020-03-27 17:37:48 +01:00
|
|
|
from .cli import APICli
|
2018-02-08 17:19:47 +01:00
|
|
|
from .discovery import APIDiscovery
|
2019-08-13 14:20:42 +02:00
|
|
|
from .dns import APICoreDNS
|
2020-10-05 12:19:25 +02:00
|
|
|
from .docker import APIDocker
|
2018-04-12 22:15:08 +02:00
|
|
|
from .hardware import APIHardware
|
2019-03-27 17:20:05 +01:00
|
|
|
from .homeassistant import APIHomeAssistant
|
|
|
|
from .host import APIHost
|
2018-10-10 16:46:34 +02:00
|
|
|
from .info import APIInfo
|
2019-04-05 12:13:44 +02:00
|
|
|
from .ingress import APIIngress
|
2020-05-21 13:30:54 +02:00
|
|
|
from .multicast import APIMulticast
|
2020-08-24 16:58:02 +02:00
|
|
|
from .network import APINetwork
|
2020-09-11 16:05:57 +02:00
|
|
|
from .observer import APIObserver
|
2020-05-21 13:30:54 +02:00
|
|
|
from .os import APIOS
|
2017-12-26 00:51:07 +01:00
|
|
|
from .proxy import APIProxy
|
2020-10-13 12:54:17 +02:00
|
|
|
from .resolution import APIResoulution
|
2018-03-12 23:40:06 +01:00
|
|
|
from .security import SecurityMiddleware
|
2019-03-27 17:20:05 +01:00
|
|
|
from .services import APIServices
|
|
|
|
from .snapshots import APISnapshots
|
|
|
|
from .supervisor import APISupervisor
|
2017-03-30 16:01:52 +02:00
|
|
|
|
2019-08-22 12:48:02 +02:00
|
|
|
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
2017-03-30 16:01:52 +02:00
|
|
|
|
|
|
|
|
2020-01-09 10:16:20 +01:00
|
|
|
MAX_CLIENT_SIZE: int = 1024 ** 2 * 16
|
|
|
|
|
|
|
|
|
2018-01-02 21:21:29 +01:00
|
|
|
class RestAPI(CoreSysAttributes):
|
2020-02-21 17:55:41 +01:00
|
|
|
"""Handle RESTful API for Supervisor."""
|
2017-03-30 16:01:52 +02:00
|
|
|
|
2019-03-27 17:20:05 +01:00
|
|
|
def __init__(self, coresys: CoreSys):
|
2018-09-18 23:47:47 +02:00
|
|
|
"""Initialize Docker base wrapper."""
|
2019-03-27 17:20:05 +01:00
|
|
|
self.coresys: CoreSys = coresys
|
|
|
|
self.security: SecurityMiddleware = SecurityMiddleware(coresys)
|
|
|
|
self.webapp: web.Application = web.Application(
|
2020-01-09 10:16:20 +01:00
|
|
|
client_max_size=MAX_CLIENT_SIZE,
|
2020-10-12 15:56:29 +02:00
|
|
|
middlewares=[
|
|
|
|
self.security.system_validation,
|
|
|
|
self.security.token_validation,
|
|
|
|
],
|
2019-05-27 12:35:06 +02:00
|
|
|
)
|
2017-03-30 16:01:52 +02:00
|
|
|
|
2017-03-30 23:26:26 +02:00
|
|
|
# service stuff
|
2019-03-27 17:20:05 +01:00
|
|
|
self._runner: web.AppRunner = web.AppRunner(self.webapp)
|
|
|
|
self._site: Optional[web.TCPSite] = None
|
2017-03-30 16:01:52 +02:00
|
|
|
|
2019-03-27 17:20:05 +01:00
|
|
|
async def load(self) -> None:
|
2018-01-02 21:21:29 +01:00
|
|
|
"""Register REST API Calls."""
|
2020-10-06 11:26:56 +02:00
|
|
|
self._register_addons()
|
|
|
|
self._register_audio()
|
|
|
|
self._register_auth()
|
2020-03-27 17:37:48 +01:00
|
|
|
self._register_cli()
|
2020-10-06 11:26:56 +02:00
|
|
|
self._register_discovery()
|
|
|
|
self._register_dns()
|
|
|
|
self._register_docker()
|
2018-04-12 22:15:08 +02:00
|
|
|
self._register_hardware()
|
2018-01-02 21:21:29 +01:00
|
|
|
self._register_homeassistant()
|
2020-10-06 11:26:56 +02:00
|
|
|
self._register_host()
|
|
|
|
self._register_info()
|
2019-04-05 12:13:44 +02:00
|
|
|
self._register_ingress()
|
2020-10-06 11:26:56 +02:00
|
|
|
self._register_multicast()
|
|
|
|
self._register_network()
|
|
|
|
self._register_observer()
|
|
|
|
self._register_os()
|
|
|
|
self._register_panel()
|
|
|
|
self._register_proxy()
|
2020-10-13 12:54:17 +02:00
|
|
|
self._register_resolution()
|
2018-02-08 17:19:47 +01:00
|
|
|
self._register_services()
|
2020-10-06 11:26:56 +02:00
|
|
|
self._register_snapshots()
|
|
|
|
self._register_supervisor()
|
2018-01-02 21:21:29 +01:00
|
|
|
|
2019-03-27 17:20:05 +01:00
|
|
|
def _register_host(self) -> None:
|
2018-09-18 23:47:47 +02:00
|
|
|
"""Register hostcontrol functions."""
|
2018-01-02 21:21:29 +01:00
|
|
|
api_host = APIHost()
|
|
|
|
api_host.coresys = self.coresys
|
2017-03-30 16:01:52 +02:00
|
|
|
|
2019-05-27 12:35:06 +02:00
|
|
|
self.webapp.add_routes(
|
|
|
|
[
|
|
|
|
web.get("/host/info", api_host.info),
|
2020-03-28 15:56:57 +01:00
|
|
|
web.get("/host/logs", api_host.logs),
|
2019-05-27 12:35:06 +02:00
|
|
|
web.post("/host/reboot", api_host.reboot),
|
|
|
|
web.post("/host/shutdown", api_host.shutdown),
|
|
|
|
web.post("/host/reload", api_host.reload),
|
|
|
|
web.post("/host/options", api_host.options),
|
|
|
|
web.get("/host/services", api_host.services),
|
|
|
|
web.post("/host/services/{service}/stop", api_host.service_stop),
|
|
|
|
web.post("/host/services/{service}/start", api_host.service_start),
|
|
|
|
web.post("/host/services/{service}/restart", api_host.service_restart),
|
|
|
|
web.post("/host/services/{service}/reload", api_host.service_reload),
|
|
|
|
]
|
|
|
|
)
|
2017-04-05 08:55:19 +02:00
|
|
|
|
2020-08-24 16:58:02 +02:00
|
|
|
def _register_network(self) -> None:
|
|
|
|
"""Register network functions."""
|
|
|
|
api_network = APINetwork()
|
|
|
|
api_network.coresys = self.coresys
|
|
|
|
|
|
|
|
self.webapp.add_routes(
|
|
|
|
[
|
|
|
|
web.get("/network/info", api_network.info),
|
|
|
|
web.get(
|
|
|
|
"/network/interface/{interface}/info", api_network.interface_info
|
|
|
|
),
|
|
|
|
web.post(
|
|
|
|
"/network/interface/{interface}/update",
|
|
|
|
api_network.interface_update,
|
|
|
|
),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-03-27 17:37:48 +01:00
|
|
|
def _register_os(self) -> None:
|
|
|
|
"""Register OS functions."""
|
|
|
|
api_os = APIOS()
|
|
|
|
api_os.coresys = self.coresys
|
|
|
|
|
|
|
|
self.webapp.add_routes(
|
|
|
|
[
|
|
|
|
web.get("/os/info", api_os.info),
|
|
|
|
web.post("/os/update", api_os.update),
|
|
|
|
web.post("/os/config/sync", api_os.config_sync),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
def _register_cli(self) -> None:
|
|
|
|
"""Register HA cli functions."""
|
|
|
|
api_cli = APICli()
|
|
|
|
api_cli.coresys = self.coresys
|
2018-06-22 22:54:03 +02:00
|
|
|
|
2019-05-27 12:35:06 +02:00
|
|
|
self.webapp.add_routes(
|
|
|
|
[
|
2020-03-27 17:37:48 +01:00
|
|
|
web.get("/cli/info", api_cli.info),
|
|
|
|
web.get("/cli/stats", api_cli.stats),
|
|
|
|
web.post("/cli/update", api_cli.update),
|
2019-05-27 12:35:06 +02:00
|
|
|
]
|
|
|
|
)
|
2018-06-22 22:54:03 +02:00
|
|
|
|
2020-09-11 16:05:57 +02:00
|
|
|
def _register_observer(self) -> None:
|
|
|
|
"""Register Observer functions."""
|
|
|
|
api_observer = APIObserver()
|
|
|
|
api_observer.coresys = self.coresys
|
|
|
|
|
|
|
|
self.webapp.add_routes(
|
|
|
|
[
|
|
|
|
web.get("/observer/info", api_observer.info),
|
|
|
|
web.get("/observer/stats", api_observer.stats),
|
|
|
|
web.post("/observer/update", api_observer.update),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-04-05 23:26:22 +02:00
|
|
|
def _register_multicast(self) -> None:
|
|
|
|
"""Register Multicast functions."""
|
|
|
|
api_multicast = APIMulticast()
|
|
|
|
api_multicast.coresys = self.coresys
|
|
|
|
|
|
|
|
self.webapp.add_routes(
|
|
|
|
[
|
|
|
|
web.get("/multicast/info", api_multicast.info),
|
|
|
|
web.get("/multicast/stats", api_multicast.stats),
|
2020-04-08 14:08:09 +02:00
|
|
|
web.get("/multicast/logs", api_multicast.logs),
|
2020-04-05 23:26:22 +02:00
|
|
|
web.post("/multicast/update", api_multicast.update),
|
|
|
|
web.post("/multicast/restart", api_multicast.restart),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2019-03-27 17:20:05 +01:00
|
|
|
def _register_hardware(self) -> None:
|
2018-09-18 23:47:47 +02:00
|
|
|
"""Register hardware functions."""
|
2018-04-12 22:15:08 +02:00
|
|
|
api_hardware = APIHardware()
|
|
|
|
api_hardware.coresys = self.coresys
|
|
|
|
|
2019-05-27 12:35:06 +02:00
|
|
|
self.webapp.add_routes(
|
|
|
|
[
|
|
|
|
web.get("/hardware/info", api_hardware.info),
|
|
|
|
web.get("/hardware/audio", api_hardware.audio),
|
2019-09-02 13:28:49 +02:00
|
|
|
web.post("/hardware/trigger", api_hardware.trigger),
|
2019-05-27 12:35:06 +02:00
|
|
|
]
|
|
|
|
)
|
2018-04-12 22:15:08 +02:00
|
|
|
|
2019-03-27 17:20:05 +01:00
|
|
|
def _register_info(self) -> None:
|
2018-10-10 16:46:34 +02:00
|
|
|
"""Register info functions."""
|
|
|
|
api_info = APIInfo()
|
|
|
|
api_info.coresys = self.coresys
|
2018-09-26 11:39:45 +02:00
|
|
|
|
2019-05-27 12:35:06 +02:00
|
|
|
self.webapp.add_routes([web.get("/info", api_info.info)])
|
2018-09-26 11:39:45 +02:00
|
|
|
|
2020-10-13 12:54:17 +02:00
|
|
|
def _register_resolution(self) -> None:
|
|
|
|
"""Register info functions."""
|
|
|
|
api_resolution = APIResoulution()
|
|
|
|
api_resolution.coresys = self.coresys
|
|
|
|
|
2020-10-14 17:14:25 +02:00
|
|
|
self.webapp.add_routes(
|
|
|
|
[
|
|
|
|
web.get("/resolution", api_resolution.base),
|
|
|
|
web.post("/resolution/{suggestion}", api_resolution.apply_suggestion),
|
|
|
|
web.post(
|
|
|
|
"/resolution/{suggestion}/dismiss",
|
|
|
|
api_resolution.dismiss_suggestion,
|
|
|
|
),
|
|
|
|
]
|
|
|
|
)
|
2020-10-13 12:54:17 +02:00
|
|
|
|
2019-03-27 17:20:05 +01:00
|
|
|
def _register_auth(self) -> None:
|
2018-10-12 12:21:48 +02:00
|
|
|
"""Register auth functions."""
|
|
|
|
api_auth = APIAuth()
|
|
|
|
api_auth.coresys = self.coresys
|
|
|
|
|
2020-01-15 18:16:19 +01:00
|
|
|
self.webapp.add_routes(
|
|
|
|
[web.post("/auth", api_auth.auth), web.post("/auth/reset", api_auth.reset)]
|
|
|
|
)
|
2018-10-12 12:21:48 +02:00
|
|
|
|
2019-03-27 17:20:05 +01:00
|
|
|
def _register_supervisor(self) -> None:
|
2018-09-18 23:47:47 +02:00
|
|
|
"""Register Supervisor functions."""
|
2018-01-02 21:21:29 +01:00
|
|
|
api_supervisor = APISupervisor()
|
|
|
|
api_supervisor.coresys = self.coresys
|
2017-03-30 16:54:13 +02:00
|
|
|
|
2019-05-27 12:35:06 +02:00
|
|
|
self.webapp.add_routes(
|
|
|
|
[
|
|
|
|
web.get("/supervisor/ping", api_supervisor.ping),
|
|
|
|
web.get("/supervisor/info", api_supervisor.info),
|
|
|
|
web.get("/supervisor/stats", api_supervisor.stats),
|
|
|
|
web.get("/supervisor/logs", api_supervisor.logs),
|
|
|
|
web.post("/supervisor/update", api_supervisor.update),
|
|
|
|
web.post("/supervisor/reload", api_supervisor.reload),
|
|
|
|
web.post("/supervisor/options", api_supervisor.options),
|
2019-08-07 17:26:32 +02:00
|
|
|
web.post("/supervisor/repair", api_supervisor.repair),
|
2019-05-27 12:35:06 +02:00
|
|
|
]
|
|
|
|
)
|
2017-03-30 16:54:13 +02:00
|
|
|
|
2019-03-27 17:20:05 +01:00
|
|
|
def _register_homeassistant(self) -> None:
|
2018-09-18 23:47:47 +02:00
|
|
|
"""Register Home Assistant functions."""
|
2018-01-02 21:21:29 +01:00
|
|
|
api_hass = APIHomeAssistant()
|
|
|
|
api_hass.coresys = self.coresys
|
2017-03-30 16:01:52 +02:00
|
|
|
|
2019-05-27 12:35:06 +02:00
|
|
|
self.webapp.add_routes(
|
|
|
|
[
|
2020-02-05 10:57:57 +01:00
|
|
|
web.get("/core/info", api_hass.info),
|
|
|
|
web.get("/core/logs", api_hass.logs),
|
|
|
|
web.get("/core/stats", api_hass.stats),
|
|
|
|
web.post("/core/options", api_hass.options),
|
|
|
|
web.post("/core/update", api_hass.update),
|
|
|
|
web.post("/core/restart", api_hass.restart),
|
|
|
|
web.post("/core/stop", api_hass.stop),
|
|
|
|
web.post("/core/start", api_hass.start),
|
|
|
|
web.post("/core/check", api_hass.check),
|
|
|
|
web.post("/core/rebuild", api_hass.rebuild),
|
2020-02-21 17:55:41 +01:00
|
|
|
# Remove with old Supervisor fallback
|
2019-05-27 12:35:06 +02:00
|
|
|
web.get("/homeassistant/info", api_hass.info),
|
|
|
|
web.get("/homeassistant/logs", api_hass.logs),
|
|
|
|
web.get("/homeassistant/stats", api_hass.stats),
|
|
|
|
web.post("/homeassistant/options", api_hass.options),
|
|
|
|
web.post("/homeassistant/update", api_hass.update),
|
|
|
|
web.post("/homeassistant/restart", api_hass.restart),
|
|
|
|
web.post("/homeassistant/stop", api_hass.stop),
|
|
|
|
web.post("/homeassistant/start", api_hass.start),
|
|
|
|
web.post("/homeassistant/check", api_hass.check),
|
|
|
|
web.post("/homeassistant/rebuild", api_hass.rebuild),
|
|
|
|
]
|
|
|
|
)
|
2017-12-26 00:51:07 +01:00
|
|
|
|
2019-03-27 17:20:05 +01:00
|
|
|
def _register_proxy(self) -> None:
|
2018-09-18 23:47:47 +02:00
|
|
|
"""Register Home Assistant API Proxy."""
|
2018-01-02 21:21:29 +01:00
|
|
|
api_proxy = APIProxy()
|
|
|
|
api_proxy.coresys = self.coresys
|
2017-12-26 00:51:07 +01:00
|
|
|
|
2019-05-27 12:35:06 +02:00
|
|
|
self.webapp.add_routes(
|
|
|
|
[
|
2020-02-05 10:57:57 +01:00
|
|
|
web.get("/core/api/websocket", api_proxy.websocket),
|
|
|
|
web.get("/core/websocket", api_proxy.websocket),
|
|
|
|
web.get("/core/api/stream", api_proxy.stream),
|
|
|
|
web.post("/core/api/{path:.+}", api_proxy.api),
|
|
|
|
web.get("/core/api/{path:.+}", api_proxy.api),
|
|
|
|
web.get("/core/api/", api_proxy.api),
|
2020-02-21 17:55:41 +01:00
|
|
|
# Remove with old Supervisor fallback
|
2019-05-27 12:35:06 +02:00
|
|
|
web.get("/homeassistant/api/websocket", api_proxy.websocket),
|
|
|
|
web.get("/homeassistant/websocket", api_proxy.websocket),
|
|
|
|
web.get("/homeassistant/api/stream", api_proxy.stream),
|
|
|
|
web.post("/homeassistant/api/{path:.+}", api_proxy.api),
|
|
|
|
web.get("/homeassistant/api/{path:.+}", api_proxy.api),
|
|
|
|
web.get("/homeassistant/api/", api_proxy.api),
|
|
|
|
]
|
|
|
|
)
|
2017-03-30 16:01:52 +02:00
|
|
|
|
2019-03-27 17:20:05 +01:00
|
|
|
def _register_addons(self) -> None:
|
2018-09-18 23:47:47 +02:00
|
|
|
"""Register Add-on functions."""
|
2018-01-02 21:21:29 +01:00
|
|
|
api_addons = APIAddons()
|
|
|
|
api_addons.coresys = self.coresys
|
2017-04-12 00:06:17 +02:00
|
|
|
|
2019-05-27 12:35:06 +02:00
|
|
|
self.webapp.add_routes(
|
|
|
|
[
|
|
|
|
web.get("/addons", api_addons.list),
|
|
|
|
web.post("/addons/reload", api_addons.reload),
|
|
|
|
web.get("/addons/{addon}/info", api_addons.info),
|
|
|
|
web.post("/addons/{addon}/install", api_addons.install),
|
|
|
|
web.post("/addons/{addon}/uninstall", api_addons.uninstall),
|
|
|
|
web.post("/addons/{addon}/start", api_addons.start),
|
|
|
|
web.post("/addons/{addon}/stop", api_addons.stop),
|
|
|
|
web.post("/addons/{addon}/restart", api_addons.restart),
|
|
|
|
web.post("/addons/{addon}/update", api_addons.update),
|
|
|
|
web.post("/addons/{addon}/options", api_addons.options),
|
2020-08-30 17:58:13 +02:00
|
|
|
web.post(
|
|
|
|
"/addons/{addon}/options/validate", api_addons.options_validate
|
|
|
|
),
|
2019-05-27 12:35:06 +02:00
|
|
|
web.post("/addons/{addon}/rebuild", api_addons.rebuild),
|
|
|
|
web.get("/addons/{addon}/logs", api_addons.logs),
|
|
|
|
web.get("/addons/{addon}/icon", api_addons.icon),
|
|
|
|
web.get("/addons/{addon}/logo", api_addons.logo),
|
|
|
|
web.get("/addons/{addon}/changelog", api_addons.changelog),
|
2020-01-20 10:01:22 +01:00
|
|
|
web.get("/addons/{addon}/documentation", api_addons.documentation),
|
2019-05-27 12:35:06 +02:00
|
|
|
web.post("/addons/{addon}/stdin", api_addons.stdin),
|
|
|
|
web.post("/addons/{addon}/security", api_addons.security),
|
|
|
|
web.get("/addons/{addon}/stats", api_addons.stats),
|
|
|
|
]
|
|
|
|
)
|
2017-04-12 00:06:17 +02:00
|
|
|
|
2019-04-05 12:13:44 +02:00
|
|
|
def _register_ingress(self) -> None:
|
|
|
|
"""Register Ingress functions."""
|
|
|
|
api_ingress = APIIngress()
|
|
|
|
api_ingress.coresys = self.coresys
|
|
|
|
|
2019-05-27 12:35:06 +02:00
|
|
|
self.webapp.add_routes(
|
|
|
|
[
|
|
|
|
web.post("/ingress/session", api_ingress.create_session),
|
|
|
|
web.get("/ingress/panels", api_ingress.panels),
|
|
|
|
web.view("/ingress/{token}/{path:.*}", api_ingress.handler),
|
|
|
|
]
|
|
|
|
)
|
2019-04-05 12:13:44 +02:00
|
|
|
|
2019-03-27 17:20:05 +01:00
|
|
|
def _register_snapshots(self) -> None:
|
2018-09-18 23:47:47 +02:00
|
|
|
"""Register snapshots functions."""
|
2018-01-02 21:21:29 +01:00
|
|
|
api_snapshots = APISnapshots()
|
|
|
|
api_snapshots.coresys = self.coresys
|
2017-07-05 18:16:14 +02:00
|
|
|
|
2019-05-27 12:35:06 +02:00
|
|
|
self.webapp.add_routes(
|
|
|
|
[
|
|
|
|
web.get("/snapshots", api_snapshots.list),
|
|
|
|
web.post("/snapshots/reload", api_snapshots.reload),
|
|
|
|
web.post("/snapshots/new/full", api_snapshots.snapshot_full),
|
|
|
|
web.post("/snapshots/new/partial", api_snapshots.snapshot_partial),
|
|
|
|
web.post("/snapshots/new/upload", api_snapshots.upload),
|
|
|
|
web.get("/snapshots/{snapshot}/info", api_snapshots.info),
|
2020-10-10 15:08:13 +02:00
|
|
|
web.delete("/snapshots/{snapshot}", api_snapshots.remove),
|
2019-05-27 12:35:06 +02:00
|
|
|
web.post(
|
|
|
|
"/snapshots/{snapshot}/restore/full", api_snapshots.restore_full
|
|
|
|
),
|
|
|
|
web.post(
|
|
|
|
"/snapshots/{snapshot}/restore/partial",
|
|
|
|
api_snapshots.restore_partial,
|
|
|
|
),
|
|
|
|
web.get("/snapshots/{snapshot}/download", api_snapshots.download),
|
2020-10-10 15:08:13 +02:00
|
|
|
# Old, remove at end of 2020
|
|
|
|
web.post("/snapshots/{snapshot}/remove", api_snapshots.remove),
|
2019-05-27 12:35:06 +02:00
|
|
|
]
|
|
|
|
)
|
2017-07-05 18:16:14 +02:00
|
|
|
|
2019-03-27 17:20:05 +01:00
|
|
|
def _register_services(self) -> None:
|
2018-09-18 23:47:47 +02:00
|
|
|
"""Register services functions."""
|
2018-02-08 17:19:47 +01:00
|
|
|
api_services = APIServices()
|
|
|
|
api_services.coresys = self.coresys
|
|
|
|
|
2019-05-27 12:35:06 +02:00
|
|
|
self.webapp.add_routes(
|
|
|
|
[
|
|
|
|
web.get("/services", api_services.list),
|
|
|
|
web.get("/services/{service}", api_services.get_service),
|
|
|
|
web.post("/services/{service}", api_services.set_service),
|
|
|
|
web.delete("/services/{service}", api_services.del_service),
|
|
|
|
]
|
|
|
|
)
|
2018-02-08 17:19:47 +01:00
|
|
|
|
2019-03-27 17:20:05 +01:00
|
|
|
def _register_discovery(self) -> None:
|
2018-09-18 23:47:47 +02:00
|
|
|
"""Register discovery functions."""
|
2018-02-08 17:19:47 +01:00
|
|
|
api_discovery = APIDiscovery()
|
|
|
|
api_discovery.coresys = self.coresys
|
|
|
|
|
2019-05-27 12:35:06 +02:00
|
|
|
self.webapp.add_routes(
|
|
|
|
[
|
|
|
|
web.get("/discovery", api_discovery.list),
|
|
|
|
web.get("/discovery/{uuid}", api_discovery.get_discovery),
|
|
|
|
web.delete("/discovery/{uuid}", api_discovery.del_discovery),
|
|
|
|
web.post("/discovery", api_discovery.set_discovery),
|
2019-08-13 14:20:42 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
def _register_dns(self) -> None:
|
|
|
|
"""Register DNS functions."""
|
|
|
|
api_dns = APICoreDNS()
|
|
|
|
api_dns.coresys = self.coresys
|
|
|
|
|
|
|
|
self.webapp.add_routes(
|
|
|
|
[
|
|
|
|
web.get("/dns/info", api_dns.info),
|
|
|
|
web.get("/dns/stats", api_dns.stats),
|
|
|
|
web.get("/dns/logs", api_dns.logs),
|
|
|
|
web.post("/dns/update", api_dns.update),
|
|
|
|
web.post("/dns/options", api_dns.options),
|
2019-08-18 11:46:23 +02:00
|
|
|
web.post("/dns/restart", api_dns.restart),
|
2020-01-09 22:27:39 +01:00
|
|
|
web.post("/dns/reset", api_dns.reset),
|
2020-02-25 18:37:06 +01:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
def _register_audio(self) -> None:
|
|
|
|
"""Register Audio functions."""
|
|
|
|
api_audio = APIAudio()
|
|
|
|
api_audio.coresys = self.coresys
|
|
|
|
|
|
|
|
self.webapp.add_routes(
|
|
|
|
[
|
|
|
|
web.get("/audio/info", api_audio.info),
|
|
|
|
web.get("/audio/stats", api_audio.stats),
|
|
|
|
web.get("/audio/logs", api_audio.logs),
|
|
|
|
web.post("/audio/update", api_audio.update),
|
|
|
|
web.post("/audio/restart", api_audio.restart),
|
2020-02-26 11:48:11 +01:00
|
|
|
web.post("/audio/reload", api_audio.reload),
|
2020-02-27 16:25:04 +01:00
|
|
|
web.post("/audio/profile", api_audio.set_profile),
|
2020-02-28 17:52:12 +01:00
|
|
|
web.post("/audio/volume/{source}/application", api_audio.set_volume),
|
2020-02-26 11:48:11 +01:00
|
|
|
web.post("/audio/volume/{source}", api_audio.set_volume),
|
2020-02-28 17:52:12 +01:00
|
|
|
web.post("/audio/mute/{source}/application", api_audio.set_mute),
|
|
|
|
web.post("/audio/mute/{source}", api_audio.set_mute),
|
2020-02-26 11:48:11 +01:00
|
|
|
web.post("/audio/default/{source}", api_audio.set_default),
|
2019-05-27 12:35:06 +02:00
|
|
|
]
|
|
|
|
)
|
2018-02-08 17:19:47 +01:00
|
|
|
|
2019-03-27 17:20:05 +01:00
|
|
|
def _register_panel(self) -> None:
|
2018-09-18 23:47:47 +02:00
|
|
|
"""Register panel for Home Assistant."""
|
2018-05-28 23:16:03 +02:00
|
|
|
panel_dir = Path(__file__).parent.joinpath("panel")
|
2019-05-27 12:35:06 +02:00
|
|
|
self.webapp.add_routes([web.static("/app", panel_dir)])
|
2017-05-13 17:41:46 +02:00
|
|
|
|
2020-10-05 12:19:25 +02:00
|
|
|
def _register_docker(self) -> None:
|
|
|
|
"""Register docker configuration functions."""
|
|
|
|
api_docker = APIDocker()
|
|
|
|
api_docker.coresys = self.coresys
|
|
|
|
|
|
|
|
self.webapp.add_routes(
|
|
|
|
[
|
2020-10-06 11:26:56 +02:00
|
|
|
web.get("/docker/info", api_docker.info),
|
2020-10-05 12:19:25 +02:00
|
|
|
web.get("/docker/registries", api_docker.registries),
|
|
|
|
web.post("/docker/registries", api_docker.create_registry),
|
2020-10-06 16:02:22 +02:00
|
|
|
web.delete("/docker/registries/{hostname}", api_docker.remove_registry),
|
2020-10-05 12:19:25 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2019-03-27 17:20:05 +01:00
|
|
|
async def start(self) -> None:
|
2018-09-18 23:47:47 +02:00
|
|
|
"""Run RESTful API webserver."""
|
2018-06-20 12:17:33 +02:00
|
|
|
await self._runner.setup()
|
2018-07-25 01:46:54 +02:00
|
|
|
self._site = web.TCPSite(
|
2019-05-27 12:35:06 +02:00
|
|
|
self._runner, host="0.0.0.0", port=80, shutdown_timeout=5
|
|
|
|
)
|
2017-03-30 16:01:52 +02:00
|
|
|
|
|
|
|
try:
|
2018-06-20 12:17:33 +02:00
|
|
|
await self._site.start()
|
2017-03-30 16:01:52 +02:00
|
|
|
except OSError as err:
|
2020-05-21 12:00:52 +02:00
|
|
|
_LOGGER.critical("Failed to create HTTP server at 0.0.0.0:80 -> %s", err)
|
2018-07-25 01:46:54 +02:00
|
|
|
else:
|
|
|
|
_LOGGER.info("Start API on %s", self.sys_docker.network.supervisor)
|
2017-03-30 16:01:52 +02:00
|
|
|
|
2019-03-27 17:20:05 +01:00
|
|
|
async def stop(self) -> None:
|
2018-09-18 23:47:47 +02:00
|
|
|
"""Stop RESTful API webserver."""
|
2018-06-30 22:06:18 +02:00
|
|
|
if not self._site:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Shutdown running API
|
2018-06-20 12:17:33 +02:00
|
|
|
await self._site.stop()
|
|
|
|
await self._runner.cleanup()
|
2018-07-25 01:46:54 +02:00
|
|
|
|
|
|
|
_LOGGER.info("Stop API on %s", self.sys_docker.network.supervisor)
|