|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright 2016 Red Hat, Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +""" |
| 17 | +Update Stratis introspection data. |
| 18 | +""" |
| 19 | + |
| 20 | +# isort: STDLIB |
| 21 | +import xml.etree.ElementTree as ET |
| 22 | + |
| 23 | +# isort: THIRDPARTY |
| 24 | +import dbus |
| 25 | + |
| 26 | +# isort: FIRSTPARTY |
| 27 | +from dbus_python_client_gen import make_class |
| 28 | + |
| 29 | +# a minimal chunk of introspection data, enough for the methods needed. |
| 30 | +SPECS = { |
| 31 | + "org.freedesktop.DBus.Introspectable": """ |
| 32 | +<interface name="org.freedesktop.DBus.Introspectable"> |
| 33 | +<method name="Introspect"> |
| 34 | +<arg name="xml_data" type="s" direction="out"/> |
| 35 | +</method> |
| 36 | +</interface> |
| 37 | +""", |
| 38 | + "org.storage.stratis2.Manager.r1": """ |
| 39 | +<interface name="org.storage.stratis2.Manager.r1"> |
| 40 | +<method name="CreatePool"> |
| 41 | +<arg name="name" type="s" direction="in"/> |
| 42 | +<arg name="redundancy" type="(bq)" direction="in"/> |
| 43 | +<arg name="devices" type="as" direction="in"/> |
| 44 | +<arg name="key_desc" type="(bs)" direction="in"/> |
| 45 | +<arg name="result" type="(b(oao))" direction="out"/> |
| 46 | +<arg name="return_code" type="q" direction="out"/> |
| 47 | +<arg name="return_string" type="s" direction="out"/> |
| 48 | +</method> |
| 49 | +</interface> |
| 50 | +""", |
| 51 | + "org.storage.stratis2.pool.r1": """ |
| 52 | +<interface name="org.storage.stratis2.pool.r1"> |
| 53 | +<method name="CreateFilesystems"> |
| 54 | +<arg name="specs" type="as" direction="in"/> |
| 55 | +<arg name="results" type="(ba(os))" direction="out"/> |
| 56 | +<arg name="return_code" type="q" direction="out"/> |
| 57 | +<arg name="return_string" type="s" direction="out"/> |
| 58 | +</method> |
| 59 | +</interface> |
| 60 | +""", |
| 61 | +} |
| 62 | + |
| 63 | +_SERVICE = "org.storage.stratis2" |
| 64 | + |
| 65 | +_INTROSPECTABLE_IFACE = "org.freedesktop.DBus.Introspectable" |
| 66 | +_MANAGER_IFACE = "org.storage.stratis2.Manager.r1" |
| 67 | +_POOL_IFACE = "org.storage.stratis2.pool.r1" |
| 68 | +_TIMEOUT = 120000 |
| 69 | + |
| 70 | +Introspectable = make_class( |
| 71 | + "Introspectable", ET.fromstring(SPECS[_INTROSPECTABLE_IFACE]), _TIMEOUT |
| 72 | +) |
| 73 | +Manager = make_class("Manager", ET.fromstring(SPECS[_MANAGER_IFACE]), _TIMEOUT) |
| 74 | +Pool = make_class("Pool", ET.fromstring(SPECS[_POOL_IFACE]), _TIMEOUT) |
| 75 | + |
| 76 | +# EDIT these fields to get the interfaces and revisions desired |
| 77 | +TOP_OBJECT_INTERFACES = [ |
| 78 | + "org.freedesktop.DBus.ObjectManager", |
| 79 | + "org.storage.stratis2.FetchProperties.r2", |
| 80 | + "org.storage.stratis2.Manager.r2", |
| 81 | + "org.storage.stratis2.Report.r1", |
| 82 | +] |
| 83 | +POOL_OBJECT_INTERFACES = ["org.storage.stratis2.pool.r1"] |
| 84 | +BLOCKDEV_OBJECT_INTERFACES = ["org.storage.stratis2.blockdev.r2"] |
| 85 | +FILESYSTEM_OBJECT_INTERFACES = ["org.storage.stratis2.filesystem"] |
| 86 | + |
| 87 | + |
| 88 | +def _add_data(proxy, interfaces, table): |
| 89 | + """ |
| 90 | + Introspect on the proxy, get the information for the specified interfaces, |
| 91 | + and add it to the table. |
| 92 | +
|
| 93 | + :param proxy: dbus Proxy object |
| 94 | + :param list interfaces: list of interesting interface names |
| 95 | + :param dict table: table from interface names to introspection data as text |
| 96 | + """ |
| 97 | + string_data = Introspectable.Methods.Introspect(proxy, {}) |
| 98 | + xml_data = ET.fromstring(string_data) |
| 99 | + for interface in xml_data: |
| 100 | + interface_name = interface.attrib["name"] |
| 101 | + if interface_name in interfaces: |
| 102 | + table[interface_name] = ET.tostring(interface).decode("utf-8").rstrip(" \n") |
| 103 | + |
| 104 | + if not (frozenset(interfaces) <= frozenset(table.keys())): |
| 105 | + exit( |
| 106 | + "not %s <= %s" |
| 107 | + % (sorted(frozenset(interfaces)), sorted(frozenset(table.keys())),) |
| 108 | + ) |
| 109 | + |
| 110 | + |
| 111 | +def main(): |
| 112 | + """ |
| 113 | + The main method. |
| 114 | + """ |
| 115 | + specs = {} |
| 116 | + bus = dbus.SystemBus() |
| 117 | + |
| 118 | + proxy = bus.get_object(_SERVICE, "/org/storage/stratis2", introspect=False) |
| 119 | + |
| 120 | + ( |
| 121 | + (_, (pool_object_path, dev_object_paths)), |
| 122 | + return_code, |
| 123 | + return_msg, |
| 124 | + ) = Manager.Methods.CreatePool( |
| 125 | + proxy, |
| 126 | + { |
| 127 | + "name": "pool_name", |
| 128 | + "redundancy": (True, 0), |
| 129 | + "devices": ["/fake/fake"], |
| 130 | + "key_desc": (False, ""), |
| 131 | + }, |
| 132 | + ) |
| 133 | + |
| 134 | + if return_code != 0: |
| 135 | + exit(return_msg) |
| 136 | + |
| 137 | + pool_proxy = bus.get_object(_SERVICE, pool_object_path, introspect=False) |
| 138 | + |
| 139 | + blockdev_proxy = bus.get_object(_SERVICE, dev_object_paths[0], introspect=False) |
| 140 | + |
| 141 | + ((_, (filesystems)), return_code, return_msg,) = Pool.Methods.CreateFilesystems( |
| 142 | + pool_proxy, {"specs": ["fs_name"],}, |
| 143 | + ) |
| 144 | + |
| 145 | + if return_code != 0: |
| 146 | + exit(return_msg) |
| 147 | + |
| 148 | + filesystem_object_path = filesystems[0][0] |
| 149 | + filesystem_proxy = bus.get_object( |
| 150 | + _SERVICE, filesystem_object_path, introspect=False |
| 151 | + ) |
| 152 | + |
| 153 | + _add_data(proxy, TOP_OBJECT_INTERFACES, specs) |
| 154 | + _add_data(pool_proxy, POOL_OBJECT_INTERFACES, specs) |
| 155 | + _add_data(blockdev_proxy, BLOCKDEV_OBJECT_INTERFACES, specs) |
| 156 | + _add_data(filesystem_proxy, FILESYSTEM_OBJECT_INTERFACES, specs) |
| 157 | + |
| 158 | + print("SPECS = {") |
| 159 | + |
| 160 | + # format the lines in a way that will satisfy black |
| 161 | + print( |
| 162 | + ",\n".join( |
| 163 | + ' "%s": """\n%s\n"""' % (key, value) |
| 164 | + for (key, value) in sorted(specs.items()) |
| 165 | + ) |
| 166 | + + "," |
| 167 | + ) |
| 168 | + print("}") |
| 169 | + |
| 170 | + |
| 171 | +if __name__ == "__main__": |
| 172 | + main() |
0 commit comments