blob: 5cedbed286d70b16630a37e84917a323eaffd858 [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 -050010
Patrick Williamsd8c6f5a2022-12-08 06:18:18 -060011import subprocess
12
13import dbus
14import dbus.mainloop.glib
15import dbus.service
16from obmc.dbuslib.bindings import get_dbus
Norman Jamesf066e872015-10-07 15:29:51 -050017
Patrick Williams75fe8cc2022-07-22 16:12:12 -050018FLASH_DOWNLOAD_PATH = "/tmp"
19DBUS_NAME = "org.openbmc.managers.Download"
20OBJ_NAME = "/org/openbmc/managers/Download"
Norman Jamesf066e872015-10-07 15:29:51 -050021TFTP_PORT = 69
Norman Jamesf066e872015-10-07 15:29:51 -050022
Brad Bishopf5110e12016-08-30 19:28:45 -040023
Norman Jamesf066e872015-10-07 15:29:51 -050024class DownloadManagerObject(dbus.service.Object):
Brad Bishopf5110e12016-08-30 19:28:45 -040025 def __init__(self, bus, name):
26 dbus.service.Object.__init__(self, bus, name)
27 bus.add_signal_receiver(
28 self.DownloadHandler,
29 dbus_interface="org.openbmc.Flash",
30 signal_name="Download",
Patrick Williams75fe8cc2022-07-22 16:12:12 -050031 path_keyword="path",
32 )
Brad Bishopf5110e12016-08-30 19:28:45 -040033 bus.add_signal_receiver(
34 self.TftpDownloadHandler,
35 signal_name="TftpDownload",
Patrick Williams75fe8cc2022-07-22 16:12:12 -050036 path_keyword="path",
37 )
Norman James8585b212016-01-29 08:08:30 -060038
Patrick Williams75fe8cc2022-07-22 16:12:12 -050039 @dbus.service.signal(DBUS_NAME, signature="ss")
Brad Bishopf5110e12016-08-30 19:28:45 -040040 def DownloadComplete(self, outfile, filename):
Patrick Williams75fe8cc2022-07-22 16:12:12 -050041 print("Download Complete: " + outfile)
Brad Bishopf5110e12016-08-30 19:28:45 -040042 return outfile
Norman Jamesf066e872015-10-07 15:29:51 -050043
Patrick Williams75fe8cc2022-07-22 16:12:12 -050044 @dbus.service.signal(DBUS_NAME, signature="s")
Brad Bishopf5110e12016-08-30 19:28:45 -040045 def DownloadError(self, filename):
46 pass
Norman Jamesf066e872015-10-07 15:29:51 -050047
Brad Bishopf5110e12016-08-30 19:28:45 -040048 def TftpDownloadHandler(self, ip, filename, path=None):
49 try:
50 filename = str(filename)
Patrick Williams75fe8cc2022-07-22 16:12:12 -050051 print("Downloading: " + filename + " from " + ip)
52 outfile = FLASH_DOWNLOAD_PATH + "/" + os.path.basename(filename)
Brad Bishopf5110e12016-08-30 19:28:45 -040053 rc = subprocess.call(
Patrick Williams75fe8cc2022-07-22 16:12:12 -050054 ["tftp", "-l", outfile, "-r", filename, "-g", ip]
55 )
56 if rc == 0:
Brad Bishopf5110e12016-08-30 19:28:45 -040057 self.DownloadComplete(outfile, filename)
58 else:
59 self.DownloadError(filename)
Norman Jamesf066e872015-10-07 15:29:51 -050060
Brad Bishopf5110e12016-08-30 19:28:45 -040061 except Exception as e:
Patrick Williams75fe8cc2022-07-22 16:12:12 -050062 print("ERROR DownloadManager: " + str(e))
Brad Bishopf5110e12016-08-30 19:28:45 -040063 self.DownloadError(filename)
Norman James8585b212016-01-29 08:08:30 -060064
Brad Bishopf5110e12016-08-30 19:28:45 -040065 # TODO: this needs to be deprecated.
66 # Shouldn't call flash interface from here
67 def DownloadHandler(self, url, filename, path=None):
68 try:
69 filename = str(filename)
Patrick Williams75fe8cc2022-07-22 16:12:12 -050070 print("Downloading: " + filename + " from " + url)
71 outfile = FLASH_DOWNLOAD_PATH + "/" + os.path.basename(filename)
72 subprocess.call(["tftp", "-l", outfile, "-r", filename, "-g", url])
Brad Bishopf5110e12016-08-30 19:28:45 -040073 obj = bus.get_object("org.openbmc.control.Flash", path)
74 intf = dbus.Interface(obj, "org.openbmc.Flash")
75 intf.update(outfile)
Norman James8585b212016-01-29 08:08:30 -060076
Brad Bishopf5110e12016-08-30 19:28:45 -040077 except Exception as e:
Patrick Williams75fe8cc2022-07-22 16:12:12 -050078 print("ERROR DownloadManager: " + str(e))
Brad Bishopf5110e12016-08-30 19:28:45 -040079 obj = bus.get_object("org.openbmc.control.Flash", path)
80 intf = dbus.Interface(obj, "org.openbmc.Flash")
Patrick Williams75fe8cc2022-07-22 16:12:12 -050081 intf.error("Download Error: " + filename)
Norman Jamesf066e872015-10-07 15:29:51 -050082
83
Patrick Williams75fe8cc2022-07-22 16:12:12 -050084if __name__ == "__main__":
Norman Jamesf066e872015-10-07 15:29:51 -050085 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -040086 bus = get_dbus()
Norman Jamesf066e872015-10-07 15:29:51 -050087 obj = DownloadManagerObject(bus, OBJ_NAME)
88 mainloop = gobject.MainLoop()
Brad Bishop70852a32016-06-29 22:58:51 -040089 name = dbus.service.BusName(DBUS_NAME, bus)
Brad Bishopf5110e12016-08-30 19:28:45 -040090
CamVan Nguyend65b2d52018-02-27 15:14:41 -060091 print("Running Download Manager")
Norman Jamesf066e872015-10-07 15:29:51 -050092 mainloop.run()
Brad Bishop53066752016-09-21 08:48:04 -040093
94# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4