-
Notifications
You must be signed in to change notification settings - Fork 0
/
kleio_server.py
878 lines (727 loc) · 29.3 KB
/
kleio_server.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
""" Interface to Kleio Server"""
import logging
import socket
import os
from time import sleep
from typing import List, Tuple
import warnings
import docker
import secrets
import requests
from jsonrpcclient import request, Error, Ok, parse
import urllib.request
from .schemas import KleioFile, TokenInfo
class KleioServer:
"""This class interfaces to a Kleio server through its JSON-RPC api.
It also provides convenience methods
to start a server in Docker locally.
This class is not intended to be used directly.
Use KleioServer.start() and KleioServer.attach() to create instances of KleioServer.
Args:
container (docker.models.containers.Container): runing kleio server container
url (str): kleio server url if running in a different machine (container=None)
token (str): kleio server token if running in a different machine (container=None)
kleio_home (str): kleio server home directory. If None and container is not None
then kleio_home is obtained
from the container. If not none
"""
#: kleio server host
host: str
#: kleio server url
url: str
#: kleio server token
token: str
#: kleio server home directory
kleio_home: str
#: kleio server container
container: docker.models.containers.Container
@staticmethod
def start(
kleio_image: str = "timelinkserver/kleio-server",
kleio_version: str | None = "latest",
kleio_home: str | None = None,
kleio_token: str | None = None,
consistency: str = "cached",
port: int = None,
update: bool = False,
reuse: bool = True,
stop_duplicates: bool = False,
):
"""Starts a kleio server in docker
Args:
image (str): kleio server image, defaults to "timelinkserver/kleio-server"
version (str, optional): kleio-server version, defaults to "latest"
kleio_home (str, optional): kleio home directory,
defaults to None -> current directory
token (str, optional): kleio server token,
defaults to None -> generate a random token
consistency (str, optional): consistency of the volume mount, defaults to "cached"
port (int, optional): port to map to 8088,
defaults to None -> find a free port starting at 8088
update (bool, optional): update kleio server image, defaults to False
reuse (bool, optional): if True, reuse an existing kleio server container
with same keio_home, defaults to True.
stop_duplicates (bool, optional): if True, stop and remove duplicate containers
with same kleio_home, defaults to False.
Returns:
KleioServer: KleioServer object
"""
if kleio_image is None:
kleio_image = "timelinkserver/kleio-server"
if kleio_version is None:
kleio_version = "latest"
if not is_docker_running():
raise RuntimeError("Docker is not running")
container: docker.models.containers.Container = start_kleio_server(
image=kleio_image,
version=kleio_version,
kleio_home=kleio_home,
token=kleio_token,
consistency=consistency,
port=port,
update=update,
reuse=reuse,
stop_duplicates=stop_duplicates,
)
return KleioServer(container)
@staticmethod
def attach(url: str, token: str, kleio_home: str = None):
"""Attach to a already running Kleio Server.
Use this either to attach to a running server
outside docker (for instance in a Prolog session)
or to a server running in another machine (should use
htpps url in that case)
Args:
url (str): kleio server url
token (str): kleio server token
kleio_home (str, optional): kleio server home directory
Returns:
KleioServer: KleioServer object
"""
return KleioServer(url=url, token=token, kleio_home=kleio_home)
@staticmethod
def get_server(kleio_home: str = None, kleio_version="latest"):
"""Check if a kleio server is running in docker mapped to a given kleio home directory.
If yes return a KleioServer object, otherwise return None
If a specific version is required, it can be specified in kleio_version
Args:
kleio_home (str, optional): kleio home directory;
defaults to None -> any kleio home.
Returns:
KleioServer or None: KleioServer object or None
"""
if is_docker_running() is False:
raise Exception("Docker is not running")
container = get_kserver_container(
kleio_home=kleio_home, kleio_version=kleio_version
)
if container is not None:
return KleioServer(container)
else:
return None
@staticmethod
def is_server_running(kleio_home: str = None, kleio_version="latest"):
"""Check if a kleio server is running in docker mapped to a given kleio home directory.
Args:
kleio_home (str, optional): kleio home directory;
defaults to None -> any kleio home.
kleio_version (str, optional): kleio server version; defaults to "latest"
if not specified only checks latest
Return True of False
Args:
kleio_home (str, optional): kleio home directory;
defaults to None -> any kleio home.
Returns:
bool: True if server is running, False otherwise
"""
if is_docker_running() is False:
raise Exception("Docker is not running")
container = get_kserver_container(
kleio_home=kleio_home, kleio_version=kleio_version
)
return container is not None
@staticmethod
def find_local_kleio_home(path: str = None):
"""Find kleio home directory.
Kleio home directory is the directory where Kleio Server finds sources and auxiliary
files like structures, mappings and inferences.
It can be in the current directory, parent directory, or tests directory.
It can be named "kleio-home", "timelink-home", or "mhk-home".
A special case is when the current directory is "notebooks".
In this case, kleio-home is assumed to be the parent directory of "notebooks"
and thus set up as the timelink-project template.
Args:
path (str, optional): path to start searching from;
defaults to None -> current directory.
Returns:
str: kleio home directory
"""
return find_local_kleio_home(path=path)
@staticmethod
def make_token():
"""Get the kleio server token from environment or generate a new one if not set.
Returns:
str: kleio server token
"""
token = os.environ.get("KLEIO_ADMIN_TOKEN")
if token is None:
token = random_token()
os.environ["KLEIO_ADMIN_TOKEN"] = token
return token
def __init__(
self,
container: docker.models.containers.Container = None,
url: str = None,
token: str = None,
kleio_home: str = None,
):
"""Not to be used directly.
See :py:meth:`KleioServer.start` and :meth:`KleioServer.attach`
To start a kleio server locally in docker use :meth:`KleioServer.start`
To attach to a running kleio server, local or remote, use :meth:`KleioServer.attach`
Args:
container (docker.models.containers.Container): runing kleio server container;
if container is None, then url, token and kleio_home
must be provided.
url (str): kleio server url if running in a different machine (container=None)
token (str): kleio server token if running in a different machine (container=None)
kleio_home (str): kleio server home directory. If None and container is not None
then kleio_home is obtained from the container.
If not none
"""
if container is None:
if url is None:
raise ValueError("url must be provided if container is None")
if token is None:
raise ValueError("token must be provided if container is None")
if kleio_home is None:
raise ValueError("kleio_home must be provided if container is None")
self.url = url
self.token = token
self.kleio_home = kleio_home
self.container = None
return
if is_docker_running() is False:
raise Exception("Docker is not running")
self.container = container
port = None
try:
port = container.attrs["NetworkSettings"]["Ports"]["8088/tcp"][0][
"HostPort"
]
except KeyError:
pass
if port is None:
port = container.attrs["HostConfig"]["PortBindings"]["8088/tcp"][0][
"HostPort"
]
self.url = f"http://127.0.0.1:{port}"
self.kleio_home = [
mount["Source"]
for mount in container.attrs["Mounts"]
if mount["Destination"] == "/kleio-home"
][0]
self.token = [
env
for env in container.attrs["Config"]["Env"]
if env.startswith("KLEIO_ADMIN_TOKEN")
][0].split("=")[1]
def get_token(self):
"""Get the kleio server token
Returns:
str: kleio server token
"""
return self.token
def get_kleio_home(self):
"""Get the kleio server home directory
Returns:
str: kleio server home directory
"""
return self.kleio_home
def get_container(self):
"""Get the kleio server container
Returns:
docker.models.containers.Container: kleio server container
"""
return self.container
def get_url(self):
"""Get the kleio server url
Returns:
str: kleio server url
"""
return self.url
def __str__(self):
return f"KleioServer(url={self.url}, kleio_home={self.kleio_home})"
def call(self, method: str, params: dict):
"""Call kleio server API
Args:
method (str): kleio server API method
params (dict): kleio server API method parameters
Returns:
dict: kleio server API response
"""
url = f"{self.url}/json/"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {self.token}",
}
# we add the token to the params
params["token"] = self.token
rpc = request(method, params=params)
response = requests.post(url, json=rpc, headers=headers)
parsed = parse(response.json())
if isinstance(parsed, Ok):
return parsed.result
elif isinstance(parsed, Error):
code, message, data, id = parsed
raise Exception(f"Error {code}: {message} ({data} id:{id})")
return response
def stop(self):
"""Stop the kleio server container"""
self.container.stop()
self.container.remove()
def invalidate_user(self, user: str):
"""Invalidate a user
Args:
user (str): user to invalidate
Returns:
dict: kleio server API response
"""
pars = {"user": user}
return self.call("users_invalidate", pars)
def generate_token(self, user: str, info: TokenInfo):
"""Generate a token for a user
Args:
user (str): user to generate token for
info (TokenInfo): token information
Returns:
dict: kleio server API response
"""
pars = {"user": user, "info": info.model_dump()}
return self.call("tokens_generate", pars)
def translation_status(
self, path: str, recurse: str, status: str = None
) -> List[KleioFile]:
"""Get translations from kleio server
:param path: path to the directory in sources
:type path: str
:param recurse: if "yes" recurse in subdirectories
:type recurse: str
:param status: filter by translation status
V = valid translations
T = need translation (source more recent than translation)
E = translation with errors
W = translation with warnings
P = translation being processed
Q = file queued for translation
:type status: str, optional
:return: list of KleioFile objects
:rtype: list[KleioFile]
"""
if status is None:
pars = {"path": path, "recurse": recurse}
else:
pars = {"path": path, "recurse": recurse, "status": status}
translations = self.call("translations_get", pars)
result = []
for t in translations:
kfile = KleioFile(**t)
result.append(kfile)
return result
def translate(self, path: str, recurse: str = "yes", spawn: str = "yes"):
"""Translate sources from kleio server
:param path: path to the directory in sources
:type path: str
:param recurse: if "yes" recurse in subdirectories
:type recurse: str, optional
:param spawn: if "yes" spawn a translation process for each file
:type spawn: str, optional
:return: kleio server API response
:rtype: dict
"""
pars = {"path": path}
if recurse is not None:
pars["recurse"] = recurse
if spawn is not None:
pars["spawn"] = spawn
return self.call("translations_translate", pars)
def translation_clean(self, path: str, recurse: str):
"""clean translations from kleio server
Removes translation results from kleio server.
:param path: path to the directory in sources
:type path: str
:param recurse: if "yes" recurse in subdirectories
:type recurse: str
:return: kleio server API response
:rtype: dict
"""
pars = {"path": path, "recurse": recurse}
return self.call("translations_delete", pars)
def get_sources(self, path: str, recurse: str):
"""Get sources from kleio server
:param path: path to the directory in sources
:type path: str
:param recurse: if "yes" recurse in subdirectories
:type recurse: str
:return: kleio server API response
:rtype: dict
"""
pars = {"path": path, "recurse": recurse}
return self.call("sources_get", pars)
def get_report(self, rpt_url: str) -> str:
"""Get report from kleio server
:param rpt_url: url of the report in the Kleio Server
:type rpt_url: str
:return: kleio server API response
:rtype: dict
"""
if not rpt_url.startswith("/"):
rpt_url = f"/{rpt_url}"
server_url = f"{self.get_url()}{rpt_url}"
return self.get_url_content(server_url)
def get_url_content(self, server_url: str) -> str:
"""Get content from Kleio Server
Args:
server_url (str): url of the content in the Kleio Server
"""
headers = {"Authorization": f"Bearer {self.get_token()}"}
req = urllib.request.Request(server_url, headers=headers)
with urllib.request.urlopen(req) as source:
response_content = source.read().decode("utf-8")
return response_content
def is_docker_running():
"""Check if docker is running"""
try:
client = docker.from_env()
client.ping()
return True
except Exception as exec:
logging.error(f"Could not connect to Docker. Is it running? {exec}")
return False
def find_local_kleio_home(path: str = None):
"""Find kleio home directory.
Kleio home directory is the directory where Kleio Server finds sources and auxiliary
files like structures, mappings and inferences.
It can be in the current directory, parent directory, or tests directory.
It can be named "kleio-home", "timelink-home", or "mhk-home".
Special cases:
* if the current directory is "notebooks", kleio-home is assumed that Kleio home
to be the parent directory of "notebooks"
* if there is a "tests" subdirectory, kleio-home is searched in childs of "tests"
"""
kleio_home = None
timelink_home_names = ["kleio-home", "timelink-home", "mhk-home"]
if path is None:
# get the current directory
current_dir = os.getcwd()
else:
current_dir = path
# get the user home directory
user_home = os.path.expanduser("~")
# check if current_dir is "notebooks"
if os.path.basename(current_dir) == "notebooks":
kleio_home = os.path.dirname(current_dir)
else:
# check if kleio-home exists in current directory,
# parents of current directory up to user_home,
# or tests sub directory of current directory
dir_path = current_dir
while dir_path != user_home:
for home_dir in timelink_home_names:
if os.path.isdir(f"{dir_path}/{home_dir}"):
kleio_home = f"{dir_path}/{home_dir}"
break
if kleio_home:
break
dir_path = os.path.dirname(dir_path)
if dir_path == os.path.dirname(dir_path):
break # we reached the root
if kleio_home is None:
# check if current_dir is "tests"
if os.path.isdir(f"{current_dir}/tests"):
dir_path = f"{current_dir}/tests"
# check if kleio-home exists in tests directory
for home_dir in timelink_home_names:
if os.path.isdir(f"{dir_path}/{home_dir}"):
kleio_home = f"{dir_path}/{home_dir}"
break
if kleio_home is None:
kleio_home = current_dir
return kleio_home
def get_kserver_home(
container: docker.models.containers.Container = None,
container_number: int = 0,
kleio_version="latest",
):
"""Get the kleio server home directory
Args:
container (docker.models.containers.Container, optional): kleio server container;
defaults to None -> get by number.
container_number (int, optional): container number. Defaults to 0.
kleio_version (str, optional): kleio server version; defaults to "latest"
Returns the volume mapped to /kleio-home in the kleio server container"""
if container is None:
container = get_kserver_container_list(kleio_version=kleio_version)[
container_number
]
kleio_home = None
if container is not None:
kleio_home_mount = [
mount["Source"]
for mount in container.attrs["Mounts"]
if mount["Destination"] == "/kleio-home"
]
if len(kleio_home_mount) > 0:
kleio_home = kleio_home_mount[0]
else:
kleio_home = None
return kleio_home
def list_kleio_server_containers(
kleio_version: str = None,
) -> List[Tuple[str, str, int, docker.models.containers.Container]]:
"""List running kleio server containers
Args:
kleio_version (str, optional): kleio server version; defaults
to None -> any version.
Returns:
a tuple (name, kleio_home, port, token, container) for each container
"""
containers: list[docker.models.containers.Container] = get_kserver_container_list(
kleio_version=kleio_version
)
if containers is None or len(containers) == 0:
return []
result = []
for container in containers:
kleio_home = get_kserver_home(container)
port = container.attrs["NetworkSettings"]["Ports"]["8088/tcp"][0]["HostPort"]
token = get_kserver_token(container)
result.append((container.name, kleio_home, port, token, container))
return result
def get_kserver_container(
kleio_home: str = None, kleio_version="latest", stop_duplicates=False
):
"""Check if a kleio server is running in docker, possibly mapped to
a given kleio home directory.
Args:
kleio_home (str, optional): kleio home directory; defaults to None -> any kleio home.
kleio_version (str, optional): kleio server version; defaults to "latest"
if not specified only checks latest
stop_duplicates (bool, optional): if True, stop and remove duplicate containers
with same kleio_home; defaults to False.
Returns:
docker.models.containers.Container: the Kleio server container
"""
containers: list[docker.models.containers.Container] = get_kserver_container_list(
kleio_version=kleio_version
)
if containers is None:
return None
elif kleio_home is not None:
found = False
kleio_home = os.path.abspath(kleio_home)
for container in containers:
kleio_home_mount = [
mount["Source"]
for mount in container.attrs["Mounts"]
if mount["Destination"] == "/kleio-home"
]
if kleio_home_mount[0] == kleio_home:
if not found:
first_found = container
found = True
else:
if stop_duplicates:
container.stop()
container.remove()
else:
break
if not found:
return None
else:
return first_found
else:
return containers[0]
def get_kserver_container_list(
kleio_version: str = None,
) -> None | List[docker.models.containers.Container]:
"""Get the Kleio server containers currently running in docker
Running containers are inspected to detect
those based on images with "kleio-server" on a tag.
Args:
kleio_version (str, optional): kleio server version; defaults to "latest"
Returns:
docker.models.containers.Container: the Kleio server container
"""
if is_docker_running() is False:
raise Exception("Docker is not running")
client: docker.DockerClient = docker.from_env()
allcontainers = client.containers.list()
allimages_tags = [(t, c) for c in allcontainers for t in c.image.tags]
if kleio_version is None:
containers = {c for t, c in allimages_tags if "kleio-server" in t}
else:
containers = {
c for t, c in allimages_tags if f"kleio-server:{kleio_version}" in t
}
if len(containers) > 0:
return list(containers)
else:
return None
def get_kserver_token(
container: docker.models.containers.Container = None,
container_number: int = 0,
kleio_version="latest",
) -> str:
"""Get the Kleio server container admin token
Args:
container (docker.models.containers.Container, optional): kleio server container;
defaults to None -> get the first container.
container_number (int, optional): container number. Defaults to 0.
kleio_version (str, optional): kleio server version; defaults to "latest"
Returns:
str: the kleio server container token
"""
if container is None:
container = get_kserver_container_list(kleio_version=kleio_version)[
container_number
]
token = [
env
for env in container.attrs["Config"]["Env"]
if env.startswith("KLEIO_ADMIN_TOKEN")
][0].split("=")[1]
return token
def find_free_port(from_port: int = 8088, to_port: int = 8099):
for port in range(from_port, to_port + 1):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
s.bind(("localhost", port))
return port
except OSError:
pass
raise OSError("No free ports available in the range 8088-8099")
def start_kleio_server(
image: str = "timelinkserver/kleio-server",
version: str | None = None,
kleio_home: str | None = None,
token: str | None = None,
consistency: str = "cached",
port: int = None,
update: bool = False,
reuse: bool = True,
stop_duplicates: bool = False,
):
"""Starts a kleio server in docker
Args:
image (str, optional): kleio server image. Defaults to "time-link/kleio-server".
version (str | None, optional): kleio-server version; defaults to "latest".
kleio_home (str | None, optional): kleio home directory. Defaults to current directory.
token (str | None, optional): kleio server token; if None -> generate a random token.
consistency (str, optional): consistency of the volume mount. Defaults to "cached".
port (int, optional): port to map to 8088, if none find a free port starting at 8088.
update (bool, optional): update kleio server image. Defaults to False.
reuse (bool, optional): if True, reuse an existing kleio server container
with same keio_home; defaults to True.
stop_duplicates (bool, optional): if True, stop and remove duplicate containers
Usage:
update=True will pull the latest image; if it is newer than existing one
any container running with the same kleio_home will be stopped and removed,
a new container will be started with the new image and reuse=True will be ignored.
"""
# check if kleio server is already running in docker
if is_docker_running() is False:
raise RuntimeError("Docker is not running")
exists = get_kserver_container(
kleio_home=kleio_home, kleio_version=version, stop_duplicates=stop_duplicates
)
client = docker.from_env()
if update is True:
if version is None:
get_version = "latest"
else:
get_version = version
# Pull the latest image
latest_image = client.images.pull('timelinkserver/kleio-server', tag=get_version)
# Get the currently used image
current_image = client.images.get(f'timelinkserver/kleio-server:{get_version}')
# Compare the IDs
if latest_image.id != current_image.id:
logging.info("A newer version is available.")
if exists is not None:
logging.info("Current container was stopped and removed.")
exists.stop()
exists.remove()
exists = None
else:
logging.info("You are using Kleio Server %s (updated)", get_version)
if exists is not None:
if reuse:
return exists
else: # if exists and not reuse stop existing
exists.stop()
exists.remove()
else:
warnings.warn(
f"Kleio server is already running in docker mapped to {kleio_home}",
stacklevel=1,
)
# if kleio_home is None, use current directory
if kleio_home is None:
kleio_home = os.getcwd()
else:
kleio_home = os.path.abspath(kleio_home)
# check if dir exists
if not os.path.exists(kleio_home):
raise FileNotFoundError(f"Directory {kleio_home} does not exist")
# ensure that kleio_home/system/conf/kleio exists
# TODO: remove this
# os.makedirs(f"{kleio_home}/system/conf/kleio", exist_ok=True)
if token is None:
token = random_token()
if port is None:
port = find_free_port(8088, 8099)
kleio_container = client.containers.run(
image=f"{image}:{version}",
detach=True,
ports={"8088/tcp": port},
environment={
"KLEIO_ADMIN_TOKEN": token,
# TODO ports, workers and DEBUG
},
mounts=[
docker.types.Mount(
target="/kleio-home",
source=kleio_home,
type="bind",
read_only=False,
consistency=consistency,
)
],
)
timeout = 15
stop_time = 3
elapsed_time = 0
# this necessary to get the status
cont = client.containers.get(kleio_container.id)
while (
cont.status not in ["running"] and elapsed_time < timeout
):
sleep(stop_time)
cont.reload()
elapsed_time += stop_time
continue
return kleio_container
def random_token(length=32):
"""Generate a random token"""
alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
return "".join(secrets.choice(alphabet) for i in range(length))
def stop_kleio_server(container: docker.models.containers.Container = None):
"""Stop kleio server"""
if is_docker_running() is False:
raise Exception("Docker is not running")
if container is None:
container = get_kserver_container()
container = get_kserver_container()
container.stop()
container.remove()