Class: Nvoi::Cli::Deploy::Steps::CleanupImages

Inherits:
Object
  • Object
show all
Defined in:
lib/nvoi/cli/deploy/steps/cleanup_images.rb

Overview

CleanupImages handles cleanup of old container images

Instance Method Summary collapse

Constructor Details

#initialize(config, ssh, log) ⇒ CleanupImages

Returns a new instance of CleanupImages.



9
10
11
12
13
# File 'lib/nvoi/cli/deploy/steps/cleanup_images.rb', line 9

def initialize(config, ssh, log)
  @config = config
  @ssh = ssh
  @log = log
end

Instance Method Details

#run(current_tag) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/nvoi/cli/deploy/steps/cleanup_images.rb', line 15

def run(current_tag)
  keep_count = @config.keep_count_value
  prefix = @config.container_prefix

  @log.info "Cleaning up old images (keeping %d)", keep_count

  containerd = External::Containerd.new(@ssh)

  # List all images
  all_tags = containerd.list_images("#{prefix}:*")

  # Sort by tag (timestamp), keep newest
  sorted_tags = all_tags.sort.reverse
  keep_tags = sorted_tags.take(keep_count)

  # Make sure current tag is kept
  keep_tags << current_tag unless keep_tags.include?(current_tag)
  keep_tags << "latest"

  containerd.cleanup_old_images(prefix, keep_tags.uniq)

  @log.success "Old images cleaned up"
end