blob: 16ae6d0bd73c7515a02dbf4104527faf629e84f4 [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
12try:
13 import obmc_system_config as System
14 have_system = True
15except ImportError:
16 have_system = False
Norman James19e45912015-10-04 20:19:41 -050017
18INTF_NAME = 'org.openbmc.InventoryItem'
Norman James323ed972015-12-09 09:06:37 -060019DBUS_NAME = 'org.openbmc.Inventory'
Brad Bishop985b2a52016-08-30 20:06:23 -040020
21if have_system:
22 FRUS = System.FRU_INSTANCES
23else:
24 FRUS = {}
Norman James19e45912015-10-04 20:19:41 -050025
Brad Bishopc91b0bf2016-08-30 20:03:48 -040026
27class Inventory(DbusProperties, DbusObjectManager):
28 def __init__(self, bus, name):
Brad Bishopf47f5fa2016-09-08 22:29:01 -040029 super(Inventory, self).__init__(
30 conn=bus,
31 object_path=name)
Norman James19e45912015-10-04 20:19:41 -050032
33
Brad Bishop84e73b52016-05-12 15:57:52 -040034class InventoryItem(DbusProperties):
Brad Bishopc91b0bf2016-08-30 20:03:48 -040035 def __init__(self, bus, name, data):
Brad Bishopf47f5fa2016-09-08 22:29:01 -040036 super(InventoryItem, self).__init__(
37 conn=bus,
38 object_path=name)
Norman Jamesb714eb22015-10-26 17:12:57 -050039
Brad Bishopc91b0bf2016-08-30 20:03:48 -040040 self.name = name
Adriana Kobylak9baab4e2016-08-03 11:03:45 -050041
Brad Bishopc91b0bf2016-08-30 20:03:48 -040042 if 'present' not in data:
43 data['present'] = 'False'
44 if 'fault' not in data:
45 data['fault'] = 'False'
46 if 'version' not in data:
47 data['version'] = ''
Norman James0a5fbf82015-12-03 17:56:55 -060048
Brad Bishopc91b0bf2016-08-30 20:03:48 -040049 self.SetMultiple(INTF_NAME, data)
Adriana Kobylak9baab4e2016-08-03 11:03:45 -050050
Brad Bishopc91b0bf2016-08-30 20:03:48 -040051 ## this will load properties from cache
52 PropertyCacher.load(name, INTF_NAME, self.properties)
Norman James19e45912015-10-04 20:19:41 -050053
Brad Bishopc91b0bf2016-08-30 20:03:48 -040054 @dbus.service.method(
55 INTF_NAME, in_signature='a{sv}', out_signature='')
56 def update(self, data):
57 self.SetMultiple(INTF_NAME, data)
58 PropertyCacher.save(self.name, INTF_NAME, self.properties)
Norman James96da5c22015-10-15 10:17:06 -050059
Brad Bishopc91b0bf2016-08-30 20:03:48 -040060 @dbus.service.method(
61 INTF_NAME, in_signature='s', out_signature='')
62 def setPresent(self, present):
63 self.Set(INTF_NAME, 'present', present)
64 PropertyCacher.save(self.name, INTF_NAME, self.properties)
65
66 @dbus.service.method(
67 INTF_NAME, in_signature='s', out_signature='')
68 def setFault(self, fault):
69 self.Set(INTF_NAME, 'fault', fault)
70 PropertyCacher.save(self.name, INTF_NAME, self.properties)
Norman James19e45912015-10-04 20:19:41 -050071
72
Norman Jamesc9c92dc2015-11-17 08:59:07 -060073def getVersion():
Brad Bishopc91b0bf2016-08-30 20:03:48 -040074 version = "Error"
75 with open('/etc/os-release', 'r') as f:
76 for line in f:
77 p = line.rstrip('\n')
78 parts = line.rstrip('\n').split('=')
79 if (parts[0] == "VERSION_ID"):
80 version = parts[1]
81 version = version.strip('"')
82 return version
Norman Jamesc9c92dc2015-11-17 08:59:07 -060083
84
Norman James19e45912015-10-04 20:19:41 -050085if __name__ == '__main__':
86 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -040087 bus = get_dbus()
Norman James19e45912015-10-04 20:19:41 -050088 mainloop = gobject.MainLoop()
Norman Jamesa3e47c42015-10-18 14:43:10 -050089 obj_parent = Inventory(bus, '/org/openbmc/inventory')
Matt Spinler14c82862016-08-15 13:31:10 -050090 INVENTORY_FILE = os.path.join(sys.prefix, 'share',
91 'inventory', 'inventory.json')
92
93 if os.path.exists(INVENTORY_FILE):
94 with open(INVENTORY_FILE, 'r') as f:
95 try:
96 inv = json.load(f)
97 except ValueError:
98 print "Invalid JSON detected in " + INVENTORY_FILE
99 else:
100 FRUS = inv
Norman James19e45912015-10-04 20:19:41 -0500101
102 for f in FRUS.keys():
Brad Bishopc91b0bf2016-08-30 20:03:48 -0400103 obj_path = f.replace("<inventory_root>", System.INVENTORY_ROOT)
104 obj = InventoryItem(bus, obj_path, FRUS[f])
105 obj_parent.add(obj_path, obj)
Norman Jamesc9c92dc2015-11-17 08:59:07 -0600106
Brad Bishopc91b0bf2016-08-30 20:03:48 -0400107 ## TODO: this is a hack to update bmc inventory item with version
108 ## should be done by flash object
109 if (FRUS[f]['fru_type'] == "BMC"):
110 version = getVersion()
111 obj.update({'version': version})
Norman Jamesc9c92dc2015-11-17 08:59:07 -0600112
Brad Bishopf0f3efe2016-06-29 23:20:24 -0400113 obj_parent.unmask_signals()
Brad Bishopc91b0bf2016-08-30 20:03:48 -0400114 name = dbus.service.BusName(DBUS_NAME, bus)
Norman James19e45912015-10-04 20:19:41 -0500115 print "Running Inventory Manager"
116 mainloop.run()