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