-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfindpref
executable file
·109 lines (86 loc) · 3.39 KB
/
findpref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/python3
##
## findpref - Find & Replace VM prefs
## by Chris Laprise 2018
## [email protected] https://github.com/tasket
import sys
from time import sleep
from optparse import OptionParser
import qubesadmin.vm
usage = "usage: %prog [options] --pref=name [search_value] [new_value]"
parser = OptionParser (usage)
parser.add_option ("-p", "--pref", action="store", type="string", dest="prefname",
help="(Required) Name of pref to search/change")
parser.add_option ("-e", "--exclude", action="append", dest="exclude_list",
help="Exclude VM; repeat as needed")
parser.add_option ("-y", "--yes", action="store_true", dest="go_ahead", default=False,
help="Set values without prompting")
parser.add_option ("--mtypes", action="append", dest="mtypes",
default=None, help="Match VM type: AppVM, TemplateVM, StandaloneVM")
def main(args=None, app=None):
(options, args) = parser.parse_args ()
if len(options.prefname) > 0:
if len(args) == 2:
searchval = args[0]
newval = args[1]
if not options.go_ahead and \
input("Are you sure (y/N)? ").lower() not in ["y", "yes"]:
exit(0)
elif len(args) == 1:
searchval = args[0]
newval = None
elif len(args) == 0:
searchval = None
else:
print("Too many args.")
exit(1)
else:
print("You must supply a --pref <prefname>")
exit(1)
if options.exclude_list is None:
options.exclude_list = []
if options.mtypes is None:
options.mtypes = []
qc = qubesadmin.Qubes().app.domains
vmlist = []
# Process selections
for vm in qc:
if vm.qid > 0:
if vm.name in options.exclude_list:
continue
if len(options.mtypes) > 0 and vm.klass not in options.mtypes:
continue
vmlist.append(vm)
for vm in vmlist:
try:
value = getattr(vm, options.prefname)
except AttributeError:
#print("No such property '{0}'".format(options.prefname))
continue
if searchval is None:
print("Pref '{0}' for VM {1:<20s}:".format(options.prefname,
vm.name), " '{0}'".format(str(value)))
else:
match = False
if searchval.lower() == "none" and not value:
match = True
elif value and str(value) == searchval:
match = True
if match:
print("Pref '{0}' for VM {1:<20s}:".format(options.prefname, vm.name), end=" ")
try:
if newval is not None:
setattr(vm, options.prefname, newval)
if options.prefname == "netvm" and vm.is_running():
# workaround for change netvm bug: wait and set again...
sleep(2)
setattr(vm, options.prefname, newval)
sleep(1)
print("old='{0}' new='{1}'".format(searchval, newval))
else:
print("'{0}'".format(searchval))
except:
print("Error:", sys.exc_info()[0])
continue
if __name__ == '__main__':
sys.exit(main())