From 8aaef404de0b99887b61f0ae8fa43dcb8a1922aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Fri, 15 May 2015 03:07:03 +0200 Subject: [PATCH] qvm-prefs: add an option to get a single VM property Could be useful for scripts --- doc/qvm-tools/qvm-prefs.rst | 3 +++ qvm-tools/qvm-prefs | 50 +++++++++++++++++++++++++++++-------- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/doc/qvm-tools/qvm-prefs.rst b/doc/qvm-tools/qvm-prefs.rst index 596fa9a2..ef1c6878 100644 --- a/doc/qvm-tools/qvm-prefs.rst +++ b/doc/qvm-tools/qvm-prefs.rst @@ -11,6 +11,7 @@ qvm-prefs - list/set various per-VM properties SYNOPSIS ======== | qvm-prefs -l [options] +| qvm-prefs -g [options] | qvm-prefs -s [options] [...] @@ -20,6 +21,8 @@ OPTIONS Show this help message and exit -l, --list List properties of a specified VM +-g, --get + Get a single property of a specified VM -s, --set Set properties of a specified VM diff --git a/qvm-tools/qvm-prefs b/qvm-tools/qvm-prefs index 10c8947f..36b57f76 100755 --- a/qvm-tools/qvm-prefs +++ b/qvm-tools/qvm-prefs @@ -109,6 +109,28 @@ def do_list(vm): print fmt.format ("internal", vm.internal) + +def do_get(vms, vm, prop): + + if prop == 'config': + prop = 'conf_file' + elif prop == 'dir': + prop = 'dir_path' + elif prop == 'last_backup': + prop = 'backup_timestamp' + if not hasattr(vm, prop): + print >>sys.stderr, "VM '{}' has no attribute '{}'".format(vm.name, + prop) + return + if getattr(vm, prop, None) is None: + # not set or set to None + return + if prop in ['template', 'netvm', 'dispvm_netvm']: + print getattr(vm, prop).name + else: + print str(getattr(vm, prop)) + + def set_label(vms, vm, args): if len (args) != 1: print >> sys.stderr, "Missing label name argument!" @@ -495,14 +517,20 @@ def do_set(vms, vm, property, args): def main(): usage = "usage: %prog -l [options] \n"\ + "usage: %prog -g [options] \n"\ "usage: %prog -s [options] [...]\n"\ "List/set various per-VM properties." parser = OptionParser (usage) - parser.add_option ("-l", "--list", action="store_true", dest="do_list", default=False) - parser.add_option ("-s", "--set", action="store_true", dest="do_set", default=False) - parser.add_option ("--force-root", action="store_true", dest="force_root", default=False, - help="Force to run, even with root privileges") + parser.add_option("-l", "--list", action="store_true", dest="do_list", + default=False) + parser.add_option("-s", "--set", action="store_true", dest="do_set", + default=False) + parser.add_option ("-g", "--gry", action="store_true", dest="do_get", + default=False) + parser.add_option("--force-root", action="store_true", dest="force_root", + default=False, + help="Force to run, even with root privileges") (options, args) = parser.parse_args () if (len (args) < 1): @@ -517,11 +545,10 @@ def main(): print >> sys.stderr, "... or use --force-root to continue anyway." exit(1) - if options.do_list and options.do_set: - print >> sys.stderr, "You cannot provide -l and -s at the same time!" - exit (1) - - + if options.do_list + options.do_set + options.do_get > 1: + print >> sys.stderr, "You can provide at most one of -l, -g and -s at " \ + "the same time!" + exit(1) if options.do_set: qvm_collection = QubesVmCollection() @@ -555,8 +582,9 @@ def main(): qvm_collection.unlock_db() exit(1) - - else: + elif options.do_get or len(args) == 2: + do_get(qvm_collection, vm, args[1]) + else: # do_list do_list(vm)