def render_single_yaml( self, input_path: str, output_path: str, checker: BaseChecker = None, raw: bool = False, ) -> None: """ Read in single YAML file from specified path, render environment variables, then write out to a known location in the working dir. """ self.logger.info("Rendering YAML at %s to %s", input_path, output_path) with open(input_path, "r") as fh: content = fh.read() if not raw: env = dict(get_kv(self.workflow.environment.render(), multiple=True)) rendered = render(content, env=env) rendered = render_jinja(rendered, env=env) else: rendered = content if checker: check_errors = checker.check(rendered) if check_errors: raise errors.ManifestCheck("\n".join(check_errors)) with open(output_path, "w") as fh: fh.write(rendered)
def _copy_environment(self, data): """ Processes CF_COPY_ENV_FROM environment entries """ environments = {} # first get the env from each service for service_name, service_data in data["services"].items(): environment = service_data.get("environment") if environment: _env = {} for item in environment: k, v = get_kv(item) _env[k] = v environments[service_name] = _env # go through each service environment and apply any copies found for service_name, service_data in data["services"].items(): environment = service_data.get("environment") if not environment: continue new_env = {} for item in environment: key, val = get_kv(item) new_env[key] = val if not item.startswith(COPY_ENV_VAR): continue _env = environments.get(val) if not _env: raise EnvError( f"Unable to find val={val} to copy into service_name={service_name}" ) new_env.update(_env) service_data["environment"] = listify_kv(new_env) return data
def test_get_kv_multiple(self, *mocks): """Ensure multiple items are parsed """ data = utils.get_kv("FOO=one\nBAR=two", multiple=True) self.assertEqual([("FOO", "one"), ("BAR", "two")], data)
def test_get_kv(self, *mocks): """Ensure a single item is parsed """ data = utils.get_kv("FOO=one") self.assertEqual(("FOO", "one"), data)