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