blob: 82a1305f98db5001bcc722bf7307ad2851620380 [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
Norman James19e45912015-10-04 20:19:41 -05005import gobject
6import dbus
7import dbus.service
8import dbus.mainloop.glib
Brad Bishopee1b1542016-05-12 16:55:00 -04009import obmc.dbuslib.propertycacher as PropertyCacher
Brad Bishop84e73b52016-05-12 15:57:52 -040010from obmc.dbuslib.bindings import get_dbus, DbusProperties, DbusObjectManager
Brad Bishop985b2a52016-08-30 20:06:23 -040011
Norman James19e45912015-10-04 20:19:41 -050012
13INTF_NAME = 'org.openbmc.InventoryItem'
Norman James323ed972015-12-09 09:06:37 -060014DBUS_NAME = 'org.openbmc.Inventory'
Brad Bishop985b2a52016-08-30 20:06:23 -040015
Brad Bishop3d13c282016-09-21 08:07:47 -040016FRUS = {}
Norman James19e45912015-10-04 20:19:41 -050017
Brad Bishopc91b0bf2016-08-30 20:03:48 -040018
19class Inventory(DbusProperties, DbusObjectManager):
20 def __init__(self, bus, name):
Brad Bishopf47f5fa2016-09-08 22:29:01 -040021 super(Inventory, self).__init__(
22 conn=bus,
23 object_path=name)
Norman James19e45912015-10-04 20:19:41 -050024
25
Brad Bishop84e73b52016-05-12 15:57:52 -040026class InventoryItem(DbusProperties):
Brad Bishopc91b0bf2016-08-30 20:03:48 -040027 def __init__(self, bus, name, data):
Brad Bishopf47f5fa2016-09-08 22:29:01 -040028 super(InventoryItem, self).__init__(
29 conn=bus,
30 object_path=name)
Norman Jamesb714eb22015-10-26 17:12:57 -050031
Brad Bishopc91b0bf2016-08-30 20:03:48 -040032 self.name = name
Adriana Kobylak9baab4e2016-08-03 11:03:45 -050033
Brad Bishopc91b0bf2016-08-30 20:03:48 -040034 if 'present' not in data:
35 data['present'] = 'False'
36 if 'fault' not in data:
37 data['fault'] = 'False'
38 if 'version' not in data:
39 data['version'] = ''
Norman James0a5fbf82015-12-03 17:56:55 -060040
Brad Bishopc91b0bf2016-08-30 20:03:48 -040041 self.SetMultiple(INTF_NAME, data)
Adriana Kobylak9baab4e2016-08-03 11:03:45 -050042
Brad Bishopc91b0bf2016-08-30 20:03:48 -040043 ## this will load properties from cache
44 PropertyCacher.load(name, INTF_NAME, self.properties)
Norman James19e45912015-10-04 20:19:41 -050045
Brad Bishopc91b0bf2016-08-30 20:03:48 -040046 @dbus.service.method(
47 INTF_NAME, in_signature='a{sv}', out_signature='')
48 def update(self, data):
49 self.SetMultiple(INTF_NAME, data)
50 PropertyCacher.save(self.name, INTF_NAME, self.properties)
Norman James96da5c22015-10-15 10:17:06 -050051
Brad Bishopc91b0bf2016-08-30 20:03:48 -040052 @dbus.service.method(
53 INTF_NAME, in_signature='s', out_signature='')
54 def setPresent(self, present):
55 self.Set(INTF_NAME, 'present', present)
56 PropertyCacher.save(self.name, INTF_NAME, self.properties)
57
58 @dbus.service.method(
59 INTF_NAME, in_signature='s', out_signature='')
60 def setFault(self, fault):
61 self.Set(INTF_NAME, 'fault', fault)
62 PropertyCacher.save(self.name, INTF_NAME, self.properties)
Norman James19e45912015-10-04 20:19:41 -050063
64
Norman Jamesc9c92dc2015-11-17 08:59:07 -060065def getVersion():
Brad Bishopc91b0bf2016-08-30 20:03:48 -040066 version = "Error"
67 with open('/etc/os-release', 'r') as f:
68 for line in f:
69 p = line.rstrip('\n')
70 parts = line.rstrip('\n').split('=')
71 if (parts[0] == "VERSION_ID"):
72 version = parts[1]
73 version = version.strip('"')
74 return version
Norman Jamesc9c92dc2015-11-17 08:59:07 -060075
76
Norman James19e45912015-10-04 20:19:41 -050077if __name__ == '__main__':
78 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -040079 bus = get_dbus()
Norman James19e45912015-10-04 20:19:41 -050080 mainloop = gobject.MainLoop()
Norman Jamesa3e47c42015-10-18 14:43:10 -050081 obj_parent = Inventory(bus, '/org/openbmc/inventory')
Matt Spinler14c82862016-08-15 13:31:10 -050082 INVENTORY_FILE = os.path.join(sys.prefix, 'share',
83 'inventory', 'inventory.json')
84
85 if os.path.exists(INVENTORY_FILE):
Brad Bishop3d13c282016-09-21 08:07:47 -040086 import json
Matt Spinler14c82862016-08-15 13:31:10 -050087 with open(INVENTORY_FILE, 'r') as f:
88 try:
89 inv = json.load(f)
90 except ValueError:
91 print "Invalid JSON detected in " + INVENTORY_FILE
92 else:
93 FRUS = inv
Brad Bishop3d13c282016-09-21 08:07:47 -040094 else:
95 try:
96 import obmc_system_config as System
97 FRUS = System.FRU_INSTANCES
98 except ImportError:
99 pass
Norman James19e45912015-10-04 20:19:41 -0500100
101 for f in FRUS.keys():
Brad Bishopc91b0bf2016-08-30 20:03:48 -0400102 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 Jamesc9c92dc2015-11-17 08:59:07 -0600105
Brad Bishopc91b0bf2016-08-30 20:03:48 -0400106 ## 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 Jamesc9c92dc2015-11-17 08:59:07 -0600111
Brad Bishopf0f3efe2016-06-29 23:20:24 -0400112 obj_parent.unmask_signals()
Brad Bishopc91b0bf2016-08-30 20:03:48 -0400113 name = dbus.service.BusName(DBUS_NAME, bus)
Norman James19e45912015-10-04 20:19:41 -0500114 print "Running Inventory Manager"
115 mainloop.run()