Skip to content

Commit

Permalink
Address docker/podman differences in ps
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelldls committed Mar 20, 2024
1 parent ff2cdc6 commit 957d7ef
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 31 deletions.
48 changes: 26 additions & 22 deletions src/edge_containers_cli/cmds/local_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,42 +165,46 @@ def stop(self):
def ps(self, all: bool, wide: bool):
all_arg = " --all" if all else ""

# Retrieve data (json option exposes more data than table)
result_json = shell.run_command(
f"{self.docker.docker} ps{all_arg} --filter label=is_IOC=true "
f"--format json",
# List services
avail_services = shell.run_command(
f"{self.docker.docker} ps{all_arg} -q --filter label=is_IOC=true",
interactive=False,
)
log.debug(result_json)
if not self.docker.is_docker:
services_dicts = json.load(StringIO(str(result_json)))
else:
# Docker does not format as a list of json but per line
services_dicts = []
for line in str(result_json).strip().split("\n"):
services_dicts.append(json.load(StringIO(line)))

if not services_dicts:
if not avail_services:
if all_arg:
print("No deployed services found")
else:
print("No running services found")
raise typer.Exit()

# Retrieve data
result_json = shell.run_command(
f"{self.docker.docker} inspect "
f"$({self.docker.docker} ps{all_arg} -q --filter label=is_IOC=true)",
interactive=False,
)
log.debug(result_json)
services_dicts = json.load(StringIO(str(result_json)))

select_data = []
for service in services_dicts:
# Make docker output look like podman
if self.docker.is_docker:
service["Name"] = service["Name"][1:] # inspect leads name with /

# Note if adding more keys that there are differences
# between what is given between docker and podman
# between what is available between docker and podman
select_data.append(
{
"name": service["Names"][0],
"version": service["Labels"]["version"],
"running": service["State"] == "running",
"restarts": service["Restarts"],
"deployed": service["CreatedAt"][:19]
if self.docker.is_docker
else datetime.fromtimestamp(service["Created"]),
"image": service["Image"],
"name": service["Name"],
"version": service["Config"]["Labels"]["version"],
"running": "true" if service["State"]["Running"] else "false",
"restarts": service["RestartCount"],
"deployed": datetime.strptime(
service["Created"].split(".")[0], "%Y-%m-%dT%H:%M:%S"
),
"image": service["Config"]["Image"],
}
)
log.debug(select_data)
Expand Down
46 changes: 37 additions & 9 deletions tests/data/local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,44 @@ stop:
rsp: True

ps:
- cmd: docker ps --filter label=is_IOC=true --format json
- cmd: docker ps -q --filter label=is_IOC=true
rsp: |
0
1
- cmd: docker inspect $(docker ps -q --filter label=is_IOC=true)
rsp: >
{
"CreatedAt":"",
"Image": "",
"Labels":{"version":""},
"Names":[""],
"State":"",
"Restarts":""
}
[
{
"Created": "2024-03-19T11:10:53.736808252Z",
"State": {
"Running": true
},
"Name": "/bl01t-ea-test-01",
"RestartCount": 0,
"Config": {
"Image": "ghcr.io/test",
"Labels": {
"is_IOC": "true",
"version": "0"
}
}
},
{
"Created": "2024-03-19T11:10:53.736808252Z",
"State": {
"Running": true
},
"Name": "/bl01t-ea-test-02",
"RestartCount": 0,
"Config": {
"Image": "ghcr.io/test",
"Labels": {
"is_IOC": "true",
"version": "0"
}
}
}
]
validate:
- cmd: docker run --rm -w .* -v .*:.* -v \/tmp:\/tmp ghcr.io\/epics-containers\/yajsv -v .*:.* -s \/tmp\/ec_tests\/schema.json .*/bl45p-ea-ioc-01\/config\/ioc.yaml
Expand Down

0 comments on commit 957d7ef

Please sign in to comment.