Refactor, DRY
authorAdam Dickmeiss <adam@indexdata.dk>
Fri, 6 Jan 2017 12:52:39 +0000 (13:52 +0100)
committerAdam Dickmeiss <adam@indexdata.dk>
Fri, 6 Jan 2017 12:52:39 +0000 (13:52 +0100)
id-deb-build/docker-build.sh

index 5508897..455ad08 100755 (executable)
@@ -11,6 +11,62 @@ Options:
 EOF
     exit $1
 }
+
+
+centos_map()
+{
+    dist=$1
+    case $dist in
+       centos5)
+           F=5;;
+       centos6)
+           F=6;;
+       centos7)
+           F=7;;
+       *)
+           echo "$dist not supported"
+           exit 1
+    esac
+    return $F
+}
+
+prepare_deb()
+{
+    TYPE=$1
+    DISTS=$2
+
+    for dist in ${DISTS}; do
+       test -f build/$TYPE/$dist && sudo rm -fr build/$TYPE/$dist
+       mkdir -p build/$TYPEn
+       cp -a deb-src build/$TYPE/$dist
+       cp ${ID_DEB_BUILD}/in-docker.sh build/$TYPE/$dist
+    done
+}
+
+run_deb()
+{
+    TYPE=$1
+    DISTS=$2
+
+    for dist in ${DISTS}; do
+       cd build/$TYPE/$dist
+       echo "FROM $TYPE:$dist" >Dockerfile
+       echo 'RUN apt-get update && apt-get -q install -y wget devscripts equivs' >>Dockerfile
+       echo 'RUN wget -q -O - http://ftp.indexdata.dk/debian/indexdata.asc|apt-key add -' >>Dockerfile
+       echo "RUN echo \"deb http://ftp.indexdata.dk/$TYPE $dist main\" >/etc/apt/sources.list.d/indexdata.list" >>Dockerfile
+       docker build -t indexdata-build-$TYPE-$dist .
+       echo -n "$TYPE $dist "
+       docker run --rm  -v "$PWD:/build" -w /build/$PRODUCT-$VERSION indexdata-build-$TYPE-$dist ../in-docker.sh >log 2>&1
+       if test $? -eq 0; then
+               echo Done
+       else
+               echo "Fail. Inspect build/$TYPE/$dist/log"
+               exit 1
+       fi
+       cd ../../..
+    done
+}
+
 D0=`dirname $0`
 ID_DEB_BUILD=`cd $D0; pwd`
 
@@ -91,17 +147,9 @@ if $upload; then
     echo "Uploading to ${UPLOAD_URI}"
     if $centos; then
        for dist in ${CENTOS_DIST}; do
-           case $dist in
-               centos5)
-                   F=5.5;;
-               centos6)
-                   F=6;;
-               centos7)
-                   F=7;;
-               *)
-                   echo "$dist not supported"
-                   exit 1
-           esac
+           centos_map $dist
+           F=$?
+           test $F = "5" && F=5.5
            ssh ${UPLOAD_HOST} "mkdir -p ${UPLOAD_PATH}/${PRODUCT}/redhat/centos/$F/RPMS"
            ssh ${UPLOAD_HOST} "mkdir -p ${UPLOAD_PATH}/${PRODUCT}/redhat/centos/$F/SRPMS"
            scp -r build/centos/$dist/rpmbuild/RPMS ${UPLOAD_URI}/redhat/centos/$F
@@ -140,7 +188,6 @@ if $upload; then
     exit 0
 fi
 
-
 if $centos; then
     for dist in ${CENTOS_DIST}; do
        test -d build/centos/$dist && sudo rm -fr build/centos/$dist
@@ -167,7 +214,6 @@ if $debian; then
         echo "$MKDEBSRC not found"
         exit 1
     fi
-    cp ${ID_DEB_BUILD}/in-docker.sh .
     if ! ${ID_DEB_BUILD}/id-mk-deb-changelog.sh; then
         echo "id-mk-deb-changelog.sh failed"
     fi
@@ -185,43 +231,22 @@ if $debian; then
         echo "${DSC} does not exist"
         exit 1
     fi
+fi
+if test "${DEBIAN_DIST}"; then
+    prepare_deb debian ${DEBIAN_DIST}
+fi
 
