@@ -24,59 +24,59 @@ echo -e "Amazon Web Services - Looking for old KubeNow's AMIs:\n "
2424
2525# Now we start the process of deregistering the old Kubenow AMI across all the other regions
2626for reg in ${aws_regions[*]} ; do
27-
28- # We update the default region so to correctly perform checks in each region via awscli
29- export AWS_DEFAULT_REGION=" $reg "
30- printf " Current region is: %s\n" " $AWS_DEFAULT_REGION "
31-
32- # Extracting both KubeNow images that are flagged as "test" or "current"
33- # Using tee and wc -l (which often returns 0) because of set -e. In case aws and grep return 1
34- aws ec2 describe-images --filters " Name=name,Values=kubenow-*-*" " Name=owner-id,Values=105135433346" | tee /tmp/aws_out_images.json
35- tot_no_amis=$( grep -i " imageid" < /tmp/aws_out_images.json | wc -l)
36- counter_del_img=0
37- counter_del_snap=0
38-
39- if [ " $tot_no_amis " -gt " 0" ]; then
40- # Finding Image IDs of AMIs older than 1 day which needed to be deregistered
41- index=0
42- while [ " $index " -lt " $tot_no_amis " ]; do
43-
44- img_date=$( jq " .Images[$index ] | .CreationDate" /tmp/aws_out_images.json | sed -e ' s/^"//' -e ' s/"$//' -e ' s/T.*//' )
45-
46- if [[ ! " $img_date " > " $del_date " ]]; then
47- # Extracting AMI's "Name" and "ImageId"
48- name=$( jq " .Images[$index ] | .Name" /tmp/aws_out_images.json | sed -e ' s/^"//' -e ' s/"$//' )
49- ami_id_to_deregister=$( jq " .Images[$index ] | .ImageId" /tmp/aws_out_images.json | sed -e ' s/^"//' -e ' s/"$//' )
50- echo -e " Following old AMI dated $img_date is found \nName: $name \nAMI ID:$ami_id_to_deregister \n"
51-
52- # Find if there are any snapshots attached to the Image need to be deregister
53- aws ec2 describe-images --image-ids " $ami_id_to_deregister " | grep snap | awk ' { print $2 }' | sed -e ' s/^"//' -e ' s/,$//' -e ' s/"$//' > /tmp/old_snaps.txt
54-
55- # Deregistering the AMI
56- echo -e " Starting the deregister of KubewNow AMI: $ami_id_to_deregister ...\n"
57- aws ec2 deregister-image --image-id " $ami_id_to_deregister "
58- counter_del_img=$(( counter_del_img+ 1 ))
59-
60- # Deleting snapshots attached to AMI
61- while read -r line; do
62- echo -e " Deleting the associated snapshots: $line \n"
63- aws ec2 delete-snapshot --snapshot-id " $line "
64- counter_del_snap=$(( counter_del_snap+ 1 ))
65- done < /tmp/old_snaps.txt
66- fi
67-
68- index=$(( index+ 1 ))
69- done
70-
71- if [ " $counter_del_img " == " 0" ]; then
72- echo -e " No old images dated $del_date were found \n"
73- fi
74-
75- else
76- echo -e " No KubeNow AMIs flagged as test or current found"
77- fi
78-
79- echo -e " \nNo of deleted of AMI: $counter_del_img \nNo of deleted snapshots: $counter_del_snap \n"
27+
28+ # We update the default region so to correctly perform checks in each region via awscli
29+ export AWS_DEFAULT_REGION=" $reg "
30+ printf " Current region is: %s\n" " $AWS_DEFAULT_REGION "
31+
32+ # Extracting both KubeNow images that are flagged as "test" or "current"
33+ # Using tee and wc -l (which often returns 0) because of set -e. In case aws and grep return 1
34+ aws ec2 describe-images --filters " Name=name,Values=kubenow-*-*" " Name=owner-id,Values=105135433346" | tee /tmp/aws_out_images.json
35+ tot_no_amis=$( grep -i " imageid" < /tmp/aws_out_images.json | wc -l)
36+ counter_del_img=0
37+ counter_del_snap=0
38+
39+ if [ " $tot_no_amis " -gt " 0" ]; then
40+ # Finding Image IDs of AMIs older than 1 day which needed to be deregistered
41+ index=0
42+ while [ " $index " -lt " $tot_no_amis " ]; do
43+
44+ img_date=$( jq " .Images[$index ] | .CreationDate" /tmp/aws_out_images.json | sed -e ' s/^"//' -e ' s/"$//' -e ' s/T.*//' )
45+
46+ if [[ ! " $img_date " > " $del_date " ]]; then
47+ # Extracting AMI's "Name" and "ImageId"
48+ name=$( jq " .Images[$index ] | .Name" /tmp/aws_out_images.json | sed -e ' s/^"//' -e ' s/"$//' )
49+ ami_id_to_deregister=$( jq " .Images[$index ] | .ImageId" /tmp/aws_out_images.json | sed -e ' s/^"//' -e ' s/"$//' )
50+ echo -e " Following old AMI dated $img_date is found \nName: $name \nAMI ID:$ami_id_to_deregister \n"
51+
52+ # Find if there are any snapshots attached to the Image need to be deregister
53+ aws ec2 describe-images --image-ids " $ami_id_to_deregister " | grep snap | awk ' { print $2 }' | sed -e ' s/^"//' -e ' s/,$//' -e ' s/"$//' > /tmp/old_snaps.txt
54+
55+ # Deregistering the AMI
56+ echo -e " Starting the deregister of KubewNow AMI: $ami_id_to_deregister ...\n"
57+ aws ec2 deregister-image --image-id " $ami_id_to_deregister "
58+ counter_del_img=$(( counter_del_img + 1 ))
59+
60+ # Deleting snapshots attached to AMI
61+ while read -r line; do
62+ echo -e " Deleting the associated snapshots: $line \n"
63+ aws ec2 delete-snapshot --snapshot-id " $line "
64+ counter_del_snap=$(( counter_del_snap + 1 ))
65+ done < /tmp/old_snaps.txt
66+ fi
67+
68+ index=$(( index + 1 ))
69+ done
70+
71+ if [ " $counter_del_img " == " 0" ]; then
72+ echo -e " No old images dated $del_date were found \n"
73+ fi
74+
75+ else
76+ echo -e " No KubeNow AMIs flagged as test or current found"
77+ fi
78+
79+ echo -e " \nNo of deleted of AMI: $counter_del_img \nNo of deleted snapshots: $counter_del_snap \n"
8080done
8181
8282# Now taking care of the S3 buckets for KubeNow images
@@ -85,38 +85,38 @@ s3_buckets=("us-east-1" "eu-central-1")
8585# Extracting List of objects from each bucket
8686for buck in ${s3_buckets[*]} ; do
8787
88- echo -e " AWS S3 $buck - Looking for old KubeNow bucket objects:\n"
89- # Technicality here about the set -e at the beginning at grep's exit code -1. Thus using tee which no matter the outcomes, will return 0
90- aws s3 ls s3://kubenow-" $buck " --region " $buck " --human-readable | grep -E ' kubenow-v([0-9]*)([ab0-9]*)-([0-9]*)-([a-z0-9]*)-([test]*)([current]*).qcow2([.md5]*)' | tee /tmp/aws_s3_objs.txt
88+ echo -e " AWS S3 $buck - Looking for old KubeNow bucket objects:\n"
89+ # Technicality here about the set -e at the beginning at grep's exit code -1. Thus using tee which no matter the outcomes, will return 0
90+ aws s3 ls s3://kubenow-" $buck " --region " $buck " --human-readable | grep -E ' kubenow-v([0-9]*)([ab0-9]*)-([0-9]*)-([a-z0-9]*)-([test]*)([current]*).qcow2([.md5]*)' | tee /tmp/aws_s3_objs.txt
9191
92- no_obj_to_check=$( wc -l < /tmp/aws_s3_objs.txt)
93- echo -e " No of bucket object to be checked: $no_obj_to_check \n"
92+ no_obj_to_check=$( wc -l < /tmp/aws_s3_objs.txt)
93+ echo -e " No of bucket object to be checked: $no_obj_to_check \n"
9494
95- counter_del_s3_obj=0
95+ counter_del_s3_obj=0
9696
97- if [ " $no_obj_to_check " -gt " 0" ]; then
97+ if [ " $no_obj_to_check " -gt " 0" ]; then
9898
99- while read -r line; do
100- obj_date=$( echo " $line " | awk ' {print $1}' )
101- obj_name=$( echo " $line " | awk ' {print $5}' )
102-
103- if [[ ! " $obj_date " > " $del_date " ]]; then
104- echo -e " Following old KubeNow bucket object dated: $obj_date is found\nName: $obj_name \n"
105- echo -e " Starting the delete bucket object: $obj_name ...\n"
106- aws s3 rm " s3://kubenow-$buck /$obj_name " --region " $buck "
107- counter_del_s3_obj=$(( counter_del_s3 _obj+ 1 ))
108- fi
109- done < /tmp/aws_s3_objs.txt
110-
111- if [ " $counter_del_s3_obj " == " 0" ]; then
112- echo -e " No old bucket objects dated $del_date were found \n"
113- fi
114-
115- else
116- echo -e " \nNo KubeNow bucket objects flagged as test or current found"
99+ while read -r line; do
100+ obj_date=$( echo " $line " | awk ' {print $1}' )
101+ obj_name=$( echo " $line " | awk ' {print $5}' )
102+
103+ if [[ ! " $obj_date " > " $del_date " ]]; then
104+ echo -e " Following old KubeNow bucket object dated: $obj_date is found\nName: $obj_name \n"
105+ echo -e " Starting the delete bucket object: $obj_name ...\n"
106+ aws s3 rm " s3://kubenow-$buck /$obj_name " --region " $buck "
107+ counter_del_s3_obj=$(( counter_del_s3 _obj + 1 ))
108+ fi
109+ done < /tmp/aws_s3_objs.txt
110+
111+ if [ " $counter_del_s3_obj " == " 0" ]; then
112+ echo -e " No old bucket objects dated $del_date were found \n"
117113 fi
118114
119- echo -e " \nNo of deleted bucket object: $counter_del_s3_obj \n"
115+ else
116+ echo -e " \nNo KubeNow bucket objects flagged as test or current found"
117+ fi
118+
119+ echo -e " \nNo of deleted bucket object: $counter_del_s3_obj \n"
120120done
121121
122122echo -e " Done.\n"
0 commit comments