summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDevaev Maxim <[email protected]>2018-11-18 08:11:18 +0300
committerDevaev Maxim <[email protected]>2018-11-18 08:11:18 +0300
commit9762c82d0a6c49f052e2d499a6e54585fde4b0a0 (patch)
treeb9a5e844e4656f77deefe5faa6c46ab5d6fde312
parent51dd9a7428fc0249dd605397ccc955a9fd1cd5cd (diff)
moved lua crutch to kvmd
-rw-r--r--Makefile9
-rw-r--r--PKGBUILD3
-rw-r--r--configs/kvmd/platforms/kvmd.v1-hdmi.yaml3
-rw-r--r--configs/kvmd/platforms/kvmd.v1-vga.yaml3
-rw-r--r--configs/nginx/apps/kvm/manifest.json8
-rw-r--r--configs/nginx/nginx.conf35
-rw-r--r--extras/.gitignore (renamed from configs/nginx/apps/.gitignore)0
-rw-r--r--extras/kvm/manifest.yaml6
-rw-r--r--kvmd/apps/kvmd/__init__.py10
-rw-r--r--kvmd/apps/kvmd/info.py51
-rw-r--r--kvmd/apps/kvmd/server.py13
-rw-r--r--requirements.txt1
-rw-r--r--testenv/Dockerfile5
-rw-r--r--testenv/kvmd.yaml1
-rw-r--r--testenv/requirements.txt1
15 files changed, 98 insertions, 51 deletions
diff --git a/Makefile b/Makefile
index 85b496c9..c9391736 100644
--- a/Makefile
+++ b/Makefile
@@ -4,10 +4,8 @@ TESTENV_VIDEO ?= /dev/video0
TESTENV_LOOP ?= /dev/loop7
TESTENV_CMD ?= /bin/bash -c " \
(socat PTY,link=$(TESTENV_HID) PTY,link=/dev/ttyS11 &) \
- && rm -rf /etc/nginx/* \
- && cp -r /usr/share/kvmd/configs/nginx/* /etc/nginx \
- && mkdir -p /etc/kvmd \
- && cp /usr/share/kvmd/configs/kvmd/*.yaml /etc/kvmd \
+ && cp -r /usr/share/kvmd/configs.default/nginx/* /etc/nginx \
+ && cp /usr/share/kvmd/configs.default/kvmd/*.yaml /etc/kvmd \
&& cp /testenv/kvmd.yaml /etc/kvmd \
&& nginx -c /etc/nginx/nginx.conf \
&& ln -s $(TESTENV_VIDEO) /dev/kvmd-streamer \
@@ -33,7 +31,8 @@ run: _testenv
- docker run --rm \
--volume `pwd`/kvmd:/kvmd:ro \
--volume `pwd`/web:/usr/share/kvmd/web:ro \
- --volume `pwd`/configs:/usr/share/kvmd/configs:ro \
+ --volume `pwd`/extras:/usr/share/kvmd/extras:ro \
+ --volume `pwd`/configs:/usr/share/kvmd/configs.default:ro \
--volume `pwd`/testenv:/testenv:ro \
--device $(TESTENV_LOOP):/dev/kvmd-msd \
--device $(TESTENV_VIDEO):$(TESTENV_VIDEO) \
diff --git a/PKGBUILD b/PKGBUILD
index 8c5bb904..a95857b8 100644
--- a/PKGBUILD
+++ b/PKGBUILD
@@ -42,6 +42,7 @@ package() {
mkdir -p "$pkgdir/usr/share/kvmd"
mkdir -p "$pkgdir/etc/kvmd"
cp -r web "$pkgdir/usr/share/kvmd"
- cp -r configs "$pkgdir/usr/share/kvmd"
+ cp -r extras "$pkgdir/usr/share/kvmd"
+ cp -r configs "$pkgdir/usr/share/kvmd/configs.default"
find "$pkgdir" -name ".gitignore" -delete
}
diff --git a/configs/kvmd/platforms/kvmd.v1-hdmi.yaml b/configs/kvmd/platforms/kvmd.v1-hdmi.yaml
index 46cbbd37..e62790d1 100644
--- a/configs/kvmd/platforms/kvmd.v1-hdmi.yaml
+++ b/configs/kvmd/platforms/kvmd.v1-hdmi.yaml
@@ -1,5 +1,5 @@
# Don't touch this file otherwise your device may stop working.
-# You can find a workable configuration in /usr/share/kvmd/configs/kvmd.
+# You can find a working configuration in /usr/share/kvmd/configs.default/kvmd.
kvmd:
server:
@@ -9,6 +9,7 @@ kvmd:
info:
meta: /etc/kvmd/meta.yaml
+ extras: /usr/share/kvmd/extras
hid:
pinout:
diff --git a/configs/kvmd/platforms/kvmd.v1-vga.yaml b/configs/kvmd/platforms/kvmd.v1-vga.yaml
index dd3ef4db..42baee7a 100644
--- a/configs/kvmd/platforms/kvmd.v1-vga.yaml
+++ b/configs/kvmd/platforms/kvmd.v1-vga.yaml
@@ -1,5 +1,5 @@
# Don't touch this file otherwise your device may stop working.
-# You can find a workable configuration in /usr/share/kvmd/configs/kvmd.
+# You can find a workable configuration in /usr/share/kvmd/configs.default/kvmd.
kvmd:
server:
@@ -9,6 +9,7 @@ kvmd:
info:
meta: /etc/kvmd/meta.yaml
+ extras: /usr/share/kvmd/extras
hid:
pinout:
diff --git a/configs/nginx/apps/kvm/manifest.json b/configs/nginx/apps/kvm/manifest.json
deleted file mode 100644
index ed19dc67..00000000
--- a/configs/nginx/apps/kvm/manifest.json
+++ /dev/null
@@ -1,8 +0,0 @@
-{
- "name": "KVM",
- "description": "Open KVM session in a web browser",
- "icon": "/svg/kvm.svg",
- "path": "/kvm.html",
- "place": 0,
- "keyboard_cap": true
-}
diff --git a/configs/nginx/nginx.conf b/configs/nginx/nginx.conf
index 22271d20..ae2703ec 100644
--- a/configs/nginx/nginx.conf
+++ b/configs/nginx/nginx.conf
@@ -6,7 +6,7 @@ worker_processes 4;
# error_log /tmp/nginx.error.log;
error_log stderr;
-include /etc/nginx/apps/*/main-ctx.conf;
+include /usr/share/kvmd/extras/*/nginx.main-ctx.conf;
events {
worker_connections 1024;
@@ -40,26 +40,12 @@ http {
server 127.0.0.1:8082 fail_timeout=0s max_fails=0;
}
- include /etc/nginx/apps/*/http-ctx.conf;
+ include /usr/share/kvmd/extras/*/nginx.http-ctx.conf;
- lua_shared_dict WS_TOKENS 10m;
- init_by_lua_block {
- WS_TOKEN_EXPIRES = 10;
-
- local cjson = require("cjson");
- local io = require("io");
-
- local apps = {};
- local apps_dir_path = "/etc/nginx/apps";
- for app in io.popen("ls " .. apps_dir_path):lines() do
- local manifest_file = assert(io.open(apps_dir_path .. "/" .. app .. "/manifest.json", "r"));
- local manifest = cjson.decode(manifest_file:read("*all"));
- manifest_file:close();
- apps[app] = manifest;
- end
-
- APPS_JSON = cjson.encode(apps);
- }
+#PROD lua_shared_dict WS_TOKENS 10m;
+#PROD init_by_lua_block {
+#PROD WS_TOKEN_EXPIRES = 10;
+#PROD }
#PROD server {
#PROD listen 80;
@@ -98,13 +84,6 @@ http {
}
}
- location /get_apps {
- default_type "application/json";
- content_by_lua_block {
- ngx.say(APPS_JSON);
- }
- }
-
location /kvmd/ws {
#PROD auth_basic off;
#PROD access_by_lua_block {
@@ -165,6 +144,6 @@ http {
proxy_ignore_headers X-Accel-Buffering;
}
- include /etc/nginx/apps/*/server-ctx.conf;
+ include /usr/share/kvmd/extras/*/nginx.server-ctx.conf;
}
}
diff --git a/configs/nginx/apps/.gitignore b/extras/.gitignore
index e69de29b..e69de29b 100644
--- a/configs/nginx/apps/.gitignore
+++ b/extras/.gitignore
diff --git a/extras/kvm/manifest.yaml b/extras/kvm/manifest.yaml
new file mode 100644
index 00000000..c51d26a0
--- /dev/null
+++ b/extras/kvm/manifest.yaml
@@ -0,0 +1,6 @@
+name: KVM
+description: Open KVM session in a web browser
+icon: /svg/kvm.svg
+keyboard_cap: true
+daemon: kvmd
+place: 0
diff --git a/kvmd/apps/kvmd/__init__.py b/kvmd/apps/kvmd/__init__.py
index 155b8141..519adf0b 100644
--- a/kvmd/apps/kvmd/__init__.py
+++ b/kvmd/apps/kvmd/__init__.py
@@ -8,6 +8,7 @@ from ...logging import Log
from ... import gpio
+from .info import InfoManager
from .hid import Hid
from .atx import Atx
from .msd import MassStorageDevice
@@ -24,6 +25,12 @@ def main() -> None:
log = Log(loop)
+ info_manager = InfoManager(
+ meta_path=str(config["info"]["meta"]),
+ extras_path=str(config["info"]["extras"]),
+ loop=loop,
+ )
+
hid = Hid(
reset=int(config["hid"]["pinout"]["reset"]),
device_path=str(config["hid"]["device"]),
@@ -77,12 +84,13 @@ def main() -> None:
Server(
log=log,
+ info_manager=info_manager,
+
hid=hid,
atx=atx,
msd=msd,
streamer=streamer,
- meta_path=str(config["info"]["meta"]),
heartbeat=float(config["server"]["heartbeat"]),
streamer_shutdown_delay=float(config["streamer"]["shutdown_delay"]),
msd_chunk_size=int(config["msd"]["chunk_size"]),
diff --git a/kvmd/apps/kvmd/info.py b/kvmd/apps/kvmd/info.py
new file mode 100644
index 00000000..57b63b0f
--- /dev/null
+++ b/kvmd/apps/kvmd/info.py
@@ -0,0 +1,51 @@
+import os
+import asyncio
+
+from typing import Dict
+
+import dbus # pylint: disable=import-error
+import dbus.exceptions # pylint: disable=import-error
+
+from ...yaml import load_yaml_file
+
+
+# =====
+class InfoManager:
+ def __init__(
+ self,
+ meta_path: str,
+ extras_path: str,
+ loop: asyncio.AbstractEventLoop,
+ ) -> None:
+
+ self.__meta_path = meta_path
+ self.__extras_path = extras_path
+
+ self.__loop = loop
+
+ async def get_meta(self) -> Dict:
+ return (await self.__loop.run_in_executor(None, load_yaml_file, self.__meta_path))
+
+ async def get_extras(self) -> Dict:
+ return (await self.__loop.run_in_executor(None, self.__sync_get_extras))
+
+ def __sync_get_extras(self) -> Dict:
+ try:
+ bus = dbus.SystemBus()
+
+ def is_enabled(daemon: str) -> bool:
+ obj = bus.get_object("org.freedesktop.systemd1", "/org/freedesktop/systemd1")
+ get_unit_state = obj.get_dbus_method("GetUnitFileState", "org.freedesktop.systemd1.Manager")
+ return (get_unit_state(daemon + ".service") in ["enabled", "enabled-runtime", "static", "indirect", "generated"])
+
+ except dbus.exceptions.DBusException:
+ is_enabled = (lambda daemon: True)
+
+ extras: Dict[str, Dict] = {}
+ for app in os.listdir(self.__extras_path):
+ if app[0] != "." and os.path.isdir(os.path.join(self.__extras_path, app)):
+ extras[app] = load_yaml_file(os.path.join(self.__extras_path, app, "manifest.yaml"))
+ daemon = extras[app].get("daemon", "")
+ if isinstance(daemon, str) and daemon.strip():
+ extras[app]["enabled"] = is_enabled(daemon.strip())
+ return extras
diff --git a/kvmd/apps/kvmd/server.py b/kvmd/apps/kvmd/server.py
index ae66cbc4..5a7e33fe 100644
--- a/kvmd/apps/kvmd/server.py
+++ b/kvmd/apps/kvmd/server.py
@@ -18,10 +18,10 @@ from ...logging import Log
from ...aioregion import RegionIsBusyError
-from ...yaml import load_yaml_file
-
from ... import __version__
+from .info import InfoManager
+
from .hid import Hid
from .atx import Atx
@@ -108,12 +108,13 @@ class Server: # pylint: disable=too-many-instance-attributes
def __init__( # pylint: disable=too-many-arguments
self,
log: Log,
+ info_manager: InfoManager,
+
hid: Hid,
atx: Atx,
msd: MassStorageDevice,
streamer: Streamer,
- meta_path: str,
heartbeat: float,
streamer_shutdown_delay: float,
msd_chunk_size: int,
@@ -122,12 +123,13 @@ class Server: # pylint: disable=too-many-instance-attributes
) -> None:
self.__log = log
+ self.__info_manager = info_manager
+
self.__hid = hid
self.__atx = atx
self.__msd = msd
self.__streamer = streamer
- self.__meta_path = meta_path
self.__heartbeat = heartbeat
self.__streamer_shutdown_delay = streamer_shutdown_delay
self.__msd_chunk_size = msd_chunk_size
@@ -190,7 +192,8 @@ class Server: # pylint: disable=too-many-instance-attributes
"streamer": await self.__streamer.get_version(),
},
"streamer": self.__streamer.get_app(),
- "meta": load_yaml_file(self.__meta_path),
+ "meta": await self.__info_manager.get_meta(),
+ "extras": await self.__info_manager.get_extras(),
})
@_wrap_exceptions_for_web("Log error")
diff --git a/requirements.txt b/requirements.txt
index 6ca74376..daf938ae 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -6,3 +6,4 @@ pyyaml
pyserial
setproctitle
systemd-python
+dbus-python
diff --git a/testenv/Dockerfile b/testenv/Dockerfile
index 59106908..78226b17 100644
--- a/testenv/Dockerfile
+++ b/testenv/Dockerfile
@@ -43,8 +43,8 @@ RUN pacman -Syy \
python \
python-pip \
python-systemd \
+ python-dbus \
nginx-mainline \
- lua51-cjson \
ustreamer \
socat \
htmlhint \
@@ -56,4 +56,7 @@ RUN pacman -Syy \
COPY testenv/requirements.txt requirements.txt
RUN pip install -r requirements.txt
+RUN rm -rf /etc/nginx/* \
+ && mkdir /etc/kvmd
+
CMD /bin/bash
diff --git a/testenv/kvmd.yaml b/testenv/kvmd.yaml
index 9f1f355b..68035bd2 100644
--- a/testenv/kvmd.yaml
+++ b/testenv/kvmd.yaml
@@ -6,6 +6,7 @@ kvmd:
info:
meta: /etc/kvmd/meta.yaml
+ extras: /usr/share/kvmd/extras
hid:
pinout:
diff --git a/testenv/requirements.txt b/testenv/requirements.txt
index 63aa4112..4f95d90a 100644
--- a/testenv/requirements.txt
+++ b/testenv/requirements.txt
@@ -6,4 +6,5 @@ pyyaml
pyserial
setproctitle
systemd-python
+dbus-python
tox