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 |
Brad Bishop | ee1b154 | 2016-05-12 16:55:00 -0400 | [diff] [blame] | 11 | import obmc.dbuslib.propertycacher as PropertyCacher |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 12 | from obmc.dbuslib.bindings import get_dbus, DbusProperties, DbusObjectManager |
Brad Bishop | 0b380f7 | 2016-06-10 00:29:50 -0400 | [diff] [blame] | 13 | import obmc_system_config as System |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 14 | |
| 15 | INTF_NAME = 'org.openbmc.InventoryItem' |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 16 | DBUS_NAME = 'org.openbmc.Inventory' |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 17 | FRUS = System.FRU_INSTANCES |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 18 | |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 19 | class Inventory(DbusProperties,DbusObjectManager): |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 20 | def __init__(self,bus,name): |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 21 | DbusProperties.__init__(self) |
| 22 | DbusObjectManager.__init__(self) |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 23 | dbus.service.Object.__init__(self,bus,name) |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 24 | self.InterfacesAdded(name,self.properties) |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 25 | |
| 26 | |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 27 | class InventoryItem(DbusProperties): |
Norman James | 0a5fbf8 | 2015-12-03 17:56:55 -0600 | [diff] [blame] | 28 | def __init__(self,bus,name,data): |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 29 | DbusProperties.__init__(self) |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 30 | dbus.service.Object.__init__(self,bus,name) |
Norman James | b714eb2 | 2015-10-26 17:12:57 -0500 | [diff] [blame] | 31 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 32 | self.name = name |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 33 | |
Norman James | ab777b1 | 2015-10-29 06:19:18 -0500 | [diff] [blame] | 34 | ## this will load properties from cache |
Norman James | 0a5fbf8 | 2015-12-03 17:56:55 -0600 | [diff] [blame] | 35 | if (data.has_key('present') == False): |
| 36 | data['present'] = 'False' |
| 37 | if (data.has_key('fault') == False): |
| 38 | data['fault'] = 'False' |
| 39 | if (data.has_key('version') == False): |
| 40 | data['version'] = '' |
| 41 | |
Norman James | b714eb2 | 2015-10-26 17:12:57 -0500 | [diff] [blame] | 42 | self.SetMultiple(INTF_NAME,data) |
Norman James | ab777b1 | 2015-10-29 06:19:18 -0500 | [diff] [blame] | 43 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 44 | |
| 45 | @dbus.service.method(INTF_NAME, |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 46 | in_signature='a{sv}', out_signature='') |
| 47 | def update(self,data): |
Norman James | b714eb2 | 2015-10-26 17:12:57 -0500 | [diff] [blame] | 48 | self.SetMultiple(INTF_NAME,data) |
Norman James | ab777b1 | 2015-10-29 06:19:18 -0500 | [diff] [blame] | 49 | PropertyCacher.save(self.name,INTF_NAME,self.properties) |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 50 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 51 | @dbus.service.method(INTF_NAME, |
Norman James | 96da5c2 | 2015-10-15 10:17:06 -0500 | [diff] [blame] | 52 | in_signature='s', out_signature='') |
| 53 | def setPresent(self,present): |
Norman James | b714eb2 | 2015-10-26 17:12:57 -0500 | [diff] [blame] | 54 | self.Set(INTF_NAME,'present',present) |
Norman James | 96da5c2 | 2015-10-15 10:17:06 -0500 | [diff] [blame] | 55 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 56 | @dbus.service.method(INTF_NAME, |
Norman James | 96da5c2 | 2015-10-15 10:17:06 -0500 | [diff] [blame] | 57 | in_signature='s', out_signature='') |
| 58 | def setFault(self,fault): |
Norman James | b714eb2 | 2015-10-26 17:12:57 -0500 | [diff] [blame] | 59 | self.Set(INTF_NAME,'fault',fault) |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 60 | |
| 61 | |
Norman James | c9c92dc | 2015-11-17 08:59:07 -0600 | [diff] [blame] | 62 | def getVersion(): |
| 63 | version = "Error" |
| 64 | with open('/etc/os-release', 'r') as f: |
| 65 | for line in f: |
| 66 | p = line.rstrip('\n') |
| 67 | parts = line.rstrip('\n').split('=') |
Adriana Kobylak | 4e13e9f | 2016-04-25 15:08:01 -0500 | [diff] [blame] | 68 | if (parts[0] == "VERSION_ID"): |
Norman James | c9c92dc | 2015-11-17 08:59:07 -0600 | [diff] [blame] | 69 | version = parts[1] |
| 70 | return version |
| 71 | |
| 72 | |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 73 | if __name__ == '__main__': |
| 74 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 75 | bus = get_dbus() |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 76 | mainloop = gobject.MainLoop() |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 77 | obj_parent = Inventory(bus, '/org/openbmc/inventory') |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 78 | |
| 79 | for f in FRUS.keys(): |
| 80 | obj_path=f.replace("<inventory_root>",System.INVENTORY_ROOT) |
Norman James | 0a5fbf8 | 2015-12-03 17:56:55 -0600 | [diff] [blame] | 81 | obj = InventoryItem(bus,obj_path,FRUS[f]) |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 82 | obj_parent.add(obj_path,obj) |
Norman James | c9c92dc | 2015-11-17 08:59:07 -0600 | [diff] [blame] | 83 | |
| 84 | ## TODO: this is a hack to update bmc inventory item with version |
| 85 | ## should be done by flash object |
| 86 | if (FRUS[f]['fru_type'] == "BMC"): |
| 87 | version = getVersion() |
| 88 | obj.update({'version': version}) |
| 89 | |
Brad Bishop | a81e98a | 2016-05-02 23:07:36 -0400 | [diff] [blame] | 90 | name = dbus.service.BusName(DBUS_NAME,bus) |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 91 | print "Running Inventory Manager" |
| 92 | mainloop.run() |
| 93 | |