|
1 | 1 | import subprocess
|
2 | 2 | from sys import version_info
|
3 |
| -from typing import Optional, Tuple |
| 3 | +from typing import Optional, Tuple, List |
| 4 | + |
| 5 | +import click |
4 | 6 |
|
5 | 7 |
|
6 | 8 | class UnsupportedVersion(Exception):
|
@@ -53,3 +55,39 @@ def run_on_host(
|
53 | 55 | completed.stdout.decode("utf-8") if completed.stdout else "",
|
54 | 56 | completed.stderr.decode("utf-8") if completed.stderr else "",
|
55 | 57 | )
|
| 58 | + |
| 59 | + |
| 60 | +def run_in_docker( |
| 61 | + cmd: str, |
| 62 | + image_name: str, |
| 63 | + env_vars: Optional[List[str]] = [], |
| 64 | + no_bin_bash: Optional[bool] = False, |
| 65 | + capture_output: Optional[bool] = False, |
| 66 | + silent: Optional[bool] = False, |
| 67 | +) -> Tuple[bool, str]: |
| 68 | + """Shells out and runs the cmd in docker |
| 69 | +
|
| 70 | + Args: |
| 71 | + cmd (str): The command you want to run |
| 72 | + env_vars (Optional[dict], optional): Dict of env vars to pass to the docker container. |
| 73 | + """ |
| 74 | + if not silent: |
| 75 | + click.secho(f"Executing command `{cmd}` in compose...", fg="yellow") |
| 76 | + |
| 77 | + docker_cmd = ["docker compose run --service-ports --rm"] |
| 78 | + docker_cmd.extend(env_vars) |
| 79 | + docker_cmd.append(image_name) |
| 80 | + if no_bin_bash: |
| 81 | + docker_cmd.append(cmd) |
| 82 | + else: |
| 83 | + docker_cmd.append(f'/bin/bash -c "{cmd}" ') |
| 84 | + |
| 85 | + ex_code, std_out, std_err = run_on_host(" ".join(docker_cmd), False, capture_output) |
| 86 | + if capture_output: |
| 87 | + if ex_code == 0: |
| 88 | + return (True, std_out) |
| 89 | + return (False, std_err) |
| 90 | + |
| 91 | + if ex_code == 0: |
| 92 | + return (True, "Success! Palm completed with exit code 0") |
| 93 | + return (False, f"Fail! Palm exited with code {ex_code}") |
0 commit comments