File: removeOldImages.py

package info (click to toggle)
subuser 0.6.2-3
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 4,208 kB
  • sloc: python: 5,201; sh: 380; makefile: 73
file content (70 lines) | stat: -rw-r--r-- 2,976 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
# -*- coding: utf-8 -*-

#external imports
from collections import OrderedDict
#internal imports
import subuserlib.verify
import subuserlib.print
import subuserlib.classes.docker.dockerDaemon as dockerDaemon

def getInstalledImagesThatAreInUse(user):
  """
  Returns a dictionary of currently installed images that are currently used by a subsuser directly, or indirectly (as a dependency of another image).

  Returns {imageId(string) : InstalledImage}
  """
  installedImagesThatAreInUse = OrderedDict() # {imageId : installedImage}
  for _,subuser in user.registry.subusers.items():
    if subuser.imageId:
      try:
        installedImage = subuser.user.installedImages[subuser.imageId]
        for inUseInstalledImage in installedImage.getImageLineage():
          installedImagesThatAreInUse[inUseInstalledImage.imageId] = inUseInstalledImage
      except KeyError:
        user.registry.log("Warning: No image for %s installed."%subuser.name)
  return installedImagesThatAreInUse

def removeOldImages(user,dryrun=False,yes=False,sourceRepo=None,imageSourceName=None):
  installedImagesThatAreInUse = getInstalledImagesThatAreInUse(user)
  imagesToBeRemoved = []
  for installedImageId,installedImage in user.installedImages.items():
    if (   (not installedImageId in installedImagesThatAreInUse)
       and (not sourceRepo or installedImage.sourceRepoId == sourceRepo.id)
       and (not imageSourceName or installedImage.imageSourceName == imageSourceName)):
      imagesToBeRemoved.append(installedImage)
  if not imagesToBeRemoved:
    user.registry.log("There are no unused images to be removed.")
    return
  user.registry.log("The following images are uneeded and would be deleted.")
  user.registry.log("DOCKER-ID : SUBUSER-ID")
  # List images to be removed
  for installedImage in imagesToBeRemoved:
    user.registry.log("Removing unneeded image "+installedImage.imageId + " : " + installedImage.imageSource.getIdentifier())
  if dryrun:
    return
  # Ask user if we should continue?
  try:
    user.registry.log("Would you like to remove these images now? [Y/n]:",verbosityLevel=4)
    answer = input("Would you like to remove these images now? [Y/n]:")
    removeImages = not (answer == "n")
  except EOFError: # When not running interactively...
    user.registry.log("")
    removeImages = True
  if yes or removeImages:
    # This is a very basic emulation of a topological sort,
    # so we can remove images which depend on eachother.
    nextRound = imagesToBeRemoved
    didSomething = True
    while nextRound and didSomething:
      thisRound = nextRound
      nextRound = []
      didSomething = False
      for installedImage in thisRound:
        installedImage.removeCachedRuntimes()
        try:
          installedImage.removeDockerImage()
          didSomething = True
        except dockerDaemon.ContainerDependsOnImageException:
          nextRound.append(installedImage)
    subuserlib.verify.verify(user)
    user.registry.commit()