@@ -80,7 +80,7 @@ def _check_job_image_exists(gpu: bool) -> None:
80
80
def _get_name (name : str , env_file : str ) -> str :
81
81
if not name and env_file :
82
82
with open (env_file ) as f :
83
- name = yaml .safe_load (f .read ()).get ("name" , None )
83
+ name = yaml .safe_load (f .read ()).get ("manifest" ). get ( " name" , None )
84
84
if not name :
85
85
raise ValueError (
86
86
"Either specify environment name in environment yaml or with `--name`."
@@ -146,7 +146,14 @@ def _create(
146
146
if not os .path .exists (env_file ):
147
147
raise FileNotFoundError (f"Environment file { env_file } is not found." )
148
148
149
- slug = f"{ name } _v{ version } " .replace (" " , "" ).replace ("." , "_" ).lower ()
149
+ conda_dep = None
150
+ with open (env_file ) as mfile :
151
+ conda_dep = yaml .safe_load (mfile .read ())
152
+ # If manifest exists in the environment.yaml file, use that
153
+ manifest = conda_dep .get ("manifest" , {})
154
+ slug = manifest .get (
155
+ "slug" , f"{ name } _v{ version } " .replace (" " , "" ).replace ("." , "_" ).lower ()
156
+ )
150
157
pack_folder_path = os .path .join (
151
158
os .path .abspath (os .path .expanduser (conda_pack_folder )), slug
152
159
)
@@ -171,23 +178,38 @@ def _create(
171
178
172
179
os .makedirs (pack_folder_path , exist_ok = True )
173
180
181
+ logger .info (
182
+ f"Preparing manifest. Manifest in the environment: { conda_dep .get ('manifest' )} "
183
+ )
174
184
manifest = _fetch_manifest_template ()
175
- manifest ["manifest" ]["name" ] = name
185
+ if "name" not in manifest :
186
+ manifest ["manifest" ]["name" ] = name
176
187
manifest ["manifest" ]["slug" ] = slug
177
- manifest ["manifest" ]["type" ] = "published"
178
- manifest ["manifest" ]["version" ] = version
188
+ if "type" not in manifest :
189
+ logger .info ("Setting manifest to published" )
190
+ manifest ["manifest" ]["type" ] = "published"
191
+ if "version" not in manifest :
192
+ manifest ["manifest" ]["version" ] = version
179
193
manifest ["manifest" ]["arch_type" ] = "GPU" if gpu else "CPU"
180
194
181
195
manifest ["manifest" ]["create_date" ] = datetime .utcnow ().strftime (
182
196
"%a, %b %d, %Y, %H:%M:%S %Z UTC"
183
197
)
184
- manifest ["manifest" ]["manifest_version" ] = "1.0"
198
+
199
+ if not "manifest_version" in manifest :
200
+ manifest ["manifest" ]["manifest_version" ] = "1.0"
185
201
186
202
logger .info (f"Creating conda environment { slug } " )
187
- conda_dep = None
188
- with open (env_file ) as mfile :
189
- conda_dep = yaml .safe_load (mfile .read ())
190
- conda_dep ["manifest" ] = manifest ["manifest" ]
203
+ manifest_dict = {
204
+ k : manifest ["manifest" ][k ]
205
+ for k in manifest ["manifest" ]
206
+ if manifest ["manifest" ][k ]
207
+ }
208
+ if "manifest" in conda_dep :
209
+ conda_dep ["manifest" ].update (manifest_dict )
210
+ else :
211
+ conda_dep ["manifest" ] = manifest_dict
212
+ logger .info (f"Updated conda environment manifest: { conda_dep .get ('manifest' )} " )
191
213
192
214
if is_in_notebook_session () or NO_CONTAINER :
193
215
command = f"conda env create --prefix { pack_folder_path } --file { os .path .abspath (os .path .expanduser (env_file ))} "
@@ -200,13 +222,12 @@ def _create(
200
222
)
201
223
202
224
create_command = f"conda env create --prefix { docker_pack_folder_path } --file { docker_env_file_path } "
203
-
225
+
204
226
volumes = {
205
227
pack_folder_path : {"bind" : docker_pack_folder_path },
206
228
os .path .abspath (os .path .expanduser (env_file )): {
207
229
"bind" : docker_env_file_path
208
230
},
209
-
210
231
}
211
232
212
233
if gpu :
@@ -217,26 +238,42 @@ def _create(
217
238
if prepare_publish :
218
239
tmp_file = tempfile .NamedTemporaryFile (suffix = ".yaml" )
219
240
# Save the manifest in the temp file that can be mounted inside the container so that archiving will work
220
- with open (tmp_file .name , 'w' ) as f :
221
- yaml .safe_dump (conda_dep , f )
241
+ with open (tmp_file .name , "w" ) as f :
242
+ yaml .safe_dump (conda_dep , f )
222
243
223
- pack_script = os .path .join (os .path .dirname (os .path .abspath (__file__ )), "pack.py" )
244
+ pack_script = os .path .join (
245
+ os .path .dirname (os .path .abspath (__file__ )), "pack.py"
246
+ )
224
247
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' )} "
225
248
226
249
# add pack script and manifest file to the mount so that archive can be created in the same container run
227
250
condapack_script = {
228
- pack_script : {"bind" : os .path .join (DEFAULT_IMAGE_HOME_DIR , "pack.py" )},
229
- tmp_file .name : {"bind" : os .path .join (DEFAULT_IMAGE_HOME_DIR , "manifest.yaml" )}
251
+ pack_script : {
252
+ "bind" : os .path .join (DEFAULT_IMAGE_HOME_DIR , "pack.py" )
253
+ },
254
+ tmp_file .name : {
255
+ "bind" : os .path .join (DEFAULT_IMAGE_HOME_DIR , "manifest.yaml" )
256
+ },
230
257
}
231
- volumes = {** volumes , ** condapack_script } # | not supported in python 3.8
258
+ volumes = {
259
+ ** volumes ,
260
+ ** condapack_script ,
261
+ } # | not supported in python 3.8
232
262
233
263
run_container (
234
- image = image , bind_volumes = volumes , entrypoint = "/bin/bash -c " , env_vars = {}, command = f" '{ create_command } && { pack_command } '"
264
+ image = image ,
265
+ bind_volumes = volumes ,
266
+ entrypoint = "/bin/bash -c " ,
267
+ env_vars = {},
268
+ command = f" '{ create_command } && { pack_command } '" ,
235
269
)
236
270
else :
237
271
run_container (
238
- image = image , bind_volumes = volumes , env_vars = {}, command = create_command
239
- )
272
+ image = image ,
273
+ bind_volumes = volumes ,
274
+ env_vars = {},
275
+ command = create_command ,
276
+ )
240
277
except Exception :
241
278
if os .path .exists (pack_folder_path ):
242
279
shutil .rmtree (pack_folder_path )
@@ -507,9 +544,11 @@ def publish(**kwargs) -> None:
507
544
conda_pack_folder = exec_config ["conda_pack_folder" ],
508
545
gpu = exec_config .get ("gpu" , False ),
509
546
overwrite = exec_config ["overwrite" ],
510
- prepare_publish = True
547
+ prepare_publish = True ,
548
+ )
549
+ skip_archive = (
550
+ True # The conda pack archive is already created during create process.
511
551
)
512
- skip_archive = True # The conda pack archive is already created during create process.
513
552
else :
514
553
slug = exec_config .get ("slug" )
515
554
if not slug :
@@ -526,10 +565,10 @@ def publish(**kwargs) -> None:
526
565
oci_profile = exec_config .get ("oci_profile" ),
527
566
overwrite = exec_config ["overwrite" ],
528
567
auth_type = exec_config ["auth" ],
529
- skip_archive = skip_archive
568
+ skip_archive = skip_archive ,
530
569
)
531
570
532
-
571
+
533
572
def _publish (
534
573
conda_slug : str ,
535
574
conda_uri_prefix : str ,
@@ -538,7 +577,7 @@ def _publish(
538
577
oci_profile : str ,
539
578
overwrite : bool ,
540
579
auth_type : str ,
541
- skip_archive : bool = False
580
+ skip_archive : bool = False ,
542
581
) -> None :
543
582
"""Publish a local conda pack to object storage location
544
583
@@ -616,7 +655,11 @@ def _publish(
616
655
pack_script = os .path .join (os .path .dirname (os .path .abspath (__file__ )), "pack.py" )
617
656
if not skip_archive :
618
657
if is_in_notebook_session () or NO_CONTAINER :
658
+ # Set the CONDA_PUBLISH_TYPE environment variable so that the `type` attribute inside the manifest is not changed
659
+ publish_type = os .environ .get ("CONDA_PUBLISH_TYPE" )
619
660
command = f"python { pack_script } --conda-path { pack_folder_path } "
661
+ if publish_type :
662
+ command = f"CONDA_PUBLISH_TYPE={ publish_type } { command } "
620
663
run_command (command , shell = True )
621
664
else :
622
665
volumes = {
@@ -641,7 +684,9 @@ def _publish(
641
684
NOT_ALLOWED_CHARS = "@#$%^&*/"
642
685
643
686
if any (chr in conda_slug for chr in NOT_ALLOWED_CHARS ):
644
- raise ValueError (f"Invalid conda_slug. Found { NOT_ALLOWED_CHARS } in slug name. Please use a different slug name." )
687
+ raise ValueError (
688
+ f"Invalid conda_slug. Found { NOT_ALLOWED_CHARS } in slug name. Please use a different slug name."
689
+ )
645
690
pack_file = os .path .join (pack_folder_path , f"{ conda_slug } .tar.gz" )
646
691
if not os .path .exists (pack_file ):
647
692
raise RuntimeError (f"Pack { pack_file } was not created." )
@@ -664,14 +709,19 @@ def _publish(
664
709
str (manifest ["version" ]),
665
710
publish_slug ,
666
711
)
667
- manifest ["pack_path" ] = os .path .join (
668
- prefix ,
669
- manifest .get ("arch_type" , "CPU" ).lower (),
670
- manifest ["name" ],
671
- str (manifest ["version" ]),
672
- publish_slug ,
673
- )
674
- manifest ["pack_uri" ] = pack_uri
712
+ if os .environ .get ("CONDA_PUBLISH_TYPE" ) != "service" :
713
+ # Set these values only for published conda pack
714
+ manifest ["pack_path" ] = os .path .join (
715
+ prefix ,
716
+ manifest .get ("arch_type" , "CPU" ).lower (),
717
+ manifest ["name" ],
718
+ str (manifest ["version" ]),
719
+ publish_slug ,
720
+ )
721
+ manifest ["pack_uri" ] = pack_uri
722
+ else :
723
+ manifest ["type" ] = "published"
724
+
675
725
with open (manifest_location , "w" ) as f :
676
726
yaml .safe_dump (env , f )
677
727
if pack_size > 100 :
0 commit comments