Skip to content

Commit 6a239ad

Browse files
authored
Fixed Linting Errors
1 parent 6832853 commit 6a239ad

File tree

2 files changed

+142
-101
lines changed

2 files changed

+142
-101
lines changed

old_docker_images_auto_delete/README.md

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,34 @@ During Containerized Deployment of applications, often some VMs are particularly
55
Since the VMs have a fixed Disk Size, often storage issues arise, which lead to failure in the building of the containers leading to breakage in the CICD pipeline.
66

77
Therefore it's necessary to remove older versions of the container images on a regular basis to avoid storage issues.
8-
A script has to be written, which would perform the clean-up process based on the image tags.
98

10-
It will preserve all the docker images containing the latest tag.
9+
**This script will preserve all the docker images containing the latest tag.
1110
For a particular repository, the tag with the highest number would be preserved.
12-
A provision is made to add exception images that would be never stopped.
11+
A provision is made to add exception images that would be never stopped.**
1312

14-
---
15-
<br>
13+
## Setup instructions
1614

17-
## Rules Implemented:
18-
19-
1. All images with 'latest' tag would not be touched.
20-
2. For a particular repository, the tag with the highest number would be preserved.
21-
3. A provision is made to add exception images which would be never stopped.
15+
Make sure to have Docker Installed in your System.
16+
Run the `main.py` file.
17+
18+
19+
## Detailed explanation of script
20+
21+
- All images with 'Alpine, Buster, Slim & Latest' tag would not be touched.
22+
- For a particular repository, the tag with the highest number would be preserved.
23+
- A provision is made to add exception images which would be never stopped.
24+
25+
## Output
26+
27+
```
28+
Deleting <Old Docker Image Name:Version>...
29+
```
30+
31+
## Author(s)
32+
33+
Avik Kundu
34+
35+
## Disclaimer
36+
37+
Be Careful Before Running this code.
38+
Docker Image deletion cannot be reverted back.

old_docker_images_auto_delete/main.py

Lines changed: 115 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,145 @@
11
# Python Program to auto-delete old docker images
22
"""
33
Rules:
4-
54
1. All images with 'latest' tag would not be touched.
65
2. For a particular repository, the tag with the highest number would be preserved.
76
3. A provision is made to add exception images which would be never stopped.
8-
97
"""
108

119
import subprocess as sp
1210
import re
13-
import operator, itertools
11+
import operator
12+
import itertools
13+
1414

1515
class DeleteImage:
16-
16+
"""
17+
Deleting Old Docker Images
18+
"""
1719
def __init__(self):
18-
self.imgList = []
19-
self.hashList = []
20-
20+
self.img_list = []
21+
self.hash_list = []
2122

