blob: 5e532933bc1ff919f3b7f3e1be1a9da22cebeedf [file] [log] [blame]
Norman James42c1be82015-10-22 14:34:26 -05001#!/usr/bin/python -u
Norman James19e45912015-10-04 20:19:41 -05002
3import os
4import sys
5import gobject
6import dbus
7import dbus.service
8import dbus.mainloop.glib
9import cPickle
10import json
Brad Bishopee1b1542016-05-12 16:55:00 -040011import obmc.dbuslib.propertycacher as PropertyCacher
Brad Bishop84e73b52016-05-12 15:57:52 -040012from obmc.dbuslib.bindings import get_dbus, DbusProperties, DbusObjectManager
Norman James19e45912015-10-04 20:19:41 -050013
14if (len(sys.argv) < 2):
15 print "Usage: inventory_items.py [system name]"
16 exit(1)
17System = __import__(sys.argv[1])
Norman James19e45912015-10-04 20:19:41 -050018
19
20INTF_NAME = 'org.openbmc.InventoryItem'
Norman James323ed972015-12-09 09:06:37 -060021DBUS_NAME = 'org.openbmc.Inventory'
Norman James19e45912015-10-04 20:19:41 -050022FRUS = System.FRU_INSTANCES
Norman James19e45912015-10-04 20:19:41 -050023
Brad Bishop84e73b52016-05-12 15:57:52 -040024class Inventory(DbusProperties,DbusObjectManager):
Norman James19e45912015-10-04 20:19:41 -050025 def __init__(self,bus,name):
Brad Bishop84e73b52016-05-12 15:57:52 -040026 DbusProperties.__init__(self)
27 DbusObjectManager.__init__(self)
Norman James19e45912015-10-04 20:19:41 -050028 dbus.service.Object.__init__(self,bus,name)
Norman James323ed972015-12-09 09:06:37 -060029 self.InterfacesAdded(name,self.properties)
Norman James19e45912015-10-04 20:19:41 -050030
31
Brad Bishop84e73b52016-05-12 15:57:52 -040032class InventoryItem(DbusProperties):
Norman James0a5fbf82015-12-03 17:56:55 -060033 def __init__(self,bus,name,data):
Brad Bishop84e73b52016-05-12 15:57:52 -040034 DbusProperties.__init__(self)
Norman James19e45912015-10-04 20:19:41 -050035 dbus.service.Object.__init__(self,bus,name)
Norman Jamesb714eb22015-10-26 17:12:57 -050036
Norman Jamesa3e47c42015-10-18 14:43:10 -050037 self.name = name
Norman Jamescfc2b442015-10-31 17:31:46 -050038
Norman Jamesab777b12015-10-29 06:19:18 -050039 ## this will load properties from cache
Norman James0a5fbf82015-12-03 17:56:55 -060040 if (data.has_key('present') == False):
41 data['present'] = 'False'
42 if (data.has_key('fault') == False):
43 data['fault'] = 'False'
44 if (data.has_key('version') == False):
45 data['version'] = ''
46
Norman Jamesb714eb22015-10-26 17:12:57 -050047 self.SetMultiple(INTF_NAME,data)
Norman Jamesab777b12015-10-29 06:19:18 -050048
Norman Jamesa3e47c42015-10-18 14:43:10 -050049
50 @dbus.service.method(INTF_NAME,
Norman James19e45912015-10-04 20:19:41 -050051 in_signature='a{sv}', out_signature='')
52 def update(self,data):
Norman Jamesb714eb22015-10-26 17:12:57 -050053 self.SetMultiple(INTF_NAME,data)
Norman Jamesab777b12015-10-29 06:19:18 -050054 PropertyCacher.save(self.name,INTF_NAME,self.properties)
Norman James19e45912015-10-04 20:19:41 -050055
Norman Jamesa3e47c42015-10-18 14:43:10 -050056 @dbus.service.method(INTF_NAME,
Norman James96da5c22015-10-15 10:17:06 -050057 in_signature='s', out_signature='')
58 def setPresent(self,present):
Norman Jamesb714eb22015-10-26 17:12:57 -050059 self.Set(INTF_NAME,'present',present)
Norman James96da5c22015-10-15 10:17:06 -050060
Norman Jamesa3e47c42015-10-18 14:43:10 -050061 @dbus.service.method(INTF_NAME,
Norman James96da5c22015-10-15 10:17:06 -050062 in_signature='s', out_signature='')
63 def setFault(self,fault):
Norman Jamesb714eb22015-10-26 17:12:57 -050064 self.Set(INTF_NAME,'fault',fault)
Norman James19e45912015-10-04 20:19:41 -050065
66
Norman Jamesc9c92dc2015-11-17 08:59:07 -060067def getVersion():
68 version = "Error"
69 with open('/etc/os-release', 'r') as f:
70 for line in f:
71 p = line.rstrip('\n')
72 parts = line.rstrip('\n').split('=')
Adriana Kobylak4e13e9f2016-04-25 15:08:01 -050073 if (parts[0] == "VERSION_ID"):
Norman Jamesc9c92dc2015-11-17 08:59:07 -060074 version = parts[1]
75 return version
76
77
Norman James19e45912015-10-04 20:19:41 -050078if __name__ == '__main__':
79 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -040080 bus = get_dbus()
Norman James19e45912015-10-04 20:19:41 -050081 mainloop = gobject.MainLoop()
Norman Jamesa3e47c42015-10-18 14:43:10 -050082 obj_parent = Inventory(bus, '/org/openbmc/inventory')
Norman James19e45912015-10-04 20:19:41 -050083
84 for f in FRUS.keys():
85 obj_path=f.replace("<inventory_root>",System.INVENTORY_ROOT)
Norman James0a5fbf82015-12-03 17:56:55 -060086 obj = InventoryItem(bus,obj_path,FRUS[f])
Norman James323ed972015-12-09 09:06:37 -060087 obj_parent.add(obj_path,obj)
Norman Jamesc9c92dc2015-11-17 08:59:07 -060088
89 ## TODO: this is a hack to update bmc inventory item with version
90 ## should be done by flash object
91 if (FRUS[f]['fru_type'] == "BMC"):
92 version = getVersion()
93 obj.update({'version': version})
94
Brad Bishopa81e98a2016-05-02 23:07:36 -040095 name = dbus.service.BusName(DBUS_NAME,bus)
Norman James19e45912015-10-04 20:19:41 -050096 print "Running Inventory Manager"
97 mainloop.run()
98