Norman James | 42c1be8 | 2015-10-22 14:34:26 -0500 | [diff] [blame] | 1 | #!/usr/bin/python -u |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 2 | |
| 3 | import sys |
| 4 | import gobject |
| 5 | import dbus |
| 6 | import dbus.service |
| 7 | import dbus.mainloop.glib |
Norman James | 0b73e7d | 2016-01-19 14:03:50 -0600 | [diff] [blame] | 8 | import subprocess |
Norman James | 408920d | 2015-10-08 07:02:45 -0500 | [diff] [blame] | 9 | import Openbmc |
| 10 | if (len(sys.argv) < 2): |
| 11 | print "Usage: download_manager.py [system name]" |
| 12 | exit(1) |
| 13 | System = __import__(sys.argv[1]) |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 14 | |
| 15 | |
| 16 | DBUS_NAME = 'org.openbmc.managers.Download' |
| 17 | OBJ_NAME = '/org/openbmc/managers/Download' |
| 18 | TFTP_PORT = 69 |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 19 | |
| 20 | class DownloadManagerObject(dbus.service.Object): |
| 21 | def __init__(self,bus,name): |
| 22 | dbus.service.Object.__init__(self,bus,name) |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 23 | bus.add_signal_receiver(self.DownloadHandler, |
Norman James | 8585b21 | 2016-01-29 08:08:30 -0600 | [diff] [blame] | 24 | dbus_interface = "org.openbmc.Flash", |
| 25 | signal_name = "Download", path_keyword = "path") |
| 26 | bus.add_signal_receiver(self.TftpDownloadHandler, |
| 27 | signal_name = "TftpDownload", path_keyword = "path") |
| 28 | |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 29 | |
Norman James | 1899818 | 2015-10-11 21:54:53 -0500 | [diff] [blame] | 30 | @dbus.service.signal(DBUS_NAME,signature='ss') |
| 31 | def DownloadComplete(self,outfile,filename): |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 32 | print "Download Complete: "+outfile |
| 33 | return outfile |
| 34 | |
Norman James | 1899818 | 2015-10-11 21:54:53 -0500 | [diff] [blame] | 35 | @dbus.service.signal(DBUS_NAME,signature='s') |
| 36 | def DownloadError(self,filename): |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 37 | pass |
| 38 | |
Norman James | 8585b21 | 2016-01-29 08:08:30 -0600 | [diff] [blame] | 39 | def TftpDownloadHandler(self,ip,filename,path = None): |
| 40 | try: |
| 41 | filename = str(filename) |
| 42 | print "Downloading: "+filename+" from "+ip |
| 43 | outfile = System.FLASH_DOWNLOAD_PATH+"/"+filename |
| 44 | rc = subprocess.call(["tftp", "-l",outfile,"-r",filename,"-g",ip]) |
| 45 | if (rc == 0): |
| 46 | self.DownloadComplete(outfile,filename) |
| 47 | else: |
| 48 | self.DownloadError(filename) |
| 49 | |
| 50 | |
| 51 | except Exception as e: |
| 52 | print "ERROR DownloadManager: "+str(e) |
| 53 | self.DownloadError(filename) |
| 54 | |
| 55 | |
| 56 | ## TODO: this needs to be deprecated. Shouldn't call flash interface from here |
Norman James | 166acf4 | 2015-10-22 07:11:51 -0500 | [diff] [blame] | 57 | def DownloadHandler(self,url,filename,path = None): |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 58 | try: |
| 59 | filename = str(filename) |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 60 | print "Downloading: "+filename+" from "+url |
Norman James | 408920d | 2015-10-08 07:02:45 -0500 | [diff] [blame] | 61 | outfile = System.FLASH_DOWNLOAD_PATH+"/"+filename |
Norman James | 8af7d4a | 2016-01-06 14:09:08 -0600 | [diff] [blame] | 62 | subprocess.call(["tftp", "-l",outfile,"-r",filename,"-g",url]) |
Norman James | 166acf4 | 2015-10-22 07:11:51 -0500 | [diff] [blame] | 63 | obj = bus.get_object("org.openbmc.control.Flash",path) |
| 64 | intf = dbus.Interface(obj,"org.openbmc.Flash") |
| 65 | intf.update(outfile) |
Norman James | 8af7d4a | 2016-01-06 14:09:08 -0600 | [diff] [blame] | 66 | |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 67 | except Exception as e: |
| 68 | print "ERROR DownloadManager: "+str(e) |
Norman James | 166acf4 | 2015-10-22 07:11:51 -0500 | [diff] [blame] | 69 | obj = bus.get_object("org.openbmc.control.Flash",path) |
| 70 | intf = dbus.Interface(obj,"org.openbmc.Flash") |
| 71 | intf.error("Download Error: "+filename) |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 72 | |
| 73 | |
| 74 | if __name__ == '__main__': |
| 75 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 76 | bus = Openbmc.getDBus() |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 77 | name = dbus.service.BusName(DBUS_NAME, bus) |
| 78 | obj = DownloadManagerObject(bus, OBJ_NAME) |
| 79 | mainloop = gobject.MainLoop() |
| 80 | |
| 81 | print "Running Download Manager" |
| 82 | mainloop.run() |
| 83 | |