Adding more comments, renaming RunningContainer to Container.

This commit is contained in:
Adsooi 2022-01-01 16:41:23 +01:00
parent acdd24b1e3
commit 116857fa47
Signed by: Ad5001
GPG key ID: EF45F9C6AFE20160
3 changed files with 52 additions and 40 deletions

View file

@ -16,34 +16,46 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
property RunningContainer.found false # Only true when some containers matching all filters passed to the object have been found.
property RunningContainer.count 0 # Type: bool
property Container.found false
property RunningContainer.lines # Count of containers matching all filters found.
property RunningContainer.id # Type: int
property RunningContainer.image property Container.count 0
property RunningContainer.created
property RunningContainer.status # Summary of all informations found on containers.
property RunningContainer.ports # Containers are separated by a new line and values by " ; ".
property RunningContainer.name 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
# Constructor to fetch one or many running container. # Constructor to fetch one or many running container.
# Arguments are the various filters to find containers. See: RunningContainer.filter for reference. # Arguments are the various filters to find containers. See: Container.filter for reference.
# NOTE: Multiple containers can be matched. If so, all properties contain the value for each container per line. # 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. # NOTE: If no container is found, a message will be printed, and the constructor will exit with false.
# Signature: ([string[] filters]) # Signature: ([string[] filters])
RunningContainer.constructor() { Container.constructor() {
args=( "$@" ) args=( "$@" )
this.lines = "$($(Docker.Utils.DockerCommand) ps --all | awk -F " {2,}" 'NR>1 { print $1,";",$2,";",$3,";",$4,";",$5,";",$6,($7 != "" ? (";"$7) : "") }')" this.lines = "$($(Docker.Utils.DockerCommand) ps --all | awk -F " {2,}" 'NR>1 { print $1,";",$2,";",$3,";",$4,";",$5,";",$6,($7 != "" ? (";"$7) : "") }')"
# Apply filters # Apply filters
for ((i=0; i < ${#args[@]}; i++)); do for ((i=0; i < ${#args[@]}; i++)); do
filterName=${args[$i]} filterName="${args[$i]}"
case $filterName in case $filterName in
with_id | with_image | created | with_status | with_port | named) with_id | with_image | created | with_status | with_port | named)
# The next argument is the value of the filter, so we skip it. # The next argument is the value of the filter, so we skip it.
i=$(expr "$i" + 1) i=$(expr "$i" + 1)
filterValue=${args[$i]} filterValue="${args[$i]}"
this._baseFilter $filterName "$filterValue" this._baseFilter $filterName "$filterValue"
;; ;;
running | exited | stopped) running | exited | stopped)
@ -65,8 +77,8 @@ RunningContainer.constructor() {
} }
# Finds all containers in "docker ps" with a certain field matching a value and returns their lines. # Finds all containers in "docker ps" with a certain field matching a value and returns their lines.
# Signature: (<int fieldId>, <string operation>, <string fieldValue>) # Signature: (<int fieldId>, <string operation>, <string fieldValue>) -> string
RunningContainer.findLineByValue() { Container.findLineByValue() {
fieldId=$1 fieldId=$1
operation=$2 operation=$2
fieldValue=$3 fieldValue=$3
@ -74,12 +86,12 @@ RunningContainer.findLineByValue() {
this.lines | awk -F " ; " '$'"$fieldId"' '"$operation"' "'"$fieldValue"'" { print $1,";",$2,";",$3,";",$4,";",$5,";",$6,($7 != "" ? (";"$7) : "") }' this.lines | awk -F " ; " '$'"$fieldId"' '"$operation"' "'"$fieldValue"'" { print $1,";",$2,";",$3,";",$4,";",$5,";",$6,($7 != "" ? (";"$7) : "") }'
} }
# Internal function which filters. See: RunningContainer.filter for reference. # Internal function which filters. See: Container.filter for reference.
# Signature: (<string filterName>, [string filterValue]) # Signature: (<string filterName>, [string filterValue])
RunningContainer._baseFilter() { Container._baseFilter() {
filterName=$1 filterName=$1
filterValue=$2 filterValue=$2
#echo -n "Applying filter $filterName (previous count $(this.lines | wc -l))..." 1>&2 #echo "Applying filter $filterName (previous count $(this.lines | wc -l))..." 1>&2
case $filterName in case $filterName in
with_id) with_id)
this.lines = "$(this.findLineByValue 1 == "$filterValue")" this.lines = "$(this.findLineByValue 1 == "$filterValue")"
@ -94,7 +106,7 @@ RunningContainer._baseFilter() {
this.lines = "$(this.findLineByValue 5 == "$filterValue")" this.lines = "$(this.findLineByValue 5 == "$filterValue")"
;; ;;
running) running)
this.lines = "$(RunningContainer.findLineByValue 5 ~ "Up")" this.lines = "$(Container.findLineByValue 5 ~ "Up")"
;; ;;
exited | stopped) exited | stopped)
this.lines = "$(this.findLineByValue 5 "~" "Exited")" this.lines = "$(this.findLineByValue 5 "~" "Exited")"
@ -115,7 +127,7 @@ RunningContainer._baseFilter() {
# Updates property data from fetched lines. # Updates property data from fetched lines.
# No argument # No argument
RunningContainer._updateProperties() { Container._updateProperties() {
if [ ! -z "$(this.lines)" ]; then if [ ! -z "$(this.lines)" ]; then
this.found = true this.found = true
this.count = $(this.lines | wc -l) this.count = $(this.lines | wc -l)
@ -143,8 +155,8 @@ RunningContainer._updateProperties() {
# - "with_port" to find all having this port forwarded # - "with_port" to find all having this port forwarded
# - "running" to find all containers running # - "running" to find all containers running
# - "exited" or "stopped" to find all containers stopped. # - "exited" or "stopped" to find all containers stopped.
# Signature: (<string filterName>, [string filterValue]) # Signature: (<string filterName>, [string filterValue]) -> void
RunningContainer.filter() { Container.filter() {
filterName=$1 filterName=$1
filterValue=$2 filterValue=$2
this._baseFilter $filterName $filterValue this._baseFilter $filterName $filterValue
@ -152,8 +164,8 @@ RunningContainer.filter() {
} }
# Applies a docker command to all containers # Applies a docker command to all containers
# Signature: (<string command>, [string[] arguments]) # Signature: (<string command>, [string[] arguments]) -> void|string
RunningContainer.applyToAll() { Container.applyToAll() {
cmd=$1 cmd=$1
args=${@:2} args=${@:2}
for container in $(this.id); do for container in $(this.id); do
@ -162,38 +174,38 @@ RunningContainer.applyToAll() {
} }
# Kills all containers assigned to this instance. # Kills all containers assigned to this instance.
# Signature: ([string[] arguments]) # Signature: ([string[] arguments]) -> void
RunningContainer.kill() { Container.kill() {
$this.applyToAll kill $@ $this.applyToAll kill $@
} }
# Removes all containers assigned to this instance. # Removes all containers assigned to this instance.
# Signature: ([string[] arguments]) # Signature: ([string[] arguments]) -> void
RunningContainer.rm() { Container.rm() {
$this.applyToAll rm $@ $this.applyToAll rm $@
} }
# Removes all containers assigned to this instance. # Removes all containers assigned to this instance.
# Signature: ([string[] arguments]) # Signature: ([string[] arguments]) -> void
RunningContainer.remove() { Container.remove() {
$this.applyToAll rm $@ $this.applyToAll rm $@
} }
# Waits for all containers assigned to this instance to stop and echo their exit code. # Waits for all containers assigned to this instance to stop and echo their exit code.
# Signature: ([string[] arguments]) # Signature: ([string[] arguments]) -> void
RunningContainer.wait() { Container.wait() {
$this.applyToAll wait $@ $this.applyToAll wait $@
} }
# Displays the logs of the containers # Displays the logs of the containers
# Signature: ([string[] arguments]) # Signature: ([string[] arguments]) -> string
RunningContainer.logs() { Container.logs() {
$this.applyToAll logs $@ $this.applyToAll logs $@
} }
# Executes a command to all the containers assigned to the instance. # Executes a command to all the containers assigned to the instance.
# Signature: ([string[] options] <string command>) # Signature: ([string[] options] <string command>) -> void|string
RunningContainer.exec() { Container.exec() {
cmd=${@[-1]} cmd=${@[-1]}
options=${@:$(exec ${#@} - 1)} options=${@:$(exec ${#@} - 1)}
for container in $(this.id); do for container in $(this.id); do

View file

@ -28,6 +28,6 @@ property Docker.Executable "$(which docker)"
static_class Utils "Utils.shc" static_class Utils "Utils.shc"
class RunningContainer "RunningContainer.shc" class Container "Container.shc"
class Image "Image.shc" class Image "Image.shc"

View file

@ -19,14 +19,14 @@
# This class contains various utils required for Docker. # This class contains various utils required for Docker.
# Returns the base command for docker, with it's executable and sudo if necessary. # Returns the base command for docker, with it's executable and sudo if necessary.
# Returns string. # Signature: () -> string
Utils.DockerCommand() { Utils.DockerCommand() {
if [ $(Docker.RequiresSudo) == true ]; then echo -n "sudo "; fi if [ $(Docker.RequiresSudo) == true ]; then echo -n "sudo "; fi
echo "$(Docker.Executable)" echo "$(Docker.Executable)"
} }
# Checks if the user has permission to use docker. # Checks if the user has permission to use docker.
# Retuns bool. # Signature: () -> bool
Utils.hasPermission() { Utils.hasPermission() {
$(Utils.DockerCommand) ps 2>&1 | grep -q "permission denied" $(Utils.DockerCommand) ps 2>&1 | grep -q "permission denied"
# Check status of last command. If permission denied is found, then we do not have permission to use docker. # Check status of last command. If permission denied is found, then we do not have permission to use docker.