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 Apr 25, 2022
1 parent 837e1cf commit a546105
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions podman/domain/containers.py
Expand Up @@ -2,6 +2,7 @@
import io
import json
import logging
import shlex
from contextlib import suppress
from typing import Any, Dict, Iterable, Iterator, List, Mapping, Optional, Sequence, Tuple, Union

Expand Down Expand Up @@ -131,7 +132,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 @@ -163,16 +164,41 @@ def exec_run(
demux: Return stdout and stderr separately
Returns:
TBD
First item is a string of response text
Second item is the command response code
Raises:
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 shlex.split(cmd),
# "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, this will store command output
start_resp = self.client.post(f"/exec/{exec_id}/start", data=json.dumps(
{"Detach": detach, "Tty": tty}
))
start_resp.raise_for_status()
# get and return exec information
response = self.client.get(f"/exec/{exec_id}/json")
response.raise_for_status()
return start_resp.text, 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 a546105

Please sign in to comment.