-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathutdocker.py
154 lines (109 loc) · 3.34 KB
/
utdocker.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
import docker
from pykit import ututil
dd = ututil.dd
# default network config for unittest
net_config = {
'network1': {
'subnet': '192.168.52.0/24',
'gateway': '192.168.52.254',
}
}
def get_client():
dcli = docker.Client(base_url='unix://var/run/docker.sock')
return dcli
def does_container_exist(name):
dcli = get_client()
try:
dcli.inspect_container(name)
return True
except docker.errors.NotFound:
return False
def stop_container(*names):
dcli = get_client()
for name in names:
try:
dcli.stop(container=name)
except Exception as e:
dd(repr(e), ' while trying to stop docker container: ' + repr(name))
def remove_container(*names):
dcli = get_client()
for name in names:
try:
dcli.kill(name)
except Exception as e:
dd(repr(e) + ' while killing container: ' + repr(name))
try:
dcli.remove_container(name)
except Exception as e:
dd(repr(e) + ' while removing container: ' + repr(name))
def create_network():
net_name = 'network1'
dcli = get_client()
try:
dcli.inspect_network(net_name)
return
except docker.errors.NotFound as e:
dd(repr(e))
ipam_pool = docker.utils.create_ipam_pool(**net_config[net_name])
ipam_config = docker.utils.create_ipam_config(
pool_configs=[ipam_pool]
)
dcli.create_network(net_name, driver="bridge", ipam=ipam_config)
def start_container(name, image,
ip=None,
command='',
port_bindings=None,
volume_bindings=None,
env=None):
net_name = 'network1'
dcli = get_client()
kwargs = {}
_config_kwargs = {}
if env is not None:
kwargs['environment'] = env
if port_bindings is not None:
kwargs['ports'] = port_bindings.keys()
_config_kwargs['port_bindings'] = port_bindings
if volume_bindings is not None:
volumes = []
for bind in volume_bindings:
volumes.append(bind.split(':')[1])
kwargs['volumes'] = volumes
_config_kwargs['binds'] = volume_bindings
kwargs['host_config'] = dcli.create_host_config(
**_config_kwargs)
if ip is not None:
net_cfg = dcli.create_networking_config({
net_name: dcli.create_endpoint_config(ipv4_address=ip,)
})
kwargs['networking_config'] = net_cfg
if not does_container_exist(name):
dcli.create_container(
name=name,
image=image,
command=command,
**kwargs
)
dcli.start(container=name)
def pull_image(image):
dcli = get_client()
rst = dcli.images(image)
if len(rst) > 0:
dd(image + ' is ready')
dd(rst)
return
# 'daocloud.io/zookeeper:3.4.10' --> ('daocloud.io/zookeeper', '3.4.10')
rst = dcli.pull(*image.split(':'))
dd(rst)
def build_image(image, path):
dcli = get_client()
rst = dcli.images(image)
if len(rst) > 0:
dd(image + ' is ready')
dd(rst)
return
dd('build: ' + image + ' from ' + path)
for line in dcli.build(path=path,
nocache=True,
tag=image):
dd('build ' + image + ':', line)