-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnapshot_list.py
131 lines (96 loc) · 4.04 KB
/
snapshot_list.py
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import sys
import argparse
import datetime
import importlib
import libvirt
import pprint
import xml.etree.ElementTree as ET
import virpy
import virpy.classes
import virpy.utils
'''
https://libvirt.org/html/
https://libvirt.org/html/libvirt-libvirt-domain-snapshot.html
python -c 'import libvirt; help(libvirt)'
'''
def create_handler(parser):
parser.add_argument('domain')
parser.add_argument('--parent', action='store_true')
group1 = parser.add_mutually_exclusive_group()
group1.add_argument('--roots', action='store_true')
group1.add_argument('--from', dest='from_name')
group2 = parser.add_mutually_exclusive_group()
group2.add_argument('--leaves', action='store_true')
group2.add_argument('--no-leaves', action='store_true')
group3 = parser.add_mutually_exclusive_group()
group3.add_argument('--metadata', action='store_true')
group3.add_argument('--no-metadata', action='store_true')
group4 = parser.add_mutually_exclusive_group()
group4.add_argument('--inactive', action='store_true')
group4.add_argument('--active', action='store_true')
group4.add_argument('--disk-only', action='store_true')
group5 = parser.add_mutually_exclusive_group()
group5.add_argument('--internal', action='store_true')
group5.add_argument('--external', action='store_true')
#parser.add_argument('--tree', action='store_true')
#parser.add_argument('--current', action='store_true')
#parser.add_argument('--descendants', action='store_true')
#parser.add_argument('--name', action='store_true')
parser.add_argument('--topological', action='store_true')
return SnapshotListCommand()
class SnapshotListCommand(virpy.classes.Command):
def run(self, conn, args):
dom = virpy.utils.lookupDomain(conn, args.domain)
#pprint.pprint(dir(dom))
flags_db = {
'roots': libvirt.VIR_DOMAIN_SNAPSHOT_LIST_ROOTS,
'leaves': libvirt.VIR_DOMAIN_SNAPSHOT_LIST_LEAVES,
'no_leaves': libvirt.VIR_DOMAIN_SNAPSHOT_LIST_NO_LEAVES,
'metadata': libvirt.VIR_DOMAIN_SNAPSHOT_LIST_METADATA,
'no_metadata': libvirt.VIR_DOMAIN_SNAPSHOT_LIST_NO_METADATA,
'inactive': libvirt.VIR_DOMAIN_SNAPSHOT_LIST_INACTIVE,
'active': libvirt.VIR_DOMAIN_SNAPSHOT_LIST_ACTIVE,
'disk_only': libvirt.VIR_DOMAIN_SNAPSHOT_LIST_DISK_ONLY,
'internal': libvirt.VIR_DOMAIN_SNAPSHOT_LIST_INTERNAL,
'external': libvirt.VIR_DOMAIN_SNAPSHOT_LIST_EXTERNAL,
#'descendants': libvirt.VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS,
'topological': libvirt.VIR_DOMAIN_SNAPSHOT_LIST_TOPOLOGICAL,
}
flags = virpy.utils.setBitsByArgs(args, flags_db)
data = None
for obj in dom.listAllSnapshots(flags):
#pprint.pprint(dir(obj))
name = obj.getName()
xmlRoot = ET.fromstring(obj.getXMLDesc())
#try:
# parent = obj.getParent().getName()
#except libvirt.libvirtError:
# parent = None
parent = xmlRoot.findtext('parent/name')
#print(parent)
# creationTime -> datetime -> string
ts = int(xmlRoot.findtext('creationTime'))
dt = datetime.datetime.fromtimestamp(ts)
ct = dt.strftime('%Y-%m-%d %H:%M:%S')
rec = {
'name': name,
'creationTime': ct,
'state': xmlRoot.findtext('state'),
}
rec |= {'parent': parent, } if args.parent else {}
# bool(obj.isCurrent())
if args.roots:
if parent is None:
data = rec
break
continue
if args.from_name is not None:
if args.from_name == parent:
data = rec
break
continue
if data is None:
data = []
data.append(rec)
return data
# EOF