enforce_labels.bash 810 Bytes
Newer Older
1
2
#!/bin/bash

Jon Azpiazu's avatar
Jon Azpiazu committed
3
4
5
6
7
if [ "$#" -ne 1 ]; then
    echo "Usage: $0 IMAGE_NAME"
    exit 1
fi

8
9
10
11
image_name="$1"

declare -A enforced_labels=( ["com.jfrog.artifactory.retention.maxCount"]="10" ["com.jfrog.artifactory.retention.maxDays"]="7")

Jon Azpiazu's avatar
Jon Azpiazu committed
12
if ! docker inspect --type=image "$1" > /dev/null ; then
13
14
15
16
    echo "Error: image does not exist"
    exit 0
fi

Jon Azpiazu's avatar
Jon Azpiazu committed
17
18
19
for label in "${!enforced_labels[@]}" ; do
    if ! docker inspect -f "{{json .Config.Labels }}" "$image_name" | jq -e '.['\""$label"\"']' > /dev/null ; then
        echo "Key not found"
20
21
        echo "$label = ${enforced_labels[$label]}"
        label_args+=" --label $label=\"${enforced_labels[$label]}\""
Jon Azpiazu's avatar
Jon Azpiazu committed
22
    fi
23
24
done

Jon Azpiazu's avatar
Jon Azpiazu committed
25
if [ -n "${label_args+x}" ]; then
26
    run_command="echo \"FROM $image_name\" | docker build $label_args -t $image_name -"
27
    eval "$run_command"
28
fi