Skip to content

Commit a2c4fe6

Browse files
Merge pull request #103 from jakeberesford-palmetto/feature/run-in-docker-util
Abstract run_in_docker to a utility function
2 parents 82a46ca + bc98863 commit a2c4fe6

File tree

3 files changed

+46
-24
lines changed

3 files changed

+46
-24
lines changed

palm/environment.py

+6-21
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from pydantic import BaseModel
77

88
from palm.plugin_manager import PluginManager
9-
from palm.utils import run_on_host
9+
from palm.utils import run_on_host, run_in_docker
1010

1111
from .code_generator import CodeGenerator
1212
from .palm_config import PalmConfig
@@ -23,27 +23,12 @@ def run_in_docker(
2323
cmd: str,
2424
env_vars: Optional[dict] = {},
2525
no_bin_bash: Optional[bool] = False,
26+
silent: Optional[bool] = False,
2627
) -> Tuple[bool, str]:
27-
"""Shells out and runs the cmd in docker
28-
29-
Args:
30-
cmd (str): The command you want to run
31-
env_vars (Optional[dict], optional): Dict of env vars to pass to the docker container.
32-
"""
33-
click.secho(f"Executing command `{cmd}` in compose...", fg="yellow")
34-
35-
docker_cmd = ["docker compose run --service-ports --rm"]
36-
docker_cmd.extend(self._build_env_vars(env_vars))
37-
docker_cmd.append(self.palm.image_name)
38-
if no_bin_bash:
39-
docker_cmd.append(cmd)
40-
else:
41-
docker_cmd.append(f'/bin/bash -c "{cmd}" ')
42-
43-
ex_code, _, _ = run_on_host(" ".join(docker_cmd))
44-
if ex_code == 0:
45-
return (True, "Success! Palm completed with exit code 0")
46-
return (False, f"Fail! Palm exited with code {ex_code}")
28+
env_vars_list = self._build_env_vars(env_vars)
29+
return run_in_docker(
30+
cmd, self.palm.image_name, env_vars_list, no_bin_bash, silent
31+
)
4732

4833
def run_on_host(
4934
self,

palm/plugins/base_plugin_config.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ def set(self) -> dict:
1919
"""Setter for plugin config
2020
2121
This method should be implemented by the plugin to set the config
22-
for the plugin. It should return True if the config was set successfully,
23-
and False if it was not.
22+
for the plugin. It should return a dict of the config that was set.
2423
2524
Returns:
2625
dict: The config that was set

palm/utils.py

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import subprocess
22
from sys import version_info
3-
from typing import Optional, Tuple
3+
from typing import Optional, Tuple, List
4+
5+
import click
46

57

68
class UnsupportedVersion(Exception):
@@ -53,3 +55,39 @@ def run_on_host(
5355
completed.stdout.decode("utf-8") if completed.stdout else "",
5456
completed.stderr.decode("utf-8") if completed.stderr else "",
5557
)
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

Comments
 (0)