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