1
1
#!/usr/bin/env python
2
2
# -*- coding: utf-8; -*-
3
3
4
- # Copyright (c) 2021, 2023 Oracle and/or its affiliates.
4
+ # Copyright (c) 2021, 2024 Oracle and/or its affiliates.
5
5
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
6
6
"""Contains classes for conversion between ADS runtime and OCI Data Science Job implementation.
7
7
This module is for ADS developers only.
@@ -305,10 +305,29 @@ def extract(self, dsc_job):
305
305
self ._extract_envs ,
306
306
self ._extract_artifact ,
307
307
self ._extract_runtime_minutes ,
308
+ self ._extract_properties ,
308
309
]
309
310
for extraction in extractions :
310
311
runtime_spec .update (extraction (dsc_job ))
311
312
return self .RUNTIME_CLASS (self ._format_env_var (runtime_spec ))
313
+
314
+ def _extract_properties (self , dsc_job ) -> dict :
315
+ """Extract the job runtime properties from data science job.
316
+
317
+ This is the base method which does not extract the job runtime properties.
318
+ Sub-class should implement the extraction if needed.
319
+
320
+ Parameters
321
+ ----------
322
+ dsc_job : DSCJob or oci.datascience.models.Job
323
+ The data science job containing runtime information.
324
+
325
+ Returns
326
+ -------
327
+ dict
328
+ A runtime specification dictionary for initializing a runtime.
329
+ """
330
+ return {}
312
331
313
332
def _extract_args (self , dsc_job ) -> dict :
314
333
"""Extracts the command line arguments from data science job.
@@ -942,9 +961,12 @@ def _extract_artifact(self, dsc_job):
942
961
class ContainerRuntimeHandler (RuntimeHandler ):
943
962
RUNTIME_CLASS = ContainerRuntime
944
963
CMD_DELIMITER = ","
945
- CONST_CONTAINER_IMAGE = "CONTAINER_CUSTOM_IMAGE"
946
- CONST_CONTAINER_ENTRYPOINT = "CONTAINER_ENTRYPOINT"
947
- CONST_CONTAINER_CMD = "CONTAINER_CMD"
964
+
965
+ def translate (self , runtime : Runtime ) -> dict :
966
+ payload = super ().translate (runtime )
967
+ job_env_config = self ._translate_env_config (runtime )
968
+ payload ["job_environment_configuration_details" ] = job_env_config
969
+ return payload
948
970
949
971
def _translate_artifact (self , runtime : Runtime ):
950
972
"""Specifies a dummy script as the job artifact.
@@ -964,29 +986,34 @@ def _translate_artifact(self, runtime: Runtime):
964
986
os .path .dirname (__file__ ), "../../templates" , "container.py"
965
987
)
966
988
967
- def _translate_env (self , runtime : ContainerRuntime ) -> dict :
968
- """Translate the environment variable .
989
+ def _translate_env_config (self , runtime : Runtime ) -> dict :
990
+ """Converts runtime properties to ``OcirContainerJobEnvironmentConfigurationDetails`` payload required by OCI Data Science job .
969
991
970
992
Parameters
971
993
----------
972
- runtime : GitPythonRuntime
973
- An instance of GitPythonRuntime
994
+ runtime : Runtime
995
+ The runtime containing the properties to be converted.
974
996
975
997
Returns
976
998
-------
977
999
dict
978
- A dictionary containing environment variables for OCI data science job.
1000
+ A dictionary storing the ``OcirContainerJobEnvironmentConfigurationDetails`` payload for OCI data science job.
979
1001
"""
980
- if not runtime .image :
981
- raise ValueError ("Specify container image for ContainerRuntime." )
982
- envs = super ()._translate_env (runtime )
983
- spec_mappings = {
984
- ContainerRuntime .CONST_IMAGE : self .CONST_CONTAINER_IMAGE ,
985
- ContainerRuntime .CONST_ENTRYPOINT : self .CONST_CONTAINER_ENTRYPOINT ,
986
- ContainerRuntime .CONST_CMD : self .CONST_CONTAINER_CMD ,
1002
+ job_environment_configuration_details = {
1003
+ "job_environment_type" : runtime .job_env_type
987
1004
}
988
- envs .update (self ._translate_specs (runtime , spec_mappings , self .CMD_DELIMITER ))
989
- return envs
1005
+
1006
+ for key , value in ContainerRuntime .attribute_map .items ():
1007
+ property = runtime .get_spec (key , None )
1008
+ if key in [
1009
+ ContainerRuntime .CONST_CMD ,
1010
+ ContainerRuntime .CONST_ENTRYPOINT
1011
+ ] and isinstance (property , str ):
1012
+ property = self .split_args (property )
1013
+ if property is not None :
1014
+ job_environment_configuration_details [value ] = property
1015
+
1016
+ return job_environment_configuration_details
990
1017
991
1018
@staticmethod
992
1019
def split_args (args : str ) -> list :
@@ -1031,17 +1058,37 @@ def _extract_envs(self, dsc_job):
1031
1058
"""
1032
1059
spec = super ()._extract_envs (dsc_job )
1033
1060
envs = spec .pop (ContainerRuntime .CONST_ENV_VAR , {})
1034
- if self .CONST_CONTAINER_IMAGE not in envs :
1035
- raise IncompatibleRuntime ()
1036
- spec [ContainerRuntime .CONST_IMAGE ] = envs .pop (self .CONST_CONTAINER_IMAGE )
1037
- cmd = self .split_args (envs .pop (self .CONST_CONTAINER_CMD , "" ))
1038
- if cmd :
1039
- spec [ContainerRuntime .CONST_CMD ] = cmd
1040
- entrypoint = self .split_args (envs .pop (self .CONST_CONTAINER_ENTRYPOINT , "" ))
1041
- if entrypoint :
1042
- spec [ContainerRuntime .CONST_ENTRYPOINT ] = entrypoint
1061
+
1043
1062
if envs :
1044
1063
spec [ContainerRuntime .CONST_ENV_VAR ] = envs
1064
+
1065
+ return spec
1066
+
1067
+ def _extract_properties (self , dsc_job ) -> dict :
1068
+ """Extract the runtime properties from data science job.
1069
+
1070
+ Parameters
1071
+ ----------
1072
+ dsc_job : DSCJob or oci.datascience.models.Job
1073
+ The data science job containing runtime information.
1074
+
1075
+ Returns
1076
+ -------
1077
+ dict
1078
+ A runtime specification dictionary for initializing a runtime.
1079
+ """
1080
+ spec = super ()._extract_envs (dsc_job )
1081
+
1082
+ job_env_config = getattr (dsc_job , "job_environment_configuration_details" , None )
1083
+ job_env_type = getattr (job_env_config , "job_environment_type" , None )
1084
+
1085
+ if not (job_env_config and job_env_type == "OCIR_CONTAINER" ):
1086
+ raise IncompatibleRuntime ()
1087
+
1088
+ for key , value in ContainerRuntime .attribute_map .items ():
1089
+ property = getattr (job_env_config , value , None )
1090
+ if property is not None :
1091
+ spec [key ] = property
1045
1092
return spec
1046
1093
1047
1094
0 commit comments