Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicates showing in list output #233

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 40 additions & 76 deletions toolbox
Original file line number Diff line number Diff line change
Expand Up @@ -507,29 +507,6 @@ image_reference_has_domain()
)


images_get_details()
(
images="$1"

if ! echo "$images" | while read -r image; do
[ "$image" = "" ] 2>&3 && continue

if ! $prefix_sudo podman images \
--format "{{.ID}} {{.Repository}}:{{.Tag}} {{.Created}}" \
--noheading \
"$image" 2>&3; then
echo "$base_toolbox_command: failed to get details for image $image" >&2
return 1
fi
echo
done; then
return 1
fi

return 0
)


is_etc_profile_d_toolbox_a_bind_mount()
{
container="$1"
Expand All @@ -541,29 +518,6 @@ is_etc_profile_d_toolbox_a_bind_mount()
}


list_container_names()
(
if ! containers_old=$($prefix_sudo podman ps \
--all \
--filter "label=com.redhat.component=fedora-toolbox" \
--format "{{.Names}}" 2>&3); then
echo "$base_toolbox_command: failed to list containers with com.redhat.component=fedora-toolbox" >&2
return 1
fi

if ! containers=$($prefix_sudo podman ps \
--all \
--filter "label=com.github.debarshiray.toolbox=true" \
--format "{{.Names}}" 2>&3); then
echo "$base_toolbox_command: failed to list containers with com.github.debarshiray.toolbox=true" >&2
return 1
fi

printf "%s\n%s\n" "$containers_old" "$containers" | sort 2>&3 | uniq 2>&3
return 0
)


pull_base_toolbox_image()
(
domain=""
Expand Down Expand Up @@ -1195,31 +1149,37 @@ help()
)


list_images()
get_images()
(
output=""

if ! images_old=$($prefix_sudo podman images \
--filter "label=com.redhat.component=fedora-toolbox" \
--format "{{.Repository}}:{{.Tag}}" 2>&3); then
--format "{{.ID}} {{.Repository}}:{{.Tag}} {{.Created}}" 2>&3); then
echo "$base_toolbox_command: failed to list images with com.redhat.component=fedora-toolbox" >&2
return 1
fi

if ! images=$($prefix_sudo podman images \
--filter "label=com.github.debarshiray.toolbox=true" \
--format "{{.Repository}}:{{.Tag}}" 2>&3); then
--format "{{.ID}} {{.Repository}}:{{.Tag}} {{.Created}}" 2>&3); then
echo "$base_toolbox_command: failed to list images with com.github.debarshiray.toolbox=true" >&2
return 1
fi

images=$(printf "%s\n%s\n" "$images_old" "$images" | sort 2>&3 | uniq 2>&3)
if ! details=$(images_get_details "$images"); then
printf "%s\n%s\n" "$images_old" "$images" | sort 2>&3 | uniq 2>&3
return 0
)


list_images()
(
output=""

if ! images_info=$(get_images); then
return 1
fi

if [ "$details" != "" ] 2>&3; then
table_data=$(printf "%s\t%s\t%s\n" "IMAGE ID" "IMAGE NAME" "CREATED"; echo "$details")
if [ "$images_info" != "" ] 2>&3; then
table_data=$(printf "%s\t%s\t%s\n" "IMAGE ID" "IMAGE NAME" "CREATED"; echo "$images_info")
if ! output=$(echo "$table_data" | sed "s/ \{2,\}/\t/g" 2>&3 | column -s "$tab" -t 2>&3); then
echo "$base_toolbox_command: failed to parse list of images" >&2
return 1
Expand All @@ -1236,23 +1196,25 @@ list_images()
)


containers_get_details()
get_containers()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a reason why we first get the names, and then the rest of the details. :) Granted, it's a very obscure reason.

Since Podman's filters don't support using a logical or, we had to separately query the com.redhat.component and com.github.debarshiray.toolbox tagged images and merge the results into a single list.

(I wonder if we can conjure up a logical or now that Podman filters support regular expressions.)

Merging the lists using uniq(1) is tricky. We use attributes like {{.Created}} for each item, which can potentially change across two different Podman invocations if the item is new enough to be less than a minute old, and hence prevent uniq(1) from combining them into one entry.

That's why the names are first queried, merged, and then the attributes are read.

Now that we bumped our minimum Podman requirement to 1.4.0, it would've been nice if the regular expression support was also part of 1.4.0. Sadly, it isn't. It landed only in 1.5.0. Given that we still run into the occasional Podman regression (eg., containers/podman#3946) I am not comfortable hard bumping the baseline to 1.5.0. At least not yet. It would be nice to be able to keep using 1.4.0 on a best effort basis.

This might mean that we either figure out a way to detect the regular expression support at run-time and use it, or we fix this bug as part of our rewrite in Go because we'd just vendor in the necessary libpod version. I am leaning towards the latter option, myself.

(
containers="$1"

if ! echo "$containers" | while read -r container; do
[ "$container" = "" ] 2>&3 && continue
if ! containers_old=$($prefix_sudo podman ps \
--all \
--filter "label=com.redhat.component=fedora-toolbox" \
--format "{{.ID}} {{.Names}} {{.Created}} {{.Status}} {{.Image}}" 2>&3); then
echo "$base_toolbox_command: failed to list containers with com.redhat.component=fedora-toolbox" >&2
return 1
fi

if ! $prefix_sudo podman ps --all \
--filter "name=$container" \
--format "{{.ID}} {{.Names}} {{.Created}} {{.Status}} {{.Image}}" 2>&3; then
echo "$base_toolbox_command: failed to get details for container $container" >&2
return 1
fi
done; then
if ! containers=$($prefix_sudo podman ps \
--all \
--filter "label=com.github.debarshiray.toolbox=true" \
--format "{{.ID}} {{.Names}} {{.Created}} {{.Status}} {{.Image}}" 2>&3); then
echo "$base_toolbox_command: failed to list containers with com.github.debarshiray.toolbox=true" >&2
return 1
fi

printf "%s\n%s\n" "$containers_old" "$containers" | sort 2>&3 | uniq 2>&3
return 0
)

Expand All @@ -1261,17 +1223,13 @@ list_containers()
(
output=""

if ! containers=$(list_container_names); then
return 1
fi

if ! details=$(containers_get_details "$containers"); then
if ! containers=$(get_containers); then
return 1
fi

if [ "$details" != "" ] 2>&3; then
if [ "$containers" != "" ] 2>&3; then
table_data=$(printf "%s\t%s\t%s\t%s\t%s\n" "CONTAINER ID" "CONTAINER NAME" "CREATED" "STATUS" "IMAGE NAME"
echo "$details")
echo "$containers")
if ! output=$(echo "$table_data" | sed "s/ \{2,\}/\t/g" 2>&3 | column -s "$tab" -t 2>&3); then
echo "$base_toolbox_command: failed to parse list of containers" >&2
return 1
Expand Down Expand Up @@ -1992,8 +1950,14 @@ case $op in
fi
fi

$ls_images && [ "$images" != "" ] && echo "$images"
$ls_containers && [ "$containers" != "" ] && echo "$containers"
debarshiray marked this conversation as resolved.
Show resolved Hide resolved
if $ls_images && [ "$images" != "" ]; then
echo "$images"
fi

if $ls_containers && [ "$containers" != "" ]; then
echo "$containers"
fi

exit
;;
rm | rmi )
Expand Down