From 1107720623ee96eb1a752c377cce7b942bb5c64c 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 | 6 ++++++ modules/tftp/tftp_api.rb | 14 ++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/modules/tftp/server.rb b/modules/tftp/server.rb index 98218d8bd..a7cbe25fe 100644 --- a/modules/tftp/server.rb +++ b/modules/tftp/server.rb @@ -63,6 +63,12 @@ def delete_file(file) logger.debug "TFTP: Skipping a request to delete a file which doesn't exists" end end + + def self.outdated_files(duration) + Dir.glob(File.join(Proxy::TFTP::Plugin.settings.tftproot, "boot", "*-{vmlinuz,initrd.img}")).filter do |file| + File.file?(file) && (File.mtime(file) + (token_duration * 60)) < Time.now + end + end end class Syslinux < Server diff --git a/modules/tftp/tftp_api.rb b/modules/tftp/tftp_api.rb index 1bc6276c7..2724cdd16 100644 --- a/modules/tftp/tftp_api.rb +++ b/modules/tftp/tftp_api.rb @@ -71,5 +71,19 @@ def create_default(variant) get "/serverName" do {"serverName" => (Proxy::TFTP::Plugin.settings.tftp_servername || "")}.to_json end + + # Purge old entries + post "/prune" do + duration = params[:duration] + + if duration.nil? || duration.to_i <= 0 + log_halt 400, "Invalid duration; needs to be a valid integer > 0" + end + + Proxy::TFTP::Server.outdated_files(duration.to_i).each do |file| + logger.debug("Removing outdated file", file) + File.unlink(file) + end + end end end