blob: 168e3d6adf966f9a9e2aaf7cfa3c237a0f594d10 [file] [log] [blame]
Brad Bishopc91b0bf2016-08-30 20:03:48 -04001#!/usr/bin/env python
Norman James19e45912015-10-04 20:19:41 -05002
Brad Bishop0f608de2016-09-08 22:48:22 -04003import os
4import sys
CamVan Nguyend65b2d52018-02-27 15:14:41 -06005# TODO: openbmc/openbmc#2994 remove python 2 support
6try: # python 2
7 import gobject
8except ImportError: # python 3
9 from gi.repository import GObject as gobject
Norman James19e45912015-10-04 20:19:41 -050010import dbus
11import dbus.service
12import dbus.mainloop.glib
Brad Bishopee1b1542016-05-12 16:55:00 -040013import obmc.dbuslib.propertycacher as PropertyCacher
Brad Bishop84e73b52016-05-12 15:57:52 -040014from obmc.dbuslib.bindings import get_dbus, DbusProperties, DbusObjectManager
Brad Bishop985b2a52016-08-30 20:06:23 -040015
Norman James19e45912015-10-04 20:19:41 -050016
17INTF_NAME = 'org.openbmc.InventoryItem'
Norman James323ed972015-12-09 09:06:37 -060018DBUS_NAME = 'org.openbmc.Inventory'
Brad Bishop985b2a52016-08-30 20:06:23 -040019
Brad Bishop3d13c282016-09-21 08:07:47 -040020FRUS = {}
Norman James19e45912015-10-04 20:19:41 -050021
Brad Bishopc91b0bf2016-08-30 20:03:48 -040022
23class Inventory(DbusProperties, DbusObjectManager):
24 def __init__(self, bus, name):
Brad Bishopf47f5fa2016-09-08 22:29:01 -040025 super(Inventory, self).__init__(
26 conn=bus,
27 object_path=name)
Norman James19e45912015-10-04 20:19:41 -050028
29
Brad Bishop84e73b52016-05-12 15:57:52 -040030class InventoryItem(DbusProperties):
Brad Bishopc91b0bf2016-08-30 20:03:48 -040031 def __init__(self, bus, name, data):
Brad Bishopf47f5fa2016-09-08 22:29:01 -040032 super(InventoryItem, self).__init__(
33 conn=bus,
34 object_path=name)
Norman Jamesb714eb22015-10-26 17:12:57 -050035
Brad Bishopc91b0bf2016-08-30 20:03:48 -040036 self.name = name
Adriana Kobylak9baab4e2016-08-03 11:03:45 -050037
Brad Bishopc91b0bf2016-08-30 20:03:48 -040038 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 James0a5fbf82015-12-03 17:56:55 -060044
Brad Bishopc91b0bf2016-08-30 20:03:48 -040045 self.SetMultiple(INTF_NAME, data)
Adriana Kobylak9baab4e2016-08-03 11:03:45 -050046
Adriana Kobylak24341f92018-01-26 15:07:23 -060047 # this will load properties from cache
Brad Bishopc91b0bf2016-08-30 20:03:48 -040048 PropertyCacher.load(name, INTF_NAME, self.properties)
Norman James19e45912015-10-04 20:19:41 -050049
Brad Bishopc91b0bf2016-08-30 20:03:48 -040050 @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 James96da5c22015-10-15 10:17:06 -050055
Brad Bishopc91b0bf2016-08-30 20:03:48 -040056 @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 James19e45912015-10-04 20:19:41 -050067
68
Norman Jamesc9c92dc2015-11-17 08:59:07 -060069def getVersion():
Brad Bishopc91b0bf2016-08-30 20:03:48 -040070 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 Jamesc9c92dc2015-11-17 08:59:07 -060079
80
Norman James19e45912015-10-04 20:19:41 -050081if __name__ == '__main__':
82 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -040083 bus = get_dbus()
Norman James19e45912015-10-04 20:19:41 -050084 mainloop = gobject.MainLoop()
Norman Jamesa3e47c42015-10-18 14:43:10 -050085 obj_parent = Inventory(bus, '/org/openbmc/inventory')
Matt Spinler14c82862016-08-15 13:31:10 -050086 INVENTORY_FILE = os.path.join(sys.prefix, 'share',
87 'inventory', 'inventory.json')
88
89 if os.path.exists(INVENTORY_FILE):
Brad Bishop3d13c282016-09-21 08:07:47 -040090 import json
Matt Spinler14c82862016-08-15 13:31:10 -050091 with open(INVENTORY_FILE, 'r') as f:
92 try:
93 inv = json.load(f)
94 except ValueError:
CamVan Nguyend65b2d52018-02-27 15:14:41 -060095 print("Invalid JSON detected in " + INVENTORY_FILE)
Matt Spinler14c82862016-08-15 13:31:10 -050096 else:
97 FRUS = inv
Brad Bishop3d13c282016-09-21 08:07:47 -040098 else:
99 try:
100 import obmc_system_config as System
101 FRUS = System.FRU_INSTANCES
102 except ImportError:
103 pass
Norman James19e45912015-10-04 20:19:41 -0500104
CamVan Nguyend65b2d52018-02-27 15:14:41 -0600105 for f in list(FRUS.keys()):
Brad Bishop7e5ec462016-09-21 09:09:48 -0400106 import obmc.inventory
107 obj_path = f.replace("<inventory_root>", obmc.inventory.INVENTORY_ROOT)
Brad Bishopc91b0bf2016-08-30 20:03:48 -0400108 obj = InventoryItem(bus, obj_path, FRUS[f])
109 obj_parent.add(obj_path, obj)
Norman Jamesc9c92dc2015-11-17 08:59:07 -0600110
Adriana Kobylak24341f92018-01-26 15:07:23 -0600111 # TODO: this is a hack to update bmc inventory item with version
112 # should be done by flash object
Brad Bishopc91b0bf2016-08-30 20:03:48 -0400113 if (FRUS[f]['fru_type'] == "BMC"):
114 version = getVersion()
115 obj.update({'version': version})
Norman Jamesc9c92dc2015-11-17 08:59:07 -0600116
Brad Bishopf0f3efe2016-06-29 23:20:24 -0400117 obj_parent.unmask_signals()
Brad Bishopc91b0bf2016-08-30 20:03:48 -0400118 name = dbus.service.BusName(DBUS_NAME, bus)
CamVan Nguyend65b2d52018-02-27 15:14:41 -0600119 print("Running Inventory Manager")
Norman James19e45912015-10-04 20:19:41 -0500120 mainloop.run()
Brad Bishop53066752016-09-21 08:48:04 -0400121
122# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4