Norman James | 42c1be8 | 2015-10-22 14:34:26 -0500 | [diff] [blame] | 1 | #!/usr/bin/python -u |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 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 |
Norman James | ab777b1 | 2015-10-29 06:19:18 -0500 | [diff] [blame] | 11 | import PropertyCacher |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 12 | |
| 13 | if (len(sys.argv) < 2): |
| 14 | print "Usage: inventory_items.py [system name]" |
| 15 | exit(1) |
| 16 | System = __import__(sys.argv[1]) |
| 17 | import Openbmc |
| 18 | |
| 19 | |
| 20 | INTF_NAME = 'org.openbmc.InventoryItem' |
| 21 | DBUS_NAME = 'org.openbmc.managers.Inventory' |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 22 | ENUM_INTF = 'org.openbmc.Object.Enumerate' |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 23 | |
| 24 | FRUS = System.FRU_INSTANCES |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 25 | |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 26 | class Inventory(Openbmc.DbusProperties): |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 27 | def __init__(self,bus,name): |
| 28 | dbus.service.Object.__init__(self,bus,name) |
| 29 | self.objects = [ ] |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 30 | self.ObjectAdded(name,ENUM_INTF) |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 31 | |
| 32 | def addItem(self,item): |
| 33 | self.objects.append(item) |
| 34 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 35 | @dbus.service.method(ENUM_INTF, |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 36 | in_signature='', out_signature='a{sa{sv}}') |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 37 | def enumerate(self): |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 38 | tmp_obj = {} |
| 39 | for item in self.objects: |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 40 | tmp_obj[str(item.name)]=item.GetAll(INTF_NAME) |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 41 | return tmp_obj |
| 42 | |
| 43 | |
| 44 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 45 | class InventoryItem(Openbmc.DbusProperties): |
Norman James | b714eb2 | 2015-10-26 17:12:57 -0500 | [diff] [blame] | 46 | def __init__(self,bus,name,is_fru,fru_type): |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 47 | Openbmc.DbusProperties.__init__(self) |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 48 | dbus.service.Object.__init__(self,bus,name) |
Norman James | b714eb2 | 2015-10-26 17:12:57 -0500 | [diff] [blame] | 49 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 50 | self.name = name |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 51 | |
Norman James | ab777b1 | 2015-10-29 06:19:18 -0500 | [diff] [blame] | 52 | ## this will load properties from cache |
| 53 | PropertyCacher.load(name,INTF_NAME,self.properties) |
Norman James | c9c92dc | 2015-11-17 08:59:07 -0600 | [diff] [blame] | 54 | data = {'is_fru': is_fru, 'fru_type': fru_type, 'present': 'Inactive', 'fault': 'None', 'version': 'None' } |
Norman James | b714eb2 | 2015-10-26 17:12:57 -0500 | [diff] [blame] | 55 | self.SetMultiple(INTF_NAME,data) |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 56 | self.ObjectAdded(name,INTF_NAME) |
Norman James | ab777b1 | 2015-10-29 06:19:18 -0500 | [diff] [blame] | 57 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 58 | |
| 59 | @dbus.service.method(INTF_NAME, |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 60 | in_signature='a{sv}', out_signature='') |
| 61 | def update(self,data): |
Norman James | b714eb2 | 2015-10-26 17:12:57 -0500 | [diff] [blame] | 62 | self.SetMultiple(INTF_NAME,data) |
Norman James | ab777b1 | 2015-10-29 06:19:18 -0500 | [diff] [blame] | 63 | PropertyCacher.save(self.name,INTF_NAME,self.properties) |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 64 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 65 | @dbus.service.method(INTF_NAME, |
Norman James | 96da5c2 | 2015-10-15 10:17:06 -0500 | [diff] [blame] | 66 | in_signature='s', out_signature='') |
| 67 | def setPresent(self,present): |
Norman James | b714eb2 | 2015-10-26 17:12:57 -0500 | [diff] [blame] | 68 | self.Set(INTF_NAME,'present',present) |
Norman James | 96da5c2 | 2015-10-15 10:17:06 -0500 | [diff] [blame] | 69 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 70 | @dbus.service.method(INTF_NAME, |
Norman James | 96da5c2 | 2015-10-15 10:17:06 -0500 | [diff] [blame] | 71 | in_signature='s', out_signature='') |
| 72 | def setFault(self,fault): |
Norman James | b714eb2 | 2015-10-26 17:12:57 -0500 | [diff] [blame] | 73 | self.Set(INTF_NAME,'fault',fault) |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 74 | |
| 75 | |
Norman James | c9c92dc | 2015-11-17 08:59:07 -0600 | [diff] [blame] | 76 | def getVersion(): |
| 77 | version = "Error" |
| 78 | with open('/etc/os-release', 'r') as f: |
| 79 | for line in f: |
| 80 | p = line.rstrip('\n') |
| 81 | parts = line.rstrip('\n').split('=') |
| 82 | if (parts[0] == "BUILD_ID"): |
| 83 | version = parts[1] |
| 84 | return version |
| 85 | |
| 86 | |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 87 | if __name__ == '__main__': |
| 88 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 89 | bus = Openbmc.getDBus() |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 90 | name = dbus.service.BusName(DBUS_NAME,bus) |
| 91 | mainloop = gobject.MainLoop() |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 92 | obj_parent = Inventory(bus, '/org/openbmc/inventory') |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 93 | |
| 94 | for f in FRUS.keys(): |
| 95 | obj_path=f.replace("<inventory_root>",System.INVENTORY_ROOT) |
Norman James | b714eb2 | 2015-10-26 17:12:57 -0500 | [diff] [blame] | 96 | obj = InventoryItem(bus,obj_path,FRUS[f]['is_fru'],FRUS[f]['fru_type']) |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 97 | obj_parent.addItem(obj) |
Norman James | c9c92dc | 2015-11-17 08:59:07 -0600 | [diff] [blame] | 98 | |
| 99 | ## TODO: this is a hack to update bmc inventory item with version |
| 100 | ## should be done by flash object |
| 101 | if (FRUS[f]['fru_type'] == "BMC"): |
| 102 | version = getVersion() |
| 103 | obj.update({'version': version}) |
| 104 | |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 105 | print "Running Inventory Manager" |
| 106 | mainloop.run() |
| 107 | |