Add rm and rmi commands

https://github.com/debarshiray/toolbox/pull/45
This commit is contained in:
Ondřej Zoder 2019-02-19 16:59:19 +01:00 committed by Debarshi Ray
parent 40cb8caf46
commit 7acc9936cf
5 changed files with 310 additions and 0 deletions

View file

@ -9,6 +9,8 @@ manuals = [
'toolbox-create.1',
'toolbox-enter.1',
'toolbox-list.1',
'toolbox-rm.1',
'toolbox-rmi.1',
]
foreach manual: manuals

52
doc/toolbox-rm.1.md Normal file
View file

@ -0,0 +1,52 @@
% toolbox-rm(1)
## NAME
toolbox\-rm - Remove one or more toolbox containers
## SYNOPSIS
**toolbox rm** [*--all*] [*--force*] [*CONTAINER*...]
## DESCRIPTION
Removes one or more toolbox containers from the host. The container should
have been created using the `toolbox create` command.
A toolbox container is an OCI container. Therefore, `toolbox rm` can be used
interchangeably with `podman rm`.
## OPTIONS ##
The following options are understood:
**--all, -a**
Remove all toolbox containers. It can be used in conjuction with `--force` as
well.
**--force, -f**
Force the removal of running and paused toolbox containers.
## EXAMPLES
### Remove a toolbox container named `fedora-toolbox-gegl:30`
```
$ toolbox rm fedora-toolbox-gegl:30
```
### Remove all toolbox containers, but not those that are running or paused
```
$ toolbox rm --all
```
### Remove all toolbox containers, including ones that are running or paused
```
$ toolbox rm --all --force
```
## SEE ALSO
`buildah(1)`, `podman(1)`, `podman-rm(1)`

52
doc/toolbox-rmi.1.md Normal file
View file

@ -0,0 +1,52 @@
% toolbox-rmi(1)
## NAME
toolbox\-rmi - Remove one or more toolbox images
## SYNOPSIS
**toolbox rmi** [*--all*] [*--force*] [*IMAGE*...]
## DESCRIPTION
Removes one or more toolbox images from the host. The image should have been
created using the `toolbox create` command.
A toolbox image is an OCI image. Therefore, `toolbox rmi` can be used
interchangeably with `podman rmi`.
## OPTIONS ##
The following options are understood:
**--all, -a**
Remove all toolbox images. It can be used in conjuction with `--force` as well.
**--force, -f**
Force the removal of toolbox images that are used by toolbox containers. The
dependent containers will be removed as well.
## EXAMPLES
### Remove a toolbox image named `localhost/fedora-toolbox-gegl:30`
```
$ toolbox rmi localhost/fedora-toolbox-gegl:30
```
### Remove all toolbox images, but not those that are used by containers
```
$ toolbox rmi --all
```
### Remove all toolbox images and their dependent containers
```
$ toolbox rmi --all --force
```
## SEE ALSO
`buildah(1)`, `podman(1)`, `podman-rm(1)`

View file

@ -57,6 +57,14 @@ Enter an existing toolbox container for interactive use.
List existing toolbox containers and images.
**toolbox-rm(1)**
Remove one or more toolbox containers.
**toolbox-rmi(1)**
Remove one or more toolbox images.
## SEE ALSO
`buildah(1)`, `podman(1)`

196
toolbox
View file

