blob: c815b37ed0861ea3b4096a144bcedeba0067f09a [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
Brad Bishopf327ddd2016-09-13 22:37:35 -04006import json
Norman James19e45912015-10-04 20:19:41 -05007import dbus
8import dbus.service
9import dbus.mainloop.glib
Brad Bishopee1b1542016-05-12 16:55:00 -040010import obmc.dbuslib.propertycacher as PropertyCacher
Brad Bishop84e73b52016-05-12 15:57:52 -040011from obmc.dbuslib.bindings import get_dbus, DbusProperties, DbusObjectManager
Brad Bishop985b2a52016-08-30 20:06:23 -040012
13try:
14 import obmc_system_config as System
15 have_system = True
16except ImportError:
17 have_system = False
Norman James19e45912015-10-04 20:19:41 -050018
19INTF_NAME = 'org.openbmc.InventoryItem'
Norman James323ed972015-12-09 09:06:37 -060020DBUS_NAME = 'org.openbmc.Inventory'
Brad Bishop985b2a52016-08-30 20:06:23 -040021
22if have_system:
23 FRUS = System.FRU_INSTANCES
24else:
25 FRUS = {}
Norman James19e45912015-10-04 20:19:41 -050026
Brad Bishopc91b0bf2016-08-30 20:03:48 -040027
28class Inventory(DbusProperties, DbusObjectManager):
29 def __init__(self, bus, name):
Brad Bishopf47f5fa2016-09-08 22:29:01 -040030 super(Inventory, self).__init__(
31 conn=bus,
32 object_path=name)
Norman James19e45912015-10-04 20:19:41 -050033
34
Brad Bishop84e73b52016-05-12 15:57:52 -040035class InventoryItem(DbusProperties):
Brad Bishopc91b0bf2016-08-30 20:03:48 -040036 def __init__(self, bus, name, data):
Brad Bishopf47f5fa2016-09-08 22:29:01 -040037 super(InventoryItem, self).__init__(
38 conn=bus,
39 object_path=name)
Norman Jamesb714eb22015-10-26 17:12:57 -050040
Brad Bishopc91b0bf2016-08-30 20:03:48 -040041 self.name = name
Adriana Kobylak9baab4e2016-08-03 11:03:45 -050042
Brad Bishopc91b0bf2016-08-30 20:03:48 -040043 if 'present' not in data:
44 data['present'] = 'False'
45 if 'fault' not in data:
46 data['fault'] = 'False'
47 if 'version' not in data:
48 data['version'] = ''
Norman James0a5fbf82015-12-03 17:56:55 -060049
Brad Bishopc91b0bf2016-08-30 20:03:48 -040050 self.SetMultiple(INTF_NAME, data)
Adriana Kobylak9baab4e2016-08-03 11:03:45 -050051
Brad Bishopc91b0bf2016-08-30 20:03:48 -040052 ## this will load properties from cache
53 PropertyCacher.load(name, INTF_NAME, self.properties)
Norman James19e45912015-10-04 20:19:41 -050054
Brad Bishopc91b0bf2016-08-30 20:03:48 -040055 @dbus.service.method(
56 INTF_NAME, in_signature='a{sv}', out_signature='')
57 def update(self, data):
58 self.SetMultiple(INTF_NAME, data)
59 PropertyCacher.save(self.name, INTF_NAME, self.properties)
Norman James96da5c22015-10-15 10:17:06 -050060
Brad Bishopc91b0bf2016-08-30 20:03:48 -040061 @dbus.service.method(
62 INTF_NAME, in_signature='s', out_signature='')
63 def setPresent(self, present):
64 self.Set(INTF_NAME, 'present', present)
65 PropertyCacher.save(self.name, INTF_NAME, self.properties)
66
67 @dbus.service.method(
68 INTF_NAME, in_signature='s', out_signature='')
69 def setFault(self, fault):
70 self.Set(INTF_NAME, 'fault', fault)
71 PropertyCacher.save(self.name, INTF_NAME, self.properties)
Norman James19e45912015-10-04 20:19:41 -050072
73
Norman Jamesc9c92dc2015-11-17 08:59:07 -060074def getVersion():
Brad Bishopc91b0bf2016-08-30 20:03:48 -040075 version = "Error"
76 with open('/etc/os-release', 'r') as f:
77 for line in f:
78 p = line.rstrip('\n')
79 parts = line.rstrip('\n').split('=')
80 if (parts[0] == "VERSION_ID"):
81 version = parts[1]
82 version = version.strip('"')
83 return version
Norman Jamesc9c92dc2015-11-17 08:59:07 -060084
85
Norman James19e45912015-10-04 20:19:41 -050086if __name__ == '__main__':
87 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -040088 bus = get_dbus()
Norman James19e45912015-10-04 20:19:41 -050089 mainloop = gobject.MainLoop()
Norman Jamesa3e47c42015-10-18 14:43:10 -050090 obj_parent = Inventory(bus, '/org/openbmc/inventory')
Matt Spinler14c82862016-08-15 13:31:10 -050091 INVENTORY_FILE = os.path.join(sys.prefix, 'share',
92 'inventory', 'inventory.json')
93
94 if os.path.exists(INVENTORY_FILE):
95 with open(INVENTORY_FILE, 'r') as f:
96 try:
97 inv = json.load(f)
98 except ValueError:
99 print "Invalid JSON detected in " + INVENTORY_FILE
100 else:
101 FRUS = inv
Norman James19e45912015-10-04 20:19:41 -0500102
103 for f in FRUS.keys():
Brad Bishopc91b0bf2016-08-30 20:03:48 -0400104 obj_path = f.replace("<inventory_root>", System.INVENTORY_ROOT)
105 obj = InventoryItem(bus, obj_path, FRUS[f])
106 obj_parent.add(obj_path, obj)
Norman Jamesc9c92dc2015-11-17 08:59:07 -0600107
Brad Bishopc91b0bf2016-08-30 20:03:48 -0400108 ## TODO: this is a hack to update bmc inventory item with version
109 ## should be done by flash object
110 if (FRUS[f]['fru_type'] == "BMC"):
111 version = getVersion()
112 obj.update({'version': version})
Norman Jamesc9c92dc2015-11-17 08:59:07 -0600113
Brad Bishopf0f3efe2016-06-29 23:20:24 -0400114 obj_parent.unmask_signals()
Brad Bishopc91b0bf2016-08-30 20:03:48 -0400115 name = dbus.service.BusName(DBUS_NAME, bus)
Norman James19e45912015-10-04 20:19:41 -0500116 print "Running Inventory Manager"
117 mainloop.run()