-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-clean.py
executable file
·166 lines (134 loc) · 5.5 KB
/
docker-clean.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2021 Jair Reis <[email protected]>
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""
This python CLI application was inspired by an article about how
remove images, containers and volumes.
All in order to help clean up development environments,
after the great ease of creating docker environments.
The script performs a cleanup on the development machine.
Reference article: http://www.macoratti.net/19/02/dock_limp1.htm
"""
import os
import sys
import logging
import argparse
# Setting terminal colors and ascii art header
RED = "\033[31;1m"
GREEN = "\033[32;1m"
CIANO = "\033[36;1m"
BLUE = "\033[1;94m"
RESET = "\033[0;0m"
# Setting header
HEADER = f"""{BLUE}
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡟⠛⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⣀⣀⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⠀⠀⢸⡇⠀⠀⡇⠀⠀⢸⣿⣿⣿⣿⣿⡟⠻⣿⣿⣿⣿⣿⣿
⣿⣿⣿⡿⠿⠿⣿⠶⠶⢾⡷⠶⠶⡷⠶⠶⢾⠿⠿⠿⣿⣿⠀⠀⠈⣿⣿⣿⣿⣿
⣿⣿⣿⡇⠀⠀⣿⠀⠀⢸⡇⠀⠀⡇⠀⠀⢸⠀⠀⠀⣿⣿⡀⠀⠀⠀⠀⠀⢉⣿
⣿⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠀⠀⢀⣀⣠⣴⣿⣿
⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣾⣿⣿⣿⣿⣿
⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣾⣿⣿⣿⣿⣿⣿
⣿⣿⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣷⣶⣦⣤⣤⣤⣤⣤⣤⣶⣶⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
DOCKER-CLEAN
by Jair Reis
==============================
{RESET}"""
# List of commands
INFO = "docker info"
CLEAR_IMAGES = "docker rmi $(docker images -q)"
CLEAR_CONTAINERS = "docker rm $(docker ps -a -q)"
CLEAR_VOLUMES = "docker volume rm $(docker volume ls -q)"
CLEAR_NETWORKS = "docker network rm $(docker network ls -q)"
# Setting the logging configurations
logging.basicConfig(format='%(message)s - docker-clean - %(asctime)s',
level=logging.DEBUG)
def show_header():
"""Show the script intro."""
os.system("clear")
print(HEADER)
def show_docker_infos():
"""Show all info about docker"""
logging.debug(f"📝 {BLUE}Show docker info{RESET}")
os.system("docker info")
def clear_container():
"""This function clear all containers"""
# cleaning containers
logging.debug(f"🗑 {BLUE}Cleaning containers{RESET}")
os.system(CLEAR_CONTAINERS)
#docker rm $(docker ps -a -q) -f(docker rm $(docker ps -a -q) -f)
def clear_images():
"""This function clear all images"""
logging.debug(f"🗑 {BLUE}Cleaning images{RESET}")
os.system(CLEAR_IMAGES)
#docker rmi $(docker images -q) -f(docker rmi $(docker images -q) -f)
def clear_volumes():
"""This function clear all volumes"""
# Cleaning volumes
logging.debug(f"🗑 {BLUE}Cleaning volumes{RESET}")
os.system(CLEAR_VOLUMES)
def clear_networks():
"""This function clear all networks"""
# Cleaning volumes
logging.debug(f"🗑 {BLUE}Cleaning networks{RESET}")
os.system(CLEAR_NETWORKS)
def clear_all():
"""Clear all containers, volumes, images and networks"""
logging.debug(f"🗑 {BLUE}Cleaning all{RESET}")
clear_container()
clear_images()
clear_volumes()
clear_networks()
def main():
# Setting command line
parser = argparse.ArgumentParser(
prog='DOCKER-CLEAN',
description= "Cleaning docker enviroment",
epilog='''This cli app, help clean all images,
containers, volumes and networks
in docker development environment.''')
parser.add_argument('--info', help="Show dockers infos", action='store_true')
parser.add_argument('--images', '-i', help="Remove all docker images",
action='store_true')
parser.add_argument('--containers', '-c', help="Remove all docker containers",
action='store_true')
parser.add_argument('--volumes', '-v', help="Remove all docker volumens",
action='store_true')
parser.add_argument('--networks', '-n', help="Remove all networks",
action='store_true')
parser.add_argument('--all', '-a', help="Cleaning all docker enviroment",
action='store_true')
args = parser.parse_args()
if args.info:
show_docker_infos()
elif args.images:
clear_images()
elif args.containers:
clear_container()
elif args.volumes:
clear_volumes()
elif args.networks:
clear_networks()
elif args.all:
clear_all()
if __name__ == "__main__":
# Call the header function
show_header()
sys.exit(main())