If template is a valid template engine, process the cmd and cwd through that engine.
(cmd, cwd, template, saltenv=None, pillarenv=None, pillar_override=None)
| 132 | |
| 133 | |
| 134 | def _render_cmd(cmd, cwd, template, saltenv=None, pillarenv=None, pillar_override=None): |
| 135 | """ |
| 136 | If template is a valid template engine, process the cmd and cwd through |
| 137 | that engine. |
| 138 | """ |
| 139 | if saltenv is None: |
| 140 | try: |
| 141 | saltenv = __opts__.get("saltenv", "base") |
| 142 | except NameError: |
| 143 | saltenv = "base" |
| 144 | |
| 145 | if not template: |
| 146 | return (cmd, cwd) |
| 147 | |
| 148 | # render the path as a template using path_template_engine as the engine |
| 149 | if template not in salt.utils.templates.TEMPLATE_REGISTRY: |
| 150 | raise CommandExecutionError( |
| 151 | f"Attempted to render file paths with unavailable engine {template}" |
| 152 | ) |
| 153 | |
| 154 | kwargs = {} |
| 155 | kwargs["salt"] = __salt__ |
| 156 | if pillarenv is not None or pillar_override is not None: |
| 157 | pillarenv = pillarenv or __opts__["pillarenv"] |
| 158 | kwargs["pillar"] = _gather_pillar(pillarenv, pillar_override) |
| 159 | else: |
| 160 | kwargs["pillar"] = __pillar__ |
| 161 | kwargs["grains"] = __grains__ |
| 162 | kwargs["opts"] = __opts__ |
| 163 | kwargs["saltenv"] = saltenv |
| 164 | |
| 165 | def _render(contents): |
| 166 | # write out path to temp file |
| 167 | tmp_path_fn = salt.utils.files.mkstemp() |
| 168 | with salt.utils.files.fopen(tmp_path_fn, "w+") as fp_: |
| 169 | fp_.write(salt.utils.stringutils.to_str(contents)) |
| 170 | data = salt.utils.templates.TEMPLATE_REGISTRY[template]( |
| 171 | tmp_path_fn, to_str=True, **kwargs |
| 172 | ) |
| 173 | salt.utils.files.safe_rm(tmp_path_fn) |
| 174 | if not data["result"]: |
| 175 | # Failed to render the template |
| 176 | raise CommandExecutionError( |
| 177 | "Failed to execute cmd with error: {}".format(data["data"]) |
| 178 | ) |
| 179 | else: |
| 180 | return data["data"] |
| 181 | |
| 182 | cmd = _render(cmd) |
| 183 | cwd = _render(cwd) |
| 184 | return (cmd, cwd) |
| 185 | |
| 186 | |
| 187 | def _check_loglevel(level="info"): |
no test coverage detected