-    if test "${DEBIAN_DIST}"; then
-       for dist in ${DEBIAN_DIST}; do
-           test -f build/debian/$dist && sudo rm -fr build/debian/$dist
-           mkdir -p build/debian
-           cp -a deb-src build/debian/$dist
-           cp ${ID_DEB_BUILD}/in-docker.sh build/debian/$dist
-       done
-    fi
-    if test "${UBUNTU_DIST}"; then
-       for dist in ${UBUNTU_DIST}; do
-           sudo rm -fr build/ubuntu/$dist
-           mkdir -p build/ubuntu
-           cp -a deb-src build/ubuntu/$dist
-           cp ${ID_DEB_BUILD}/in-docker.sh build/ubuntu/$dist
-       done
-    fi
+if test "${UBUNTU_DIST}"; then
+    prepare_deb ubuntu ${UBUNTU_DIST}
 fi
 
 if test "${CENTOS_DIST}"; then
     for dist in ${CENTOS_DIST}; do
-       case $dist in
-           centos5)
-               F=5.5
-               D=5;;
-           centos6)
-               F=6
-               D=6;;
-           centos7)
-               F=7
-               D=7;;
-           *)
-               echo "$dist not supported"
-               exit 1
-       esac
+       centos_map $dist
+       D=$?
+       F=$D
+       test $F = "5" && F=5.5
        cd build/centos/$dist
-
        echo "FROM centos:$D" >Dockerfile
        echo "RUN yum install -y wget yum-utils redhat-rpm-config rpm-build gcc gcc-c++ make" >>Dockerfile
        echo "RUN rpm --import http://ftp.indexdata.com/pub/yum/centos/$F/RPM-GPG-KEY-indexdata" >>Dockerfile
@@ -240,42 +265,10 @@ if test "${CENTOS_DIST}"; then
     done
 fi
 if test "${DEBIAN_DIST}"; then
-    for dist in ${DEBIAN_DIST}; do
-       cd build/debian/$dist
-       echo "FROM debian:$dist" >Dockerfile
-       echo 'RUN apt-get update && apt-get -q install -y wget devscripts equivs' >>Dockerfile
-       echo 'RUN wget -q -O - http://ftp.indexdata.dk/debian/indexdata.asc|apt-key add -' >>Dockerfile
-       echo "RUN echo \"deb http://ftp.indexdata.dk/debian $dist main\" >/etc/apt/sources.list.d/indexdata.list" >>Dockerfile
-       docker build -t indexdata-build-debian-$dist .
-       echo -n "Debian $dist "
-       docker run --rm  -v "$PWD:/build" -w /build/$PRODUCT-$VERSION indexdata-build-debian-$dist ../in-docker.sh >log 2>&1
-       if test $? -eq 0; then
-               echo Done
-       else
-               echo "Fail. Inspect build/debian/$dist/log"
-               exit 1
-       fi
-       cd ../../..
-    done
+    run_deb debian ${DEBIAN_DIST}
 fi
 if test "${UBUNTU_DIST}"; then
-    for dist in ${UBUNTU_DIST}; do
-       cd build/ubuntu/$dist
-       echo "FROM ubuntu:$dist" >Dockerfile
-       echo 'RUN apt-get update && apt-get -q install -y wget devscripts equivs' >>Dockerfile
-       echo 'RUN wget -q -O - http://ftp.indexdata.dk/debian/indexdata.asc|apt-key add -' >>Dockerfile
-       echo "RUN echo \"deb http://ftp.indexdata.dk/ubuntu $dist main\" >/etc/apt/sources.list.d/indexdata.list" >>Dockerfile
-       docker build -t indexdata-build-ubuntu-$dist .
-       echo -n "Ubuntu $dist "
-       docker run --rm  -v "$PWD:/build" -w /build/$PRODUCT-$VERSION indexdata-build-ubuntu-$dist ../in-docker.sh >log 2>&1
-       if test $? -eq 0; then
-               echo Done
-       else
-               echo "Fail. Inspect build/ubuntu/$dist/log"
-               exit 1
-       fi
-       cd ../../..
-    done
+    run_deb ubuntu ${UBUNTU_DIST}
 fi
 
 # Local Variables: