#!/usr/bin/env bash # A fake docker client for PipelineTests: .gitea/scripts/pulizia-docker.sh runs against a small daemon written in text # files, and the test reads what is left. No daemon, no network. It models only what the cleanup relies on, the way # Docker does it: # - filters of «ps»: label filters must all match, the values of any other key are alternatives (status, name as a # regular expression, ancestor by name or id); # - «rm» refuses a running container without -f; # - «image rm » only untags when the image has other names, and refuses an image a container uses (always if the # container runs, without -f if it is stopped); # - «image prune --filter until=72h» takes only images without a name, older than 72 hours, used by no container. # Any other call is an error (exit 3), so the test sees every command the cleanup starts to use. # # $FAKE_DOCKER_WORLD/containers id|name|state|minutes ago|image as given to «docker run»|image id|labels # (labels as Docker gives them to the container: those of the image plus those of «run») # $FAKE_DOCKER_WORLD/images id|names (comma separated; empty: dangling)|minutes ago|labels # $FAKE_DOCKER_WORLD/volumes one name per line # $FAKE_DOCKER_WORLD/calls every call, one per line (written here) set -uo pipefail world="${FAKE_DOCKER_WORLD:?}" printf 'docker %s\n' "$*" >> "$world/calls" now=$(date -u +%s) touch "$world/containers" "$world/images" "$world/volumes" unexpected() { echo "fake docker: unexpected call: $*" >&2 exit 3 } created() { date -u -d "@$((now - $1 * 60))" +%Y-%m-%dT%H:%M:%S.123456789Z; } # The value of a label, or $3 when it is missing («docker ps» prints nothing, «inspect» prints ). label_value() { local labels=",$1," key="$2" rest [[ "$labels" == *",$key="* ]] || { echo "${3:-}"; return; } rest="${labels#*,"$key"=}" echo "${rest%%,*}" } has_label() { local labels=",$1," want="$2" if [[ "$want" == *=* ]]; then [[ "$labels" == *",$want,"* ]] else [[ "$labels" == *",$want="* ]] fi } # Image id for a name or an id; empty when there is none. resolve_image() { local ref="$1" id names age labels while IFS='|' read -r id names age labels; do [[ -n "$id" ]] || continue if [[ "$id" == "$ref" || ",$names," == *",$ref,"* ]]; then echo "$id" return fi done < "$world/images" } # The image line for an id; empty when there is none. image_line() { local line while IFS= read -r line; do [[ -n "$line" && "${line%%|*}" == "$1" ]] && { echo "$line"; return; } done < "$world/images" } # The container line for an id or a name; empty when there is none. find_container() { local ref="$1" line id name rest while IFS= read -r line; do IFS='|' read -r id name rest <<< "$line" if [[ -n "$id" && ("$id" == "$ref" || "$name" == "$ref") ]]; then echo "$line" return fi done < "$world/containers" } # Rewrites a world file without the lines whose first field is $2. drop() { local file="$1" id="$2" line : > "$file.new" while IFS= read -r line; do [[ "${line%%|*}" == "$id" ]] || printf '%s\n' "$line" >> "$file.new" done < "$file" mv "$file.new" "$file" } used_by() { local image="$1" want="$2" id name state age config image_id labels while IFS='|' read -r id name state age config image_id labels; do [[ -n "$id" && "$image_id" == "$image" ]] || continue [[ "$want" == any || "$state" == running ]] && return 0 done < "$world/containers" return 1 } matches() { local name="$1" state="$2" image_id="$3" labels="$4" shift 4 local -A seen=() ok=() local filter key value for filter in "$@"; do key="${filter%%=*}" value="${filter#*=}" case "$key" in label) has_label "$labels" "$value" || return 1 ;; status) seen[$key]=1; [[ "$state" == "$value" ]] && ok[$key]=1 ;; name) seen[$key]=1; [[ "$name" =~ $value ]] && ok[$key]=1 ;; ancestor) seen[$key]=1; [[ -n "$image_id" && "$(resolve_image "$value")" == "$image_id" ]] && ok[$key]=1 ;; *) unexpected "filter $filter" ;; esac done for key in "${!seen[@]}"; do [[ -n "${ok[$key]:-}" ]] || return 1 done return 0 } ps_command() { local quiet=0 format='{{.ID}} {{.Names}}' filters=() while (($#)); do case "$1" in -a | --no-trunc) ;; -q) quiet=1 ;; --filter) filters+=("$2"); shift ;; --format) format="$2"; shift ;; *) unexpected "ps $1" ;; esac shift done local id name state age config image_id labels out key while IFS='|' read -r id name state age config image_id labels; do [[ -n "$id" ]] || continue matches "$name" "$state" "$image_id" "$labels" "${filters[@]}" || continue if ((quiet)); then echo "$id" continue fi out="${format//"{{.ID}}"/$id}" out="${out//"{{.Names}}"/$name}" while [[ "$out" =~ \{\{\.Label\ \"([^\"]+)\"\}\} ]]; do key="${BASH_REMATCH[1]}" out="${out/"${BASH_REMATCH[0]}"/$(label_value "$labels" "$key" "")}" done printf '%s\n' "$out" done < "$world/containers" return 0 } container_inspect() { local format="" [[ "${1:-}" == -f ]] && { format="$2"; shift 2; } local line id name state age config image_id labels line=$(find_container "${1:?}") [[ -n "$line" ]] || { echo "Error: No such container: $1" >&2; exit 1; } IFS='|' read -r id name state age config image_id labels <<< "$line" case "$format" in "") echo "[{\"Id\": \"$id\"}]" ;; "{{.Created}}") created "$age" ;; "{{.Config.Image}}") echo "$config" ;; "{{.Name}}") echo "/$name" ;; '{{index .Config.Labels "febe.ci.prova"}}') label_value "$labels" febe.ci.prova "" ;; *) unexpected "container inspect -f $format" ;; esac } rm_command() { local force=0 while [[ "${1:-}" == -* ]]; do case "$1" in -f) force=1 ;; -v) ;; *) unexpected "rm $1" ;; esac shift done local line id name state rest line=$(find_container "${1:?}") [[ -n "$line" ]] || { echo "Error: No such container: $1" >&2; exit 1; } IFS='|' read -r id name state rest <<< "$line" if [[ "$state" == running && "$force" == 0 ]]; then echo "Error: cannot remove container \"/$name\": container is running: stop the container before removing or force remove" >&2 exit 1 fi drop "$world/containers" "$id" } image_ls() { local format="" reference="" while (($#)); do case "$1" in --filter) [[ "$2" == reference=* ]] || unexpected "image ls --filter $2"; reference="${2#reference=}"; shift ;; --format) format="$2"; shift ;; *) unexpected "image ls $1" ;; esac shift done [[ "$format" == "{{.Repository}}:{{.Tag}}" ]] || unexpected "image ls --format $format" local id names age labels tag while IFS='|' read -r id names age labels; do [[ -n "$id" ]] || continue IFS=',' read -r -a tags <<< "$names" for tag in "${tags[@]}"; do [[ -z "$reference" || "${tag%:*}" == "$reference" || "$tag" == "$reference" ]] && echo "$tag" done done < "$world/images" return 0 } image_inspect() { local format="" [[ "${1:-}" == -f ]] && { format="$2"; shift 2; } local wanted id names age labels wanted=$(resolve_image "${1:?}") [[ -n "$wanted" ]] || { echo "Error: No such image: $1" >&2; exit 1; } while IFS='|' read -r id names age labels; do [[ "$id" == "$wanted" ]] || continue case "$format" in "") echo "[{\"Id\": \"$id\"}]" ;; "{{.Created}}") created "$age" ;; "{{len .RepoTags}}") if [[ -z "$names" ]]; then echo 0; else IFS=',' read -r -a tags <<< "$names"; echo "${#tags[@]}"; fi ;; *) unexpected "image inspect -f $format" ;; esac done < "$world/images" } image_rm() { local force=0 [[ "${1:-}" == -f ]] && { force=1; shift; } local ref="${1:?}" wanted id names age labels left="" tag wanted=$(resolve_image "$ref") [[ -n "$wanted" ]] || { echo "Error: No such image: $ref" >&2; exit 1; } IFS='|' read -r id names age labels <<< "$(image_line "$wanted")" tags=() [[ -z "$names" ]] || IFS=',' read -r -a tags <<< "$names" if [[ "$ref" != "$id" && ${#tags[@]} -gt 1 ]]; then for tag in "${tags[@]}"; do [[ "$tag" == "$ref" ]] || left="${left:+$left,}$tag"; done drop "$world/images" "$id" printf '%s|%s|%s|%s ' "$id" "$left" "$age" "$labels" >> "$world/images" echo "Untagged: $ref" return 0 fi if [[ "$ref" == "$id" && ${#tags[@]} -gt 1 && "$force" == 0 ]]; then echo "Error: conflict: unable to delete $id (must be forced) - image is referenced in multiple repositories" >&2 exit 1 fi if used_by "$id" running || { [[ "$force" == 0 ]] && used_by "$id" any; }; then echo "Error: conflict: unable to remove repository reference \"$ref\" - container is using its referenced image" >&2 exit 1 fi drop "$world/images" "$id" echo "Deleted: $id" } image_prune() { [[ "$*" == "-f --filter until=72h" ]] || unexpected "image prune $*" local id names age labels gone=() while IFS='|' read -r id names age labels; do [[ -n "$id" && -z "$names" && "$age" -ge $((72 * 60)) ]] || continue used_by "$id" any || gone+=("$id") done < "$world/images" for id in "${gone[@]}"; do drop "$world/images" "$id" echo "deleted: $id" done echo "Total reclaimed space: 0B" } case "${1:-}" in ps) shift; ps_command "$@" ;; container) [[ "${2:-}" == inspect ]] || unexpected "$*"; shift 2; container_inspect "$@" ;; rm) shift; rm_command "$@" ;; image) sub="${2:-}" shift 2 case "$sub" in ls) image_ls "$@" ;; inspect) image_inspect "$@" ;; rm) image_rm "$@" ;; prune) image_prune "$@" ;; *) unexpected "image $sub" ;; esac ;; buildx) [[ "${2:-}" == rm ]] || unexpected "$*"; echo "ERROR: no builder \"${4:-}\" found" >&2; exit 1 ;; volume) case "${2:-}" in inspect) grep -qxF -- "${3:?}" "$world/volumes" || { echo "Error: no such volume: $3" >&2; exit 1; } ;; rm) [[ "${3:-}" == -f ]] || unexpected "$*" grep -vxF -- "${4:?}" "$world/volumes" > "$world/volumes.new" mv "$world/volumes.new" "$world/volumes" ;; *) unexpected "$*" ;; esac ;; system) [[ "${2:-}" == df ]] || unexpected "$*"; echo "TYPE TOTAL ACTIVE SIZE RECLAIMABLE" ;; *) unexpected "$*" ;; esac