From 75d992b0411002dd97c8dba3fd54a56d1dfa4768 Mon Sep 17 00:00:00 2001 From: Ben Grande Date: Thu, 13 Jun 2024 13:29:30 +0200 Subject: [PATCH] fix: use Admin API for fast queries --- salt/sys-cacher/files/admin/list-extra-tag.sh | 66 ------------------- salt/sys-cacher/files/admin/list_extra_tag.py | 31 +++++++++ salt/sys-cacher/files/admin/tag.py | 30 +++++++++ salt/sys-cacher/files/admin/tag.sh | 31 --------- salt/sys-cacher/list-extra-tag.sls | 2 +- salt/sys-cacher/tag.sls | 2 +- 6 files changed, 63 insertions(+), 99 deletions(-) delete mode 100755 salt/sys-cacher/files/admin/list-extra-tag.sh create mode 100755 salt/sys-cacher/files/admin/list_extra_tag.py create mode 100755 salt/sys-cacher/files/admin/tag.py delete mode 100755 salt/sys-cacher/files/admin/tag.sh diff --git a/salt/sys-cacher/files/admin/list-extra-tag.sh b/salt/sys-cacher/files/admin/list-extra-tag.sh deleted file mode 100755 index 0482eaf..0000000 --- a/salt/sys-cacher/files/admin/list-extra-tag.sh +++ /dev/null @@ -1,66 +0,0 @@ -#!/bin/sh - -# SPDX-FileCopyrightText: 2023 - 2024 Benjamin Grande M. S. -# -# SPDX-License-Identifier: AGPL-3.0-or-later - -set -eu - -get_os_distro(){ - distro_qube="${1}" - os_distro="$(qvm-features "${distro_qube}" os-distribution || true)" -} - -tagged="$(qvm-ls --no-spinner --raw-list --tags updatevm-sys-cacher | tr "\n" " ")" - -wanted="" -for qube in ${tagged}; do - get_os_distro "${qube}" - case "${os_distro}" in - debian|kali|kicksecure|parrot| \ - ubuntu|linuxmint| \ - arch|blackarch| \ - fedora) - continue - ;; - "") - ## AppVMs and DispVMs do not report the features, discover from - ## their templates. - klass="$(qvm-prefs "${qube}" klass)" - case "${klass}" in - TemplateVM|StandaloneVM) - ## WARN: creates false positives in case qube never did an update to - ## report the OS ID, thus reporting both supported qubes that are - ## not updated yet and unsupported that didn't update yet also. - wanted="${wanted:+"${wanted} "}${qube}" - ;; - AppVM|DispVM) - case "${klass}" in - AppVM) - template="$(qvm-prefs "${qube}" template)" - ;; - DispVM) - app="$(qvm-prefs "${qube}" template)" - template="$(qvm-prefs "${app}" template)" - ;; - esac - get_os_distro "${template}" - case "${os_distro}" in - debian|kali|kicksecure|parrot| \ - ubuntu|linuxmint| \ - arch|blackarch| \ - fedora) - continue - ;; - ## Qube is not supported. - *) wanted="${wanted:+"${wanted} "}${qube}";; - esac - ;; - esac - ;; - ## Qube is not supported. - *) wanted="${wanted:+"${wanted} "}${qube}";; - esac -done - -echo "${wanted}" | tr " " "\n" diff --git a/salt/sys-cacher/files/admin/list_extra_tag.py b/salt/sys-cacher/files/admin/list_extra_tag.py new file mode 100755 index 0000000..72693ff --- /dev/null +++ b/salt/sys-cacher/files/admin/list_extra_tag.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +# SPDX-FileCopyrightText: 2024 Benjamin Grande M. S. +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +"""list-extra-tag - List qubes tagged for cacher incorrectly""" + +import qubesadmin +import qubesadmin.vm + +def main(): + """main""" + wanted_domains = ['debian', 'fedora', 'arch'] + ## TODO: remove after https://github.com/QubesOS/qubes-core-agent-linux/pull/504 + wanted_domains_extra = wanted_domains + ['kali', 'kicksecure', 'parrot', + 'ubuntu', 'linuxmint', 'blackarch'] + domains = [ + vm.name + for vm in qubesadmin.Qubes().domains + if "updatevm-sys-cacher" in vm.tags + and vm.features.check_with_template("os-distribution-like") + not in wanted_domains + and vm.features.check_with_template("os-distribution") + not in wanted_domains_extra + ] + print("\n".join(domains)) + + +if __name__ == "__main__": + main() diff --git a/salt/sys-cacher/files/admin/tag.py b/salt/sys-cacher/files/admin/tag.py new file mode 100755 index 0000000..d234eb0 --- /dev/null +++ b/salt/sys-cacher/files/admin/tag.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +# SPDX-FileCopyrightText: 2024 Benjamin Grande M. S. +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +"""list-extra-tag - List qubes that can be tagged for cacher""" + +import qubesadmin +import qubesadmin.vm + +def main(): + """main""" + wanted_domains = ['debian', 'fedora', 'arch'] + ## TODO: remove after https://github.com/QubesOS/qubes-core-agent-linux/pull/504 + wanted_domains_extra = wanted_domains + ['kali', 'kicksecure', 'parrot', + 'ubuntu', 'linuxmint', 'blackarch'] + domains = [ + vm.name + for vm in qubesadmin.Qubes().domains + if vm.klass == "TemplateVM" + and "whonix-updatevm" not in vm.tags + and (vm.features.get("os-distribution-like") in wanted_domains + or vm.features.get("os-distribution") in wanted_domains_extra) + ] + print("\n".join(domains)) + + +if __name__ == "__main__": + main() diff --git a/salt/sys-cacher/files/admin/tag.sh b/salt/sys-cacher/files/admin/tag.sh deleted file mode 100755 index 175ec16..0000000 --- a/salt/sys-cacher/files/admin/tag.sh +++ /dev/null @@ -1,31 +0,0 @@ -#!/bin/sh - -# SPDX-FileCopyrightText: 2023 - 2024 Benjamin Grande M. S. -# -# SPDX-License-Identifier: AGPL-3.0-or-later - -set -eu - -exclude="$(qvm-ls --no-spinner --raw-list --tags whonix-updatevm \ - | sed "s/^./--exclude &/" | tr "\n" " ")" - -# shellcheck disable=SC2086 -templates="$(qvm-ls --no-spinner --raw-data --fields=NAME,CLASS --all ${exclude} \ - | awk -v class="TemplateVM" -F "|" '$2 ~ class {print $1}' \ - | tr "\n" " ")" - -wanted="" -for qube in ${templates}; do - os_distro="$(qvm-features "${qube}" os-distribution || true)" - case "${os_distro}" in - debian|kali|kicksecure|parrot| \ - ubuntu|linuxmint| \ - arch|blackarch| \ - fedora) - wanted="${wanted:+"${wanted} "}${qube}" - ;; - *) continue - esac -done - -echo "${wanted}" | tr " " "\n" diff --git a/salt/sys-cacher/list-extra-tag.sls b/salt/sys-cacher/list-extra-tag.sls index eb7fbb1..f4190e7 100644 --- a/salt/sys-cacher/list-extra-tag.sls +++ b/salt/sys-cacher/list-extra-tag.sls @@ -4,7 +4,7 @@ SPDX-FileCopyrightText: 2023 - 2024 Benjamin Grande M. S.