22-
# Storing All the Docker Image Details Found on the System to a File
23-
def getAll(self):
24-
file = open("temp.txt","r+")
23+
24+
def get_all(self):
25+
"""
26+
Storing All the Docker Image Details Found on the System to a File
27+
"""
28+
file = open("temp.txt","r+" , encoding='utf-8')
2529
file.truncate(0)
26-
sp.run("sudo docker image list --format '{{.Repository}}~{{.Tag}}' >> temp.txt", shell=True , capture_output=True)
30+
(sp.run("sudo docker image list --format '{{.Repository}}~{{.Tag}}' >> temp.txt",
31+
shell=True , capture_output=True, check=True))
2732
file.close()
28-
29-
30-
#Add other exceptions here
31-
def isExcluded(self , tag):
32-
33-
reg = r"alpine|buster|slim|latest" #Excluding all the images with Alpine, Buster, Slim & Latest Tags
34-
m = re.search(reg, tag)
35-
if m != None:
36-
return 0
33+
34+
35+
36+
def is_excluded(self , tag):
37+
"""
38+
Add other exceptions here
39+
"""
40+
#Excluding all the images with Alpine, Buster, Slim & Latest Tags
41+
reg = r"alpine|buster|slim|latest"
42+
flag = re.search(reg, tag)
43+
if flag is not None:
44+
return 0
3745
return 1
38-
39-
# Loading data from the File to the Python program
40-
def loadAll(self):
41-
f = open("temp.txt", "r")
42-
43-
for line in f:
46+
47+
48+
def load_all(self):
49+
"""
50+
Loading data from the File to the Python program
51+
"""
52+
file = open("temp.txt", "r" , encoding='utf-8')
53+
54+
for line in file:
4455
line = line.rstrip("\n")
4556
image = line.split('~')
46-
if self.isExcluded(image[1]):
47-
48-
regex = r"^(((\d+\.)?(\d+\.)?(\*|\d+)))(\-(dev|stage|prod))*$"
57+
if self.is_excluded(image[1]):
58+
59+
regex = r"^(((\d+\.)?(\d+\.)?(\*|\d+)))(\-(dev|stage|prod))*$"
4960
match = re.search(regex, image[1])
50-
51-
imgDict = { 'Repository':image[0] , 'Tag': match.group(2) }
52-
self.imgList.append(imgDict)
53-
f.close()
54-
55-
56-
def manData(self):
61+
62+
img_dict = { 'Repository':image[0] , 'Tag': match.group(2) }
63+
self.img_list.append(img_dict)
64+
file.close()
65+
66+
67+
def man_data(self):
68+
"""
69+
Manipulating Data to perform the reqd operation
70+
"""
5771
key = operator.itemgetter('Repository')
58-
b = [{'Repository': x, 'Tag': [d['Tag'] for d in y]}
59-
for x, y in itertools.groupby(sorted(self.imgList, key=key), key=key)]
60-
61-
self.imgList.clear()
62-
self.imgList = b.copy()
63-
64-
65-
# Sorting Tags according to the Version Numbers
66-
def sortTag(self):
67-
68-
for img in self.imgList:
69-
temp = img['Tag'].copy()
70-
71-
for n, i in enumerate(img['Tag']):
72-
img['Tag'][n] = int(i.replace('.', ''))
73-
74-
maxLen = len(str(max(abs(x) for x in img['Tag'])))
75-
templateString = '{:<0' + str(maxLen) + '}'
76-
finalList = []
77-
78-
for n, i in enumerate(img['Tag']):
79-
finalList.append(templateString.format(i))
80-
72+
b_key = [{'Repository': x, 'Tag': [d['Tag'] for d in y]}
73+
for x, y in itertools.groupby(sorted(self.img_list, key=key), key=key)]
74+
75+
self.img_list.clear()
76+
self.img_list = b_key.copy()
77+
78+
79+
def sort_tag(self):
80+
"""
81+
Sorting Tags according to the Version Numbers
82+
"""
83+
84+
for img in self.img_list:
85+
temp = img['Tag'].copy()
86+
87+
for new, i in enumerate(img['Tag']):
88+
img['Tag'][new] = int(i.replace('.', ''))
89+
90+
max_len = len(str(max(abs(x) for x in img['Tag'])))
91+
template_string = '{:<0' + str(max_len) + '}'
92+
final_list = []
93+
94+
for new, i in enumerate(img['Tag']):
95+
final_list.append(template_string.format(i))
96+
8197
for i in range(0 , len(img['Tag'])):
82-
hashMap = { 'TagsManipulated': finalList[i] , 'TagsOriginal': temp[i] }
83-
self.hashList.append(hashMap)
84-
85-
finalList.sort()
86-
98+
hash_map = { 'TagsManipulated': final_list[i] , 'TagsOriginal': temp[i] }
99+
self.hash_list.append(hash_map)
100+
101+
final_list.sort()
102+
87103
img['Tag'].clear()
88-
img['Tag'].extend(finalList[:-1])
89-
90-
print(self.hashList)
91-
print (self.imgList)
92-
93-
94-
def hashFunc(self , tag):
95-
for hashMap in self.hashList:
96-
if tag == hashMap['TagsManipulated']:
97-
t = hashMap['TagsOriginal']
104+
img['Tag'].extend(final_list[:-1])
105+
106+
print(self.hash_list)
107+
print (self.img_list)
108+
109+
110+
def hash_function(self , tag):
111+
"""
112+
Hash Function for Error Detection
113+
"""
114+
for hash_map in self.hash_list:
115+
if tag == hash_map['TagsManipulated']:
116+
temp = hash_map['TagsOriginal']
98117
break
99118
else:
100-
t = 'Error in Manipulation'
101-
return t
102-
103-
# Running the Docker RMI Command to Delete the Older Versions
104-
def removeImage(self):
105-
for img in self.imgList:
119+
temp = 'Error in Manipulation'
120+
return temp
121+
122+
123+
def remove_image(self):
124+
"""
125+
Running the Docker RMI Command to Delete the Older Versions
126+
"""
127+
for img in self.img_list:
106128
if img['Tag']:
107129
for tag in img['Tag']:
108-
val = self.hashFunc(tag)
109-
imageURL = img['Repository'] + ":" + val
110-
print ("Deleting Image : " + imageURL )
111-
sp.run("sudo docker rmi " + imageURL, shell=True , capture_output=True)
112-
130+
val = self.hash_function(tag)
131+
image_url = img['Repository'] + ":" + val
132+
print ("Deleting Image : " + image_url )
133+
(sp.run("sudo docker rmi " + image_url,
134+
shell=True,capture_output=True , check=True))
135+
136+
113137
# Main Function
114138
if __name__ == "__main__":
115-
139+
116140
docker = DeleteImage()
117-
docker.getAll()
118-
docker.loadAll()
119-
docker.manData()
120-
docker.sortTag()
121-
docker.removeImage()
141+
docker.get_all()
142+
docker.load_all()
143+
docker.man_data()
144+
docker.sort_tag()
145+
docker.remove_image()

0 commit comments

Comments
 (0)