From b1e487a7a819d92cbbe9233caa6d38cad23e5272 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sun, 23 Jan 2022 16:22:50 +0300 Subject: app.py: fix loginctl output parsing --- app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app.py') diff --git a/app.py b/app.py index 1ae9760..36a705e 100755 --- a/app.py +++ b/app.py @@ -394,7 +394,7 @@ def systemd_users(): for line in lines: # This assumes user names cannot contain spaces. # loginctl list-users output must be in the UID NAME format. - info = line.split(' ', 2) + info = line.lstrip().split(' ', 2) if len(info) < 2: raise RuntimeError(f'invalid `loginctl list-users` output:\n{output}') uid, user = info[0], info[1] -- cgit v1.2.3 From d4ccff8141cc4325d5ffc8890a7be96c0a7f12e7 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sun, 23 Jan 2022 17:13:49 +0300 Subject: dump exceptions to browser's console --- app.py | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) (limited to 'app.py') diff --git a/app.py b/app.py index 36a705e..3638151 100755 --- a/app.py +++ b/app.py @@ -70,18 +70,23 @@ def hostname(): class Response: - def __init__(self, data): - self.data = data + DEFAULT_STATUS = http.server.HTTPStatus.OK + + @staticmethod + def body_from_json(body): + return json.dumps(body, ensure_ascii=False, indent=4) + + def __init__(self, body, status=None): + if status is None: + status = Response.DEFAULT_STATUS + self.status = status + self.body = body def headers(self): yield 'Content-Type', 'text/html; charset=utf-8' - @staticmethod - def dump_json(data): - return json.dumps(data, ensure_ascii=False, indent=4) - - def body(self): - return self.dump_json(self.data) + def encode_body(self): + return self.body.encode(errors='replace') def write_as_cgi_script(self): self.write_headers_as_cgi_script() @@ -93,22 +98,22 @@ class Response: print() def write_body_as_cgi_script(self): - if self.data is not None: - print(self.body()) + if self.body is not None: + print(self.body) - def write_as_request_handler(self, handler): - handler.send_response(http.server.HTTPStatus.OK) - self.write_headers_as_request_handler(handler) - self.write_body_as_request_handler(handler) + def write_to_request_handler(self, handler): + handler.send_response(self.status) + self.write_headers_to_request_handler(handler) + self.write_body_to_request_handler(handler) - def write_headers_as_request_handler(self, handler): + def write_headers_to_request_handler(self, handler): for name, val in self.headers(): handler.send_header(name, val) handler.end_headers() - def write_body_as_request_handler(self, handler): - if self.data is not None: - handler.wfile.write(self.body().encode(errors='replace')) + def write_body_to_request_handler(self, handler): + if self.body is not None: + handler.wfile.write(self.encode_body()) def run_do(*args, **kwargs): @@ -195,7 +200,7 @@ class Loginctl(Systemd): class Task(abc.ABC): def complete(self): self.run() - return Response(self.result()) + return Response(Response.body_from_json(self.result())) @abc.abstractmethod def run(self): -- cgit v1.2.3 From 93e644a245f8d5f91332164846559de032faed21 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sun, 23 Jan 2022 17:31:59 +0300 Subject: app.py: include command's output in the exception --- app.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'app.py') diff --git a/app.py b/app.py index 3638151..1ed34a0 100755 --- a/app.py +++ b/app.py @@ -118,6 +118,11 @@ class Response: def run_do(*args, **kwargs): output = subprocess.run(args, stdin=DEVNULL, stdout=PIPE, stderr=STDOUT, universal_newlines=True, **kwargs) + # Include the output in the exception's message: + try: + output.check_returncode() + except Exception as e: + raise RuntimeError("Command's output was this:\n" + output.stdout) from e return output.stdout -- cgit v1.2.3 From c1720c98a468c5856c29d06c2260d896b53aa160 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Tue, 25 Jan 2022 02:29:31 +0300 Subject: app.py: don't query user info if no users --- app.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'app.py') diff --git a/app.py b/app.py index 1ed34a0..67cdd6d 100755 --- a/app.py +++ b/app.py @@ -411,9 +411,11 @@ def systemd_users(): yield User(uid, user) def show_users(users): + user_args = [user.name for user in users] + if not user_args: + return None properties = 'UID', 'Name', 'RuntimePath' prop_args = (arg for prop in properties for arg in ('-p', prop)) - user_args = (user.name for user in users) output = Loginctl('show-user', *prop_args, '--value', *user_args).run().result() lines = output.splitlines() # Assuming that for muptiple users, the properties will be separated by -- cgit v1.2.3 From 9b8f2fb8de9bd2bf1b13a84ab06e394aa9d7cb02 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Tue, 25 Jan 2022 02:30:02 +0300 Subject: app.py: don't do systemctl --user if no user instance --- app.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'app.py') diff --git a/app.py b/app.py index 67cdd6d..dd3edfc 100755 --- a/app.py +++ b/app.py @@ -311,7 +311,10 @@ class UserInstanceStatusTaskList(Task): self.tasks = {user.name: UserInstanceStatusTask.su(user) for user in systemd_users()} else: # As a regular user, we can only query ourselves. - self.tasks = {get_current_user().name: UserInstanceStatusTask()} + self.tasks = {} + user = get_current_user() + if user_instance_active(user): + self.tasks[user.name] = UserInstanceStatusTask() def run(self): for task in self.tasks.values(): @@ -363,6 +366,19 @@ def get_current_user(): return User(entry.pw_uid, entry.pw_name) +def user_instance_active(user): + # I'm pretty sure this is the way to determine if the user instance is + # running? + # Source: https://www.freedesktop.org/software/systemd/man/user@.service.html + unit_name = f'user@{user.uid}.service' + cmd = Systemctl.system('is-active', unit_name, '--quiet') + try: + cmd.run().result() + return True + except Exception: + return False + + # A pitiful attempt to find a list of possibly-systemd-enabled users follows # (i.e. users that might be running a per-user systemd instance). # I don't know of a better way than probing /run/user/UID. -- cgit v1.2.3