Advertisement
Guest User

Untitled

a guest
Oct 8th, 2021
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python3
  2. # coding: utf-8
  3.  
  4. import os
  5. from os.path import join, getsize
  6. import subprocess
  7.  
  8. def get_human_readable_size(size,precision=2):
  9.     suffixes=['B','KB','MB','GB','TB']
  10.     suffixIndex = 0
  11.     while size > 1024 and suffixIndex < 4:
  12.         suffixIndex += 1
  13.         size = size/1024.0
  14.     return "%.*f%s"%(precision,size,suffixes[suffixIndex])
  15.  
  16.  
  17. def scan(dir):
  18.     if (os.path.isdir("{}/_layers".format(dir))):
  19.         layers = os.listdir("{}/_layers/sha256".format(dir))
  20.         imagesize = 0
  21.         # get image size
  22.         for layer in layers:
  23.             # get size of layer
  24.             layer_dir = "{}/blobs/sha256/{}/{}".format(registry_path, layer[:2], layer)
  25.             if os.path.exists(layer_dir):
  26.               for root, dirs, files in os.walk(layer_dir):
  27.                   imagesize += (sum(getsize(join(root, name)) for name in files))
  28.         repos.append({'dir': dir, 'size': imagesize})
  29.  
  30.     for subdir in os.listdir(dir):
  31.         if (os.path.isdir("{}/{}".format(dir, subdir))):
  32.             scan("{}/{}".format(dir, subdir))
  33.  
  34. registry_path = '/var/opt/gitlab/gitlab-rails/shared/registry/docker/registry/v2'
  35. repos = []
  36.  
  37. for dir in os.listdir("{}/repositories".format(registry_path)):
  38.     scan("{}/repositories/{}".format(registry_path, dir))
  39.  
  40. repos.sort(key=lambda k: k['size'], reverse=True)
  41. for repo in repos:
  42.     print("{}: {}".format(repo['dir'], get_human_readable_size(repo['size'])))
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement