-
Notifications
You must be signed in to change notification settings - Fork 0
/
countDocker.sh
executable file
·104 lines (93 loc) · 2.87 KB
/
countDocker.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
# Used to check for existence of pre-requisites
check_for_exec() {
if ! [ -x "$(command -v $1)" ]; then
echo "Error: $1 is not installed." >&2
exit 1
fi
}
# A simple progress bar
function ProgressBar {
# Process data
let _progress=(${1}*100/${2}*100)/100
let _done=(${_progress}*4)/10
let _left=40-$_done
# Build progressbar string lengths
_fill=$(printf "%${_done}s")
_empty=$(printf "%${_left}s")
printf "\rProgress : [${_fill// /#}${_empty// /-}] ${_progress}%%"
}
# Will accumulate results from the AQL batches
OUTPUT_FILE="docker_images_sha.txt"
# Clearing previous results
echo "" > $OUTPUT_FILE
# Run a single batch. Params are starting index (inclusive), end index (exclusive) and the entire array of repository names.
# Results will be written to OUTPUT_FILE
function RunBatch {
_start=${1}
_end=${2}
shift
shift
_arr=("$@")
AQL1='items.find ({"$or":[{'
AQL2=""
if [[ "$_start" == "$_end" ]]; then
return
fi
for (( i2=$_start; i2<$_end; i2++ ))
do
AQL2="${AQL2}\"repo\":${_arr[i2]},"
done
AQL2=`echo ${AQL2} | rev | cut -c2- | rev`
AQL3='}],"type":"file", "created":{"$last":"3mo"}, "name":"manifest.json" }).include("sha256")'
AQL="$AQL1$AQL2$AQL3"
#echo "AQL: ${AQL}"
RES=`$CLI rt curl -XPOST -H "Content-Type: text/plain" -d "$AQL" api/search/aql --silent`
#echo $RES
RES=`echo "$RES" | jq -r '.results[] | .sha256'`
echo ${RES} >> $OUTPUT_FILE
}
min() {
printf "%s\n" "${@:2}" | sort "$1" | head -n1
}
CLI="jf"
# Testing whether we should use 'jfrog' and not 'jf'
if [[ "$1" == "legacy" ]]; then
CLI="jfrog"
fi
for i in "$CLI" "jq"
do
check_for_exec "$i"
done
# Retrieve all repositories of type Docker which are not Virtual
REPOS=`$CLI rt curl /api/repositories --silent | jq '.[] | select(.packageType == "Docker" and .type != "VIRTUAL") | .key'`
if [[ "${REPOS}" == "" ]]; then
echo "No Docker repositories to scan"
exit 0
fi
TOTAL=`echo "$REPOS" | wc -l | cut -w -f 2`
echo "Going through ${TOTAL} Docker repositories"
#echo "$REPOS"
# Convert the lines into an array
SAVEIFS=$IFS # Save current IFS (Internal Field Separator)
IFS=$'\n' # Change IFS to newline char
arr2=($REPOS)
IFS=$SAVEIFS # Restore original IFS
BATCH_SIZE=122
BATCH_COUNT=$((TOTAL/BATCH_SIZE+1))
#echo "TOTAL: ${TOTAL}"
#echo "BATCH SIZE: ${BATCH_SIZE}"
#echo "BATCH COUNT: ${BATCH_COUNT}"
for (( i=0; i<$BATCH_COUNT; i++ ))
do
sPos=$((i*BATCH_SIZE))
ePos=$(((i+1)*BATCH_SIZE))
ePos=`min -g $ePos $TOTAL`
#echo "from: $sPos to: $ePos"
ProgressBar $i $((BATCH_COUNT-1))
RunBatch $sPos $ePos "${arr2[@]}"
done
# Now need just the unique count of the SHAs
FINAL_RES=`cat $OUTPUT_FILE | tr ' ' '\n' | sort | uniq | wc -l | cut -w -f 2`
echo
echo "Docker images through last 3 months: ${FINAL_RES}"