Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import os |
| 4 | import sys |
| 5 | import gobject |
| 6 | import dbus |
| 7 | import dbus.service |
| 8 | import dbus.mainloop.glib |
| 9 | import cPickle |
| 10 | import json |
| 11 | |
| 12 | if (len(sys.argv) < 2): |
| 13 | print "Usage: inventory_items.py [system name]" |
| 14 | exit(1) |
| 15 | System = __import__(sys.argv[1]) |
| 16 | import Openbmc |
| 17 | |
| 18 | |
| 19 | INTF_NAME = 'org.openbmc.InventoryItem' |
| 20 | DBUS_NAME = 'org.openbmc.managers.Inventory' |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 21 | ENUM_INTF = 'org.openbmc.Object.Enumerate' |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 22 | |
| 23 | FRUS = System.FRU_INSTANCES |
| 24 | FRU_PATH = System.FRU_PATH |
| 25 | |
| 26 | class Inventory(dbus.service.Object): |
| 27 | def __init__(self,bus,name): |
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 28 | global FRU_PATH |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 29 | dbus.service.Object.__init__(self,bus,name) |
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 30 | if not os.path.exists(FRU_PATH): |
| 31 | os.makedirs(FRU_PATH) |
| 32 | |
| 33 | |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 34 | self.objects = [ ] |
| 35 | |
| 36 | def addItem(self,item): |
| 37 | self.objects.append(item) |
| 38 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 39 | @dbus.service.method(ENUM_INTF, |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 40 | in_signature='', out_signature='a{sa{sv}}') |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 41 | def enumerate(self): |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 42 | tmp_obj = {} |
| 43 | for item in self.objects: |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 44 | tmp_obj[str(item.name)]=item.GetAll(INTF_NAME) |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 45 | return tmp_obj |
| 46 | |
| 47 | |
| 48 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 49 | class InventoryItem(Openbmc.DbusProperties): |
| 50 | def __init__(self,bus,name): |
| 51 | Openbmc.DbusProperties.__init__(self) |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 52 | dbus.service.Object.__init__(self,bus,name) |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 53 | self.name = name |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 54 | self.cache = True |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 55 | self.Set(INTF_NAME,'is_fru',False) |
| 56 | self.Set(INTF_NAME,'fru_type',0) |
| 57 | self.Set(INTF_NAME,'present',"INACTIVE") |
| 58 | self.Set(INTF_NAME,'fault',"NONE") |
| 59 | |
| 60 | |
| 61 | |
| 62 | @dbus.service.method(INTF_NAME, |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 63 | in_signature='a{sv}', out_signature='') |
| 64 | def update(self,data): |
| 65 | ## translate dbus data into basic data types |
| 66 | for k in data.keys(): |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 67 | self.setField(k,data[k]) |
| 68 | |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 69 | self.saveToCache() |
| 70 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 71 | @dbus.service.method(INTF_NAME, |
Norman James | 96da5c2 | 2015-10-15 10:17:06 -0500 | [diff] [blame] | 72 | in_signature='s', out_signature='') |
| 73 | def setPresent(self,present): |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 74 | self.setField('present',present) |
Norman James | 96da5c2 | 2015-10-15 10:17:06 -0500 | [diff] [blame] | 75 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 76 | @dbus.service.method(INTF_NAME, |
Norman James | 96da5c2 | 2015-10-15 10:17:06 -0500 | [diff] [blame] | 77 | in_signature='s', out_signature='') |
| 78 | def setFault(self,fault): |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 79 | self.setField('fault',fault) |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 80 | |
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame] | 81 | def setField(self,field,value): |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 82 | f = str(field) |
| 83 | d = Openbmc.DbusVariable(f,value) |
| 84 | self.Set(INTF_NAME,f,d.getBaseValue()) |
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame] | 85 | |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 86 | def isCached(self): |
| 87 | return self.cache |
| 88 | |
| 89 | def getCacheFilename(self): |
| 90 | global FRU_PATH |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 91 | name = self.name.replace('/','.') |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 92 | filename = FRU_PATH+name[1:]+".fru" |
| 93 | return filename |
| 94 | |
| 95 | def saveToCache(self): |
| 96 | if (self.isCached() == False): |
| 97 | return |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 98 | print "Caching: "+self.name |
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame] | 99 | try: |
| 100 | output = open(self.getCacheFilename(), 'wb') |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 101 | ## save properties |
| 102 | cPickle.dump(self.properties[INTF_NAME],output) |
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame] | 103 | except Exception as e: |
| 104 | print "ERROR: "+str(e) |
| 105 | finally: |
| 106 | output.close() |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 107 | |
| 108 | def loadFromCache(self): |
| 109 | if (self.isCached() == False): |
| 110 | return; |
| 111 | ## overlay with pickled data |
| 112 | filename=self.getCacheFilename() |
| 113 | if (os.path.isfile(filename)): |
| 114 | print "Loading from cache: "+filename |
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame] | 115 | try: |
| 116 | p = open(filename, 'rb') |
| 117 | data2 = cPickle.load(p) |
| 118 | for k in data2.keys(): |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 119 | self.setField(k,data2[k]) |
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame] | 120 | except Exception as e: |
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 121 | print "No cache file found: " +str(e) |
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame] | 122 | finally: |
| 123 | p.close() |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 124 | |
| 125 | |
| 126 | if __name__ == '__main__': |
| 127 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 128 | bus = Openbmc.getDBus() |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 129 | name = dbus.service.BusName(DBUS_NAME,bus) |
| 130 | mainloop = gobject.MainLoop() |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 131 | obj_parent = Inventory(bus, '/org/openbmc/inventory') |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 132 | |
| 133 | for f in FRUS.keys(): |
| 134 | obj_path=f.replace("<inventory_root>",System.INVENTORY_ROOT) |
| 135 | obj = InventoryItem(bus,obj_path) |
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame] | 136 | obj.setField('is_fru',FRUS[f]['is_fru']) |
| 137 | obj.setField('fru_type',FRUS[f]['fru_type']) |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 138 | obj.loadFromCache(); |
| 139 | obj_parent.addItem(obj) |
| 140 | |
| 141 | print "Running Inventory Manager" |
| 142 | mainloop.run() |
| 143 | |