Class: IKE::Artifactory::DockerCleaner
- Inherits:
-
Object
- Object
- IKE::Artifactory::DockerCleaner
- Defined in:
- lib/ike_artifactory/docker_cleaner.rb
Instance Attribute Summary collapse
-
#actually_delete ⇒ Object
readonly
Returns the value of attribute actually_delete.
-
#client ⇒ Object
readonly
is not tested.
-
#days_old ⇒ Object
Returns the value of attribute days_old.
-
#delete_empty_path ⇒ Object
readonly
Returns the value of attribute delete_empty_path.
-
#folder ⇒ Object
Returns the value of attribute folder.
-
#logger ⇒ Object
readonly
is not tested.
-
#most_recent_images ⇒ Object
readonly
is not tested.
-
#repo_host ⇒ Object
Returns the value of attribute repo_host.
-
#repo_key ⇒ Object
Returns the value of attribute repo_key.
-
#tags_to_exclude ⇒ Object
Returns the value of attribute tags_to_exclude.
Instance Method Summary collapse
- #cleanup! ⇒ Object
-
#initialize(repo_host:, repo_key:, folder:, days_old:, tags_to_exclude:, user:, password:, most_recent_images:, log_level: ::Logger::INFO, actually_delete: false, delete_empty_path: false) ⇒ DockerCleaner
constructor
A new instance of DockerCleaner.
Constructor Details
#initialize(repo_host:, repo_key:, folder:, days_old:, tags_to_exclude:, user:, password:, most_recent_images:, log_level: ::Logger::INFO, actually_delete: false, delete_empty_path: false) ⇒ DockerCleaner
Returns a new instance of DockerCleaner.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ike_artifactory/docker_cleaner.rb', line 20 def initialize(repo_host:, repo_key:, folder:, days_old:, tags_to_exclude:, user:, password:, most_recent_images:, log_level: ::Logger::INFO, actually_delete: false, delete_empty_path: false) @repo_host = repo_host @repo_key = repo_key @folder = folder @days_old = days_old @tags_to_exclude = || [] @most_recent_images = most_recent_images @actually_delete = actually_delete @delete_empty_path = delete_empty_path @client = IKE::Artifactory::Client.new( :server => repo_host, :repo_key => repo_key, :user => user, :password => password ) @logger = Logger.new(STDOUT) logger.level = log_level end |
Instance Attribute Details
#actually_delete ⇒ Object (readonly)
Returns the value of attribute actually_delete.
17 18 19 |
# File 'lib/ike_artifactory/docker_cleaner.rb', line 17 def actually_delete @actually_delete end |
#client ⇒ Object (readonly)
is not tested
14 15 16 |
# File 'lib/ike_artifactory/docker_cleaner.rb', line 14 def client @client end |
#days_old ⇒ Object
Returns the value of attribute days_old.
11 12 13 |
# File 'lib/ike_artifactory/docker_cleaner.rb', line 11 def days_old @days_old end |
#delete_empty_path ⇒ Object (readonly)
Returns the value of attribute delete_empty_path.
18 19 20 |
# File 'lib/ike_artifactory/docker_cleaner.rb', line 18 def delete_empty_path @delete_empty_path end |
#folder ⇒ Object
Returns the value of attribute folder.
10 11 12 |
# File 'lib/ike_artifactory/docker_cleaner.rb', line 10 def folder @folder end |
#logger ⇒ Object (readonly)
is not tested. Used for testing
16 17 18 |
# File 'lib/ike_artifactory/docker_cleaner.rb', line 16 def logger @logger end |
#most_recent_images ⇒ Object (readonly)
is not tested
15 16 17 |
# File 'lib/ike_artifactory/docker_cleaner.rb', line 15 def most_recent_images @most_recent_images end |
#repo_host ⇒ Object
Returns the value of attribute repo_host.
8 9 10 |
# File 'lib/ike_artifactory/docker_cleaner.rb', line 8 def repo_host @repo_host end |
#repo_key ⇒ Object
Returns the value of attribute repo_key.
9 10 11 |
# File 'lib/ike_artifactory/docker_cleaner.rb', line 9 def repo_key @repo_key end |
#tags_to_exclude ⇒ Object
Returns the value of attribute tags_to_exclude.
12 13 14 |
# File 'lib/ike_artifactory/docker_cleaner.rb', line 12 def @tags_to_exclude end |
Instance Method Details
#cleanup! ⇒ Object
45 46 47 48 49 50 51 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 85 86 87 88 89 |
# File 'lib/ike_artifactory/docker_cleaner.rb', line 45 def cleanup! deleted_images = [] = client.get_images(folder).sort_by { |_k,v| v }.to_h too_new_to_delete = .keys[0...most_recent_images] logger_prefix = "#{repo_host}/#{repo_key}" .each do | tag, tag_days_old | logger.debug "#{logger_prefix}: examining #{tag}" if .include?(tag) logger.info "#{logger_prefix}: tag #{tag} is explicitly excluded from cleanup" next end if too_new_to_delete.include?(tag) logger.info "#{logger_prefix}: tag #{tag} is one of the #{most_recent_images} most recent tags, preserving" next end if tag_days_old < days_old logger.info "#{logger_prefix}: tag #{tag} is less than #{days_old} days old, preserving" next end logger.info "#{logger_prefix}: removing tag #{tag}" if actually_delete client.delete_object "#{folder}/#{tag}" else logger.info("#{logger_prefix}: Not actually deleting #{tag} because actually_delete is falsy") end deleted_images.append(tag) end if delete_empty_path && deleted_images.length() == .length() logger.info "#{logger_prefix}: removing empty path #{folder}" if actually_delete client.delete_object "#{folder}" else logger.info("#{logger_prefix}: Not actually deleting #{folder} because actually_delete is falsy") end end deleted_images end |