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