Brad Bishop | c91b0bf | 2016-08-30 20:03:48 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 2 | |
Brad Bishop | 0f608de | 2016-09-08 22:48:22 -0400 | [diff] [blame] | 3 | import os |
| 4 | import sys |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 5 | |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 6 | # TODO: openbmc/openbmc#2994 remove python 2 support |
| 7 | try: # python 2 |
| 8 | import gobject |
| 9 | except ImportError: # python 3 |
| 10 | from gi.repository import GObject as gobject |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 11 | import dbus |
| 12 | import dbus.service |
| 13 | import dbus.mainloop.glib |
Brad Bishop | ee1b154 | 2016-05-12 16:55:00 -0400 | [diff] [blame] | 14 | import obmc.dbuslib.propertycacher as PropertyCacher |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 15 | from obmc.dbuslib.bindings import get_dbus, DbusProperties, DbusObjectManager |
Brad Bishop | 985b2a5 | 2016-08-30 20:06:23 -0400 | [diff] [blame] | 16 | |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 17 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 18 | INTF_NAME = "org.openbmc.InventoryItem" |
| 19 | DBUS_NAME = "org.openbmc.Inventory" |
Brad Bishop | 985b2a5 | 2016-08-30 20:06:23 -0400 | [diff] [blame] | 20 | |
Brad Bishop | 3d13c28 | 2016-09-21 08:07:47 -0400 | [diff] [blame] | 21 | FRUS = {} |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 22 | |
Brad Bishop | c91b0bf | 2016-08-30 20:03:48 -0400 | [diff] [blame] | 23 | |
| 24 | class Inventory(DbusProperties, DbusObjectManager): |
| 25 | def __init__(self, bus, name): |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 26 | super(Inventory, self).__init__(conn=bus, object_path=name) |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 27 | |
| 28 | |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 29 | class InventoryItem(DbusProperties): |
Brad Bishop | c91b0bf | 2016-08-30 20:03:48 -0400 | [diff] [blame] | 30 | def __init__(self, bus, name, data): |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 31 | super(InventoryItem, self).__init__(conn=bus, object_path=name) |
Norman James | b714eb2 | 2015-10-26 17:12:57 -0500 | [diff] [blame] | 32 | |
Brad Bishop | c91b0bf | 2016-08-30 20:03:48 -0400 | [diff] [blame] | 33 | self.name = name |
Adriana Kobylak | 9baab4e | 2016-08-03 11:03:45 -0500 | [diff] [blame] | 34 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 35 | if "present" not in data: |
| 36 | data["present"] = "False" |
| 37 | if "fault" not in data: |
| 38 | data["fault"] = "False" |
| 39 | if "version" not in data: |
| 40 | data["version"] = "" |
Norman James | 0a5fbf8 | 2015-12-03 17:56:55 -0600 | [diff] [blame] | 41 | |
Brad Bishop | c91b0bf | 2016-08-30 20:03:48 -0400 | [diff] [blame] | 42 | self.SetMultiple(INTF_NAME, data) |
Adriana Kobylak | 9baab4e | 2016-08-03 11:03:45 -0500 | [diff] [blame] | 43 | |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 44 | # this will load properties from cache |
Brad Bishop | c91b0bf | 2016-08-30 20:03:48 -0400 | [diff] [blame] | 45 | PropertyCacher.load(name, INTF_NAME, self.properties) |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 46 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 47 | @dbus.service.method(INTF_NAME, in_signature="a{sv}", out_signature="") |
Brad Bishop | c91b0bf | 2016-08-30 20:03:48 -0400 | [diff] [blame] | 48 | def update(self, data): |
| 49 | self.SetMultiple(INTF_NAME, data) |
| 50 | PropertyCacher.save(self.name, INTF_NAME, self.properties) |
Norman James | 96da5c2 | 2015-10-15 10:17:06 -0500 | [diff] [blame] | 51 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 52 | @dbus.service.method(INTF_NAME, in_signature="s", out_signature="") |
Brad Bishop | c91b0bf | 2016-08-30 20:03:48 -0400 | [diff] [blame] | 53 | def setPresent(self, present): |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 54 | self.Set(INTF_NAME, "present", present) |
Brad Bishop | c91b0bf | 2016-08-30 20:03:48 -0400 | [diff] [blame] | 55 | PropertyCacher.save(self.name, INTF_NAME, self.properties) |
| 56 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 57 | @dbus.service.method(INTF_NAME, in_signature="s", out_signature="") |
Brad Bishop | c91b0bf | 2016-08-30 20:03:48 -0400 | [diff] [blame] | 58 | def setFault(self, fault): |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 59 | self.Set(INTF_NAME, "fault", fault) |
Brad Bishop | c91b0bf | 2016-08-30 20:03:48 -0400 | [diff] [blame] | 60 | PropertyCacher.save(self.name, INTF_NAME, self.properties) |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 61 | |
| 62 | |
Norman James | c9c92dc | 2015-11-17 08:59:07 -0600 | [diff] [blame] | 63 | def getVersion(): |
Brad Bishop | c91b0bf | 2016-08-30 20:03:48 -0400 | [diff] [blame] | 64 | version = "Error" |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 65 | with open("/etc/os-release", "r") as f: |
Brad Bishop | c91b0bf | 2016-08-30 20:03:48 -0400 | [diff] [blame] | 66 | for line in f: |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 67 | p = line.rstrip("\n") |
| 68 | parts = line.rstrip("\n").split("=") |
| 69 | if parts[0] == "VERSION_ID": |
Brad Bishop | c91b0bf | 2016-08-30 20:03:48 -0400 | [diff] [blame] | 70 | version = parts[1] |
| 71 | version = version.strip('"') |
| 72 | return version |
Norman James | c9c92dc | 2015-11-17 08:59:07 -0600 | [diff] [blame] | 73 | |
| 74 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 75 | if __name__ == "__main__": |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 76 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 77 | bus = get_dbus() |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 78 | mainloop = gobject.MainLoop() |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 79 | obj_parent = Inventory(bus, "/org/openbmc/inventory") |
| 80 | INVENTORY_FILE = os.path.join( |
| 81 | sys.prefix, "share", "inventory", "inventory.json" |
| 82 | ) |
Matt Spinler | 14c8286 | 2016-08-15 13:31:10 -0500 | [diff] [blame] | 83 | |
| 84 | if os.path.exists(INVENTORY_FILE): |
Brad Bishop | 3d13c28 | 2016-09-21 08:07:47 -0400 | [diff] [blame] | 85 | import json |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 86 | |
| 87 | with open(INVENTORY_FILE, "r") as f: |
Matt Spinler | 14c8286 | 2016-08-15 13:31:10 -0500 | [diff] [blame] | 88 | try: |
| 89 | inv = json.load(f) |
| 90 | except ValueError: |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 91 | print("Invalid JSON detected in " + INVENTORY_FILE) |
Matt Spinler | 14c8286 | 2016-08-15 13:31:10 -0500 | [diff] [blame] | 92 | else: |
| 93 | FRUS = inv |
Brad Bishop | 3d13c28 | 2016-09-21 08:07:47 -0400 | [diff] [blame] | 94 | else: |
| 95 | try: |
| 96 | import obmc_system_config as System |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 97 | |
Brad Bishop | 3d13c28 | 2016-09-21 08:07:47 -0400 | [diff] [blame] | 98 | FRUS = System.FRU_INSTANCES |
| 99 | except ImportError: |
| 100 | pass |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 101 | |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 102 | for f in list(FRUS.keys()): |
Brad Bishop | 7e5ec46 | 2016-09-21 09:09:48 -0400 | [diff] [blame] | 103 | import obmc.inventory |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 104 | |
Brad Bishop | 7e5ec46 | 2016-09-21 09:09:48 -0400 | [diff] [blame] | 105 | obj_path = f.replace("<inventory_root>", obmc.inventory.INVENTORY_ROOT) |
Brad Bishop | c91b0bf | 2016-08-30 20:03:48 -0400 | [diff] [blame] | 106 | obj = InventoryItem(bus, obj_path, FRUS[f]) |
| 107 | obj_parent.add(obj_path, obj) |
Norman James | c9c92dc | 2015-11-17 08:59:07 -0600 | [diff] [blame] | 108 | |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 109 | # TODO: this is a hack to update bmc inventory item with version |
| 110 | # should be done by flash object |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 111 | if FRUS[f]["fru_type"] == "BMC": |
Brad Bishop | c91b0bf | 2016-08-30 20:03:48 -0400 | [diff] [blame] | 112 | version = getVersion() |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 113 | obj.update({"version": version}) |
Norman James | c9c92dc | 2015-11-17 08:59:07 -0600 | [diff] [blame] | 114 | |
Brad Bishop | f0f3efe | 2016-06-29 23:20:24 -0400 | [diff] [blame] | 115 | obj_parent.unmask_signals() |
Brad Bishop | c91b0bf | 2016-08-30 20:03:48 -0400 | [diff] [blame] | 116 | name = dbus.service.BusName(DBUS_NAME, bus) |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 117 | print("Running Inventory Manager") |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 118 | mainloop.run() |
Brad Bishop | 5306675 | 2016-09-21 08:48:04 -0400 | [diff] [blame] | 119 | |
| 120 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |