Advertisement
Guest User

Untitled

a guest
May 10th, 2021
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.35 KB | None | 0 0
  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.             for root, dirs, files in os.walk("{}/blobs/sha256/{}/{}".format(registry_path, layer[:2], layer)):
  25.                 imagesize += (sum(getsize(join(root, name)) for name in files))
  26.         repos.append({'dir': dir, 'size': imagesize})
  27.  
  28.     for subdir in os.listdir(dir):
  29.         if (os.path.isdir("{}/{}".format(dir, subdir))):
  30.             scan("{}/{}".format(dir, subdir))
  31.  
  32. registry_path = '/var/opt/gitlab/gitlab-rails/shared/registry/docker/registry/v2'
  33. repos = []
  34.  
  35. for dir in os.listdir("{}/repositories".format(registry_path)):
  36.     scan("{}/repositories/{}".format(registry_path, dir))
  37.  
  38. repos.sort(key=lambda k: k['size'], reverse=True)
  39. for repo in repos:
  40.     print("{}: {}".format(repo['dir'], get_human_readable_size(repo['size'])))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement