11#!/usr/bin/env python
22# -*- coding: utf-8; -*-
33
4- # Copyright (c) 2021, 2023 Oracle and/or its affiliates.
4+ # Copyright (c) 2021, 2024 Oracle and/or its affiliates.
55# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
66"""Contains classes for conversion between ADS runtime and OCI Data Science Job implementation.
77This module is for ADS developers only.
@@ -305,10 +305,29 @@ def extract(self, dsc_job):
305305 self ._extract_envs ,
306306 self ._extract_artifact ,
307307 self ._extract_runtime_minutes ,
308+ self ._extract_properties ,
308309 ]
309310 for extraction in extractions :
310311 runtime_spec .update (extraction (dsc_job ))
311312 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 {}
312331
313332 def _extract_args (self , dsc_job ) -> dict :
314333 """Extracts the command line arguments from data science job.
@@ -942,9 +961,12 @@ def _extract_artifact(self, dsc_job):
942961class ContainerRuntimeHandler (RuntimeHandler ):
943962 RUNTIME_CLASS = ContainerRuntime
944963 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
948970
949971 def _translate_artifact (self , runtime : Runtime ):
950972 """Specifies a dummy script as the job artifact.
@@ -964,29 +986,34 @@ def _translate_artifact(self, runtime: Runtime):
964986 os .path .dirname (__file__ ), "../../templates" , "container.py"
965987 )
966988
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 .
969991
970992 Parameters
971993 ----------
972- runtime : GitPythonRuntime
973- An instance of GitPythonRuntime
994+ runtime : Runtime
995+ The runtime containing the properties to be converted.
974996
975997 Returns
976998 -------
977999 dict
978- A dictionary containing environment variables for OCI data science job.
1000+ A dictionary storing the ``OcirContainerJobEnvironmentConfigurationDetails`` payload for OCI data science job.
9791001 """
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
9871004 }
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
9901017
9911018 @staticmethod
9921019 def split_args (args : str ) -> list :
@@ -1031,17 +1058,37 @@ def _extract_envs(self, dsc_job):
10311058 """
10321059 spec = super ()._extract_envs (dsc_job )
10331060 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+
10431062 if envs :
10441063 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
10451092 return spec
10461093
10471094
0 commit comments