28
28
29
29
>>> import ansys.fluent.core as pyfluent
30
30
>>> config_dict = pyfluent.launch_fluent(start_container=True, dry_run=True)
31
- Container run configuration information:
32
- image_name = 'ghcr.io/ansys/pyfluent:v23.1.0'
33
- >>> config_dict
31
+ Docker container run configuration information:
32
+
33
+ config_dict =
34
34
{'auto_remove': True,
35
- 'command': ['-gu',
36
- '-sifile=/home/user/.local/share/ansys_fluent_core/examples/serverinfo-reh96tuo.txt',
37
- '3ddp'],
35
+ 'command': ['-gu', '-sifile=/tmpdir/serverinfo-lpqsdldw.txt', '3ddp'],
38
36
'detach': True,
39
- 'environment': {'ANSYSLMD_LICENSE_FILE': '1450@license_server.com',
40
- 'REMOTING_PORTS': '57193/portspan=2'},
37
+ 'environment': {'ANSYSLMD_LICENSE_FILE': '[email protected] ',
38
+ 'REMOTING_PORTS': '54000/portspan=2'},
39
+ 'fluent_image': 'ghcr.io/ansys/pyfluent:v23.2.0',
41
40
'labels': {'test_name': 'none'},
42
- 'ports': {'57193 ': 57193 },
43
- 'volumes': ['/home/user/.local/share/ansys_fluent_core/examples:/home/user/.local/share/ansys_fluent_core/examples '],
44
- 'working_dir': '/home/user/.local/share/ansys_fluent_core/examples '}
41
+ 'ports': {'54000 ': 54000 },
42
+ 'volumes': ['/home/user/.local/share/ansys_fluent_core/examples:/tmpdir '],
43
+ 'working_dir': '/tmpdir '}
45
44
>>> config_dict.update(image_name='custom_fluent', image_tag='v23.1.0', mem_limit='1g')
46
45
>>> session = pyfluent.launch_fluent(container_dict=config_dict)
47
46
48
47
"""
49
48
import logging
50
49
import os
51
- from pathlib import Path
50
+ from pathlib import Path , PurePosixPath
52
51
import tempfile
53
52
from typing import List , Union
54
53
59
58
import docker
60
59
61
60
logger = logging .getLogger ("pyfluent.launcher" )
61
+ DEFAULT_CONTAINER_MOUNT_PATH = "/tmpdir"
62
62
63
63
64
64
def configure_container_dict (
65
65
args : List [str ],
66
66
host_mount_path : Union [str , Path ] = None ,
67
67
container_mount_path : Union [str , Path ] = None ,
68
- timeout : int = 30 ,
68
+ timeout : int = 60 ,
69
69
port : int = None ,
70
70
license_server : str = None ,
71
71
container_server_info_file : Union [str , Path ] = None ,
@@ -74,7 +74,7 @@ def configure_container_dict(
74
74
image_name : str = None ,
75
75
image_tag : str = None ,
76
76
** container_dict ,
77
- ) -> (str , dict , int , int , Path , bool ):
77
+ ) -> (dict , int , int , Path , bool ):
78
78
"""Parses the parameters listed below, and sets up the container configuration file.
79
79
80
80
Parameters
@@ -112,7 +112,7 @@ def configure_container_dict(
112
112
container_dict : dict
113
113
timeout : int
114
114
port : int
115
- container_server_info_file : Path
115
+ host_server_info_file : Path
116
116
remove_server_info_file: bool
117
117
118
118
Notes
@@ -133,7 +133,7 @@ def configure_container_dict(
133
133
134
134
if not container_mount_path :
135
135
container_mount_path = os .getenv (
136
- "PYFLUENT_CONTAINER_MOUNT_PATH" , host_mount_path
136
+ "PYFLUENT_CONTAINER_MOUNT_PATH" , DEFAULT_CONTAINER_MOUNT_PATH
137
137
)
138
138
139
139
if "volumes" not in container_dict :
@@ -183,21 +183,26 @@ def configure_container_dict(
183
183
"Specified a server info file command argument as well as "
184
184
"a container_server_info_file, pick one."
185
185
)
186
- container_server_info_file = Path (v .lstrip ("-sifile=" )).name
186
+ container_server_info_file = PurePosixPath (
187
+ v .replace ("-sifile=" , "" )
188
+ ).name
187
189
logger .debug (
188
190
f"Found server info file specification for { container_server_info_file } ."
189
191
)
190
192
191
193
if container_server_info_file :
192
194
container_server_info_file = (
193
- Path (container_mount_path ) / Path (container_server_info_file ).name
195
+ PurePosixPath (container_mount_path )
196
+ / PurePosixPath (container_server_info_file ).name
194
197
)
195
198
else :
196
199
fd , sifile = tempfile .mkstemp (
197
200
suffix = ".txt" , prefix = "serverinfo-" , dir = host_mount_path
198
201
)
199
202
os .close (fd )
200
- container_server_info_file = Path (container_mount_path ) / Path (sifile ).name
203
+ container_server_info_file = (
204
+ PurePosixPath (container_mount_path ) / Path (sifile ).name
205
+ )
201
206
202
207
if not fluent_image :
203
208
if not image_tag :
@@ -210,9 +215,11 @@ def configure_container_dict(
210
215
fluent_image = f"{ image_name } :{ image_tag } "
211
216
else :
212
217
raise ValueError (
213
- "Missing 'fluent_image' specification for Docker container launch."
218
+ "Missing 'fluent_image', or 'image_tag' and 'image_name', specification for Docker container launch."
214
219
)
215
220
221
+ container_dict ["fluent_image" ] = fluent_image
222
+
216
223
fluent_commands = ["-gu" , f"-sifile={ container_server_info_file } " ] + args
217
224
218
225
container_dict_default = {}
@@ -222,18 +229,22 @@ def configure_container_dict(
222
229
auto_remove = True ,
223
230
)
224
231
232
+ if fluent_image .split (":" )[1 ] == "v24.1.0" :
233
+ container_dict_default .update (tty = True )
234
+
225
235
for k , v in container_dict_default .items ():
226
236
if k not in container_dict :
227
237
container_dict [k ] = v
228
238
229
239
logger .debug (f"container_dict after processing: { container_dict } " )
230
240
241
+ host_server_info_file = Path (host_mount_path ) / container_server_info_file .name
242
+
231
243
return (
232
- fluent_image ,
233
244
container_dict ,
234
245
timeout ,
235
246
port ,
236
- container_server_info_file ,
247
+ host_server_info_file ,
237
248
remove_server_info_file ,
238
249
)
239
250
@@ -268,39 +279,38 @@ def start_fluent_container(args: List[str], container_dict: dict = None) -> (int
268
279
logger .debug (f"container_vars:{ container_vars } " )
269
280
270
281
(
271
- fluent_image ,
272
282
config_dict ,
273
283
timeout ,
274
284
port ,
275
- container_server_info_file ,
285
+ host_server_info_file ,
276
286
remove_server_info_file ,
277
287
) = container_vars
278
288
279
289
try :
280
- if not container_server_info_file .exists ():
281
- container_server_info_file .mkdir (exist_ok = True )
290
+ if not host_server_info_file .exists ():
291
+ host_server_info_file . parents [ 0 ] .mkdir (exist_ok = True )
282
292
283
- container_server_info_file .touch (exist_ok = True )
284
- last_mtime = container_server_info_file .stat ().st_mtime
293
+ host_server_info_file .touch (exist_ok = True )
294
+ last_mtime = host_server_info_file .stat ().st_mtime
285
295
286
296
docker_client = docker .from_env ()
287
297
288
298
logger .debug ("Starting Fluent docker container..." )
289
299
290
- docker_client .containers .run (fluent_image , ** config_dict )
300
+ docker_client .containers .run (config_dict . pop ( " fluent_image" ) , ** config_dict )
291
301
292
302
success = timeout_loop (
293
- lambda : container_server_info_file .stat ().st_mtime > last_mtime , timeout
303
+ lambda : host_server_info_file .stat ().st_mtime > last_mtime , timeout
294
304
)
295
305
296
306
if not success :
297
307
raise RuntimeError (
298
308
"Fluent container launch timeout, will have to stop container manually."
299
309
)
300
310
else :
301
- _ , _ , password = _parse_server_info_file (str (container_server_info_file ))
311
+ _ , _ , password = _parse_server_info_file (str (host_server_info_file ))
302
312
303
313
return port , password
304
314
finally :
305
- if remove_server_info_file and container_server_info_file .exists ():
306
- container_server_info_file .unlink ()
315
+ if remove_server_info_file and host_server_info_file .exists ():
316
+ host_server_info_file .unlink ()
0 commit comments