From e19f7f4383ba640d4046fcec4dad9b9526c98d6d Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Thu, 7 Sep 2023 15:06:38 +0200 Subject: [PATCH] Fixes #36729 - Implement pruning outdated TFTP files This is about replacing foreman_maintain's check_tftp_storage[1] with a native implementation. It is implemented by a /tftp/prune API endpoint which accepts the duration as a parameter. There is no plugin capability defined since Foreman can just call the endpoint and process HTTP 404 as Not Implemented. [1]: https://github.com/theforeman/foreman_maintain/blob/1e20bbd9f32a4f47193c4833fbd4012772b34dc4/definitions/checks/foreman_proxy/check_tftp_storage.rb --- modules/tftp/server.rb | 7 +++++++ modules/tftp/tftp_api.rb | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/modules/tftp/server.rb b/modules/tftp/server.rb index 98218d8bd..473841763 100644 --- a/modules/tftp/server.rb +++ b/modules/tftp/server.rb @@ -63,6 +63,13 @@ def delete_file(file) logger.debug "TFTP: Skipping a request to delete a file which doesn't exists" end end + + def self.outdated_files(age) + delete_before = Time.now - age + Dir.glob(File.join(Proxy::TFTP::Plugin.settings.tftproot, "boot", "*-{vmlinuz,initrd.img}")).filter do |file| + File.file?(file) && File.mtime(file) < delete_before + end + end end class Syslinux < Server diff --git a/modules/tftp/tftp_api.rb b/modules/tftp/tftp_api.rb index 1bc6276c7..9b30083a7 100644 --- a/modules/tftp/tftp_api.rb +++ b/modules/tftp/tftp_api.rb @@ -71,5 +71,24 @@ def create_default(variant) get "/serverName" do {"serverName" => (Proxy::TFTP::Plugin.settings.tftp_servername || "")}.to_json end + + # Purge old entries + post "/prune" do + # how old a file can be, in seconds + age = params[:age] + + if age.nil? || age.to_i <= 0 + log_halt 400, "Invalid age; needs to be a valid integer > 0" + end + + to_prune = Proxy::TFTP::Server.outdated_files(age.to_i) + + to_prune.each do |file| + logger.debug("Removing outdated file", file) + File.unlink(file) + end + + pruned.length + end end end