Skip to content

Commit 71eb85c

Browse files
Rohan GujarathiNamrata Madan
authored andcommitted
pathways: verify if conda or mamba is installed
1 parent 58d3f46 commit 71eb85c

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

src/sagemaker/remote_function/core/runtime_environment.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ def _create_conda_env(self):
135135
def _install_req_txt_in_conda_env(self, env_name, local_path):
136136
"""Install requirements.txt in the given conda environment"""
137137

138-
cmd = '/bin/bash -c "mamba run -n {} pip install -r {}"'.format(env_name, local_path)
138+
cmd = '/bin/bash -c "{} run -n {} pip install -r {}"'.format(
139+
self._get_conda_exe(), env_name, local_path
140+
)
139141
logger.info("Activating conda env and installing requirements: {}".format(cmd))
140142
_run_shell_cmd(cmd)
141143
logger.info("Requirements installed successfully in conda env {}".format(env_name))
@@ -151,6 +153,16 @@ def _write_conda_env_to_file(self, env_name):
151153
with open(file_path, "w") as output_file:
152154
output_file.write(env_name)
153155

156+
def _get_conda_exe(self):
157+
"""Checks whether conda or mamba is available to use"""
158+
159+
if not subprocess.Popen(["which", "mamba"]).wait():
160+
return "mamba"
161+
elif not subprocess.Popen(["which", "conda"]).wait():
162+
return "conda"
163+
else:
164+
raise ValueError("Neither conda nor mamba is installed on the image")
165+
154166

155167
def _run_shell_cmd(cmd: str):
156168
"""This method runs a given shell command using subprocess

src/sagemaker/remote_function/job_driver.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,15 @@ python -m sagemaker.remote_function.bootstrap "$@"
1010
if [ -f "remote_function_conda_env.txt" ]
1111
then
1212
conda_env=$(cat remote_function_conda_env.txt)
13+
14+
if which mamba >/dev/null; then
15+
conda_exe="mamba"
16+
else
17+
conda_exe="conda"
18+
fi
19+
1320
printf "INFO: Invoking remote function inside conda environment: $conda_env.\n"
14-
mamba run -n $conda_env python -m sagemaker.remote_function.invoke_function "$@"
21+
$conda_exe run -n $conda_env python -m sagemaker.remote_function.invoke_function "$@"
1522
else
1623
printf "INFO: No conda env provided. Invoking remote function\n"
1724
python -m sagemaker.remote_function.invoke_function "$@"

tests/unit/sagemaker/remote_function/core/test_runtime_environment.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,13 @@ def test_bootstrap_req_txt_error(mock_s3_download, mock_cwd, mock_session):
173173
"sagemaker.remote_function.core.runtime_environment.RuntimeEnvironmentManager._write_conda_env_to_file",
174174
Mock(),
175175
)
176+
@patch(
177+
"sagemaker.remote_function.core.runtime_environment.RuntimeEnvironmentManager._get_conda_exe",
178+
return_value="conda",
179+
)
176180
@patch("os.getcwd", return_value="/usr/local/path")
177181
@patch("sagemaker.s3.S3Downloader.download")
178-
def test_bootstrap_req_txt_with_conda_env(mock_s3_download, mock_cwd, mock_session):
182+
def test_bootstrap_req_txt_with_conda_env(mock_s3_download, mock_cwd, mock_conda_exe, mock_session):
179183
with patch("subprocess.Popen") as popen:
180184
popen.return_value.wait.return_value = 0
181185
runtime = RuntimeEnvironmentManager(TEST_S3_BASE_URI, TEST_S3_KMS_KEY, mock_session)
@@ -193,8 +197,8 @@ def test_bootstrap_req_txt_with_conda_env(mock_s3_download, mock_cwd, mock_sessi
193197
call_args = popen.call_args[0][0]
194198
assert call_args is not None
195199

196-
expected_cmd = '/bin/bash -c "mamba run -n {} pip install -r {}"'.format(
197-
job_conda_env, local_file_path
200+
expected_cmd = '/bin/bash -c "{} run -n {} pip install -r {}"'.format(
201+
mock_conda_exe.return_value, job_conda_env, local_file_path
198202
)
199203
assert call_args == expected_cmd
200204

0 commit comments

Comments
 (0)