@ -82,6 +82,26 @@ has_prefix()
)
has_substring()
(
haystack="$1"
needle="$2"
ret_val=1
case "$haystack" in
*"$needle"* )
ret_val=0
;;
* )
ret_val=1
;;
esac
return $ret_val
)
is_integer()
{
[ "$1" != "" ] && [ $1 -eq $1 ] 2>&3
@ -537,6 +557,137 @@ list_containers()
)
remove_containers()
(
ids=$1
all=$2
force=$3
ret_val=0
$force && force_option="--force"
if $all; then
if ! ids=$($prefix_sudo podman ps \
--all \
--filter "label=com.redhat.component=fedora-toolbox" \
--format "{{.ID}}" 2>&3); then
echo "$base_toolbox_command: failed to list containers" >&2
return 1
fi
if [ "$ids" != "" ]; then
ret_val=$(echo "$ids" \
| (
while read id; do
if ! $prefix_sudo podman rm $force_option "$id" >/dev/null 2>&3; then
echo "$base_toolbox_command: failed to remove container $id" >&2
ret_val=1
fi
done
echo "$ret_val"
)
)
fi
else
ret_val=$(echo "$ids" \
| sed "s/ \+/\n/g" 2>&3 \
| (
while read -r id; do
if ! labels=$($prefix_sudo podman inspect \
--format "{{.Config.Labels}}" \
--type container \
"$id" 2>&3); then
echo "$base_toolbox_command: failed to inspect $id" >&2
ret_val=1
continue
fi
if ! has_substring "$labels" "com.redhat.component:fedora-toolbox"; then
echo "$base_toolbox_command: $id is not a toolbox container" >&2
ret_val=1
continue
fi
if ! $prefix_sudo podman rm $force_option "$id" >/dev/null 2>&3; then
echo "$base_toolbox_command: failed to remove container $id" >&2
ret_val=1
fi
done
echo "$ret_val"
)
)
fi
return $ret_val
)
remove_images()
(
ids=$1
all=$2
force=$3
ret_val=0
$force && force_option="--force"
if $all; then
if ! ids=$($prefix_sudo podman images \
--filter "label=com.redhat.component=fedora-toolbox" \
--format "{{.ID}}" 2>&3); then
echo "$0: failed to list images" >&2
return 1
fi
if [ "$ids" != "" ]; then
ret_val=$(echo "$ids" \
| (
while read id; do
if ! $prefix_sudo podman rmi $force_option "$id" >/dev/null 2>&3; then
echo "$base_toolbox_command: failed to remove image $id" >&2
ret_val=1
fi
done
echo "$ret_val"
)
)
fi
else
ret_val=$(echo "$ids" \
| sed "s/ \+/\n/g" 2>&3 \
| (
while read -r id; do
if ! labels=$($prefix_sudo podman inspect \
--format "{{.Labels}}" \
--type image \
"$id" 2>&3); then
echo "$base_toolbox_command: failed to inspect $id" >&2
ret_val=1
continue
fi
if ! has_substring "$labels" "com.redhat.component:fedora-toolbox"; then
echo "$base_toolbox_command: $id is not a toolbox image" >&2
ret_val=1
continue
fi
if ! $prefix_sudo podman rmi $force_option "$id" >/dev/null 2>&3; then
echo "$base_toolbox_command: failed to remove image $id" >&2
ret_val=1
fi
done
echo "$ret_val"
)
)
fi
return $ret_val
)
exit_if_extra_operand()
{
if [ "$1" != "" ]; then
@ -615,6 +766,10 @@ usage()
echo " or: toolbox [-v | --verbose]"
echo " list [-c | --containers]"
echo " [-i | --images]"
echo " or: toolbox rm [-a | --all]"
echo " [-f | --force] [<container> ...]"
echo " or: toolbox rmi [-a | --all]"
echo " or: [-f | --force] [<image> ...]"
echo " or: toolbox --help"
}
@ -793,6 +948,47 @@ case $op in
fi
exit
;;
rm | rmi )
if is_integer "$podman_pid"; then
forward_to_host
else
rm_all=false
rm_force=false
while has_prefix "$1" -; do
case $1 in
-a | --all )
rm_all=true
;;
-f | --force )
rm_force=true
;;
* )
exit_if_unrecognized_option $1
esac
shift
done
rm_ids=""
if $rm_all; then
exit_if_extra_operand $1
else
exit_if_missing_argument "$op" "$1"
while [ "$1" != "" ]; do
rm_ids="$rm_ids $1"
shift
done
fi
rm_ids=$(echo "$rm_ids" | sed "s/^ \+//" 2>&3)
if [ "$op" = "rm" ]; then
remove_containers "$rm_ids" "$rm_all" "$rm_force"
else
remove_images "$rm_ids" "$rm_all" "$rm_force"
fi
fi
exit
;;
* )
echo "$base_toolbox_command: unrecognized command '$op'" >&2
echo "Try '$base_toolbox_command --help' for more information." >&2