-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdockers.py
More file actions
75 lines (49 loc) · 2.8 KB
/
Copy pathdockers.py
File metadata and controls
75 lines (49 loc) · 2.8 KB
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
# definition of docker properties here
import docker
class Docker:
def __init__(self) -> None:
self.client = docker.from_env()
class Image(Docker):
def __init__(self):
super().__init__()
def display_all_images(self, name: str=None, all: bool=True)-> list:
return self.client.images.list(name, all=all)
def display_image(self, image_name: str) -> str:
return self.client.images.get(image_name)
def pull_image(self, repository: str, tag: str = None)-> str:
return self.client.images.pull(repository, tag=tag)
def push_image(self, repository:str, tag:str = None) -> str:
return self.client.images.push(repository, tag=tag)
def remove_image(self, image: str) ->str:
return self.client.images.remove(image)
def search_image(self, term: str, limit: int)-> list:
return self.client.images.search(term,limit)
def tag_image(self, repository: str, tag: str, force: bool)->bool:
return self.client.images.tag(repository,tag,force)
def build_image(self, path: str, tag: str):
return self.client.images.build(path=path, tag=tag)
class Container(Docker):
def __init__(self):
super().__init__()
def run_container(self, image: str,detach : bool = True):
return self.client.containers.run(image, detach=detach)
def create_container(self, image: str, command: None)->str:
return self.client.containers.create(image, command)
def display_all_container(self, all: bool=True, since: str =None, before: str=None,limit: int=-1)->list:
return self.client.containers.list(all=all,since=since, before=before, limit=limit)
def start_container(self, container_name:str):
return self.client.containers.get(container_name).start()
def stop_container(self, container_name:str, time_out: int=10):
return self.client.containers.get(container_name).stop(time_out=time_out)
def restart_container(self, container_name:str, time_out:int=10):
return self.client.containers.get(container_name).restart(time_out=time_out)
# for formating time
#FIXME: handle the case when we have no images hence no time
def truncate_microseconds(timestamp: str) -> str:
# Split the timestamp into date, time, and microseconds
date, time, microseconds = timestamp[:-1].split("T")[0], timestamp[:-1].split("T")[1].split(".")[0], timestamp[:-1].split("T")[1].split(".")[1]
# Truncate the microseconds to 6 decimal places
truncated_microseconds = microseconds[:6]
# Combine the date, time, and truncated microseconds
truncated_timestamp = f"{date}T{time}.{truncated_microseconds}Z"
return truncated_timestamp