def render_config(workflow: "Workflow", config: dict) -> dict: # render the config prior to returning it content = yaml_dump(config) rendered_content = render(content, env=workflow.environment.data) rendered_config = yaml_load(rendered_content) print(f"rendered_config={rendered_config}") return rendered_config
def read_project_config(workflow: "Workflow") -> dict: """Reads the project config from the filesystem """ data = {} logger = get_logger() compose_flow_filename = workflow.args.compose_flow_filename if compose_flow_filename: paths = [compose_flow_filename] else: config_name_config_file = f"compose-flow.{workflow.config_basename}.yml" project_name_config_file = f"compose-flow.{workflow.project_name}.yml" paths = [ config_name_config_file, project_name_config_file, DC_CONFIG_PATH ] for item in paths: filename = os.path.basename(item) logger.debug(f"looking for {filename}") if os.path.exists(filename): with open(filename, "r") as fh: data = yaml_load(fh) break else: logger.warning( f"compose-flow config not found; tried {paths} in {os.getcwd()}") outfile = f"compose-flow-{workflow.config_name}-config.yml" with open(outfile, "w") as fh: fh.write(yaml_dump(data)) return data
def write(self) -> None: """ Writes the loaded compose file to disk """ with open(self.filename, "w") as fh: fh.write(yaml_dump(self.data))
def _compile(self, profile: dict) -> str: """ Compiles the profile into a single docker compose file Args: profile: The profile name to compile Returns: compiled compose file as a string """ if self._compiled_profile: return self._compiled_profile content = merge_profile(profile) # perform transformations on the compiled profile if content: data = yaml_load(content) # check if the environment needs to be copied from another service data = self._copy_environment(data) # see if any services need to be expanded out data = self._check_cf_config(data) # drop the compose_flow section if it exists data.pop("compose_flow", None) # for each service inject DOCKER_STACK and DOCKER_SERVICE for service_name, service_data in data.get("services", {}).items(): service_environment = service_data.setdefault( "environment", []) # convert the service_environment into a dict service_environment_d = {} for item in service_environment: item_split = item.split("=", 1) k = item_split[0] if len(item_split) > 1: v = item_split[1] else: v = None service_environment_d[k] = v for k, v in ( ("DOCKER_SERVICE", service_name), ("DOCKER_STACK", self.workflow.config_name), ): if k not in service_environment_d: service_environment_d[k] = v # reconstruct the k=v list honoring empty values service_environment_l = [] for k, v in service_environment_d.items(): if v is None: val = k else: val = f"{k}={v}" service_environment_l.append(val) # dump back out as list service_data["environment"] = service_environment_l # enforce resources self.set_resources(service_name, service_data) content = yaml_dump(data) self._compiled_profile = content return content