|
| 1 | +from mlc import utils |
| 2 | +import os |
| 3 | +from utils import * |
| 4 | + |
| 5 | + |
| 6 | +def preprocess(i): |
| 7 | + |
| 8 | + os_info = i['os_info'] |
| 9 | + env = i['env'] |
| 10 | + logger = i['automation'].logger |
| 11 | + |
| 12 | + if os_info['platform'] == 'windows': |
| 13 | + return { |
| 14 | + 'return': 1, |
| 15 | + 'error': 'Apptainer image building is not supported on Windows.'} |
| 16 | + |
| 17 | + def_file_path = env.get('MLC_APPTAINERFILE_WITH_PATH', '') |
| 18 | + if def_file_path != '' and os.path.exists(def_file_path): |
| 19 | + build_def_file = False |
| 20 | + env['MLC_BUILD_APPTAINERFILE'] = "no" |
| 21 | + else: |
| 22 | + build_def_file = True |
| 23 | + env['MLC_BUILD_APPTAINERFILE'] = "yes" |
| 24 | + |
| 25 | + # Determine image name |
| 26 | + image_name = env.get('MLC_APPTAINER_IMAGE_NAME', '') |
| 27 | + if image_name == '': |
| 28 | + image_name = "mlc-script-" + \ |
| 29 | + env.get('MLC_APPTAINER_RUN_SCRIPT_TAGS', 'default').replace( |
| 30 | + ',', '-').replace('_', '-') |
| 31 | + env['MLC_APPTAINER_IMAGE_NAME'] = image_name.lower() |
| 32 | + |
| 33 | + image_tag = env.get('MLC_APPTAINER_IMAGE_TAG', 'latest') |
| 34 | + env['MLC_APPTAINER_IMAGE_TAG'] = image_tag |
| 35 | + |
| 36 | + # SIF output path |
| 37 | + sif_name = f"{env['MLC_APPTAINER_IMAGE_NAME']}_{image_tag}.sif" |
| 38 | + sif_path = os.path.join(os.getcwd(), sif_name) |
| 39 | + env['MLC_APPTAINER_SIF_PATH'] = sif_path |
| 40 | + |
| 41 | + # Build command options |
| 42 | + build_opts = '' |
| 43 | + |
| 44 | + if is_true(env.get('MLC_APPTAINER_FAKEROOT', '')): |
| 45 | + build_opts += ' --fakeroot' |
| 46 | + |
| 47 | + if is_true(env.get('MLC_APPTAINER_BUILD_SANDBOX', '')): |
| 48 | + # Build sandbox instead of SIF |
| 49 | + sandbox_path = sif_path.replace('.sif', '_sandbox') |
| 50 | + env['MLC_APPTAINER_SIF_PATH'] = sandbox_path |
| 51 | + build_opts += ' --sandbox' |
| 52 | + |
| 53 | + if is_true(env.get('MLC_APPTAINER_BUILD_FORCE', '')): |
| 54 | + build_opts += ' --force' |
| 55 | + |
| 56 | + if is_true(env.get('MLC_APPTAINER_BUILD_NOTEST', '')): |
| 57 | + build_opts += ' --notest' |
| 58 | + |
| 59 | + if build_def_file: |
| 60 | + def_file_ref = "${MLC_APPTAINERFILE_WITH_PATH}" |
| 61 | + else: |
| 62 | + def_file_ref = def_file_path |
| 63 | + |
| 64 | + output_path = env['MLC_APPTAINER_SIF_PATH'] |
| 65 | + |
| 66 | + CMD = f"apptainer build{build_opts} {output_path} {def_file_ref}" |
| 67 | + |
| 68 | + logger.info('') |
| 69 | + logger.info(f'Apptainer build command:') |
| 70 | + logger.info(f' {CMD}') |
| 71 | + logger.info('') |
| 72 | + |
| 73 | + env['MLC_APPTAINER_BUILD_CMD'] = CMD |
| 74 | + |
| 75 | + # Save build script |
| 76 | + with open('apptainer-build.sh', 'w') as f: |
| 77 | + f.write('#!/bin/bash\n') |
| 78 | + f.write(CMD + '\n') |
| 79 | + |
| 80 | + return {'return': 0} |
| 81 | + |
| 82 | + |
| 83 | +def postprocess(i): |
| 84 | + |
| 85 | + env = i['env'] |
| 86 | + logger = i['automation'].logger |
| 87 | + |
| 88 | + sif_path = env.get('MLC_APPTAINER_SIF_PATH', '') |
| 89 | + if sif_path and os.path.exists(sif_path): |
| 90 | + logger.info(f'Apptainer image built: {sif_path}') |
| 91 | + else: |
| 92 | + logger.info(f'Apptainer image expected at: {sif_path}') |
| 93 | + |
| 94 | + return {'return': 0} |
0 commit comments