forked from Osthanes/docker_builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_utilities.sh
executable file
·211 lines (205 loc) · 10.7 KB
/
image_utilities.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/bin/bash
#********************************************************************************
# Copyright 2015 IBM
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
#********************************************************************************
debugme() {
[[ $DEBUG = 1 ]] && "$@" || :
}
if [ -f ${EXT_DIR}/cf ]; then
CFCMD=${EXT_DIR}/cf
else
CFCMD=cf
debugme ls ${EXT_DIR}
fi
log_and_echo "$DEBUGGING" "cf is $CFCMD"
if [ "${NAMESPACE}X" == "X" ]; then
log_and_echo "$ERROR" "NAMESPACE must be set in the environment before calling this script."
exit 1
fi
if [ -z $IMAGE_LIMIT ]; then
IMAGE_LIMIT=5
fi
if [ $IMAGE_LIMIT -gt 0 ]; then
ice_retry_save_output images 2> /dev/null
RESULT=$?
if [ $RESULT -eq 0 ]; then
# find the number of images and check if greater than or equal to image limit
if [ "$USE_ICE_CLI" = "1" ]; then
NUMBER_IMAGES=$(grep "${REGISTRY_URL}/${IMAGE_NAME}:[0-9]\+" iceretry.log | wc -l)
else
NUMBER_IMAGES=$(grep "${REGISTRY_URL}/${IMAGE_NAME}\s\+[0-9]\+" iceretry.log | wc -l)
fi
log_and_echo "Number of images: $NUMBER_IMAGES and Image limit: $IMAGE_LIMIT"
if [ $NUMBER_IMAGES -ge $IMAGE_LIMIT ]; then
# create array of images name
if [ "$USE_ICE_CLI" = "1" ]; then
ICE_IMAGES_ARRAY=$(grep -o "${REGISTRY_URL}/${IMAGE_NAME}:[0-9]\+" iceretry.log)
else
ICE_IMAGES_ARRAY=$(grep -o "${REGISTRY_URL}/${IMAGE_NAME}\s\+[0-9]\+" iceretry.log | awk '{print $1":"$2}')
fi
# loop the list of spaces under the org and find the name of the images that are in used
$CFCMD spaces > inspect.log 2> /dev/null
RESULT=$?
debugme echo "cf spaces output:"
debugme cat inspect.log
debugme echo "end cf spaces output"
if [ $RESULT -eq 0 ]; then
# save current space first
$CFCMD target > target.log 2> /dev/null
debugme cat target.log
#Use Show only matching chars option in grep to allow spaces in the current space name
CURRENT_SPACE=$(grep '^Space:' target.log | awk -F: '{print $2;}' | sed 's/^ *//g' | sed 's/ *$//g')
log_and_echo "$DEBUGGING" "current space is $CURRENT_SPACE"
FOUND=""
TESTED_ALL=true
#Build space array as an array to properly handle spaces in space names
SPACE_ARRAY=()
while read line; do
SPACE_ARRAY+=("$line");
done < inspect.log;
#Setting to use ice for checking different spaces as cf ic init doesn't work in pipeline on other spaces
_SET_USE_ICE_CLI="$USE_ICE_CLI"
_SET_IC_COMMAND="$IC_COMMAND"
IC_COMMAND="ice"
USE_ICE_CLI="1"
#Array needs to be in quotes to properly handle spaces in space names
for space in "${SPACE_ARRAY[@]}"
do
# cf spaces gives a couple lines of headers. skip those until we find the line
# 'name', then read the rest of the lines as space names
if [ "${FOUND}x" == "x" ]; then
if [ "${space}X" == "nameX" ]; then
FOUND="y"
fi
continue
else
$CFCMD target -s "${space}" > target.log 2> /dev/null
RESULT=$?
debugme cat target.log
if [ $RESULT -eq 0 ]; then
log_and_echo "$DEBUGGING" "Checking space ${space}"
if [ "$USE_ICE_CLI" = "1" ]; then
ice_retry_save_output ps -q -a
ICE_PS_IMAGES_ARRAY+=$(awk '{print $1}' iceretry.log | xargs -n 1 ice inspect 2>/dev/null | grep "Image" | grep -oh -e "${NAMESPACE}/${IMAGE_NAME}:[0-9]\+")
else
ice_retry init &> /dev/null
RESULT=$?
if [ $RESULT -eq 0 ]; then
ice_retry_save_output ps -a
ICE_PS_IMAGES_ARRAY+=$(awk '{print $2}' iceretry.log | grep -oh -e "${NAMESPACE}/${IMAGE_NAME}:[0-9]\+")
else
$IC_COMMAND init
log_and_echo "$ERROR" "$IC_COMMAND init command failed for space ${space}. Could not check for used images for space ${space}."
fi
fi
ICE_PS_IMAGES_ARRAY+=" "
else
log_and_echo "$ERROR" "Unable to change to space ${space}. Could not check for used images."
TESTED_ALL=false
fi
fi
done
# restore my old space
$CFCMD target -s "${CURRENT_SPACE}" > target.log 2> /dev/null
#TODO: if/when we use cf ic for space change work, a cf ic init will be required here.
#Putting IC_COMMAND back to where it was, as cf ic init doesn't work in pipeline on other spaces
IC_COMMAND="$_SET_IC_COMMAND"
USE_ICE_CLI="$_SET_USE_ICE_CLI"
debugme cat target.log
if [ "$TESTED_ALL" = true ] ; then
i=0
j=0
log_and_echo "$DEBUGGING" "images array: ${ICE_IMAGES_ARRAY}"
log_and_echo "$DEBUGGING" "ps images array: ${ICE_PS_IMAGES_ARRAY}"
for image in ${ICE_IMAGES_ARRAY[@]}
do
in_used=0
for image_used in ${ICE_PS_IMAGES_ARRAY[@]}
do
image_used=${CCS_REGISTRY_HOST}/${image_used}
if [ $image == $image_used ]; then
log_and_echo "$DEBUGGING" "${image} used by ${image_used}"
IMAGES_ARRAY_USED[i]=$image
((i++))
in_used=1
break
else
log_and_echo "$DEBUGGING" "${image} was not used by ${image_used}"
fi
done
if [ $in_used -eq 0 ]; then
#echo "IMAGES_ARRAY_NOT_USED: ${image}"
IMAGES_ARRAY_NOT_USED[j]=$image
((j++))
fi
done
# if number of images greater then image limit, then delete unused images from oldest to newest until we are under the limit or out of unused images
len_used=${#IMAGES_ARRAY_USED[*]}
len_not_used=${#IMAGES_ARRAY_NOT_USED[*]}
log_and_echo "number of images in use: ${len_used} and number of images not in use: ${len_not_used}"
log_and_echo "unused images: ${IMAGES_ARRAY_NOT_USED[@]}"
log_and_echo "used images: ${IMAGES_ARRAY_USED[@]}"
if [ $NUMBER_IMAGES -ge $IMAGE_LIMIT ]; then
if [ $len_not_used -gt 0 ]; then
# sort the IMAGES_ARRAY_NOT_USED array
IMAGES_ARRAY_NOT_USED=( $(
for el in "${IMAGES_ARRAY_NOT_USED[@]}"
do
echo "$el"
done | sort -t":" -k2 -n) )
index_not_used=0
while [ $NUMBER_IMAGES -ge $IMAGE_LIMIT ]
do
((len_not_used--))
((NUMBER_IMAGES--))
if [ "${IMAGE_REMOVE}" == "FALSE" ]; then
echo "NOT removing image"
echo "$IC_COMMAND rmi ${IMAGES_ARRAY_NOT_USED[$index_not_used]} > /dev/null"
RESULT=1
else
ice_retry rmi ${IMAGES_ARRAY_NOT_USED[$index_not_used]} > /dev/null
RESULT=$?
RESPONSE=${RET_RESPONCE}
fi
if [ $RESULT -eq 0 ]; then
log_and_echo "successfully deleted image: $IC_COMMAND rmi ${IMAGES_ARRAY_NOT_USED[$index_not_used]}"
else
log_and_echo "$ERROR" "deleting image failed: $IC_COMMAND rmi ${IMAGES_ARRAY_NOT_USED[$index_not_used]}"
log_and_echo "$ERROR" "${RESPONSE}"
fi
((index_not_used++))
if [ $len_not_used -le 0 ]; then
break
fi
done
else
log_and_echo "$LABEL" "No unused images found."
fi
if [ $len_used -ge $IMAGE_LIMIT ]; then
log_and_echo "$WARN" "Warning: Too many images in use. Unable to meet ${IMAGE_LIMIT} image limit. Consider increasing IMAGE_LIMIT."
fi
fi
else
log_and_echo "$ERROR" "Unable to check all spaces for used containers, not removing"
fi
else
log_and_echo "$ERROR" "Unable to read cf spaces. Could not check for used images."
fi
else
log_and_echo "The number of images are less than the image limit"
fi
else
log_and_echo "$ERROR" "Failed to get image list from $IC_COMMAND. Check $IC_COMMAND login."
fi
fi