def get_meta(recipe, config): """ Given a package name, find the current meta.yaml file, parse it, and return the dict. Parameters ---------- recipe : str Path to recipe (directory containing the meta.yaml file) config : str or dict Config YAML or dict """ cfg = utils.load_config(config) # TODO: Currently just uses the first env. Should turn this into # a generator. env = dict(next(iter(utils.EnvMatrix(cfg['env_matrix'])))) pth = os.path.join(recipe, 'meta.yaml') jinja_env = jinja2.Environment() content = jinja_env.from_string(open(pth, 'r', encoding='utf-8').read()).render(env) meta = yaml.round_trip_load(content, preserve_quotes=True) return meta
def dynamic_jinja_to_static_ruamel_yaml(filename): tmp_file = 'tmp.ruamel_yaml' converted_file_string, compilers_in_build = convert_jinja_syntax(filename) data = ruamel_yaml.round_trip_load(converted_file_string) with open(tmp_file, 'w') as fp: ruamel_yaml.round_trip_dump(data, fp) environment = jinja2.Environment( loader=jinja2.FileSystemLoader(searchpath='.'), trim_blocks=True, block_start_string='#%', block_end_string='%#', variable_start_string='<{', variable_end_string='}>') try: recipe_dict = environment.get_template(tmp_file).render() os.remove("tmp.ruamel_yaml") return recipe_dict, compilers_in_build except: print("ERROR in dynamic_jinja_to_static_ruamel_yaml") return ""
def yaml_round_trip_load(string): return yaml.round_trip_load(string, version="1.2")
def write_template(method, image, command, workdir, parallel, **kwargs): with open( f"{os.path.dirname(os.path.realpath(__file__))}/kubernetes-template.yaml" ) as ifile: doc = ruamel_yaml.round_trip_load(ifile, preserve_quotes=True) double = kwargs.get('double', 'OFF') rdtscp = kwargs.get('rdtscp', 'OFF') orca_method_file = kwargs.get('orca_method_file', '') arch = kwargs.get('arch', '') # Set default values default_image = GMX_IMAGE default_name = "gromacs" if method == "orca": default_image = ORCA_IMAGE default_name = "orca" # Always replace "" with "-" because "" is not kubernetes accepted char in the name method = method.replace("_", "-") # Set names timestamp = str(time.time()).replace(".", "") identificator = "{}-{}-rdtscp-{}".format(default_name, method, timestamp) doc['metadata']['name'] = identificator doc['spec']['template']['spec']['containers'][0][ 'name'] = "{}-{}-deployment-{}".format(default_name, method, timestamp) doc['spec']['template']['metadata']['labels']['app'] = identificator # Set gromacs args doc['spec']['template']['spec']['containers'][0]['args'] = [ "/bin/bash", "-c", DoubleQuotedScalarString(command) ] # If not orca, set options for gmx container if method != "orca": double_env = { 'name': "GMX_DOUBLE", 'value': DoubleQuotedScalarString("ON" if double else "OFF") } rdtscp_env = { 'name': "GMX_RDTSCP", 'value': DoubleQuotedScalarString("ON" if rdtscp else "OFF") } arch_env = { 'name': "GMX_ARCH", 'value': DoubleQuotedScalarString(arch) } doc['spec']['template']['spec']['containers'][0]['env'] = [ double_env, rdtscp_env, arch_env ] # If parallel is enabled set label so kubectl logs can print logs according to label if parallel: with open(f"{PICKLE_PATH}/lock.pkl", "rb") as fp: lock_object = pickle.load(fp) if len(lock_object['Parallel_label']) == 0: label = {"Parallel_label": identificator, "Count": 0} with open(f"{PICKLE_PATH}/lock.pkl", "wb") as fp: pickle.dump(label, fp) else: doc['spec']['template']['metadata']['labels'][ 'app'] = lock_object['Parallel_label'] # Set image doc['spec']['template']['spec']['containers'][0][ 'image'] = default_image if not image else image # Set working directory doc['spec']['template']['spec']['containers'][0][ 'workingDir'] = "/tmp/" if workdir: doc['spec']['template']['spec']['containers'][0][ 'workingDir'] += workdir # set PVC pvc_name = os.environ['PVC_NAME'] if len(pvc_name) == 0: raise Exception( "Error setting pvc_name, probably problem in setting env variable of actual container" ) doc['spec']['template']['spec']['volumes'][0]['persistentVolumeClaim'][ 'claimName'] = pvc_name # Set orca required cpus if method == "orca": no_of_procs = get_no_of_procs(orca_method_file) if no_of_procs != -1: doc['spec']['template']['spec']['containers'][0]['resources'][ 'requests']['cpu'] = no_of_procs # Write to file ofile_name = "{}-{}-rdtscp.yaml".format(default_name, method) with open(ofile_name, "w") as ofile: ruamel_yaml.round_trip_dump(doc, ofile, explicit_start=True) return ofile_name, identificator