diff options
author | Jonathan Weth <git@jonathanweth.de> | 2021-06-28 11:01:45 +0200 |
---|---|---|
committer | Jonathan Weth <git@jonathanweth.de> | 2021-06-28 11:01:45 +0200 |
commit | 9ec08cbc84b01ce82f58892556637d8934ede0a5 (patch) | |
tree | e0b3642801305d60f45359b5c9364f5a5318ba18 | |
parent | 3526def9614ac5275f176a42938d11736233f108 (diff) | |
download | RWA.Support.SessionService-9ec08cbc84b01ce82f58892556637d8934ede0a5.tar.gz RWA.Support.SessionService-9ec08cbc84b01ce82f58892556637d8934ede0a5.tar.bz2 RWA.Support.SessionService-9ec08cbc84b01ce82f58892556637d8934ede0a5.zip |
Support multiple hosts
-rw-r--r-- | poetry.lock | 19 | ||||
-rw-r--r-- | pyproject.toml | 1 | ||||
-rw-r--r-- | rwa/support/sessionservice/config.py | 5 | ||||
-rwxr-xr-x | rwa/support/sessionservice/service.py | 74 | ||||
-rw-r--r-- | rwa/support/sessionservice/session.py | 28 | ||||
-rwxr-xr-x | test_client.py | 6 |
6 files changed, 112 insertions, 21 deletions
diff --git a/poetry.lock b/poetry.lock index 4b90dfe..c501ed0 100644 --- a/poetry.lock +++ b/poetry.lock @@ -46,7 +46,7 @@ sphinxcontrib-django = ">=0.5.0,<0.6.0" name = "appdirs" version = "1.4.4" description = "A small Python module for determining appropriate platform-specific dirs, e.g. a \"user data dir\"." -category = "dev" +category = "main" optional = false python-versions = "*" @@ -1214,6 +1214,17 @@ secure = ["pyOpenSSL (>=0.14)", "cryptography (>=1.3.4)", "idna (>=2.0.0)", "cer socks = ["PySocks (>=1.5.6,!=1.5.7,<2.0)"] [[package]] +name = "usersettings" +version = "1.1.5" +description = "Portable Local Settings Storage" +category = "main" +optional = false +python-versions = "*" + +[package.dependencies] +appdirs = ">=1.2" + +[[package]] name = "websockify" version = "0.9.0" description = "Websockify." @@ -1251,7 +1262,7 @@ testing = ["pytest (>=4.6)", "pytest-checkdocs (>=1.2.3)", "pytest-flake8", "pyt [metadata] lock-version = "1.1" python-versions = "^3.7" -content-hash = "b9992b58f505b297a099abda504337cc6a7dae5f0291e63c9ae8da725fda63f3" +content-hash = "4477f63dce4f7591677d1eaa2bc01265e56d3aa06811958fdaaab7b1f93c920e" [metadata.files] alabaster = [ @@ -1926,6 +1937,10 @@ urllib3 = [ {file = "urllib3-1.26.5-py2.py3-none-any.whl", hash = "sha256:753a0374df26658f99d826cfe40394a686d05985786d946fbe4165b5148f5a7c"}, {file = "urllib3-1.26.5.tar.gz", hash = "sha256:a7acd0977125325f516bda9735fa7142b909a8d01e8b2e4c8108d0984e6e0098"}, ] +usersettings = [ + {file = "usersettings-1.1.5-py3-none-any.whl", hash = "sha256:503be61d30d0cfd98f16bb6c9be86c29e78dc476c794ffae6333ab26f38994bf"}, + {file = "usersettings-1.1.5.tar.gz", hash = "sha256:9f84b282982622d8ebfd2d42b482317ae50fb2b3a7fba22e0b0c36cac61ad673"}, +] websockify = [ {file = "websockify-0.9.0.tar.gz", hash = "sha256:c35b5b79ebc517d3b784dacfb993be413a93cda5222c6f382443ce29c1a6cada"}, ] diff --git a/pyproject.toml b/pyproject.toml index 2582378..1ffeb05 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,6 +24,7 @@ psutil = "^5.7.2" flask = "^1.1.2" argparse = "^1.0.10" dynaconf = "^3.0.0" +usersettings = "^1.1.5" [tool.poetry.dev-dependencies] aleksis-builddeps = "*" diff --git a/rwa/support/sessionservice/config.py b/rwa/support/sessionservice/config.py index f0f8369..4f39a4e 100644 --- a/rwa/support/sessionservice/config.py +++ b/rwa/support/sessionservice/config.py @@ -23,8 +23,13 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <https://www.gnu.org/licenses/>. +import usersettings from dynaconf import Dynaconf settings = Dynaconf( envvar_prefix="RWA", settings_files=["/etc/rwa/support/sessionservice/settings.toml"] ) + +user_settings = usersettings.Settings("org.ArcticaProject.RWASupportSessionService") +user_settings.add_setting("web_app_hosts", list, ["http://127.0.0.1:8000"]) +user_settings.load_settings() diff --git a/rwa/support/sessionservice/service.py b/rwa/support/sessionservice/service.py index c50da26..4798df6 100755 --- a/rwa/support/sessionservice/service.py +++ b/rwa/support/sessionservice/service.py @@ -38,6 +38,7 @@ import dbus.mainloop.glib import dbus.service from gi.repository import GLib +from .config import user_settings from .lock import is_locked, lock, unlock from .session import Session from .trigger import TriggerServerThread @@ -76,10 +77,73 @@ class RWASupportSessionService(dbus.service.Object): logging.info("D-Bus service has been started.") + def _get_web_app_hosts(self) -> str: + """Get all registered RWA.Support.WebApp hosts. + + Helper function: No D-Bus API. + """ + hosts = user_settings.web_app_hosts + return json.dumps(hosts) + @dbus.service.method("org.ArcticaProject.RWASupportSessionService", out_signature="s") - def start(self) -> str: + def get_web_app_hosts(self) -> str: + """Get all registered RWA.Support.WebApp hosts. + + :return: All registered hosts as JSON array (D-Bus string) + + **Structure of returned JSON:** + + :: + + ["https://example.org", "http://127.0.0.1:8000"] + """ + return self._get_web_app_hosts() + + @dbus.service.method( + "org.ArcticaProject.RWASupportSessionService", in_signature="s", out_signature="s" + ) + def add_web_app_host(self, host: str) -> str: + """Add a RWA.Support.WebApp host. + + :param host: Exact hostname of the RWA.Support.WebApp host (D-Bus string) + :return: All registered hosts as JSON array (D-Bus string) + + **Structure of returned JSON:** + + :: + + ["https://example.org", "http://127.0.0.1:8000"] + """ + user_settings.web_app_hosts.append(host) + user_settings.save_settings() + return self._get_web_app_hosts() + + @dbus.service.method( + "org.ArcticaProject.RWASupportSessionService", in_signature="i", out_signature="s" + ) + def remove_web_app_host(self, idx: int) -> str: + """Remove a RWA.Support.WebApp host. + + :param idx: Index of web app host (D-Bus integer) + :return: All registered hosts as JSON array (D-Bus string) + + **Structure of returned JSON:** + + :: + + ["https://example.org", "http://127.0.0.1:8000"] + """ + del user_settings.web_app_hosts[idx] + user_settings.save_settings() + return self._get_web_app_hosts() + + @dbus.service.method( + "org.ArcticaProject.RWASupportSessionService", in_signature="i", out_signature="s" + ) + def start(self, host_idx: int) -> str: """Start a new remote session and register it in RWA.Support.WebApp. + :param host_idx: Index of web app host (D-Bus integer) :return: Result as JSON (D-Bus string) **Structure of returned JSON (success):** @@ -103,10 +167,16 @@ class RWASupportSessionService(dbus.service.Object): "session, so this session won't be started." ) return json.dumps({"status": "error", "type": "multiple"}) + print(host_idx, user_settings.web_app_hosts) + + try: + host = user_settings.web_app_hosts[host_idx] + except IndexError: + return json.dumps({"status": "error", "type": "host_not_found"}) # Start session try: - session = Session(self.trigger_service.port, self.mockup_mode) + session = Session(host, self.trigger_service.port, self.mockup_mode) # Add session to sessions list self.sessions[session.pid] = session diff --git a/rwa/support/sessionservice/session.py b/rwa/support/sessionservice/session.py index e603568..81584ea 100644 --- a/rwa/support/sessionservice/session.py +++ b/rwa/support/sessionservice/session.py @@ -35,20 +35,10 @@ import port_for import psutil import requests -from .config import settings from .lock import TEMP_DIR_PATH from .log import logging from .vnc import run_vnc, save_password -API_SERVER = settings.get("api_url", "http://127.0.0.1") -BASE_URL = API_SERVER + "/app/rwasupport/api/" -REGISTER_URL = BASE_URL + "register/" -STOP_URL = BASE_URL + "stop/" -STATUS_URL = BASE_URL + "status/" -MARK_JOB_AS_DONE_URL = BASE_URL + "jobs/mark_as_done/" - -logging.info(f"Load API config: {API_SERVER}") - def random_digits(length: int): return "".join(secrets.choice(string.digits) for _ in range(length)) @@ -67,7 +57,15 @@ class Session: #: Remote has joined the session STATUS_JOINED = "active" - def __init__(self, trigger_port: int, mockup_session: bool = False): + def __init__(self, host: str, trigger_port: int, mockup_session: bool = False): + self.host = host + self.BASE_URL = self.host + "/app/rwasupport/api/" + self.REGISTER_URL = self.BASE_URL + "register/" + self.STOP_URL = self.BASE_URL + "stop/" + self.STATUS_URL = self.BASE_URL + "status/" + self.MARK_JOB_AS_DONE_URL = self.BASE_URL + "jobs/mark_as_done/" + logging.info(f"Load API config: {self.host}") + self.trigger_token = secrets.token_urlsafe(20) self.trigger_port = trigger_port self.done_jobs = [] @@ -136,7 +134,7 @@ class Session: if not self.mockup_session: try: r = requests.post( - REGISTER_URL, + self.REGISTER_URL, json={ "port": self.ws_port, "pid": self.vnc_pid, @@ -185,7 +183,7 @@ class Session: if not self.mockup_session: try: r = requests.get( - STATUS_URL, params={"id": self.session_id}, headers=self._api_headers + self.STATUS_URL, params={"id": self.session_id}, headers=self._api_headers ) logging.info( @@ -222,7 +220,7 @@ class Session: self.done_jobs.append(job["job_id"]) try: r = requests.post( - MARK_JOB_AS_DONE_URL, params={"id": job["job_id"]}, headers=self._api_headers, + self.MARK_JOB_AS_DONE_URL, params={"id": job["job_id"]}, headers=self._api_headers, ) logging.info( f"The session has marked the job {job} as done in RWA.Support.WebApp " @@ -266,7 +264,7 @@ class Session: if not triggered: try: r = requests.post( - STOP_URL, params={"id": self.session_id}, headers=self._api_headers + self.STOP_URL, params={"id": self.session_id}, headers=self._api_headers ) logging.info( "The stop action has been registered in RWA.Support.WebApp " diff --git a/test_client.py b/test_client.py index 850e8d3..0e78784 100755 --- a/test_client.py +++ b/test_client.py @@ -29,7 +29,9 @@ import dbus bus = dbus.SessionBus() -time = bus.get_object("org.ArcticaProject.RWASupportSessionService", "/RWASupportSessionService") +api = bus.get_object("org.ArcticaProject.RWASupportSessionService", "/RWASupportSessionService") -curr = time.start() +hosts = api.get_web_app_hosts() +print(hosts) +curr = api.start(0) print("Your VNC session is", curr) |