blob: 799044c36e332bd569392045d9011b14dea27583 [file] [log] [blame]
Brad Bishopf5110e12016-08-30 19:28:45 -04001#!/usr/bin/env python
Norman Jamesf066e872015-10-07 15:29:51 -05002
Gunnar Mills19029902017-10-06 14:47:10 -05003import os
Patrick Williams75fe8cc2022-07-22 16:12:12 -05004
CamVan Nguyend65b2d52018-02-27 15:14:41 -06005# TODO: openbmc/openbmc#2994 remove python 2 support
6try: # python 2
7 import gobject
8except ImportError: # python 3
9 from gi.repository import GObject as gobject
Norman Jamesf066e872015-10-07 15:29:51 -050010import dbus
11import dbus.service
12import dbus.mainloop.glib
Norman James0b73e7d2016-01-19 14:03:50 -060013import subprocess
Brad Bishop84e73b52016-05-12 15:57:52 -040014from obmc.dbuslib.bindings import get_dbus
Norman Jamesf066e872015-10-07 15:29:51 -050015
16
Patrick Williams75fe8cc2022-07-22 16:12:12 -050017FLASH_DOWNLOAD_PATH = "/tmp"
18DBUS_NAME = "org.openbmc.managers.Download"
19OBJ_NAME = "/org/openbmc/managers/Download"
Norman Jamesf066e872015-10-07 15:29:51 -050020TFTP_PORT = 69
Norman Jamesf066e872015-10-07 15:29:51 -050021
Brad Bishopf5110e12016-08-30 19:28:45 -040022
Norman Jamesf066e872015-10-07 15:29:51 -050023class DownloadManagerObject(dbus.service.Object):
Brad Bishopf5110e12016-08-30 19:28:45 -040024 def __init__(self, bus, name):
25 dbus.service.Object.__init__(self, bus, name)
26 bus.add_signal_receiver(
27 self.DownloadHandler,
28 dbus_interface="org.openbmc.Flash",
29 signal_name="Download",
Patrick Williams75fe8cc2022-07-22 16:12:12 -050030 path_keyword="path",
31 )
Brad Bishopf5110e12016-08-30 19:28:45 -040032 bus.add_signal_receiver(
33 self.TftpDownloadHandler,
34 signal_name="TftpDownload",
Patrick Williams75fe8cc2022-07-22 16:12:12 -050035 path_keyword="path",
36 )
Norman James8585b212016-01-29 08:08:30 -060037
Patrick Williams75fe8cc2022-07-22 16:12:12 -050038 @dbus.service.signal(DBUS_NAME, signature="ss")
Brad Bishopf5110e12016-08-30 19:28:45 -040039 def DownloadComplete(self, outfile, filename):
Patrick Williams75fe8cc2022-07-22 16:12:12 -050040 print("Download Complete: " + outfile)
Brad Bishopf5110e12016-08-30 19:28:45 -040041 return outfile
Norman Jamesf066e872015-10-07 15:29:51 -050042
Patrick Williams75fe8cc2022-07-22 16:12:12 -050043 @dbus.service.signal(DBUS_NAME, signature="s")
Brad Bishopf5110e12016-08-30 19:28:45 -040044 def DownloadError(self, filename):
45 pass
Norman Jamesf066e872015-10-07 15:29:51 -050046
Brad Bishopf5110e12016-08-30 19:28:45 -040047 def TftpDownloadHandler(self, ip, filename, path=None):
48 try:
49 filename = str(filename)
Patrick Williams75fe8cc2022-07-22 16:12:12 -050050 print("Downloading: " + filename + " from " + ip)
51 outfile = FLASH_DOWNLOAD_PATH + "/" + os.path.basename(filename)
Brad Bishopf5110e12016-08-30 19:28:45 -040052 rc = subprocess.call(
Patrick Williams75fe8cc2022-07-22 16:12:12 -050053 ["tftp", "-l", outfile, "-r", filename, "-g", ip]
54 )
55 if rc == 0:
Brad Bishopf5110e12016-08-30 19:28:45 -040056 self.DownloadComplete(outfile, filename)
57 else:
58 self.DownloadError(filename)
Norman Jamesf066e872015-10-07 15:29:51 -050059
Brad Bishopf5110e12016-08-30 19:28:45 -040060 except Exception as e:
Patrick Williams75fe8cc2022-07-22 16:12:12 -050061 print("ERROR DownloadManager: " + str(e))
Brad Bishopf5110e12016-08-30 19:28:45 -040062 self.DownloadError(filename)
Norman James8585b212016-01-29 08:08:30 -060063
Brad Bishopf5110e12016-08-30 19:28:45 -040064 # TODO: this needs to be deprecated.
65 # Shouldn't call flash interface from here
66 def DownloadHandler(self, url, filename, path=None):
67 try:
68 filename = str(filename)
Patrick Williams75fe8cc2022-07-22 16:12:12 -050069 print("Downloading: " + filename + " from " + url)
70 outfile = FLASH_DOWNLOAD_PATH + "/" + os.path.basename(filename)
71 subprocess.call(["tftp", "-l", outfile, "-r", filename, "-g", url])
Brad Bishopf5110e12016-08-30 19:28:45 -040072 obj = bus.get_object("org.openbmc.control.Flash", path)
73 intf = dbus.Interface(obj, "org.openbmc.Flash")
74 intf.update(outfile)
Norman James8585b212016-01-29 08:08:30 -060075
Brad Bishopf5110e12016-08-30 19:28:45 -040076 except Exception as e:
Patrick Williams75fe8cc2022-07-22 16:12:12 -050077 print("ERROR DownloadManager: " + str(e))
Brad Bishopf5110e12016-08-30 19:28:45 -040078 obj = bus.get_object("org.openbmc.control.Flash", path)
79 intf = dbus.Interface(obj, "org.openbmc.Flash")
Patrick Williams75fe8cc2022-07-22 16:12:12 -050080 intf.error("Download Error: " + filename)
Norman Jamesf066e872015-10-07 15:29:51 -050081
82
Patrick Williams75fe8cc2022-07-22 16:12:12 -050083if __name__ == "__main__":
Norman Jamesf066e872015-10-07 15:29:51 -050084 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -040085 bus = get_dbus()
Norman Jamesf066e872015-10-07 15:29:51 -050086 obj = DownloadManagerObject(bus, OBJ_NAME)
87 mainloop = gobject.MainLoop()
Brad Bishop70852a32016-06-29 22:58:51 -040088 name = dbus.service.BusName(DBUS_NAME, bus)
Brad Bishopf5110e12016-08-30 19:28:45 -040089
CamVan Nguyend65b2d52018-02-27 15:14:41 -060090 print("Running Download Manager")
Norman Jamesf066e872015-10-07 15:29:51 -050091 mainloop.run()
Brad Bishop53066752016-09-21 08:48:04 -040092
93# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4