-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean.py
82 lines (61 loc) · 2.29 KB
/
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
import subprocess
def run_command(command):
"""
Executes a shell command and captures any errors.
:param command: The command to run as a list.
"""
try:
subprocess.check_call(command)
except subprocess.CalledProcessError as e:
print(f"Error executing command: {' '.join(command)}", e)
def is_container_exist(container_name):
"""
Checks if a Docker container exists.
:param container_name: The name of the Docker container.
:return: True if the container exists, False otherwise.
"""
try:
subprocess.check_output(["docker", "inspect", "--type=container", container_name])
return True
except subprocess.CalledProcessError:
return False
def remove_container(container_name):
"""
Stops and removes a Docker container if it exists.
:param container_name: The name of the container to remove.
"""
if not is_container_exist(container_name):
print(f"Container '{container_name}' does not exist.")
return
print(f"Stopping and removing container: {container_name}")
run_command(["docker", "container", "stop", container_name])
run_command(["docker", "container", "rm", container_name])
def is_network_exist(network_name):
"""
Checks if a Docker network exists.
:param network_name: The name of the Docker network.
:return: True if the network exists, False otherwise.
"""
try:
subprocess.check_output(["docker", "network", "inspect", network_name])
return True
except subprocess.CalledProcessError:
return False
def remove_network(network_name):
"""
Removes a Docker network if it exists.
:param network_name: The name of the network to remove.
"""
if not is_network_exist(network_name):
print(f"Network '{network_name}' does not exist.")
return
print(f"Removing network: {network_name}")
run_command(["docker", "network", "rm", network_name])
def main():
container_names = ["ros-master", "ros-talker", "ros-listener"]
network_name = "ros-network"
for container_name in container_names:
remove_container(container_name)
remove_network(network_name)
if __name__ == "__main__":
main()