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.
@@ -157,6 +157,21 @@ def _translate_env(self, runtime: Runtime) -> dict:
157
157
"""
158
158
return runtime .envs
159
159
160
+ def _translate_env_config (self , runtime : Runtime ) -> dict :
161
+ """Translate the environment configuration details for container runtime.
162
+
163
+ OCI Data Science job requires ``jobEnvironmentConfigurationDetails`` payload if job is running in custom container.
164
+ This method is designed to handle the conversion of the ADS runtime properties to ``jobEnvironmentConfigurationDetails`` payload.
165
+ By default, no conversion is made in this method.
166
+ Sub-class should override this method to add conversion logic.
167
+
168
+ Returns
169
+ -------
170
+ dict
171
+ A dictionary storing the ``jobEnvironmentConfigurationDetails`` payload for OCI data science job.
172
+ """
173
+ return None
174
+
160
175
def _translate_config (self , runtime : Runtime ) -> dict :
161
176
"""Prepares the job configuration from runtime specifications.
162
177
@@ -305,10 +320,29 @@ def extract(self, dsc_job):
305
320
self ._extract_envs ,
306
321
self ._extract_artifact ,
307
322
self ._extract_runtime_minutes ,
323
+ self ._extract_properties ,
308
324
]
309
325
for extraction in extractions :
310
326
runtime_spec .update (extraction (dsc_job ))
311
327
return self .RUNTIME_CLASS (self ._format_env_var (runtime_spec ))
328
+
329
+ def _extract_properties (self , dsc_job ) -> dict :
330
+ """Extract the job runtime properties from data science job.
331
+
332
+ This is the base method which does not extract the job runtime properties.
333
+ Sub-class should implement the extraction if needed.
334
+
335
+ Parameters
336
+ ----------
337
+ dsc_job : DSCJob or oci.datascience.models.Job
338
+ The data science job containing runtime information.
339
+
340
+ Returns
341
+ -------
342
+ dict
343
+ A runtime specification dictionary for initializing a runtime.
344
+ """
345
+ return {}
312
346
313
347
def _extract_args (self , dsc_job ) -> dict :
314
348
"""Extracts the command line arguments from data science job.
@@ -942,9 +976,12 @@ def _extract_artifact(self, dsc_job):
942
976
class ContainerRuntimeHandler (RuntimeHandler ):
943
977
RUNTIME_CLASS = ContainerRuntime
944
978
CMD_DELIMITER = ","
945
- CONST_CONTAINER_IMAGE = "CONTAINER_CUSTOM_IMAGE"
946
- CONST_CONTAINER_ENTRYPOINT = "CONTAINER_ENTRYPOINT"
947
- CONST_CONTAINER_CMD = "CONTAINER_CMD"
979
+
980
+ def translate (self , runtime : Runtime ) -> dict :
981
+ payload = super ().translate (runtime )
982
+ job_env_config = self ._translate_env_config (runtime )
983
+ payload ["job_environment_configuration_details" ] = job_env_config
984
+ return payload
948
985
949
986
def _translate_artifact (self , runtime : Runtime ):
950
987
"""Specifies a dummy script as the job artifact.
@@ -964,29 +1001,34 @@ def _translate_artifact(self, runtime: Runtime):
964
1001
os .path .dirname (__file__ ), "../../templates" , "container.py"
965
1002
)
966
1003
967
- def _translate_env (self , runtime : ContainerRuntime ) -> dict :
968
- """Translate the environment variable .
1004
+ def _translate_env_config (self , runtime : Runtime ) -> dict :
1005
+ """Converts runtime properties to ``jobEnvironmentConfigurationDetails`` payload required by OCI Data Science job .
969
1006
970
1007
Parameters
971
1008
----------
972
- runtime : GitPythonRuntime
973
- An instance of GitPythonRuntime
1009
+ runtime : Runtime
1010
+ The runtime containing the properties to be converted.
974
1011
975
1012
Returns
976
1013
-------
977
1014
dict
978
- A dictionary containing environment variables for OCI data science job.
1015
+ A dictionary storing the ``jobEnvironmentConfigurationDetails`` payload for OCI data science job.
979
1016
"""
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 ,
1017
+ job_environment_configuration_details = {
1018
+ "job_environment_type" : runtime .job_env_type
987
1019
}
988
- envs .update (self ._translate_specs (runtime , spec_mappings , self .CMD_DELIMITER ))
989
- return envs
1020
+
1021
+ for key , value in ContainerRuntime .attribute_map .items ():
1022
+ property = runtime .get_spec (key , None )
1023
+ if key in [
1024
+ ContainerRuntime .CONST_CMD ,
1025
+ ContainerRuntime .CONST_ENTRYPOINT
1026
+ ] and isinstance (property , str ):
1027
+ property = self .split_args (property )
1028
+ if property is not None :
1029
+ job_environment_configuration_details [value ] = property
1030
+
1031
+ return job_environment_configuration_details
990
1032
991
1033
@staticmethod
992
1034
def split_args (args : str ) -> list :
@@ -1031,17 +1073,37 @@ def _extract_envs(self, dsc_job):
1031
1073
"""
1032
1074
spec = super ()._extract_envs (dsc_job )
1033
1075
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
1076
+
1043
1077
if envs :
1044
1078
spec [ContainerRuntime .CONST_ENV_VAR ] = envs
1079
+
1080
+ return spec
1081
+
1082
+ def _extract_properties (self , dsc_job ) -> dict :
1083
+ """Extract the runtime properties from data science job.
1084
+
1085
+ Parameters
1086
+ ----------
1087
+ dsc_job : DSCJob or oci.datascience.models.Job
1088
+ The data science job containing runtime information.
1089
+
1090
+ Returns
1091
+ -------
1092
+ dict
1093
+ A runtime specification dictionary for initializing a runtime.
1094
+ """
1095
+ spec = super ()._extract_envs (dsc_job )
1096
+
1097
+ job_env_config = getattr (dsc_job , "job_environment_configuration_details" , None )
1098
+ job_env_type = getattr (job_env_config , "job_environment_type" , None )
1099
+
1100
+ if not (job_env_config and job_env_type == "OCIR_CONTAINER" ):
1101
+ raise IncompatibleRuntime ()
1102
+
1103
+ for key , value in ContainerRuntime .attribute_map .items ():
1104
+ property = getattr (job_env_config , value , None )
1105
+ if property is not None :
1106
+ spec [key ] = property
1045
1107
return spec
1046
1108
1047
1109
0 commit comments