From 25912f57872c50febb46baa7d50fdcd2005cb02f Mon Sep 17 00:00:00 2001 From: Wojtek Porczyk Date: Thu, 1 Dec 2016 16:55:31 +0100 Subject: [PATCH] qubes/tools: add qvm-tags QubesOS/qubes-issues#865 --- doc/conf.py | 2 + doc/manpages/qvm-tags.rst | 58 +++++++++++++++++++++++ qubes/tools/qvm_tags.py | 98 +++++++++++++++++++++++++++++++++++++++ rpm_spec/core-dom0.spec | 1 + 4 files changed, 159 insertions(+) create mode 100644 doc/manpages/qvm-tags.rst create mode 100644 qubes/tools/qvm_tags.py diff --git a/doc/conf.py b/doc/conf.py index a526ff58..63bed890 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -285,6 +285,8 @@ man_pages = [ u'Gracefully shut down a VM', _man_pages_author, 1), ('manpages/qvm-start', 'qvm-start', u'Start a specified VM', _man_pages_author, 1), + ('manpages/qvm-tags', 'qvm-tags', + u'Manage tags on a qube', _man_pages_author, 1), ('manpages/qvm-template-commit', 'qvm-template-commit', u'Commit changes to a template', _man_pages_author, 1), diff --git a/doc/manpages/qvm-tags.rst b/doc/manpages/qvm-tags.rst new file mode 100644 index 00000000..3f23e2ee --- /dev/null +++ b/doc/manpages/qvm-tags.rst @@ -0,0 +1,58 @@ +.. program:: qvm-tags + +:program:`qvm-tags` -- manage domain's tags +=========================================== + +.. warning:: + + This page was autogenerated from command-line parser. It shouldn't be 1:1 + conversion, because it would add little value. Please revise it and add + more descriptive help, which normally won't fit in standard ``--help`` + option. + + After rewrite, please remove this admonition. + +Synopsis +-------- + +:command:`qvm-tags` [-h] [--verbose] [--quiet] [--query | --set | --unset] *VMNAME* [*TAG*] + +Options +------- + +.. option:: --help, -h + + Show the help message and exit. + +.. option:: --verbose, -v + + Increase verbosity. + +.. option:: --quiet, -q + + Decrease verbosity. + +.. option:: --query + + Query for the tag. Exit with zero (true) if the qube in question has the tag + and with non-zero (false) if it does not. If no tag specified, list all the + tags. + + This is the default mode. + +.. option:: --set, -s + + Set the tag. The tag argument is mandatory. If tag is already set, do + nothing. + +.. option:: --delete, --unset, -D + + Unset the tag. The tag argument is mandatory. If tag is not set, do nothing. + +Authors +------- + +| Joanna Rutkowska +| Wojtek Porczyk + +.. vim: ts=3 sw=3 et tw=80 diff --git a/qubes/tools/qvm_tags.py b/qubes/tools/qvm_tags.py new file mode 100644 index 00000000..267b6361 --- /dev/null +++ b/qubes/tools/qvm_tags.py @@ -0,0 +1,98 @@ +#!/usr/bin/python2 -O +# vim: fileencoding=utf-8 + +# +# The Qubes OS Project, https://www.qubes-os.org/ +# +# Copyright (C) 2010-2016 Joanna Rutkowska +# Copyright (C) 2016 Wojtek Porczyk +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# + +'''qvm-features - Manage domain's tags''' + +from __future__ import print_function + +import sys + +import qubes + + + +parser = qubes.tools.QubesArgumentParser( + vmname_nargs=1, + description='manage domain\'s tags') + + +mode = parser.add_mutually_exclusive_group() + +def mode_query(args): + if args.tag is None: + # list + print('\n'.join(sorted(args.vm.tags))) + else: + # real query; logic is inverted, because this is exit code + return int(args.tag not in args.vm.tags) +mode.add_argument('--query', + dest='mode', + action='store_const', + const=mode_query, + help='query for the tag; if no tag specified, list all tags;' + ' this is the default') + +def mode_set(args): + if args.tag is None: + parser.error('tag is mandatory for --set') + args.vm.tags.add(args.tag) + args.app.save() +mode.add_argument('--set', '-s', + dest='mode', + action='store_const', + const=mode_set, + help='set the tag; if tag is already set, do nothing') + +def mode_unset(args): + if args.tag is None: + parser.error('tag is mandatory for --unset') + args.vm.tags.discard(args.tag) + args.app.save() +mode.add_argument('--unset', '--delete', '-D', + dest='mode', + action='store_const', + const=mode_unset, + help='unset the tag; if tag is not set, do nothing') + + +parser.add_argument('tag', metavar='TAG', + action='store', nargs='?', + help='name of the tag') + +parser.set_defaults(mode=mode_query) + + +def main(args=None): + '''Main routine of :program:`qvm-tags`. + + :param list args: Optional arguments to override those delivered from \ + command line. + ''' + + args = parser.parse_args(args) + return args.mode(args) + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/rpm_spec/core-dom0.spec b/rpm_spec/core-dom0.spec index 78cd7ef5..93de9405 100644 --- a/rpm_spec/core-dom0.spec +++ b/rpm_spec/core-dom0.spec @@ -270,6 +270,7 @@ fi %{python_sitelib}/qubes/tools/qvm_run.py* %{python_sitelib}/qubes/tools/qvm_shutdown.py* %{python_sitelib}/qubes/tools/qvm_start.py* +%{python_sitelib}/qubes/tools/qvm_tags.py* %{python_sitelib}/qubes/tools/qvm_template_commit.py* %{python_sitelib}/qubes/tools/qvm_template_postprocess.py* %{python_sitelib}/qubes/tools/qvm_unpause.py*