From 4ca0421581c4c1087dd1f2cb9f483b270780d320 Mon Sep 17 00:00:00 2001 From: Fred LE MEUR Date: Tue, 19 Jan 2021 15:20:58 +0100 Subject: [PATCH] performance: Fix web client link to Cockpit Closes: #2022. - Differently URL based on version of Cockpit used. - Lazily evaluate when necessary. - Cache the result. [sunil: Use subprocess.run() instead of getoutput() for security] [sunil: Use long form arguments in command invocation for readability] [sunil: Perform isort] Signed-off-by: Sunil Mohan Adapa Reviewed-by: Sunil Mohan Adapa --- plinth/modules/performance/manifest.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/plinth/modules/performance/manifest.py b/plinth/modules/performance/manifest.py index 3101ea0f3..43ac3a92d 100644 --- a/plinth/modules/performance/manifest.py +++ b/plinth/modules/performance/manifest.py @@ -3,12 +3,36 @@ FreedomBox app for System Monitoring (cockpit-pcp) in ‘System’. """ +import subprocess +from functools import lru_cache + +from django.utils.functional import lazy from django.utils.translation import ugettext_lazy as _ +from plinth.utils import Version + + +@lru_cache +def _get_url(): + """Return the web client URL based on Cockpit version.""" + process = subprocess.run( + ['dpkg-query', '--showformat=${Version}', '--show', 'cockpit'], + stdout=subprocess.PIPE) + cockpit_version = process.stdout.decode() + if Version(cockpit_version) >= Version('235'): + url = '/_cockpit/metrics' + else: + url = '/_cockpit/system/graphs' + + return url + + +get_url = lazy(_get_url, str) + clients = [{ 'name': _('Cockpit'), 'platforms': [{ 'type': 'web', - 'url': '/_cockpit/system/graphs' + 'url': get_url() }] }]