2021-12-29 13:38:28 +00:00
|
|
|
#
|
|
|
|
# DockerBashOOP - Implementation of BashOOP for Docker.
|
2022-01-07 14:30:05 +00:00
|
|
|
# Copyright (C) 2022 Ad5001 <mail@ad5001.eu>
|
2021-12-29 13:38:28 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
2022-01-01 15:41:23 +00:00
|
|
|
# Only true when some containers matching all filters passed to the object have been found.
|
|
|
|
# Type: bool
|
|
|
|
property Container.found false
|
2021-12-29 19:45:55 +00:00
|
|
|
|
2022-01-01 15:41:23 +00:00
|
|
|
# Count of containers matching all filters found.
|
|
|
|
# Type: int
|
|
|
|
property Container.count 0
|
|
|
|
|
|
|
|
# Summary of all informations found on containers.
|
|
|
|
# Containers are separated by a new line and values by " ; ".
|
|
|
|
property Container.lines
|
|
|
|
# Docker container ids of matched containers.
|
|
|
|
property Container.id
|
|
|
|
# Docker container images of matched containers.
|
|
|
|
property Container.image
|
|
|
|
# Dates of creation of matched containers.
|
|
|
|
property Container.created
|
|
|
|
# Statuses summary of matched containers.
|
|
|
|
property Container.status
|
|
|
|
# List of ports of matched containers.
|
|
|
|
property Container.ports
|
|
|
|
# Names of matched containers.
|
|
|
|
property Container.name
|
2021-12-29 13:38:28 +00:00
|
|
|
|
2021-12-29 19:45:55 +00:00
|
|
|
# Constructor to fetch one or many running container.
|
2022-01-01 15:41:23 +00:00
|
|
|
# Arguments are the various filters to find containers. See: Container.filter for reference.
|
2021-12-29 13:38:28 +00:00
|
|
|
# NOTE: Multiple containers can be matched. If so, all properties contain the value for each container per line.
|
|
|
|
# NOTE: If no container is found, a message will be printed, and the constructor will exit with false.
|
2021-12-29 19:45:55 +00:00
|
|
|
# Signature: ([string[] filters])
|
2022-01-01 15:41:23 +00:00
|
|
|
Container.constructor() {
|
2021-12-29 19:45:55 +00:00
|
|
|
args=( "$@" )
|
|
|
|
this.lines = "$($(Docker.Utils.DockerCommand) ps --all | awk -F " {2,}" 'NR>1 { print $1,";",$2,";",$3,";",$4,";",$5,";",$6,($7 != "" ? (";"$7) : "") }')"
|
|
|
|
# Apply filters
|
|
|
|
for ((i=0; i < ${#args[@]}; i++)); do
|
2022-01-01 15:41:23 +00:00
|
|
|
filterName="${args[$i]}"
|
2021-12-29 19:45:55 +00:00
|
|
|
case $filterName in
|
|
|
|
with_id | with_image | created | with_status | with_port | named)
|
|
|
|
# The next argument is the value of the filter, so we skip it.
|
|
|
|
i=$(expr "$i" + 1)
|
2022-01-01 15:41:23 +00:00
|
|
|
filterValue="${args[$i]}"
|
2021-12-30 12:42:39 +00:00
|
|
|
this._baseFilter $filterName "$filterValue"
|
2021-12-29 19:45:55 +00:00
|
|
|
;;
|
|
|
|
running | exited | stopped)
|
|
|
|
this._baseFilter $filterName
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Unknown filter $filterName." 1>&2;
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
this._updateProperties
|
|
|
|
# Print error message in STDERR when no containers are found.
|
2021-12-30 12:42:39 +00:00
|
|
|
if [ "$(this.found)" == false ]; then
|
|
|
|
echo "No container found matching those filters." 1>&2
|
|
|
|
else
|
|
|
|
echo -n "Found $(this.count) containers matching filters: "
|
|
|
|
echo $(this.name)
|
|
|
|
fi
|
2021-12-29 19:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Finds all containers in "docker ps" with a certain field matching a value and returns their lines.
|
2022-01-01 15:41:23 +00:00
|
|
|
# Signature: (<int fieldId>, <string operation>, <string fieldValue>) -> string
|
|
|
|
Container.findLineByValue() {
|
2021-12-29 19:45:55 +00:00
|
|
|
fieldId=$1
|
|
|
|
operation=$2
|
|
|
|
fieldValue=$3
|
|
|
|
# YES. I know. You shouldn't do that with AWK, but variables, FOR SOME REASON, are not properly passed to AWK.
|
|
|
|
this.lines | awk -F " ; " '$'"$fieldId"' '"$operation"' "'"$fieldValue"'" { print $1,";",$2,";",$3,";",$4,";",$5,";",$6,($7 != "" ? (";"$7) : "") }'
|
|
|
|
}
|
|
|
|
|
2022-01-01 15:41:23 +00:00
|
|
|
# Internal function which filters. See: Container.filter for reference.
|
2021-12-29 19:45:55 +00:00
|
|
|
# Signature: (<string filterName>, [string filterValue])
|
2022-01-01 15:41:23 +00:00
|
|
|
Container._baseFilter() {
|
2021-12-29 19:45:55 +00:00
|
|
|
filterName=$1
|
|
|
|
filterValue=$2
|
2022-01-01 15:41:23 +00:00
|
|
|
#echo "Applying filter $filterName (previous count $(this.lines | wc -l))..." 1>&2
|
2021-12-29 19:45:55 +00:00
|
|
|
case $filterName in
|
|
|
|
with_id)
|
2021-12-30 12:42:39 +00:00
|
|
|
this.lines = "$(this.findLineByValue 1 == "$filterValue")"
|
2021-12-29 13:38:28 +00:00
|
|
|
;;
|
2021-12-29 19:45:55 +00:00
|
|
|
with_image)
|
2021-12-30 12:42:39 +00:00
|
|
|
this.lines = "$(this.findLineByValue 2 == "$filterValue")"
|
2021-12-29 13:38:28 +00:00
|
|
|
;;
|
|
|
|
created)
|
2021-12-30 12:42:39 +00:00
|
|
|
this.lines = "$(this.findLineByValue 4 == "$filterValue")"
|
2021-12-29 19:45:55 +00:00
|
|
|
;;
|
|
|
|
with_status)
|
2021-12-30 12:42:39 +00:00
|
|
|
this.lines = "$(this.findLineByValue 5 == "$filterValue")"
|
2021-12-29 13:38:28 +00:00
|
|
|
;;
|
2021-12-29 19:45:55 +00:00
|
|
|
running)
|
2022-01-01 15:41:23 +00:00
|
|
|
this.lines = "$(Container.findLineByValue 5 ~ "Up")"
|
2021-12-29 13:38:28 +00:00
|
|
|
;;
|
2021-12-29 19:45:55 +00:00
|
|
|
exited | stopped)
|
|
|
|
this.lines = "$(this.findLineByValue 5 "~" "Exited")"
|
|
|
|
;;
|
|
|
|
with_port)
|
2021-12-30 12:42:39 +00:00
|
|
|
this.lines = "$(this.findLineByValue 6 ~ "$filterValue")"
|
2021-12-29 13:38:28 +00:00
|
|
|
;;
|
|
|
|
named)
|
2021-12-30 12:42:39 +00:00
|
|
|
lines="$(this.findLineByValue 7 "~" "$filterValue")"
|
2021-12-29 13:38:28 +00:00
|
|
|
if [ -z "$lines" ]; then # Sometimes, when no ports are forwarded, the name is in the 6th field.
|
2021-12-30 12:42:39 +00:00
|
|
|
lines="$(this.findLineByValue 6 "~" "$filterValue")"
|
2021-12-29 13:38:28 +00:00
|
|
|
fi
|
2021-12-29 19:45:55 +00:00
|
|
|
this.lines = "$lines"
|
2021-12-29 13:38:28 +00:00
|
|
|
;;
|
|
|
|
esac
|
2021-12-29 19:45:55 +00:00
|
|
|
#echo " New count: $(this.lines | wc -l)." 1>&2
|
|
|
|
}
|
|
|
|
|
|
|
|
# Updates property data from fetched lines.
|
|
|
|
# No argument
|
2022-01-01 15:41:23 +00:00
|
|
|
Container._updateProperties() {
|
2021-12-29 19:45:55 +00:00
|
|
|
if [ ! -z "$(this.lines)" ]; then
|
2021-12-29 13:38:28 +00:00
|
|
|
this.found = true
|
2021-12-29 19:45:55 +00:00
|
|
|
this.count = $(this.lines | wc -l)
|
|
|
|
this.id = "$(this.lines | awk -F " ; " '{ print $1 }')"
|
|
|
|
this.image = "$(this.lines | awk -F " ; " '{ print $2 }')"
|
|
|
|
this.created = "$(this.lines | awk -F " ; " '{ print $4 }')"
|
|
|
|
this.status = "$(this.lines | awk -F " ; " '{ print $5 }')"
|
|
|
|
this.ports = "$(this.lines | awk -F " ; " '{ if($7 != "") { print $6 } }')" # If field 7 is null, that means field 6 is the name, so there are no ports.
|
|
|
|
this.name = "$(this.lines | awk -F " ; " '{ print ($7 != "") ? $7 : $6 }')"
|
2021-12-29 13:38:28 +00:00
|
|
|
else
|
|
|
|
this.found = false
|
2021-12-29 19:45:55 +00:00
|
|
|
this.count = 0
|
2021-12-29 13:38:28 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-12-29 19:45:55 +00:00
|
|
|
# Filters all containers assigned to the instance and removes all instances not corresponding to it.
|
|
|
|
# Filters with a value: with_id, with_image, created, with_status, with_port, named
|
|
|
|
# Filters without value: running, exited
|
|
|
|
# Filter types:
|
|
|
|
# - "named" to find by name
|
|
|
|
# - "created" to find by creation date,
|
|
|
|
# - "with_id" to find by container id
|
|
|
|
# - "with_image" to find by image
|
|
|
|
# - "with_status" to check status
|
|
|
|
# - "with_port" to find all having this port forwarded
|
|
|
|
# - "running" to find all containers running
|
|
|
|
# - "exited" or "stopped" to find all containers stopped.
|
2022-01-01 15:41:23 +00:00
|
|
|
# Signature: (<string filterName>, [string filterValue]) -> void
|
|
|
|
Container.filter() {
|
2021-12-29 19:45:55 +00:00
|
|
|
filterName=$1
|
|
|
|
filterValue=$2
|
|
|
|
this._baseFilter $filterName $filterValue
|
|
|
|
this._updateProperties
|
|
|
|
}
|
|
|
|
|
|
|
|
# Applies a docker command to all containers
|
2022-01-01 15:41:23 +00:00
|
|
|
# Signature: (<string command>, [string[] arguments]) -> void|string
|
|
|
|
Container.applyToAll() {
|
2021-12-29 19:45:55 +00:00
|
|
|
cmd=$1
|
|
|
|
args=${@:2}
|
|
|
|
for container in $(this.id); do
|
|
|
|
$(Docker.Utils.DockerCommand) $cmd $args $container
|
|
|
|
done
|
2021-12-29 13:38:28 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 19:45:55 +00:00
|
|
|
# Kills all containers assigned to this instance.
|
2022-01-01 15:41:23 +00:00
|
|
|
# Signature: ([string[] arguments]) -> void
|
|
|
|
Container.kill() {
|
2021-12-29 19:45:55 +00:00
|
|
|
$this.applyToAll kill $@
|
|
|
|
}
|
|
|
|
|
|
|
|
# Removes all containers assigned to this instance.
|
2022-01-01 15:41:23 +00:00
|
|
|
# Signature: ([string[] arguments]) -> void
|
|
|
|
Container.rm() {
|
2021-12-29 19:45:55 +00:00
|
|
|
$this.applyToAll rm $@
|
|
|
|
}
|
|
|
|
|
|
|
|
# Removes all containers assigned to this instance.
|
2022-01-01 15:41:23 +00:00
|
|
|
# Signature: ([string[] arguments]) -> void
|
|
|
|
Container.remove() {
|
2021-12-29 19:45:55 +00:00
|
|
|
$this.applyToAll rm $@
|
|
|
|
}
|
|
|
|
|
|
|
|
# Waits for all containers assigned to this instance to stop and echo their exit code.
|
2022-01-01 15:41:23 +00:00
|
|
|
# Signature: ([string[] arguments]) -> void
|
|
|
|
Container.wait() {
|
2021-12-29 19:45:55 +00:00
|
|
|
$this.applyToAll wait $@
|
|
|
|
}
|
2021-12-29 13:38:28 +00:00
|
|
|
|
2021-12-30 12:42:39 +00:00
|
|
|
# Displays the logs of the containers
|
2022-01-01 15:41:23 +00:00
|
|
|
# Signature: ([string[] arguments]) -> string
|
|
|
|
Container.logs() {
|
2021-12-30 12:42:39 +00:00
|
|
|
$this.applyToAll logs $@
|
|
|
|
}
|
|
|
|
|
2022-01-07 14:30:05 +00:00
|
|
|
# Starts all stopped containers
|
|
|
|
# Signature: ([string[] arguments]) -> string
|
|
|
|
Container.start() {
|
|
|
|
$this.applyToAll start $@
|
|
|
|
}
|
|
|
|
|
2021-12-30 12:42:39 +00:00
|
|
|
# Executes a command to all the containers assigned to the instance.
|
2022-01-01 15:41:23 +00:00
|
|
|
# Signature: ([string[] options] <string command>) -> void|string
|
|
|
|
Container.exec() {
|
2021-12-30 12:42:39 +00:00
|
|
|
cmd=${@[-1]}
|
|
|
|
options=${@:$(exec ${#@} - 1)}
|
|
|
|
for container in $(this.id); do
|
|
|
|
$(Docker.Utils.DockerCommand) exec $args $container $cmd
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|