Module: Dapp::Dimg::Dapp::Command::CleanupRepo
- Included in:
- Dapp
- Defined in:
- lib/dapp/dimg/dapp/command/cleanup_repo.rb
Constant Summary collapse
- DATE_POLICY =
Time.now.to_i - 60 * 60 * 24 * 30
- GIT_TAGS_LIMIT_POLICY =
10
Instance Method Summary collapse
- #actual_detailed_dimgs_images_by_scheme(registry) ⇒ Object
- #cleanup_repo ⇒ Object
- #cleanup_repo_by_nonexistent_git_base(repo_dimgs_images_by_scheme, dapp_tag_scheme) ⇒ Object
- #cleanup_repo_by_nonexistent_git_primitive(registry, detailed_dimgs_images_by_scheme) ⇒ Object
- #cleanup_repo_by_policies(registry, detailed_dimgs_images_by_scheme) ⇒ Object
- #consistent_git_remote_branches ⇒ Object
- #consistent_git_tags ⇒ Object
- #deployed_docker_images ⇒ Object
- #git_tag_by_consistent_git_tag(consistent_git_tag) ⇒ Object
- #git_tag_by_consistent_tag_name ⇒ Object
- #repo_dimg_image_should_be_ignored?(image) ⇒ Boolean
- #without_kube? ⇒ Boolean
Instance Method Details
#actual_detailed_dimgs_images_by_scheme(registry) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 28 def actual_detailed_dimgs_images_by_scheme(registry) {}.tap do |detailed_dimgs_images_by_scheme| tagging_schemes.each { |scheme| detailed_dimgs_images_by_scheme[scheme] = [] } repo_detailed_dimgs_images(registry).each do |image| next unless repo_dimg_image_should_be_ignored?(image) (detailed_dimgs_images_by_scheme[image[:labels]['dapp-tag-scheme']] ||= []) << image end end end |
#cleanup_repo ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 9 def cleanup_repo lock_repo(repo = option_repo) do log_step_with_indent(repo) do registry = dimg_registry(repo) if git_own_repo_exist? cleanup_repo_by_nonexistent_git_primitive(registry, actual_detailed_dimgs_images_by_scheme(registry)) cleanup_repo_by_policies(registry, actual_detailed_dimgs_images_by_scheme(registry)) end begin repo_dimgs = repo_dimgs_images(registry) repo_dimgstages = repo_dimgstages_images(registry) repo_dimgstages_cleanup(registry, repo_dimgs, repo_dimgstages) end if with_stages? end end end |
#cleanup_repo_by_nonexistent_git_base(repo_dimgs_images_by_scheme, dapp_tag_scheme) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 68 def cleanup_repo_by_nonexistent_git_base(repo_dimgs_images_by_scheme, dapp_tag_scheme) return if repo_dimgs_images_by_scheme[dapp_tag_scheme].empty? log_step_with_indent(:"nonexistent #{dapp_tag_scheme.split('_').join(' ')}") do repo_dimgs_images_by_scheme[dapp_tag_scheme] .select { |dimg_image| dimg_image[:labels]['dapp-tag-scheme'] == dapp_tag_scheme } .each { |dimg_image| yield dimg_image } end end |
#cleanup_repo_by_nonexistent_git_primitive(registry, detailed_dimgs_images_by_scheme) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 44 def cleanup_repo_by_nonexistent_git_primitive(registry, detailed_dimgs_images_by_scheme) %w(git_tag git_branch git_commit).each do |scheme| cleanup_repo_by_nonexistent_git_base(detailed_dimgs_images_by_scheme, scheme) do |detailed_dimg_image| delete_repo_image(registry, detailed_dimg_image) unless begin case scheme when 'git_tag' then .include?(detailed_dimg_image[:tag]) when 'git_branch' then consistent_git_remote_branches.include?(detailed_dimg_image[:tag]) when 'git_commit' then git_own_repo.commit_exists?(detailed_dimg_image[:tag]) else raise end end end unless detailed_dimgs_images_by_scheme[scheme].empty? end end |
#cleanup_repo_by_policies(registry, detailed_dimgs_images_by_scheme) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 77 def cleanup_repo_by_policies(registry, detailed_dimgs_images_by_scheme) %w(git_tag git_commit).each_with_object([]) do |scheme, dimgs_images| dimgs_images.concat begin detailed_dimgs_images_by_scheme[scheme].map do |dimg| next if dry_run? && begin if scheme == 'git_tag' !.include?(dimg[:tag]) elsif scheme == 'git_commit' !git_own_repo.commit_exists?(dimg[:tag]) end end dimg[:created_at] = begin if scheme == 'git_tag' git_own_repo.tag_at(git_tag_by_consistent_git_tag(dimg[:tag])) elsif scheme == 'git_commit' git_own_repo.commit_at(dimg[:tag]) end end dimg end.compact end end.tap do |detailed_dimgs_images| sorted_detailed_dimgs_images = detailed_dimgs_images.sort_by { |dimg| dimg[:created_at] } expired_dimgs_images, not_expired_dimgs_images = sorted_detailed_dimgs_images.partition do |dimg_image| dimg_image[:created_at] < DATE_POLICY end log_step_with_indent(:"date policy (before #{DateTime.strptime(DATE_POLICY.to_s, '%s')})") do expired_dimgs_images.each { |dimg| delete_repo_image(registry, dimg) } end {}.tap do |images_by_dimg| not_expired_dimgs_images.each { |dimg| (images_by_dimg[dimg[:dimg]] ||= []) << dimg } images_by_dimg.each do |dimg, images| log_step_with_indent(:"limit policy (> #{GIT_TAGS_LIMIT_POLICY}) (`#{dimg}`)") do images[GIT_TAGS_LIMIT_POLICY..-1].each { |dimg| delete_repo_image(registry, dimg) } end unless images[GIT_TAGS_LIMIT_POLICY..-1].nil? end end end end |
#consistent_git_remote_branches ⇒ Object
64 65 66 |
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 64 def consistent_git_remote_branches @consistent_git_remote_branches ||= git_own_repo.remote_branches.map(&method(:consistent_uniq_slugify)) end |
#consistent_git_tags ⇒ Object
60 61 62 |
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 60 def git_tag_by_consistent_tag_name.keys end |
#deployed_docker_images ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 128 def deployed_docker_images return [] if without_kube? # open kube client, get all pods and select containers' images ::Dapp::Kube::Kubernetes::Client.tap do |kube| config_file = kube.kube_config_path unless File.exist?(config_file) return [] end end client = ::Dapp::Kube::Kubernetes::Client.new namespaces = [] # check connectivity for 2 seconds begin namespaces = client.namespace_list(excon_parameters: {:connect_timeout => 30}) rescue Excon::Error::Timeout raise ::Dapp::Error::Default, code: :kube_connect_timeout end # get images from containers from pods from all namespaces. @kube_images ||= namespaces['items'].map do |item| item['metadata']['name'] end.map do |ns| client.with_namespace(ns) do client.pod_list['items'].map do |pod| pod['spec']['containers'].map{ |cont| cont['image'] } end end end.flatten.uniq.select do |image| image.start_with?(option_repo) end end |
#git_tag_by_consistent_git_tag(consistent_git_tag) ⇒ Object
120 121 122 |
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 120 def git_tag_by_consistent_git_tag(consistent_git_tag) git_tag_by_consistent_tag_name[consistent_git_tag] end |
#git_tag_by_consistent_tag_name ⇒ Object
124 125 126 |
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 124 def git_tag_by_consistent_tag_name @git_consistent_tags ||= git_own_repo..map { |t| [consistent_uniq_slugify(t), t] }.to_h end |
#repo_dimg_image_should_be_ignored?(image) ⇒ Boolean
38 39 40 41 42 |
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 38 def repo_dimg_image_should_be_ignored?(image) image_repository = [option_repo, image[:dimg]].compact.join('/') image_name = [image_repository, image[:tag]].join(':') !deployed_docker_images.include?(image_name) end |
#without_kube? ⇒ Boolean
163 164 165 |
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 163 def without_kube? !![:without_kube] end |