From: Holger Levsen Date: Wed, 14 Nov 2012 18:02:12 +0000 (+0100) Subject: implement garbage collection of liveboot images, except for updating cidb after remov... X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=820578397b109f39537e2a9b60d09b08f0d7c476;p=profitbricks%2Fjenkins-build-scripts.git implement garbage collection of liveboot images, except for updating cidb after removal. (and the actual rm call is commented out atm) --- diff --git a/keep_liveboot.py b/keep_liveboot.py index b24a00a..e9ff5a9 100755 --- a/keep_liveboot.py +++ b/keep_liveboot.py @@ -16,15 +16,22 @@ from cidb import * logger = logger_init() -def keep_liveboot(name): +def liveboot_to_keep(name): con = db_connect() cur = con.cursor() try: - cur.execute("SELECT keep_image FROM liveboot WHERE build_name = '%s'" % (name)) + cur.execute("SELECT keep_image FROM liveboot WHERE build_name = '%s' and keep_image = true" % (name)) except psycopg2.DatabaseError as e: logger.debug("SELECT keep_image FROM liveboot WHERE build_name = '%s' and keep_image = true" % (name)) return False if cur.fetchone(): return True +if __name__ == '__main__': + if len(sys.argv) != 2: + print("usage: %s $liveboot_name") + sys.exit(1) + name = sys.argv[1] + if not liveboot_to_keep(name): + sys.exit(1) diff --git a/liveboot_garbage_collection.sh b/liveboot_garbage_collection.sh index 0c514ee..17a838e 100755 --- a/liveboot_garbage_collection.sh +++ b/liveboot_garbage_collection.sh @@ -3,19 +3,48 @@ # copyright 2012 Holger Levsen # GPL2 licenced -set -x -export +#set -x +#export -# some settings +# no configuration below this block AMOUNT_TO_KEEP=15 -KEPT=0 +LIVEBOOT_IMAGE_PATH="/srv/mirror/liveboot" -for DIRECTORY in $(ls -1r) ; do - echo -n $DIRECTORY - if ! ./keep_liveboot.py "$DIRECTORY" && [ $KEPT -le $AMOUNT_TO_KEEP ] ; then - echo we can delete this one - else - echo keep it +echo +echo "liveboot garbage collection" +echo "---------------------------" +echo "- first goes through all liveboot images and keeps those with the 'keep'-flag set" +echo "- second, it will go through all liveboot images (started with the latest) and keeps images until $AMOUNT_TO_KEEP liveboot images are kept. All the others will be deleted." +echo + +KEPT=0 +FIRST_PASS=$(ls -1r $LIVEBOOT_IMAGE_PATH) +SECOND_PASS="" +for DIRECTORY in $FIRST_PASS ; do + if ./keep_liveboot.py "$DIRECTORY" ; then + echo -n "$DIRECTORY: " + echo keep flag set, keeping it. + else + SECOND_PASS="$SECOND_PASS $DIRECTORY" + fi +done +for DIRECTORY in $SECOND_PASS ; do + echo -n "$DIRECTORY: " + if [ $KEPT -le $AMOUNT_TO_KEEP ] ; then + echo keep it, there is enough space left... let "KEPT=KEPT+1" + else + echo "deleted." + echo rm -rf "$LIVEBOOT_IMAGE_PATH/$DIRECTORY" + echo FIXME: still need to remove the liveboot from cidb... fi done +echo +echo + +echo "Existing liveboot images after garbage collection:" +ls -1r $LIVEBOOT_IMAGE_PATH +echo +echo "Free diskspace on sagunt:" +df -h $LIVEBOOT_IMAGE_PATH +echo "The liveboot images are copied to other servers which might not have as much space available."