Skip to content

Commit

Permalink
Add exec_run implementation for containers
Browse files Browse the repository at this point in the history
This change adds a basic implementation of exec_run for containers
Environment variable conversions are handled from dict->list[str]
DetachKeys will need an actual argument option
detach, stream, socket, and demux arguments are currently not handled.
  • Loading branch information
JacobCallahan committed Feb 4, 2022
1 parent 4f78fe0 commit 1221f07
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions podman/domain/containers.py
Expand Up @@ -130,7 +130,7 @@ def exec_run(
stdout: bool = True,
stderr: bool = True,
stdin: bool = False,
tty: bool = False,
tty: bool = True,
privileged: bool = False,
user=None,
detach: bool = False,
Expand Down Expand Up @@ -170,10 +170,34 @@ def exec_run(
NotImplementedError: method not implemented.
APIError: when service reports error
"""
if user is None:
user = "root"

raise NotImplementedError()
user = user or "root"
if isinstance(environment, dict):
environment = [f"{k}={v}" for k, v in environment.items()]
data = {
"AttachStderr": stderr,
"AttachStdin": stdin,
"AttachStdout": stdout,
"Cmd": cmd if isinstance(cmd, list) else cmd.split(),
# "DetachKeys": detach, # This is something else
"Env": environment,
"Privileged": privileged,
"Tty": tty,
"User": user,
"WorkingDir": workdir
}
# create the exec instance
response = self.client.post(f"/containers/{self.name}/exec", data=json.dumps(data))
response.raise_for_status()
exec_id = response.json()['Id']
# start the exec instance
response = self.client.post(f"/exec/{exec_id}/start", data=json.dumps(
{"Detach": detach, "Tty": tty}
))
response.raise_for_status()
# get and return exec information
response = self.client.get(f"/exec/{exec_id}/json")
response.raise_for_status()
return response.json().get('ExitCode')

def export(self, chunk_size: int = api.DEFAULT_CHUNK_SIZE) -> Iterator[bytes]:
"""Download container's filesystem contents as a tar archive.
Expand Down

0 comments on commit 1221f07

Please sign in to comment.