-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_basics.py
246 lines (192 loc) · 9.04 KB
/
docker_basics.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# encoding: utf-8
import time
from . import *
import utils
def get_images(filter=None):
""" Get images names, with optional filter on name.
:param filter: if string, get images names containing it, if python container, get images in this set.
:return: a list of images names
"""
images = utils.Command('docker images').stdout_column(0, 1)
if filter:
if isinstance(filter, basestring):
return [x for x in images if filter in x]
else:
return [x for x in images if x in filter]
return images
def get_containers(filter=None, image=None, all=True):
""" Get containers names, with optional filter on name.
:param filter: if string, get containers names containing it, if python container (list, set, ...),
get containers in this set.
:param image: if string, get containers from this image (ignore filter).
:param all: if False, get only running containers, else get all containers.
:return: a list of containers names
"""
docker_cmd = 'docker ps -a' if all else 'docker ps'
if image:
return utils.extract_column(utils.filter_column(utils.Command(docker_cmd).stdout, 1, eq=image), -1)
else:
containers = utils.Command(docker_cmd).stdout_column(-1, 1)
if filter:
if isinstance(filter, basestring):
return [x for x in containers if filter in x]
else:
return [x for x in containers if x in filter]
return containers
def get_networks(filter=None, driver=None):
docker_cmd = 'docker network ls'
if driver:
networks = utils.extract_column(utils.filter_column(utils.Command(docker_cmd).stdout, 2, 1, eq=driver), 1)
else:
networks = utils.Command(docker_cmd).stdout_column(1, 1)
if filter:
if isinstance(filter, basestring):
return [x for x in networks if filter in x]
else:
return [x for x in networks if x in filter]
return networks
def container_stop(*container):
ret = True
for cont in container:
ret &= not utils.command('docker stop ' + cont)
return ret
def container_delete(*container):
ret = True
for cont in container:
ret &= not utils.command('docker rm ' + cont)
return ret
def image_delete(image):
return not utils.command('docker rmi ' + image)
def image_delete_and_containers(image):
""" WARNING: This will remove an image and all its dependant containers
"""
for container in get_containers(image=image):
container_stop(container)
for container in get_containers(image=image, all=True):
container_delete(container)
return image_delete(image)
def docker_build(image, tag=None, context=None):
cmd = 'docker build -f {}/Dockerfile -t {} .'.format(image, tag or image)
print(utils.yellow(cmd))
with utils.cd(context or os.path.join(ROOTDIR, 'images')):
return not utils.Command(cmd, show='Build: ').returncode
def docker_run(image, container, host=None, parameters=None):
cmd = 'docker run -d '
cmd += '--name {} '.format(container)
cmd += '-h {} '.format(host or container)
if parameters:
cmd += parameters + ' '
cmd += image
print(utils.yellow(cmd))
return not utils.command(cmd)
def docker_start(container):
return utils.command('docker start {}'.format(container))
def docker_commit(container, image):
return not utils.command('docker commit {} {}'.format(container, image))
def get_container_ip(container, raises=False):
docker_cmd = utils.Command("docker inspect --format '{{ .NetworkSettings.IPAddress }}' %s" % container)
if raises and docker_cmd.stderr:
raise RuntimeError("Container {} is not running".format(container))
return docker_cmd.stdout.strip()
def docker_exec(cmd, container, user=None, raises=False, status_only=False, stdout_only=True):
""" Executes a command on a running container via 'docker exec'
:param cmd: the command to execute
:param container: the target container
:param user: an optional user (defaults to root)
:param raises: if True, will raise a RuntimeError exception if command fails (return code != 0)
:param status_only: If True, will return True if command succeeds, False if it fails
:param stdout_only: If True, will return stdout as a string (default=True)
:return: a subprocess.Popen object, or a string if stdout_only=True, or a boolean if status_only=True
"""
docker_cmd = 'docker exec -i {} {} {}'.format('-u {}'.format(user) if user else '', container, cmd)
dock = utils.Command(docker_cmd)
if raises and dock.returncode:
raise RuntimeError(
"Error while executing <{}> on {}: [{}]".
format(docker_cmd, container, dock.stderr.strip() or dock.returncode))
if status_only:
return not dock.returncode
if stdout_only:
return dock.stdout
return dock
def docker_network(name, cmd='create', raises=True):
allowed = ('create', 'remove')
if cmd not in allowed:
raise RuntimeError("Network command must be in {}, found {}".format(allowed, cmd))
ret = utils.command('docker network {} {}'.format(cmd, name))
if ret and raises:
raise RuntimeError("Could not {} network {}".format(cmd, name))
def network_connect(network, container):
if utils.command('docker network connect {} {}'.format(network, container)):
raise RuntimeError("Could not connect {} to network {}".format(container, network))
def put_data(data, dest, container, append=False, user=None, perms=None):
""" Copy data to a file with optional append and user/perms settings.
:param data: byte string of data
:param dest: file path on target container. The directory must exist
:param container: container name
:param append: if True, the data is appended to the file, otherwise, the file is created or overwritten
:param user: if not None, set user of dest to this user
:param perms: if not None, set perms of dest to these perms. Format like chmod
"""
if append and not path_exists(dest, container):
docker_exec('touch {}'.format(dest), container)
docker_cmd = 'docker exec -i {} /bin/bash -c "cat {} {}"'.format(container, '>>' if append else '>', dest)
utils.command_input(docker_cmd, data, raises=True)
if user:
path_set_user(dest, user, container)
if perms:
set_permissions(dest, perms, container)
def put_file(source, dest, container, user=None, perms=None):
docker_cmd = 'docker cp {} {}:{}'.format(source, container, dest)
utils.command(docker_cmd, raises=True)
if user:
path_set_user(dest, user, container)
if perms:
set_permissions(dest, perms, container)
def put_directory(source, dest, container):
docker_exec('mkdir -p {}'.format(dest), container, raises=True)
with utils.cd(source):
ret = utils.command('tar zc * | docker exec -i {} tar zx -C {}'.format(container, dest))
if ret:
raise RuntimeError("Error while copying {} to {}:{}".format(source, container, dest))
def get_data(source, container):
return docker_exec('cat {}'.format(source), container, raises=True)
def path_exists(path, container):
return docker_exec('test -e {}'.format(path), container, status_only=True)
def create_user(user, container, groups=(), home=None, shell=None):
""" Create a user with optional groups, home and shell
"""
cmd = 'useradd {}{}{}'.\
format(user,
' -d {}'.format(home) if home else '',
' -s {}'.format(shell) if shell else '')
docker_exec(cmd, container)
existing_groups = utils.extract_column(docker_exec('cat /etc/group', container), 0, sep=':')
for group in groups:
if group not in existing_groups:
docker_exec('addgroup {}'.format(group), container)
docker_exec('usermod -a -G {} {}'.format(group, user), container)
def path_set_user(path, user, container, group=None, recursive=False):
cmd = 'chown{} {}{} {}'.format(' -R' if recursive else '', user, ':{}'.format(group) if group else '', path)
return docker_exec(cmd, container, status_only=True, raises=True)
def set_permissions(path, perms, container, recursive=False):
cmd = 'chmod{} {} {}'.format(' -R' if recursive else '', perms, path)
return docker_exec(cmd, container, status_only=True, raises=True)
def get_version(app, container):
output = docker_exec('apt-cache policy {}'.format(app), container, user='root')
try:
return utils.extract_column(utils.filter_column(output, 0, startswith='Install'), 1, sep=':')[0]
except IndexError:
pass
def wait_running_process(cmd, container, timeout=1):
count, step = timeout, 0.2
while count > 0:
if cmd in utils.extract_column(docker_exec('ps -A', container, user='root'), -1, 1):
return True
time.sleep(step)
count -= step
def get_processes(container, filter=None):
processes = utils.extract_column(docker_exec('ps -A', container, user='root'), -1, 1)
if filter is None:
return processes
return [proc for proc in processes if filter in proc]