Skip to content

Commit 844ebb4

Browse files
committed
??? update docker
1 parent 6372d8b commit 844ebb4

1 file changed

Lines changed: 82 additions & 56 deletions

File tree

OMPython/OMCSession.py

Lines changed: 82 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,9 +1302,17 @@ class OMCSessionDockerABC(OMCSessionABC, metaclass=abc.ABCMeta):
13021302
def __init__(
13031303
self,
13041304
timeout: float = 10.0,
1305+
# TODO: rename docker_image
1306+
docker: Optional[str] = None,
1307+
# TODO: rename docker_*
1308+
dockerContainer: Optional[str] = None,
1309+
# TODO: rename docker_*
13051310
dockerExtraArgs: Optional[list] = None,
1311+
# TODO: rename docker_*
13061312
dockerOpenModelicaPath: str | os.PathLike = "omc",
1313+
# TODO: rename docker_*
13071314
dockerNetwork: Optional[str] = None,
1315+
# TODO: rename omc_port
13081316
port: Optional[int] = None,
13091317
) -> None:
13101318
super().__init__(timeout=timeout)
@@ -1315,13 +1323,20 @@ def __init__(
13151323
self._docker_extra_args = dockerExtraArgs
13161324
self._docker_open_modelica_path = pathlib.PurePosixPath(dockerOpenModelicaPath)
13171325
self._docker_network = dockerNetwork
1326+
self._docker_container_id: str
1327+
self._docker_process: Optional[DockerPopen]
13181328

1319-
self._interactive_port = port
1320-
1321-
self._docker_container_id: Optional[str] = None
1322-
self._docker_process: Optional[DockerPopen] = None
1329+
# start up omc executable in docker container waiting for the ZMQ connection
1330+
self._omc_process, self._docker_process, self._docker_container_id = self._docker_omc_start(
1331+
docker_image=docker,
1332+
docker_cid=dockerContainer,
1333+
)
1334+
# connect to the running omc instance using ZMQ
1335+
self._omc_port = self._omc_port_get()
1336+
if port is not None and port != self._omc_port:
1337+
raise OMCSessionException(f"Port mismatch: {self._omc_port} <> {port}!")
13231338

1324-
self._cmd_prefix = self.get_cmd_prefix()
1339+
self._cmd_prefix = self.model_execution_prefix()
13251340

13261341
def _docker_process_get(self, docker_cid: str) -> Optional[DockerPopen]:
13271342
if sys.platform == 'win32':
@@ -1346,6 +1361,14 @@ def _docker_process_get(self, docker_cid: str) -> Optional[DockerPopen]:
13461361

13471362
return docker_process
13481363

1364+
@abc.abstractmethod
1365+
def _docker_omc_start(
1366+
self,
1367+
docker_image: Optional[str] = None,
1368+
docker_cid: Optional[str] = None,
1369+
) -> Tuple[subprocess.Popen, DockerPopen, str]:
1370+
pass
1371+
13491372
@staticmethod
13501373
def _getuid() -> int:
13511374
"""
@@ -1391,14 +1414,6 @@ def _omc_port_get(self) -> str:
13911414

13921415
return port
13931416

1394-
# TODO: add as abc.abstractmethod? fix definition (cid)
1395-
# def _docker_omc_cmd(
1396-
# self,
1397-
# omc_path_and_args_list: list[str],
1398-
# docker_cid_file: pathlib.Path,
1399-
# ) -> list:
1400-
# def _docker_omc_start(self) -> Tuple[subprocess.Popen, DockerPopen, str]:
1401-
14021417
def get_server_address(self) -> Optional[str]:
14031418
"""
14041419
Get the server address of the OMC server running in a Docker container.
@@ -1443,7 +1458,7 @@ class OMCSessionDocker(OMCSessionDockerABC):
14431458
def __init__(
14441459
self,
14451460
timeout: float = 10.00,
1446-
docker_image: Optional[str] = None,
1461+
docker: Optional[str] = None,
14471462
dockerExtraArgs: Optional[list] = None,
14481463
dockerOpenModelicaPath: str | os.PathLike = "omc",
14491464
dockerNetwork: Optional[str] = None,
@@ -1452,22 +1467,13 @@ def __init__(
14521467

14531468
super().__init__(
14541469
timeout=timeout,
1470+
docker=docker,
14551471
dockerExtraArgs=dockerExtraArgs,
14561472
dockerOpenModelicaPath=dockerOpenModelicaPath,
14571473
dockerNetwork=dockerNetwork,
14581474
port=port,
14591475
)
14601476

1461-
if docker_image is None:
1462-
raise OMCSessionException("Argument docker must be set!")
1463-
1464-
self._docker_image = docker_image
1465-
1466-
# start up omc executable in docker container waiting for the ZMQ connection
1467-
self._omc_process, self._docker_process, self._docker_container_id = self._docker_omc_start()
1468-
# connect to the running omc instance using ZMQ
1469-
self._omc_port = self._omc_port_get()
1470-
14711477
def __del__(self) -> None:
14721478

14731479
super().__del__()
@@ -1486,27 +1492,33 @@ def __del__(self) -> None:
14861492

14871493
def _docker_omc_cmd(
14881494
self,
1489-
omc_path_and_args_list: list[str],
1495+
docker_image: str,
14901496
docker_cid_file: pathlib.Path,
1497+
omc_path_and_args_list: list[str],
1498+
omc_port: Optional[int | str] = None,
14911499
) -> list:
14921500
"""
14931501
Define the command that will be called by the subprocess module.
14941502
"""
1503+
14951504
extra_flags = []
14961505

14971506
if sys.platform == "win32":
14981507
extra_flags = ["-d=zmqDangerousAcceptConnectionsFromAnywhere"]
1499-
if not self._interactive_port:
1508+
if not self._omc_port:
15001509
raise OMCSessionException("Docker on Windows requires knowing which port to connect to - "
15011510
"please set the interactivePort argument")
15021511

1512+
port: Optional[int] = None
1513+
if isinstance(omc_port, str):
1514+
port = int(omc_port)
1515+
elif isinstance(omc_port, int):
1516+
port = omc_port
1517+
15031518
if sys.platform == "win32":
1504-
if isinstance(self._interactive_port, str):
1505-
port = int(self._interactive_port)
1506-
elif isinstance(self._interactive_port, int):
1507-
port = self._interactive_port
1508-
else:
1509-
raise OMCSessionException("Missing or invalid interactive port!")
1519+
if not isinstance(port, int):
1520+
raise OMCSessionException("OMC on Windows needs the interactive port - "
1521+
f"missing or invalid value: {repr(omc_port)}!")
15101522
docker_network_str = ["-p", f"127.0.0.1:{port}:{port}"]
15111523
elif self._docker_network == "host" or self._docker_network is None:
15121524
docker_network_str = ["--network=host"]
@@ -1517,8 +1529,8 @@ def _docker_omc_cmd(
15171529
raise OMCSessionException(f'dockerNetwork was set to {self._docker_network}, '
15181530
'but only \"host\" or \"separate\" is allowed')
15191531

1520-
if isinstance(self._interactive_port, int):
1521-
extra_flags = extra_flags + [f"--interactivePort={int(self._interactive_port)}"]
1532+
if isinstance(port, int):
1533+
extra_flags = extra_flags + [f"--interactivePort={port}"]
15221534

15231535
omc_command = ([
15241536
"docker", "run",
@@ -1528,22 +1540,31 @@ def _docker_omc_cmd(
15281540
]
15291541
+ self._docker_extra_args
15301542
+ docker_network_str
1531-
+ [self._docker_image, self._docker_open_modelica_path.as_posix()]
1543+
+ [docker_image, self._docker_open_modelica_path.as_posix()]
15321544
+ omc_path_and_args_list
15331545
+ extra_flags)
15341546

15351547
return omc_command
15361548

1537-
def _docker_omc_start(self) -> Tuple[subprocess.Popen, DockerPopen, str]:
1549+
def _docker_omc_start(
1550+
self,
1551+
docker_image: Optional[str] = None,
1552+
docker_cid: Optional[str] = None,
1553+
) -> Tuple[subprocess.Popen, DockerPopen, str]:
1554+
1555+
if not isinstance(docker_image , str):
1556+
raise OMCSessionException("A docker image name must be provided!")
1557+
15381558
my_env = os.environ.copy()
15391559

15401560
docker_cid_file = self._temp_dir / (self._omc_filebase + ".docker.cid")
15411561

15421562
omc_command = self._docker_omc_cmd(
1563+
docker_image=docker_image,
1564+
docker_cid_file=docker_cid_file,
15431565
omc_path_and_args_list=["--locale=C",
15441566
"--interactive=zmq",
15451567
f"-z={self._random_string}"],
1546-
docker_cid_file=docker_cid_file,
15471568
)
15481569

15491570
omc_process = subprocess.Popen(omc_command,
@@ -1554,6 +1575,7 @@ def _docker_omc_start(self) -> Tuple[subprocess.Popen, DockerPopen, str]:
15541575
if not isinstance(docker_cid_file, pathlib.Path):
15551576
raise OMCSessionException(f"Invalid content for docker container ID file path: {docker_cid_file}")
15561577

1578+
# the provided value for docker_cid is not used
15571579
docker_cid = None
15581580
for _ in range(0, 40):
15591581
try:
@@ -1595,61 +1617,65 @@ def __init__(
15951617

15961618
super().__init__(
15971619
timeout=timeout,
1620+
dockerContainer=dockerContainer,
15981621
dockerExtraArgs=dockerExtraArgs,
15991622
dockerOpenModelicaPath=dockerOpenModelicaPath,
16001623
dockerNetwork=dockerNetwork,
16011624
port=port,
16021625
)
16031626

1604-
if not isinstance(dockerContainer, str):
1605-
raise OMCSessionException("Argument dockerContainer must be set!")
1606-
1607-
self._docker_container_id = dockerContainer
1608-
1609-
# start up omc executable in docker container waiting for the ZMQ connection
1610-
self._omc_process, self._docker_process = self._docker_omc_start()
1611-
# connect to the running omc instance using ZMQ
1612-
self._omc_port = self._omc_port_get()
1613-
16141627
def __del__(self) -> None:
16151628

16161629
super().__del__()
16171630

16181631
# docker container ID was provided - do NOT kill the docker process!
16191632
self._docker_process = None
16201633

1621-
def _docker_omc_cmd(self, omc_path_and_args_list) -> list:
1634+
def _docker_omc_cmd(
1635+
self,
1636+
docker_cid: str,
1637+
omc_path_and_args_list: list[str],
1638+
) -> list:
16221639
"""
16231640
Define the command that will be called by the subprocess module.
16241641
"""
16251642
extra_flags: list[str] = []
16261643

16271644
if sys.platform == "win32":
16281645
extra_flags = ["-d=zmqDangerousAcceptConnectionsFromAnywhere"]
1629-
if not self._interactive_port:
1646+
if not self._omc_port:
16301647
raise OMCSessionException("Docker on Windows requires knowing which port to connect to - "
16311648
"Please set the interactivePort argument. Furthermore, the container needs "
16321649
"to have already manually exposed this port when it was started "
16331650
"(-p 127.0.0.1:n:n) or you get an error later.")
16341651

1635-
if isinstance(self._interactive_port, int):
1636-
extra_flags = extra_flags + [f"--interactivePort={int(self._interactive_port)}"]
1652+
if isinstance(self._omc_port, int):
1653+
extra_flags = extra_flags + [f"--interactivePort={int(self._omc_port)}"]
16371654

16381655
omc_command = ([
16391656
"docker", "exec",
16401657
"--user", str(self._getuid()),
16411658
]
16421659
+ self._docker_extra_args
1643-
+ [self._docker_container_id, self._docker_open_modelica_path.as_posix()]
1660+
+ [docker_cid, self._docker_open_modelica_path.as_posix()]
16441661
+ omc_path_and_args_list
16451662
+ extra_flags)
16461663

16471664
return omc_command
16481665

1649-
def _docker_omc_start(self) -> Tuple[subprocess.Popen, DockerPopen]:
1666+
def _docker_omc_start(
1667+
self,
1668+
docker_image: Optional[str] = None,
1669+
docker_cid: Optional[str] = None,
1670+
) -> Tuple[subprocess.Popen, DockerPopen, str]:
1671+
1672+
if not isinstance(docker_cid, str):
1673+
raise OMCSessionException("A docker container ID must be provided!")
1674+
16501675
my_env = os.environ.copy()
16511676

16521677
omc_command = self._docker_omc_cmd(
1678+
docker_cid=docker_cid,
16531679
omc_path_and_args_list=["--locale=C",
16541680
"--interactive=zmq",
16551681
f"-z={self._random_string}"],
@@ -1662,13 +1688,13 @@ def _docker_omc_start(self) -> Tuple[subprocess.Popen, DockerPopen]:
16621688

16631689
docker_process = None
16641690
if isinstance(self._docker_container_id, str):
1665-
docker_process = self._docker_process_get(docker_cid=self._docker_container_id)
1691+
docker_process = self._docker_process_get(docker_cid=docker_cid)
16661692

16671693
if docker_process is None:
16681694
raise OMCSessionException(f"Docker top did not contain omc process {self._random_string} "
1669-
f"/ {self._docker_container_id}. Log-file says:\n{self.get_log()}")
1695+
f"/ {docker_cid}. Log-file says:\n{self.get_log()}")
16701696

1671-
return omc_process, docker_process
1697+
return omc_process, docker_process, docker_cid
16721698

16731699

16741700
class OMCSessionWSL(OMCSessionABC):

0 commit comments

Comments
 (0)