forked from stratis-storage/stratis-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_list_filesystem.py
More file actions
282 lines (243 loc) · 8.43 KB
/
_list_filesystem.py
File metadata and controls
282 lines (243 loc) · 8.43 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# Copyright 2024 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Filesystem listing.
"""
# isort: STDLIB
from abc import ABC, abstractmethod
from typing import Any, Callable, Dict, List, Optional
# isort: THIRDPARTY
from dateutil import parser as date_parser
from dbus import ObjectPath, String
from justbytes import Range
from ._connection import get_object
from ._constants import TOP_OBJECT
from ._formatting import (
TABLE_FAILURE_STRING,
TABLE_UNKNOWN_STRING,
TOTAL_USED_FREE,
catch_missing_property,
get_property,
print_table,
)
from ._utils import SizeTriple
def list_filesystems(
uuid_formatter: Callable, *, pool_name=None, fs_id=None
): # pylint: disable=too-many-locals
"""
List the specified information about filesystems.
"""
assert fs_id is None or pool_name is not None
# pylint: disable=import-outside-toplevel
from ._data import MOFilesystem, MOPool, ObjectManager, filesystems, pools
proxy = get_object(TOP_OBJECT)
managed_objects = ObjectManager.Methods.GetManagedObjects(proxy, {})
if pool_name is None:
props = None
pool_object_path = None
fs_props = None
requires_unique = False
else:
props = {"Name": pool_name}
pool_object_path = next(
pools(props=props).require_unique_match(True).search(managed_objects)
)[0]
fs_props = {"Pool": pool_object_path} | (
{} if fs_id is None else fs_id.managed_objects_key()
)
requires_unique = fs_id is not None
pool_object_path_to_pool_name = dict(
(path, MOPool(info).Name())
for path, info in pools(props=props).search(managed_objects)
)
filesystems_with_props = [
MOFilesystem(info)
for objpath, info in filesystems(props=fs_props)
.require_unique_match(requires_unique)
.search(managed_objects)
]
if fs_id is None:
klass = Table(
uuid_formatter,
filesystems_with_props,
pool_object_path_to_pool_name,
)
else:
klass = Detail(
uuid_formatter,
filesystems_with_props,
pool_object_path_to_pool_name,
)
klass.display()
class ListFilesystem(ABC): # pylint: disable=too-few-public-methods
"""
Handle listing a filesystem or filesystems.
"""
def __init__(
self,
uuid_formatter: Callable,
filesystems_with_props: List[Any],
pool_object_path_to_pool_name: Dict[ObjectPath, String],
):
"""
Initialize a List object.
:param uuid_formatter: function to format a UUID str or UUID
:param uuid_formatter: str or UUID -> str
:param bool stopped: whether to list stopped pools
"""
self.uuid_formatter = uuid_formatter
self.filesystems_with_props = filesystems_with_props
self.pool_object_path_to_pool_name = pool_object_path_to_pool_name
@abstractmethod
def display(self):
"""
List filesystems.
"""
@staticmethod
def size_triple(mofs: Any) -> SizeTriple:
"""
Calculate size triple
"""
return SizeTriple(
catch_missing_property(lambda mo: Range(mo.Size()), None)(mofs),
catch_missing_property(
lambda mo: get_property(mo.Used(), Range, None), None
)(mofs),
)
class Table(ListFilesystem): # pylint: disable=too-few-public-methods
"""
List filesystems using table format.
"""
def display(self):
"""
List the filesystems.
"""
def filesystem_size_quartet(
size_triple: SizeTriple, limit: Optional[Range]
) -> str:
"""
Calculate the triple to display for filesystem size.
:returns: a string a formatted string showing all three values
:rtype: str
"""
triple_str = " / ".join(
(
TABLE_FAILURE_STRING if x is None else str(x)
for x in (
size_triple.total(),
size_triple.used(),
size_triple.free(),
)
)
)
return f"{triple_str} / {limit}"
def missing_property(func: Callable[[Any], str]) -> Callable[[Any], str]:
return catch_missing_property(func, TABLE_UNKNOWN_STRING)
pool_name_func = missing_property(
lambda mo: self.pool_object_path_to_pool_name.get(
mo.Pool(), TABLE_UNKNOWN_STRING
)
)
name_func = missing_property(lambda mofs: mofs.Name())
size_func = missing_property(
lambda mofs: filesystem_size_quartet(
ListFilesystem.size_triple(mofs),
get_property(mofs.SizeLimit(), Range, None),
)
)
devnode_func = missing_property(lambda mofs: mofs.Devnode())
uuid_func = missing_property(lambda mofs: self.uuid_formatter(mofs.Uuid()))
tables = [
(
pool_name_func(mofilesystem),
name_func(mofilesystem),
size_func(mofilesystem),
devnode_func(mofilesystem),
uuid_func(mofilesystem),
)
for mofilesystem in self.filesystems_with_props
]
print_table(
[
"Pool",
"Filesystem",
f"{TOTAL_USED_FREE} / Limit",
"Device",
"UUID",
],
sorted(tables, key=lambda entry: (entry[0], entry[1])),
["<", "<", "<", "<", "<"],
)
class Detail(ListFilesystem): # pylint: disable=too-few-public-methods
"""
Do a detailed listing of filesystems.
"""
def display(self):
"""
List the filesystems.
"""
assert len(self.filesystems_with_props) == 1
fs = self.filesystems_with_props[0]
def missing_property(func: Callable[[Any], str]) -> Callable[[Any], str]:
return catch_missing_property(func, TABLE_UNKNOWN_STRING)(fs)
uuid = missing_property(lambda mo: self.uuid_formatter(mo.Uuid()))
print(f"UUID: {uuid}")
name = missing_property(lambda mo: mo.Name())
print(f"Name: {name}")
pool = missing_property(
lambda mo: self.pool_object_path_to_pool_name.get(
mo.Pool(), TABLE_UNKNOWN_STRING
),
)
print(f"Pool: {pool}")
devnode = missing_property(lambda mo: mo.Devnode())
print()
print(f"Device: {devnode}")
created = missing_property(
lambda mo: date_parser.isoparse(mo.Created())
.astimezone()
.strftime("%b %d %Y %H:%M"),
)
print()
print(f"Created: {created}")
origin = catch_missing_property(
lambda mo: get_property(mo.Origin(), self.uuid_formatter, None), None
)
print()
print(f"Snapshot origin: {origin}")
if origin is not None:
scheduled = missing_property(
lambda mo: "Yes" if mo.MergeScheduled() else "No"
)
print(f" Revert scheduled: {scheduled}")
size_triple = ListFilesystem.size_triple(fs)
print()
print("Sizes:")
print(
" Logical size of thin device: "
f"{TABLE_FAILURE_STRING if size_triple.total() is None else size_triple.total()}"
)
print(
" Total used (including XFS metadata): "
f"{TABLE_FAILURE_STRING if size_triple.used() is None else size_triple.used()}"
)
print(
" Free: "
f"{TABLE_FAILURE_STRING if size_triple.free() is None else size_triple.free()}"
)
limit = missing_property(
lambda mo: get_property(mo.SizeLimit(), lambda x: str(Range(x)), str(None))
)
print()
print(f" Size Limit: {limit}")