From 9d3871d667ea7325d2950bb894bb589cd2b7ccd4 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 11 Jan 2023 16:48:10 +0100 Subject: [PATCH] Split out proper Prune command --- lib/mrsk/commands.rb | 1 + lib/mrsk/commands/prune.rb | 13 +++++++++++++ lib/tasks/mrsk/prune.rake | 7 +++---- 3 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 lib/mrsk/commands/prune.rb diff --git a/lib/mrsk/commands.rb b/lib/mrsk/commands.rb index 47a98122..3e46ced3 100644 --- a/lib/mrsk/commands.rb +++ b/lib/mrsk/commands.rb @@ -21,5 +21,6 @@ module Mrsk::Commands end require "mrsk/commands/app" +require "mrsk/commands/prune" require "mrsk/commands/traefik" require "mrsk/commands/registry" diff --git a/lib/mrsk/commands/prune.rb b/lib/mrsk/commands/prune.rb new file mode 100644 index 00000000..4a181deb --- /dev/null +++ b/lib/mrsk/commands/prune.rb @@ -0,0 +1,13 @@ +class Mrsk::Commands::Prune < Mrsk::Commands::Base + PRUNE_IMAGES_AFTER = 30.days.in_hours.to_i + PRUNE_CONTAINERS_AFTER = 3.days.in_hours.to_i + + def images + docker :image, :prune, "-f", "--filter", "until=#{PRUNE_IMAGES_AFTER}h" + end + + def containers + docker :image, :prune, "-f", "--filter", "until=#{PRUNE_IMAGES_AFTER}h" + docker :container, :prune, "-f", "--filter", "label=service=#{config.service}", "--filter", "'until=#{PRUNE_CONTAINERS_AFTER}h'" + end +end diff --git a/lib/tasks/mrsk/prune.rake b/lib/tasks/mrsk/prune.rake index 4f730fc9..b99f49ad 100644 --- a/lib/tasks/mrsk/prune.rake +++ b/lib/tasks/mrsk/prune.rake @@ -1,7 +1,6 @@ require_relative "setup" -PRUNE_IMAGES_AFTER = 30.days.in_hours.to_i -PRUNE_CONTAINERS_AFTER = 3.days.in_hours.to_i +prune = Mrsk::Commands::Prune.new(MRSK_CONFIG) namespace :mrsk do desc "Prune unused images and stopped containers" @@ -10,12 +9,12 @@ namespace :mrsk do namespace :prune do desc "Prune unused images older than 30 days" task :images do - on(MRSK_CONFIG.hosts) { execute "docker image prune -f --filter 'until=#{PRUNE_IMAGES_AFTER}h'" } + on(MRSK_CONFIG.hosts) { execute *prune.images } end desc "Prune stopped containers for the service older than 3 days" task :containers do - on(MRSK_CONFIG.hosts) { execute "docker container prune -f --filter label=service=#{MRSK_CONFIG.service} --filter 'until=#{PRUNE_CONTAINERS_AFTER}h'" } + on(MRSK_CONFIG.hosts) { execute *prune.containers } end end end