1#!/usr/bin/env python 2 3import os 4import sys 5import gobject 6import json 7import dbus 8import dbus.service 9import dbus.mainloop.glib 10import obmc.dbuslib.propertycacher as PropertyCacher 11from obmc.dbuslib.bindings import get_dbus, DbusProperties, DbusObjectManager 12 13try: 14 import obmc_system_config as System 15 have_system = True 16except ImportError: 17 have_system = False 18 19INTF_NAME = 'org.openbmc.InventoryItem' 20DBUS_NAME = 'org.openbmc.Inventory' 21 22if have_system: 23 FRUS = System.FRU_INSTANCES 24else: 25 FRUS = {} 26 27 28class Inventory(DbusProperties, DbusObjectManager): 29 def __init__(self, bus, name): 30 super(Inventory, self).__init__( 31 conn=bus, 32 object_path=name) 33 34 35class InventoryItem(DbusProperties): 36 def __init__(self, bus, name, data): 37 super(InventoryItem, self).__init__( 38 conn=bus, 39 object_path=name) 40 41 self.name = name 42 43 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'] = '' 49 50 self.SetMultiple(INTF_NAME, data) 51 52 ## this will load properties from cache 53 PropertyCacher.load(name, INTF_NAME, self.properties) 54 55 @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) 60 61 @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) 72 73 74def getVersion(): 75 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 84 85 86if __name__ == '__main__': 87 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) 88 bus = get_dbus() 89 mainloop = gobject.MainLoop() 90 obj_parent = Inventory(bus, '/org/openbmc/inventory') 91 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 102 103 for f in FRUS.keys(): 104 obj_path = f.replace("<inventory_root>", System.INVENTORY_ROOT) 105 obj = InventoryItem(bus, obj_path, FRUS[f]) 106 obj_parent.add(obj_path, obj) 107 108 ## 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}) 113 114 obj_parent.unmask_signals() 115 name = dbus.service.BusName(DBUS_NAME, bus) 116 print "Running Inventory Manager" 117 mainloop.run() 118