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