diff options
author | 2016-10-21 01:21:30 -0400 | |
---|---|---|
committer | 2016-11-01 05:10:52 -0400 | |
commit | e6ccc6e5888c6026646b5d3cb21bb3a4272b0d27 (patch) | |
tree | 03ac677d82bd779c1b63bf6dec084087806ecaa1 /ci/build_rpm/build_rpms.sh | |
parent | 833e122a1521dca06bf473b60b374eef9c1f47b6 (diff) |
Automatically remove the container when it exits
Change-Id: I7ce146ef4dc8717e782600750660dddf45e3047f
Signed-off-by: Zhijiang <hu.zhijiang@zte.com.cn>
Diffstat (limited to 'ci/build_rpm/build_rpms.sh')
-rwxr-xr-x[-rw-r--r--] | ci/build_rpm/build_rpms.sh | 51 |
1 files changed, 48 insertions, 3 deletions
diff --git a/ci/build_rpm/build_rpms.sh b/ci/build_rpm/build_rpms.sh index cb31d001..6549098c 100644..100755 --- a/ci/build_rpm/build_rpms.sh +++ b/ci/build_rpm/build_rpms.sh @@ -9,11 +9,56 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## set -eux -DAISYDIR = $1 +DAISYDIR=$1 function build_rpm_pkg { - sudo docker build -t daisy4nfv_rpm . - sudo docker run -v DAISYDIR:/opt/daisy4nfv -t daisy4nfv_rpm \ + # Cleanup prev build resutls + rm -rf $DAISYDIR/build_output + mkdir -p $DAISYDIR/build_output + + sudo docker build -t daisy4nfv_rpm . + sudo docker run --rm -v $DAISYDIR:/opt/daisy4nfv -t daisy4nfv_rpm \ /opt/daisy4nfv/ci/build_rpm/build_rpms_docker.sh + + # Here to collect build result from $DAISYDIR/build_output +} + +function cleanup_container { + containers_to_kill=$(sudo docker ps --filter "label=daisy_image_version" \ + --format "{{.Names}}" -a) + + if [[ -z "$containers_to_kill" ]]; then + echo "No containers to cleanup." + else + volumes_to_remove=$(sudo docker inspect -f \ + '{{range .Mounts}} {{printf "%s\n" .Name }}{{end}}' \ + ${containers_to_kill} | egrep -v '(^\s*$)' | sort | uniq) + echo "Stopping containers... $containers_to_kill" + sudo docker stop -t 2 ${containers_to_kill} >/dev/null 2>&1 + + echo "Removing containers... $containers_to_kill" + sudo docker rm -v -f ${containers_to_kill} >/dev/null 2>&1 + + if [[ -z "$volumes_to_remove" ]]; then + echo "No volumes to cleanup." + else + echo "Removing volumes... $volumes_to_remove" + sudo docker volume rm ${volumes_to_remove} >/dev/null 2>&1 + fi + fi +} + +function cleanup_docker_image { + images_to_delete=$(sudo docker images -a --filter "label=daisy_image_version" \ + --format "{{.ID}}") + + echo "Removing images... $images_to_delete" + if [[ -z "$images_to_delete" ]]; then + echo "No images to cleanup" + else + sudo docker rmi -f ${images_to_delete} >/dev/null 2>&1 + fi } +cleanup_container build_rpm_pkg +cleanup_container |