|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Helper script for gathering information about stratis pools using stratis DBus API. |
| 4 | + |
| 5 | +# The script is meant to be a supporting tool for the storage role tests |
| 6 | + |
| 7 | +import sys |
| 8 | +from collections import namedtuple |
| 9 | + |
| 10 | +import json |
| 11 | + |
| 12 | +import gi |
| 13 | +gi.require_version("GLib", "2.0") |
| 14 | +gi.require_version("Gio", "2.0") |
| 15 | + |
| 16 | +from gi.repository import GLib, Gio |
| 17 | + |
| 18 | +STRATIS_SERVICE = "org.storage.stratis3" |
| 19 | +STRATIS_PATH = "/org/storage/stratis3" |
| 20 | +STRATIS_POOL_INTF = STRATIS_SERVICE + ".pool.r0" |
| 21 | + |
| 22 | +# Code for working with DBus, taken from blivet/safe_dbus.py |
| 23 | +DBUS_PROPS_IFACE = "org.freedesktop.DBus.Properties" |
| 24 | +DBUS_INTRO_IFACE = "org.freedesktop.DBus.Introspectable" |
| 25 | + |
| 26 | + |
| 27 | +class SafeDBusError(Exception): |
| 28 | + """Class for exceptions defined in this module.""" |
| 29 | + |
| 30 | + |
| 31 | +class DBusCallError(SafeDBusError): |
| 32 | + """Class for the errors related to calling methods over DBus.""" |
| 33 | + |
| 34 | + |
| 35 | +class DBusPropertyError(DBusCallError): |
| 36 | + """Class for the errors related to getting property values over DBus.""" |
| 37 | + |
| 38 | + |
| 39 | +def get_new_system_connection(): |
| 40 | + """Return a new connection to the system bus.""" |
| 41 | + |
| 42 | + return Gio.DBusConnection.new_for_address_sync( |
| 43 | + Gio.dbus_address_get_for_bus_sync(Gio.BusType.SYSTEM, None), |
| 44 | + Gio.DBusConnectionFlags.AUTHENTICATION_CLIENT |
| 45 | + | Gio.DBusConnectionFlags.MESSAGE_BUS_CONNECTION, |
| 46 | + None, None) |
| 47 | + |
| 48 | + |
| 49 | +def call_sync(service, obj_path, iface, method, args, connection=None, fds=None): |
| 50 | + if not connection: |
| 51 | + try: |
| 52 | + connection = get_new_system_connection() |
| 53 | + except GLib.GError as gerr: |
| 54 | + raise DBusCallError("Unable to connect to system bus: %s" % gerr) from gerr |
| 55 | + |
| 56 | + if connection.is_closed(): |
| 57 | + raise DBusCallError("Connection is closed") |
| 58 | + |
| 59 | + try: |
| 60 | + ret = connection.call_with_unix_fd_list_sync(service, obj_path, iface, method, |
| 61 | + args, None, Gio.DBusCallFlags.NONE, |
| 62 | + -1, fds, None) |
| 63 | + except GLib.GError as gerr: |
| 64 | + msg = "Failed to call %s method on %s with %s arguments: %s" % \ |
| 65 | + (method, obj_path, args, gerr.message) # pylint: disable=no-member |
| 66 | + raise DBusCallError(msg) from gerr |
| 67 | + |
| 68 | + if ret is None: |
| 69 | + msg = "No return from %s method on %s with %s arguments" % (method, obj_path, |
| 70 | + args) |
| 71 | + raise DBusCallError(msg) |
| 72 | + |
| 73 | + return ret[0].unpack() |
| 74 | + |
| 75 | + |
| 76 | +def get_properties_sync(service, obj_path, iface, connection=None): |
| 77 | + args = GLib.Variant('(s)', (iface,)) |
| 78 | + ret = call_sync(service, obj_path, DBUS_PROPS_IFACE, "GetAll", args, |
| 79 | + connection) |
| 80 | + return ret |
| 81 | + |
| 82 | + |
| 83 | +# Extracting and printing Stratis pool information |
| 84 | +StratisPoolInfo = namedtuple("StratisPoolInfo", ["name", "encrypted", "key_desc", |
| 85 | + "clevis_pin", "clevis_args"]) |
| 86 | + |
| 87 | + |
| 88 | +def _print_pool_info_json(pool_info): |
| 89 | + pi_dict = pool_info._asdict() |
| 90 | + pi_json = json.dumps(pi_dict) |
| 91 | + print(pi_json) |
| 92 | + |
| 93 | + |
| 94 | +def _get_pool_info(pool_path): |
| 95 | + try: |
| 96 | + properties = get_properties_sync(STRATIS_SERVICE, |
| 97 | + pool_path, |
| 98 | + STRATIS_POOL_INTF)[0] |
| 99 | + except DBusPropertyError as e: |
| 100 | + print("Error when getting DBus properties of '%s': %s" % (pool_path, str(e)), |
| 101 | + file=sys.stderr) |
| 102 | + return None |
| 103 | + |
| 104 | + if not properties: |
| 105 | + print("Failed to get DBus properties of '%s'" % pool_path, file=sys.stderr) |
| 106 | + return None |
| 107 | + |
| 108 | + description = properties.get("KeyDescription", None) |
| 109 | + if not description or not description[0] or not description[1][0]: |
| 110 | + key_desc = None |
| 111 | + else: |
| 112 | + key_desc = description[1][1] |
| 113 | + |
| 114 | + clevis_info = properties.get("ClevisInfo", None) |
| 115 | + if not clevis_info or not clevis_info[0] or not clevis_info[1][0]: |
| 116 | + clevis = None |
| 117 | + else: |
| 118 | + clevis = clevis_info[1][1] |
| 119 | + |
| 120 | + if clevis: |
| 121 | + clevis_pin = clevis[0] |
| 122 | + clevis_args = json.loads(clevis[1]) |
| 123 | + else: |
| 124 | + clevis_pin = None |
| 125 | + clevis_args = {} |
| 126 | + |
| 127 | + return StratisPoolInfo(name=properties["Name"], |
| 128 | + encrypted=properties["Encrypted"], |
| 129 | + key_desc=key_desc, |
| 130 | + clevis_pin=clevis_pin, |
| 131 | + clevis_args=clevis_args) |
| 132 | + |
| 133 | + |
| 134 | +def main(pool_name): |
| 135 | + objects = call_sync(STRATIS_SERVICE, |
| 136 | + STRATIS_PATH, |
| 137 | + "org.freedesktop.DBus.ObjectManager", |
| 138 | + "GetManagedObjects", |
| 139 | + None)[0] |
| 140 | + |
| 141 | + for path, interfaces in objects.items(): |
| 142 | + if STRATIS_POOL_INTF in interfaces.keys(): |
| 143 | + pool_info = _get_pool_info(path) |
| 144 | + if pool_info and pool_info.name == pool_name: |
| 145 | + _print_pool_info_json(pool_info) |
| 146 | + return True |
| 147 | + |
| 148 | + print("Pool %s not found on DBus" % pool_name, file=sys.stderr) |
| 149 | + print(json.dumps(None)) |
| 150 | + return True |
| 151 | + |
| 152 | + |
| 153 | +if __name__ == "__main__": |
| 154 | + if len(sys.argv) != 2: |
| 155 | + print("Usage: python %s <pool name>" % sys.argv[0]) |
| 156 | + sys.exit(1) |
| 157 | + |
| 158 | + succ = main(sys.argv[1]) |
| 159 | + sys.exit(0) if succ else sys.exit(1) |
0 commit comments