Add docker clean-up for run test 73/31873/2
authoryuyang <Gabriel.yuyang@huawei.com>
Mon, 27 Mar 2017 07:41:23 +0000 (15:41 +0800)
committeryuyang <Gabriel.yuyang@huawei.com>
Mon, 27 Mar 2017 08:57:17 +0000 (16:57 +0800)
JIRA: BOTTLENECK-148

Currently, only OPNFV CI includes env-cleanup for Bottlenecks.
It is preferable to also include the operation locally
for test automation and repeatability.

Changes:
1. Translate tab into 4 spaces

Change-Id: I9f4efb95c155e442afd3141c00f707421c61b2da
Signed-off-by: yuyang <Gabriel.yuyang@huawei.com>
docker/docker_cleanup.sh [new file with mode: 0644]
run_tests.sh

diff --git a/docker/docker_cleanup.sh b/docker/docker_cleanup.sh
new file mode 100644 (file)
index 0000000..9b7e87a
--- /dev/null
@@ -0,0 +1,106 @@
+#!/bin/bash
+##############################################################################
+# Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+usage="Script to clear up dockers and their images.
+
+usage:
+    bash $(basename "$0") [-h|--help] [-d|--docker <docker name>] [--debug]
+
+where:
+    -h|--help         show the help text
+    -d|--docker       specify dockers' name
+      <docker name>   keyword of dockers' name used to find dockers
+                          e.g. keyword "bottlenecks" to find "opnfv/bottlenecks:*"
+    --debug           print debug information with default false
+
+examples:
+    $(basename "$0")
+    $(basename "$0") -d bottlenecks --debug"
+
+clnup_debug=false
+
+while [[ $#>0 ]]; do
+    clnup_docr="$1"
+    case $clnup_docr in
+        -h|--help)
+            echo "$usage"
+            exit 0
+            shift
+        ;;
+        -d|--docker)
+            docker_name="$2"
+            shift
+
+            if [[ $2 == "--debug" ]]; then
+                clnup_debug=true
+                shift
+            fi
+        ;;
+        --debug)
+            clnup_debug=true
+            shift
+            if [[ "$1" == "-d" || "$1" == "--docker" ]]; then
+                docker_name="$2"
+                shift
+            fi
+        ;;
+        *)
+            echo "unknow options $1 $2 $3"
+            exit 1
+        ;;
+    esac
+    shift
+done
+
+
+# check if docker name is empty
+if [[ $docker_name == "" ]]; then
+    echo empty docker name
+    exit 1
+fi
+
+# clean up dockers and their images with keyword in their names
+[[ $clnup_debug == true ]] && redirect="/dev/stdout" || redirect="/dev/null"
+
+echo "$docker_name: docker containers/images cleaning up"
+
+dangling_images=($(docker images -f "dangling=true" | grep $docker_name | awk '{print $3}'))
+if [[ -n $dangling_images ]]; then
+    echo "Removing $docker_name:<none> dangling images and their containers"
+    docker images | head -1 && docker images | grep $dangling_images
+    for image_id in "${dangling_images[@]}"; do
+        echo "$docker_name: Removing dangling image $image_id"
+        docker rmi -f $image_id >${redirect}
+    done
+fi
+
+for image_id in "${dangling_images[@]}"; do
+    if [[ -n $(docker ps -a | grep $image_id) ]]; then
+        echo "$docker_name: Removing containers associated with dangling image: $image_id"
+        docker ps -a | head -1 && docker ps -a | grep $image_id
+        docker ps -a | grep $image_id | awk '{print $1}'| xargs docker rm -f >${redirect}
+    fi
+done
+
+if [[ -n $(docker ps -a | grep $docker_name) ]]; then
+    echo "Removing existing $docker_name containers"
+    docker ps -a | head -1 && docker ps -a | grep $docker_name
+    docker ps -a | grep $docker_name | awk '{print $1}' | xargs docker rm -f >$redirect
+fi
+
+if [[ -n $(docker images | grep $docker_name) ]]; then
+    echo "$docker_name: docker images to remove:"
+    docker images | head -1 && docker images | grep $docker_name
+    image_ids=($(docker images | grep $docker_name | awk '{print $3}'))
+    for image_id in "${image_ids[@]}"; do
+        echo "Removing docker image $docker_name:$tag..."
+        docker rmi $image_id >$redirect
+    done
+fi
\ No newline at end of file
index f6744bf..97cbf28 100755 (executable)
@@ -136,6 +136,15 @@ if [ "${testcase}" != "" ]; then
     done
 fi
 
+#clean up correlated docker images
+bash ${BASEDIR}/docker/docker_cleanup.sh -d bottlenecks --debug
+bash ${BASEDIR}/docker/docker_cleanup.sh -d yardstick --debug
+bash ${BASEDIR}/docker/docker_cleanup.sh -d kibana --debug
+bash ${BASEDIR}/docker/docker_cleanup.sh -d elasticsearch --debug
+bash ${BASEDIR}/docker/docker_cleanup.sh -d influxdb --debug
+
+exit 0
+
 #run tests
 if [ "${teststory}" != "" ]; then
     test_level="teststory"
@@ -151,4 +160,7 @@ if [ "${testcase}" != "" ]; then
         info "Start to run test case $i"
         run_test $i
     done
-fi
\ No newline at end of file
+fi
+
+# echo "Bottlenecks: cleaning up docker-compose images and dockers"
+# docker-compose -f $BASEDIR/docker/bottleneck-compose/docker-compose.yml down --rmi all
\ No newline at end of file