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