-
Notifications
You must be signed in to change notification settings - Fork 0
/
dkr.py
52 lines (43 loc) · 1.13 KB
/
dkr.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
import os, os.path, pwd, grp
import docker
import logging as log
from util import load_env
load_env(ns=globals())
def build_gdal():
client = docker.from_env()
user = pwd.getpwnam(DKR_USER)
group = grp.getgrnam(DKR_GROUP)
user_id = user[2]
group_id = group[2]
gdal_image = client.images.build(
path=DKR_BUILD_DIR_HOST,
tag=DKR_IMAGE_TAG,
buildargs={
'DKR_BUILD_DIR': DKR_BUILD_DIR,
'DKR_USER': DKR_USER,
'DKR_GROUP': DKR_GROUP,
'DKR_USER_ID': str(user_id),
'DKR_GROUP_ID': str(group_id)
}
)
def run_gdal(cmd, volumes=None):
client = docker.from_env()
containers = client.containers.list()
if DKR_CONTAINER_NAME in [ c.name for c in containers ]:
log.info("Already running!")
return
try:
container = client.containers.run(DKR_IMAGE_TAG,
command=cmd,
name=DKR_CONTAINER_NAME,
network_mode='host',
auto_remove=True,
volumes=volumes,
tty=True,
detach=True,
)
for chunk in container.logs(stream=True):
print(chunk.decode('UTF-8'), end='')
container.wait()
except Exception as e:
log.error(e)