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