Skip to content

Commit 9945ff2

Browse files
committed
Raise error if command has non-zero exit code.
1 parent b9fa19b commit 9945ff2

File tree

1 file changed

+59
-22
lines changed

1 file changed

+59
-22
lines changed

ads/opctl/conda/cmds.py

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,9 @@ def _create(
151151
conda_dep = yaml.safe_load(mfile.read())
152152
# If manifest exists in the environment.yaml file, use that
153153
manifest = conda_dep.get("manifest", {})
154-
slug = manifest.get("slug", f"{name}_v{version}".replace(" ", "").replace(".", "_").lower())
154+
slug = manifest.get(
155+
"slug", f"{name}_v{version}".replace(" ", "").replace(".", "_").lower()
156+
)
155157
pack_folder_path = os.path.join(
156158
os.path.abspath(os.path.expanduser(conda_pack_folder)), slug
157159
)
@@ -176,7 +178,9 @@ def _create(
176178

177179
os.makedirs(pack_folder_path, exist_ok=True)
178180

179-
logger.info(f"Preparing manifest. Manifest in the environment: {conda_dep.get('manifest')}")
181+
logger.info(
182+
f"Preparing manifest. Manifest in the environment: {conda_dep.get('manifest')}"
183+
)
180184
manifest = _fetch_manifest_template()
181185
if not "name" in conda_dep["manifest"]:
182186
manifest["manifest"]["name"] = name
@@ -196,12 +200,22 @@ def _create(
196200
manifest["manifest"]["manifest_version"] = "1.0"
197201

198202
logger.info(f"Creating conda environment {slug}")
199-
conda_dep["manifest"].update({k: manifest["manifest"][k] for k in manifest["manifest"] if manifest["manifest"][k]})
203+
conda_dep["manifest"].update(
204+
{
205+
k: manifest["manifest"][k]
206+
for k in manifest["manifest"]
207+
if manifest["manifest"][k]
208+
}
209+
)
200210
logger.info(f"Updated conda environment manifest: {conda_dep.get('manifest')}")
201211

202212
if is_in_notebook_session() or NO_CONTAINER:
203213
command = f"conda env create --prefix {pack_folder_path} --file {os.path.abspath(os.path.expanduser(env_file))}"
204-
run_command(command, shell=True)
214+
proc = run_command(command, shell=True)
215+
if proc.returncode != 0:
216+
raise RuntimeError(
217+
f"Failed to create conda environment. (exit code {proc.returncode})"
218+
)
205219
else:
206220
_check_job_image_exists(gpu)
207221
docker_pack_folder_path = os.path.join(DEFAULT_IMAGE_HOME_DIR, slug)
@@ -210,13 +224,12 @@ def _create(
210224
)
211225

212226
create_command = f"conda env create --prefix {docker_pack_folder_path} --file {docker_env_file_path}"
213-
227+
214228
volumes = {
215229
pack_folder_path: {"bind": docker_pack_folder_path},
216230
os.path.abspath(os.path.expanduser(env_file)): {
217231
"bind": docker_env_file_path
218232
},
219-
220233
}
221234

222235
if gpu:
@@ -227,26 +240,42 @@ def _create(
227240
if prepare_publish:
228241
tmp_file = tempfile.NamedTemporaryFile(suffix=".yaml")
229242
# Save the manifest in the temp file that can be mounted inside the container so that archiving will work
230-
with open(tmp_file.name, 'w') as f:
231-
yaml.safe_dump(conda_dep, f)
243+
with open(tmp_file.name, "w") as f:
244+
yaml.safe_dump(conda_dep, f)
232245

233-
pack_script = os.path.join(os.path.dirname(os.path.abspath(__file__)), "pack.py")
246+
pack_script = os.path.join(
247+
os.path.dirname(os.path.abspath(__file__)), "pack.py"
248+
)
234249
pack_command = f"python {os.path.join(DEFAULT_IMAGE_HOME_DIR, 'pack.py')} --conda-path {docker_pack_folder_path} --manifest-location {os.path.join(DEFAULT_IMAGE_HOME_DIR, 'manifest.yaml')}"
235250

236251
# add pack script and manifest file to the mount so that archive can be created in the same container run
237252
condapack_script = {
238-
pack_script: {"bind": os.path.join(DEFAULT_IMAGE_HOME_DIR, "pack.py")},
239-
tmp_file.name: {"bind": os.path.join(DEFAULT_IMAGE_HOME_DIR, "manifest.yaml")}
253+
pack_script: {
254+
"bind": os.path.join(DEFAULT_IMAGE_HOME_DIR, "pack.py")
255+
},
256+
tmp_file.name: {
257+
"bind": os.path.join(DEFAULT_IMAGE_HOME_DIR, "manifest.yaml")
258+
},
240259
}
241-
volumes = {**volumes, **condapack_script} # | not supported in python 3.8
260+
volumes = {
261+
**volumes,
262+
**condapack_script,
263+
} # | not supported in python 3.8
242264

243265
run_container(
244-
image=image, bind_volumes=volumes, entrypoint="/bin/bash -c ", env_vars={}, command=f" '{create_command} && {pack_command}'"
266+
image=image,
267+
bind_volumes=volumes,
268+
entrypoint="/bin/bash -c ",
269+
env_vars={},
270+
command=f" '{create_command} && {pack_command}'",
245271
)
246272
else:
247273
run_container(
248-
image=image, bind_volumes=volumes, env_vars={}, command=create_command
249-
)
274+
image=image,
275+
bind_volumes=volumes,
276+
env_vars={},
277+
command=create_command,
278+
)
250279
except Exception:
251280
if os.path.exists(pack_folder_path):
252281
shutil.rmtree(pack_folder_path)
@@ -517,9 +546,11 @@ def publish(**kwargs) -> None:
517546
conda_pack_folder=exec_config["conda_pack_folder"],
518547
gpu=exec_config.get("gpu", False),
519548
overwrite=exec_config["overwrite"],
520-
prepare_publish=True
549+
prepare_publish=True,
550+
)
551+
skip_archive = (
552+
True # The conda pack archive is already created during create process.
521553
)
522-
skip_archive = True # The conda pack archive is already created during create process.
523554
else:
524555
slug = exec_config.get("slug")
525556
if not slug:
@@ -536,10 +567,10 @@ def publish(**kwargs) -> None:
536567
oci_profile=exec_config.get("oci_profile"),
537568
overwrite=exec_config["overwrite"],
538569
auth_type=exec_config["auth"],
539-
skip_archive=skip_archive
570+
skip_archive=skip_archive,
540571
)
541572

542-
573+
543574
def _publish(
544575
conda_slug: str,
545576
conda_uri_prefix: str,
@@ -548,7 +579,7 @@ def _publish(
548579
oci_profile: str,
549580
overwrite: bool,
550581
auth_type: str,
551-
skip_archive: bool = False
582+
skip_archive: bool = False,
552583
) -> None:
553584
"""Publish a local conda pack to object storage location
554585
@@ -628,7 +659,11 @@ def _publish(
628659
if is_in_notebook_session() or NO_CONTAINER:
629660
# Set the CONDA_PUBLISH_TYPE environment variable so that the `type` attribute inside the manifest is not changed
630661
command = f"CONDA_PUBLISH_TYPE={os.environ.get('CONDA_PUBLISH_TYPE','')} python {pack_script} --conda-path {pack_folder_path}"
631-
run_command(command, shell=True)
662+
proc = run_command(command, shell=True)
663+
if proc.returncode != 0:
664+
raise RuntimeError(
665+
f"Failed to archive the conda environment. (exit code {proc.returncode})"
666+
)
632667
else:
633668
volumes = {
634669
pack_folder_path: {
@@ -652,7 +687,9 @@ def _publish(
652687
NOT_ALLOWED_CHARS = "@#$%^&*/"
653688

654689
if any(chr in conda_slug for chr in NOT_ALLOWED_CHARS):
655-
raise ValueError(f"Invalid conda_slug. Found {NOT_ALLOWED_CHARS} in slug name. Please use a different slug name.")
690+
raise ValueError(
691+
f"Invalid conda_slug. Found {NOT_ALLOWED_CHARS} in slug name. Please use a different slug name."
692+
)
656693
pack_file = os.path.join(pack_folder_path, f"{conda_slug}.tar.gz")
657694
if not os.path.exists(pack_file):
658695
raise RuntimeError(f"Pack {pack_file} was not created.")

0 commit comments

Comments
 (0)