From e2434db0e4558d6ffd414a497f286976006526c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Mon, 24 Jan 2022 18:26:48 +0100 Subject: Rename, and fail nicer on nspawn fact. --- facts.d/main.py | 71 ---------------------------------------------- facts.d/nspawn_machines.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 71 deletions(-) delete mode 100755 facts.d/main.py create mode 100755 facts.d/nspawn_machines.py diff --git a/facts.d/main.py b/facts.d/main.py deleted file mode 100755 index 8a46a9d..0000000 --- a/facts.d/main.py +++ /dev/null @@ -1,71 +0,0 @@ -#!/usr/bin/env python3 - -import sys - -try: - import dbus - import yaml -except ImportError: - sys.exit(1) - -bus = dbus.SystemBus() -bus_name = 'org.freedesktop.machine1' # dest -object_path = '/org/freedesktop/machine1' -try: - machined_proxy = bus.get_object(bus_name=bus_name, object_path=object_path) -except: - sys.exit(1) -iface = dbus.Interface(machined_proxy, dbus_interface='org.freedesktop.machine1.Manager') - - -machines = iface.ListMachines() -machine_names = [] -machines_info = {} -for (machine_name, *_) in machines: - machine = iface.GetMachine(machine_name) - pp = bus.get_object(bus_name='org.freedesktop.machine1', object_path=machine) - ii = dbus.Interface(pp, dbus_interface='org.freedesktop.DBus.Properties') - out_dict = {} - for key, value in ii.GetAll('org.freedesktop.machine1.Machine').items(): - # see help(dbus.types) - t = type(value) - if t == dbus.ByteArray: - raise NotImplementedError('Byte array') - elif t == dbus.Double: - v = float(value) - elif t == dbus.Boolean: - v = bool(value) - elif t in [ dbus.Byte, dbus.Int16, dbus.Int32, dbus.Int64, dbus.UInt16, dbus.UInt32, dbus.UInt64 ]: - v = int(value) - elif t in [ dbus.ObjectPath, dbus.Signature ]: - # string likes - v = str(value) - elif t == dbus.Dictionary: - # dict like - raise NotImplementedError('Dictionary') - elif t == dbus.Array: - if value.signature == dbus.Signature('y'): - v = bytes(int(x) for x in value) - elif value.signature == dbus.Signature('i'): - v = [int(x) for x in value] - else: - print(repr(value)) - print(repr(value.signature)) - raise NotImplementedError('Array') - # case dbus.UnixFd: - # raise NotImplementedError() - elif t == dbus.String: - v = str(value) - elif t == dbus.Struct: - # tuple like - raise NotImplementedError('Struct') - out_dict[str(key)] = v - machine_names.append(str(machine_name)) - machines_info[str(machine_name)] = out_dict - -out = { - 'machined-machines': machine_names, - 'machined-info': machines_info, -} - -print(yaml.dump(out)) diff --git a/facts.d/nspawn_machines.py b/facts.d/nspawn_machines.py new file mode 100755 index 0000000..f8fc9a5 --- /dev/null +++ b/facts.d/nspawn_machines.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python3 + +import sys + +try: + import dbus + import yaml +except ImportError: + sys.exit(0) + +bus = dbus.SystemBus() +bus_name = 'org.freedesktop.machine1' # dest +object_path = '/org/freedesktop/machine1' +try: + machined_proxy = bus.get_object(bus_name=bus_name, object_path=object_path) +except: + sys.exit(1) +iface = dbus.Interface(machined_proxy, dbus_interface='org.freedesktop.machine1.Manager') + + +machines = iface.ListMachines() +machine_names = [] +machines_info = {} +for (machine_name, *_) in machines: + machine = iface.GetMachine(machine_name) + pp = bus.get_object(bus_name='org.freedesktop.machine1', object_path=machine) + ii = dbus.Interface(pp, dbus_interface='org.freedesktop.DBus.Properties') + out_dict = {} + for key, value in ii.GetAll('org.freedesktop.machine1.Machine').items(): + # see help(dbus.types) + t = type(value) + if t == dbus.ByteArray: + raise NotImplementedError('Byte array') + elif t == dbus.Double: + v = float(value) + elif t == dbus.Boolean: + v = bool(value) + elif t in [ dbus.Byte, dbus.Int16, dbus.Int32, dbus.Int64, dbus.UInt16, dbus.UInt32, dbus.UInt64 ]: + v = int(value) + elif t in [ dbus.ObjectPath, dbus.Signature ]: + # string likes + v = str(value) + elif t == dbus.Dictionary: + # dict like + raise NotImplementedError('Dictionary') + elif t == dbus.Array: + if value.signature == dbus.Signature('y'): + v = bytes(int(x) for x in value) + elif value.signature == dbus.Signature('i'): + v = [int(x) for x in value] + else: + print(repr(value)) + print(repr(value.signature)) + raise NotImplementedError('Array') + # case dbus.UnixFd: + # raise NotImplementedError() + elif t == dbus.String: + v = str(value) + elif t == dbus.Struct: + # tuple like + raise NotImplementedError('Struct') + out_dict[str(key)] = v + machine_names.append(str(machine_name)) + machines_info[str(machine_name)] = out_dict + +out = { + 'machined-machines': machine_names, + 'machined-info': machines_info, +} + +print(yaml.dump(out)) -- cgit v1.2.3