Menu

[r7065]: / trunk / gui / middleware / zfs.py  Maximize  Restore  History

Download this file

225 lines (190 with data), 7.2 kB

  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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python
#-
# Copyright (c) 2010 iXsystems, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
import re
class Tnode(object):
name = None
children = None
parent = None
type = None
status = None
def __init__(self, name, doc, status=None):
self._doc = doc
self.name = name
self.children = []
self.status = status
def find_by_name(self, name):
for c in self.children:
if c.name == name:
return c
return None
def find_unavail(self):
if len(self.children) == 0:
if self.status == 'UNAVAIL':
return self
else:
unavails = []
for c in self.children:
find = c.find_unavail()
if find:
if isinstance(find, list):
unavails += find
else:
unavails.append(find)
return unavails
def append(self, tnode):
raise NotImplementedError
@staticmethod
def pprint(node, level=0):
print ' ' * level + node.name
for c in node.children:
node.pprint(c, level+1)
def _is_vdev(self, name):
if name in ('stripe', 'mirror', 'raidz', 'raidz1', 'raidz2', 'raidz3') \
or re.search(r'^(mirror|raidz|raidz1|raidz2|raidz3)(-\d+)?$', name):
return True
return False
def __iter__(self):
for c in list(self.children):
yield c
def _vdev_type(self, name):
# raidz needs to appear after other raidz types
supported_types = ('stripe', 'mirror', 'raidz3', 'raidz2', 'raidz')
for type in supported_types:
if name.startswith(type):
return type
return False
def validate(self, level=0):
raise NotImplementedError
def dump(self, level=0):
raise NotImplementedError
class Pool(Tnode):
pass
class Root(Tnode):
def append(self, node):
if not isinstance(node, Vdev):
raise Exception("Not a vdev: %s" % node)
self.children.append(node)
node.parent = self
def dump(self):
self.validate()
vdevs = []
for c in self:
vdevs.append(c.dump())
return {'name': self.name, 'vdevs': vdevs}
def validate(self):
for c in self:
c.validate()
class Vdev(Tnode):
def __repr__(self):
return "<Section: %s>" % self.name
def append(self, node):
if not isinstance(node, Dev):
raise Exception("Not a device")
self.children.append(node)
node.parent = self
def dump(self):
disks = []
for c in self:
disks.append(c.dump())
return {'disks': disks, 'type': self.type}
def validate(self):
for c in self:
c.validate()
if len(self.children) == 0:
stripe = self.parent.find_by_name("stripe")
if not stripe:
stripe = Tnode("stripe", self._doc)
stripe.type = 'stripe'
self.parent.append(stripe)
self.parent.children.remove(self)
stripe.append(self)
stripe.validate(level)
else:
self.type = self._vdev_type(self.name)
class Dev(Tnode):
def __repr__(self):
return "<Node: %s>" % self.name
def append(self, node):
raise Exception("What? You can't append child to a Dev")
def dump(self):
return self.devname
def validate(self):
for c in self:
c.validate()
# The parent of a leaf should be a vdev
if not self._is_vdev(self.parent.name) and \
self.parent.parent is not None:
raise Exception("Oh noes! This damn thing should be a vdev! %s" % self.parent)
search = self._doc.xpathEval("//class[name = 'LABEL']//provider[name = '%s']/../name" % self.name)
if len(search) > 0:
self.devname = search[0].content
else:
search = self._doc.xpathEval("//class[name = 'DEV']/geom[name = '%s']" % self.name)
if len(search) > 0:
self.devname = self.name
elif self.status == 'ONLINE':
raise Exception("It should be a valid device: %s" % self.name)
else:
self.devname = self.name
def parse_status(name, doc, data):
status = data.split('config:')[1]
roots = {'cache': None, 'logs': None, 'spares': None}
lastident = None
for line in status.split('\n'):
if line.startswith('\t'):
try:
spaces, word, status = re.search(r'^(?P<spaces>[ ]*)(?P<word>\S+)\s+(?P<status>\S+)', line[1:]).groups()
except:
spaces, word = re.search(r'^(?P<spaces>[ ]*)(?P<word>\S+)', line[1:]).groups()
status = None
ident = len(spaces) / 2
if ident < lastident:
for x in range(lastident - ident):
pnode = pnode.parent
if ident == 0:
if word != 'NAME':
tree = Root(word, doc)
tree.status = status
roots[word] = tree
pnode = tree
elif ident == 1:
if pnode._is_vdev(word):
node = Vdev(word, doc, status=status)
pnode.append(node)
pnode = node
else:
node = Vdev("stripe", doc)
node.status = status
node2 = Dev(word, doc, status=status)
pnode.append(node)
node.append(node2)
pnode = node
elif ident == 2:
node = Dev(word, doc, status=status)
pnode.append(node)
lastident = ident
return roots
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.