-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
59 lines (53 loc) · 2.07 KB
/
utils.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
from helpers import generate_uuid, logger
from config import CONSUMER_IMAGE, MU_NETWORK, CONTAINER_LABEL, COMPOSE_PROJECT
import docker
def create_container(endpoint, options):
environment_options = options if options else {}
client = docker.from_env()
environment_options["LDES_ENDPOINT_VIEW"] = endpoint
dataset_uuid = generate_uuid()
environment_options["MU_APPLICATION_GRAPH"] = f"http://datasets.vocabsearch.local/{dataset_uuid}"
environment_options["LDES_STREAM"] = f"http://datasets.vocabsearch.local/{dataset_uuid}"
environment_options["SPARQL_BATCH_SIZE"] = "150"
container_labels = [CONTAINER_LABEL]
# Make consumers show up in same overview for docker-compose ps
if COMPOSE_PROJECT:
container_labels = {
CONTAINER_LABEL: None,
"com.docker.compose.project": COMPOSE_PROJECT
}
container = client.containers.run(
CONSUMER_IMAGE,
detach=True,
environment=environment_options,
network=MU_NETWORK,
restart_policy = {"Name": "always" },
labels=container_labels
)
return container_to_json_view(container)
def container_to_json_view(container):
container_env = dict(x.split("=") for x in container.attrs["Config"]["Env"])
view = {
"id": container.attrs["Id"],
"type": "ldes-consumer",
"attributes": {
"status": container.attrs["State"]["Status"],
"feed-url": container_env["LDES_ENDPOINT_VIEW"],
"dereference-members": container_env["LDES_DEREFERENCE_MEMBERS"],
"requests-per-minute": container_env["LDES_REQUESTS_PER_MINUTE"],
"replace-versions": container_env["REPLACE_VERSIONS"],
"graph": container_env["MU_APPLICATION_GRAPH"],
"dataset": container_env["DATASET_URL"]
}
}
return view
def list_containers():
client = docker.from_env()
containers = client.containers.list(filters = { "label": [CONTAINER_LABEL]})
results = map(container_to_json_view, containers)
return {
"links": {
"self": "/ldes-consumers"
},
"data": list(results)
}