Class: GitlabJanitor::ImageCleaner

Inherits:
BaseCleaner show all
Defined in:
lib/gitlab_janitor/image_cleaner.rb,
lib/gitlab_janitor/image_cleaner/store.rb

Defined Under Namespace

Classes: Model, Store

Instance Attribute Summary collapse

Attributes inherited from BaseCleaner

#deadline, #delay, #logger

Instance Method Summary collapse

Methods inherited from BaseCleaner

#clean, #exiting?, #log_exception

Constructor Details

#initialize(image_store:, redis: nil, redis_list: 'gitlab-janitor:images_force_clean', **kwargs) ⇒ ImageCleaner

Returns a new instance of ImageCleaner.



45
46
47
48
49
50
# File 'lib/gitlab_janitor/image_cleaner.rb', line 45

def initialize(image_store:, redis: nil, redis_list: 'gitlab-janitor:images_force_clean', **kwargs)
  super(**kwargs)
  @store = Store.new(filename: image_store, logger: logger)
  @redis_url = redis
  @redis_list = redis_list
end

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store.



43
44
45
# File 'lib/gitlab_janitor/image_cleaner.rb', line 43

def store
  @store
end

Instance Method Details

#do_clean(remove: false) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/gitlab_janitor/image_cleaner.rb', line 52

def do_clean(remove: false)
  store.load

  force_clean(remove: remove)

  to_remove, keep = prepare(store.parse_images)
  store.save(skip_older: Time.now - @deadline)

  return if to_remove.empty?

  keep.each {|m| logger.debug("  KEEP #{m.name}") }

  if remove
    logger.info 'Removing images...'
    to_remove.each do |model|
      return false if exiting?

      logger.tagged(model.name) do
        logger.debug '   Removing...'
        log_exception('Remove') { out, _status = Open3.capture2e("docker rmi #{model.name}"); logger.info(out) }
        logger.debug '   Removing COMPLETED'
      end
    end
  else
    logger.info 'Skip removal due to dry run'
  end
ensure
  if remove
    logger.info 'docker image prune -f'
    out, _status = Open3.capture2e("docker image prune -f")
    logger.info(out)
  end
end

#force_clean(remove: false) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/gitlab_janitor/image_cleaner.rb', line 112

def force_clean(remove: false)
  return if @redis_url.nil?
  logger.info("Force clean image from #{@redis_url}/#{@redis_url}...")

  redis = Redis.new(url: @redis_url)
  redis.ltrim(@redis_list, 0, 100)
  now = Time.now
  redis.lrange(@redis_list, 0, -1).each do |pair|
    image, ts = pair.split('|')
    if (now - Time.at(ts.to_i)) > 10.seconds
      if remove
        logger.info("Force clean #{image}")
        log_exception('Remove') { out, _status = Open3.capture2e("docker rmi #{image}"); logger.info(out) }
      else
        logger.info("Skip Force clean #{image} due to dry run")
      end
    else
      logger.info("Delay force clean #{image} by time")
    end
  rescue StandardError => e
    logger.error("Error from force line: '#{pair}': #{e.inspect}")
  end
rescue StandardError => e
  logger.error("Unable to retrieve data from redis: #{e}")
end

#format_item(model) ⇒ Object



102
103
104
# File 'lib/gitlab_janitor/image_cleaner.rb', line 102

def format_item(model)
  "#{model.loaded_at} Age:#{model.age_text.ljust(13)} #{model.name.first(60).ljust(60)}"
end

#prepare(images) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gitlab_janitor/image_cleaner.rb', line 86

def prepare(images)
  to_remove = images

  @logger.info("Selected images: \n#{to_remove.map{|c| "  + #{format_item(c)}" }.join("\n")}")

  @logger.info("Filtering images by deadline: older than #{Fugit::Duration.parse(@deadline).deflate.to_plain_s}...")
  to_remove = select_by_deadline(to_remove)
  if to_remove.empty?
    @logger.info('Noting to remove.')
    return [], images
  end
  @logger.info("Filtered images: \n#{to_remove.map{|c| "  !! #{format_item(c)}" }.join("\n")}")

  [to_remove, (images - to_remove)]
end

#select_by_deadline(images) ⇒ Object



106
107
108
109
110
# File 'lib/gitlab_janitor/image_cleaner.rb', line 106

def select_by_deadline(images)
  images.select do |model|
    model.age > deadline
  end
end