Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. import re
  2. import subprocess
  3. import sys
  4.  
  5. from collections import namedtuple
  6.  
  7.  
  8. def first_to_last(items):
  9. "Return a new list where last item in items is moved to the front"
  10. return items[-1:] + items[:-1]
  11.  
  12.  
  13. def pad_list(items, pad_to=10, pad_with=''):
  14. "If items is shorter than pad_to, return a new list appending pad_with up to pad_to"
  15. if len(items) >= pad_to:
  16. return items
  17. return items + [pad_with for i in range(0, pad_to - len(items))]
  18.  
  19.  
  20. def namedtuplify(headers, rows):
  21. """
  22. Takes a list of headers and a list of lists that are rows
  23. and returns a list of namedtuples.
  24.  
  25. - removes any whitespaces in column names in headers
  26. - pads any rows that aren't long enough with empty strings
  27. """
  28. max_cols = len(headers)
  29. Row = namedtuple('Row', [col_name.replace(' ', '') for col_name in headers])
  30. rows = [Row(*pad_list(row, pad_to=max_cols)) for row in rows]
  31. return rows
  32.  
  33.  
  34. def get_docker_ps():
  35. # save output of ps -a as list of lists, split by whitespace, and re-organized
  36. # so that the last column is first because ports and status columns alone are
  37. # optionally empty, and we only have spaces to delimit columns, so we want
  38. # those at the end so we can easily just pad them
  39. output = [
  40. first_to_last([i.strip() for i in line.split(' ') if i.strip()])
  41. for line in subprocess.check_output(['docker', 'ps', '-a']).splitlines()
  42. ]
  43. headers, rows = output[0], output[1:]
  44. return namedtuplify(headers, rows)
  45.  
  46.  
  47. def get_docker_images():
  48. output = [
  49. [i.strip() for i in line.split(' ') if i.strip()]
  50. for line in subprocess.check_output(['docker', 'images']).splitlines()
  51. ]
  52. headers, rows = output[0], output[1:]
  53. return namedtuplify(headers, rows)
  54.  
  55.  
  56. def untagged_containers(rows=None):
  57. "List of container ids using untagged images"
  58. rows = rows or get_docker_ps()
  59. return [i.CONTAINERID for i in rows if re.match('[0-9a-f]{12}', i.IMAGE)]
  60.  
  61.  
  62. def untagged_images(rows=None):
  63. "List of image ids that are not tagged"
  64. rows = rows or get_docker_images()
  65. return [row.IMAGEID for row in rows if row.REPOSITORY == '<none>']
  66.  
  67.  
  68. if __name__ == '__main__':
  69. container_ids = untagged_containers()
  70. image_ids = untagged_images()
  71.  
  72. if not container_ids and not image_ids:
  73. print "Nothing to cleanup!"
  74. sys.exit()
  75.  
  76. if container_ids:
  77. print "Removing containers using untagged images..."
  78. for container_id in container_ids:
  79. try:
  80. output = subprocess.check_output(['docker', 'rm', container_id])
  81. except subprocess.CalledProcessError as e:
  82. print e
  83. sys.exit()
  84. print output
  85.  
  86. if image_ids:
  87. print "Removing untagged images..."
  88. for image_id in image_ids:
  89. try:
  90. output = subprocess.check_output(['docker', 'rmi', image_id])
  91. except subprocess.CalledProcessError as e:
  92. print e
  93. sys.exit()
  94. print output
  95.  
  96. print "BOOM"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement