Skip to content

Commit

Permalink
Fixes #36729 - Implement pruning outdated TFTP files
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ekohl committed Sep 7, 2023
1 parent 06092bb commit 1107720
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions modules/tftp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions modules/tftp/tftp_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1107720

Please sign in to comment.