1#!/usr/bin/env python 2 3import os 4# TODO: openbmc/openbmc#2994 remove python 2 support 5try: # python 2 6 import gobject 7except ImportError: # python 3 8 from gi.repository import GObject as gobject 9import dbus 10import dbus.service 11import dbus.mainloop.glib 12import subprocess 13from obmc.dbuslib.bindings import get_dbus 14 15 16FLASH_DOWNLOAD_PATH = '/tmp' 17DBUS_NAME = 'org.openbmc.managers.Download' 18OBJ_NAME = '/org/openbmc/managers/Download' 19TFTP_PORT = 69 20 21 22class DownloadManagerObject(dbus.service.Object): 23 def __init__(self, bus, name): 24 dbus.service.Object.__init__(self, bus, name) 25 bus.add_signal_receiver( 26 self.DownloadHandler, 27 dbus_interface="org.openbmc.Flash", 28 signal_name="Download", 29 path_keyword="path") 30 bus.add_signal_receiver( 31 self.TftpDownloadHandler, 32 signal_name="TftpDownload", 33 path_keyword="path") 34 35 @dbus.service.signal(DBUS_NAME, signature='ss') 36 def DownloadComplete(self, outfile, filename): 37 print("Download Complete: "+outfile) 38 return outfile 39 40 @dbus.service.signal(DBUS_NAME, signature='s') 41 def DownloadError(self, filename): 42 pass 43 44 def TftpDownloadHandler(self, ip, filename, path=None): 45 try: 46 filename = str(filename) 47 print("Downloading: "+filename+" from "+ip) 48 outfile = FLASH_DOWNLOAD_PATH+"/"+os.path.basename(filename) 49 rc = subprocess.call( 50 ["tftp", "-l", outfile, "-r", filename, "-g", ip]) 51 if (rc == 0): 52 self.DownloadComplete(outfile, filename) 53 else: 54 self.DownloadError(filename) 55 56 except Exception as e: 57 print("ERROR DownloadManager: "+str(e)) 58 self.DownloadError(filename) 59 60 # TODO: this needs to be deprecated. 61 # Shouldn't call flash interface from here 62 def DownloadHandler(self, url, filename, path=None): 63 try: 64 filename = str(filename) 65 print("Downloading: "+filename+" from "+url) 66 outfile = FLASH_DOWNLOAD_PATH+"/"+os.path.basename(filename) 67 subprocess.call( 68 ["tftp", "-l", outfile, "-r", filename, "-g", url]) 69 obj = bus.get_object("org.openbmc.control.Flash", path) 70 intf = dbus.Interface(obj, "org.openbmc.Flash") 71 intf.update(outfile) 72 73 except Exception as e: 74 print("ERROR DownloadManager: "+str(e)) 75 obj = bus.get_object("org.openbmc.control.Flash", path) 76 intf = dbus.Interface(obj, "org.openbmc.Flash") 77 intf.error("Download Error: "+filename) 78 79 80if __name__ == '__main__': 81 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) 82 bus = get_dbus() 83 obj = DownloadManagerObject(bus, OBJ_NAME) 84 mainloop = gobject.MainLoop() 85 name = dbus.service.BusName(DBUS_NAME, bus) 86 87 print("Running Download Manager") 88 mainloop.run() 89 90# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 91