From e6ccc6e5888c6026646b5d3cb21bb3a4272b0d27 Mon Sep 17 00:00:00 2001 From: Zhijiang Date: Fri, 21 Oct 2016 01:21:30 -0400 Subject: Automatically remove the container when it exits Change-Id: I7ce146ef4dc8717e782600750660dddf45e3047f Signed-off-by: Zhijiang --- ci/build.sh | 6 +++-- ci/build_rpm/Dockerfile | 4 +-- ci/build_rpm/build_rpms.sh | 51 ++++++++++++++++++++++++++++++++++++--- ci/build_rpm/build_rpms_docker.sh | 14 +---------- ci/build_rpm/daisy_rpm_build.sh | 48 ------------------------------------ 5 files changed, 55 insertions(+), 68 deletions(-) mode change 100644 => 100755 ci/build.sh mode change 100644 => 100755 ci/build_rpm/build_rpms.sh mode change 100644 => 100755 ci/build_rpm/build_rpms_docker.sh delete mode 100644 ci/build_rpm/daisy_rpm_build.sh (limited to 'ci') diff --git a/ci/build.sh b/ci/build.sh old mode 100644 new mode 100755 index 8d71d934..e134bdd0 --- a/ci/build.sh +++ b/ci/build.sh @@ -9,8 +9,10 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -DAISYDIR = $1 +# TODO: Let JJB to pass $WORKDIR instead of $BUILD_OUTPUT +DAISYDIR=$1/../ + cd ci/build_rpm -./build_rpms.sh DAISYDIR +./build_rpms.sh $DAISYDIR diff --git a/ci/build_rpm/Dockerfile b/ci/build_rpm/Dockerfile index b46be32b..63ad0cb5 100644 --- a/ci/build_rpm/Dockerfile +++ b/ci/build_rpm/Dockerfile @@ -1,5 +1,5 @@ FROM centos - +LABEL daisy_image_version=1.0 RUN yum -y update RUN yum -y install centos-release-openstack-mitaka RUN yum -y install \ @@ -44,4 +44,4 @@ RUN yum -y install \ python-lesscpy \ python-migrate \ python-pint \ - python-routes \ No newline at end of file + python-routes diff --git a/ci/build_rpm/build_rpms.sh b/ci/build_rpm/build_rpms.sh old mode 100644 new mode 100755 index cb31d001..6549098c --- 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 diff --git a/ci/build_rpm/build_rpms_docker.sh b/ci/build_rpm/build_rpms_docker.sh old mode 100644 new mode 100755 index 813d3fff..7313d57d --- a/ci/build_rpm/build_rpms_docker.sh +++ b/ci/build_rpm/build_rpms_docker.sh @@ -8,17 +8,5 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -tmp_rpm_build_dir=/root/daisy4nfv -rpm_build_dir=/opt/daisy4nfv -tmp_rpm_output_dir=$tmp_rpm_build_dir/build_output -rpm_output_dir=$rpm_build_dir/build_output -cp -r $rpm_build_dir $tmp_rpm_build_dir -# Build daisy rpm packages -cd $tmp_rpm_build_dir/ -make clean -./ci/build_rpm/daisy_rpm_build.sh build_output - - -# Move daisy bin file from tmp_output_dir to output_dir -mv $tmp_rpm_output_dir/installdaisy_el7_noarch.bin $rpm_output_dir +# Implement build (call make allrpm) here diff --git a/ci/build_rpm/daisy_rpm_build.sh b/ci/build_rpm/daisy_rpm_build.sh deleted file mode 100644 index a9c24044..00000000 --- a/ci/build_rpm/daisy_rpm_build.sh +++ /dev/null @@ -1,48 +0,0 @@ -#!/bin/bash -############################################################################## -# Copyright (c) 2016 ZTE Coreporation and others. -# hu.zhijiang@zte.com.cn -# sun.jing22@zte.com.cn -# 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 -############################################################################## -output_dir = "$1" - -echo "#########################################################" -echo " systemctl info: " -echo "#########################################################" -echo "###Uname:" -uname -echo "###Hostname:" -hostname - - -maxcount=3 -cnt=0 -rc=1 -while [ $cnt -lt $maxcount ] && [ $rc -ne 0 ] -do - cnt=$[cnt + 1] - echo -e "\n\n\n*** Starting build attempt # $cnt" - - mkdir daisy-dir - cd daisy-dir - git clone https://git.openstack.org/openstack/daisycloud-core - cd daisycloud-core/tools - make allrpm - - echo "######################################################" - echo " done " - echo "######################################################" - rc=$? - if [ $rc -ne 0 ]; then - echo "### Build failed with rc $rc ###" - else - echo "### Build successful at attempt # $cnt" - fi -done -cd daisy-dir -mv daisycloud-core/target/el7/noarch/installdaisy_el7_noarch.bin output_dir -exit $rc -- cgit 1.2.3-korg