ONOSFW-156 63/5563/1
authorCNlukai <lukai1@huawei.com>
Tue, 5 Jan 2016 10:15:09 +0000 (18:15 +0800)
committerCNlukai <lukai1@huawei.com>
Tue, 5 Jan 2016 10:18:24 +0000 (18:18 +0800)
Add build onos-package in onosfw

Change-Id: I7a85065ad20da038760e1de7a6ddbb9814b8ef34
Signed-off-by: CNlukai <lukai1@huawei.com>
autobuild.sh
onos-package [new file with mode: 0755]

index 0fb1810..119d9a1 100755 (executable)
@@ -68,6 +68,7 @@ export ONOS_USER=root
 export ONOS_GROUP=root
 export ONOS_CELL=sdnds-tw
 export RPMBUILDPATH=~/rpmbuild
+export ONOS_STAGE_ROOT=$1
 ##### End Set build environment #####
 
 ##### Patches #####
@@ -338,6 +339,12 @@ checkforRPMBUILD() # Checks whether RPMBUILD is installed
     fi
 }
 ##### End Check for RPMBUILD tools #####
+##### Build Onos Package #####
+buildPackage()
+{
+    $GERRITROOT/onos-package
+}
+##### End Build Onos Package #####
 
 ##### Execution order #####
 main()
@@ -353,6 +360,7 @@ main()
     # freshONOS
     buildONOS
     checkforRPMBUILD
+    buildPackage
 }
 ##### End Execution order #####
 
diff --git a/onos-package b/onos-package
new file mode 100755 (executable)
index 0000000..6cbe67f
--- /dev/null
@@ -0,0 +1,213 @@
+#!/bin/bash
+# -----------------------------------------------------------------------------
+# Packages ONOS distributable into onos.tar.gz, onos.zip or a deb file
+# -----------------------------------------------------------------------------
+
+##### Set build environment #####
+export M2_REPO=${M2_REPO:-~/.m2/repository}
+export PATH="$PATH:$ONOS_ROOT/tools/dev/bin:$ONOS_ROOT/tools/test/bin"
+export PATH="$PATH:$ONOS_ROOT/tools/build"
+export BUILD_NUMBER=${BUILD_NUMBER:-$(id -un)~$(date +'%Y/%m/%d@%H:%M')}
+export ONOS_POM_VERSION="1.5.0-SNAPSHOT"
+export ONOS_VERSION=${ONOS_VERSION:-1.3.0}
+export ONOS_BITS=onos-${ONOS_VERSION%~*}
+export ONOS_STAGE=$ONOS_STAGE_ROOT/$ONOS_BITS
+export ONOS_DEB_ROOT=$ONOS_STAGE_ROOT/deb
+export ONOS_DEB=$ONOS_STAGE.deb
+export ONOS_RPM_ROOT=$ONOS_STAGE_ROOT/rpm
+export ONOS_RPM=$ONOS_STAGE.rpm
+export ONOS_RPM_VERSION=${ONOS_POM_VERSION//-/.}
+export ONOS_TAR=$ONOS_STAGE.tar.gz
+export KARAF_VERSION=3.0.3
+export KARAF_TAR=$ONOS_STAGE/apache-karaf-$KARAF_VERSION.tar.gz
+export KARAF_DIST=apache-karaf-$KARAF_VERSION
+##### End set build environment #####
+
+set -e
+
+# Build the staging directory used to produce the packages
+function build_stage_dir() {
+    # Make sure we have the original apache karaf bits first
+    [ ! -d $M2_REPO ] && echo "M2 repository $M2_REPO not found" && exit 1
+#    [ -d $ONOS_STAGE ] && echo "ONOS stage $ONOS_STAGE already exists" && exit 1
+    if [ -d $ONOS_STAGE ]; then
+       cd $ONOS_STAGE
+    else 
+        mkdir -p $ONOS_STAGE
+        cd $ONOS_STAGE
+    fi
+    # Create the stage directory and warp into it
+   # mkdir -p $ONOS_STAGE
+   # cd $ONOS_STAGE
+
+    # Check if Apache Karaf bits are available and if not, fetch them.
+   # if [ ! -f $KARAF_ZIPi -a ! -f $KARAF_TAR ]; then
+        echo "Downloading $KARAF_TAR..."
+        wget http://downloads.onosproject.org/third-party/apache-karaf-$KARAF_VERSION.tar.gz 
+   # fi
+    #[ ! -f $KARAF_ZIP -a ! -f $KARAF_TAR ] && \
+    #    echo "Apache Karaf bits $KARAF_ZIP or $KARAF_TAR not found" && exit 1
+
+   # Unroll the Apache Karaf bits, prune them and make ONOS top-level directories.
+   # [ -f $KARAF_ZIP ] && unzip -q $KARAF_ZIP && rm -rf $ONOS_STAGE/$KARAF_DIST/demos
+   # [ -f $KARAF_TAR ] && tar zxf $KARAF_TAR && rm -rf $ONOS_STAGE/$KARAF_DIST/demos
+    echo $KARAF_TAR 
+    tar zxf $KARAF_TAR
+    mkdir bin
+
+    # Patch the log-file size in place to increase it to 10 MB
+    perl -pi.old -e "s/maxFileSize=1MB/maxFileSize=10MB/g" \
+        $ONOS_STAGE/$KARAF_DIST/etc/org.ops4j.pax.logging.cfg
+
+    # Stage the ONOS admin scripts and patch in Karaf service wrapper extras
+    cp -r $ONOS_ROOT/tools/package/bin .
+    cp -r $ONOS_ROOT/tools/package/init $ONOS_STAGE/init
+    cp -r $ONOS_ROOT/tools/package/etc/* $ONOS_STAGE/$KARAF_DIST/etc
+
+    # Stage all builtin ONOS apps for factory install
+    onos-stage-apps $ONOS_STAGE/apps $ONOS_STAGE/$KARAF_DIST/system
+
+    # Mark the org.onosproject.drivers app active by default
+    touch $ONOS_STAGE/apps/org.onosproject.drivers/active
+
+    # Patch-in proper Karaf version into the startup script
+    sed "s/\$KARAF_VERSION/$KARAF_VERSION/g" \
+        $ONOS_ROOT/tools/package/bin/onos-service > bin/onos-service
+    sed "s/\$KARAF_VERSION/$KARAF_VERSION/g" \
+        $ONOS_ROOT/tools/package/bin/onos-client > bin/onos
+    chmod a+x bin/onos-service bin/onos
+
+    if [ -d $ONOS_ROOT/tools/package/karaf-assembly/target/repo ]; then
+        cp -r $ONOS_ROOT/tools/package/karaf-assembly/target/repo/* $ONOS_STAGE/$KARAF_DIST/system
+        #FIXME we need to add onos-features manually
+        mkdir -p $ONOS_STAGE/$KARAF_DIST/system/org/onosproject/onos-features/$ONOS_POM_VERSION
+        cp $M2_REPO/org/onosproject/onos-features/$ONOS_POM_VERSION/onos-features* \
+           $ONOS_STAGE/$KARAF_DIST/system/org/onosproject/onos-features/$ONOS_POM_VERSION
+    else
+        # TODO: Deprecated so remove when confident staging works.
+        # Stage the ONOS bundles, but only those that match the version
+        mkdir -p $ONOS_STAGE/$KARAF_DIST/system/org/onosproject
+        find $M2_REPO/org/onosproject -type f -path "*/$ONOS_POM_VERSION/*" \
+            \( -name '*.jar' -o -name '*.pom' -o -name '*-features.xml' \) \
+                | grep -v -Ee '-tests.jar|-[0-9]{8}.[0-9]{6}-' \
+                | while read src; do
+            dst=$ONOS_STAGE/$KARAF_DIST/system/${src#$M2_REPO/*}
+            mkdir -p $(dirname $dst)
+            cp $src $dst
+        done
+    fi
+
+    # ONOS Patching ----------------------------------------------------------------
+
+    # Patch the Apache Karaf distribution file to add ONOS features repository
+    perl -pi.old -e "s|^(featuresRepositories=.*)|\1,mvn:org.onosproject/onos-features/$ONOS_POM_VERSION/xml/features|" \
+        $ONOS_STAGE/$KARAF_DIST/etc/org.apache.karaf.features.cfg
+
+    # Patch the Apache Karaf distribution file to load default ONOS boot features
+    export BOOT_FEATURES="webconsole,onos-api,onos-core,onos-incubator,onos-cli,onos-rest,onos-gui"
+    perl -pi.old -e "s|^(featuresBoot=.*)|\1,$BOOT_FEATURES|" \
+        $ONOS_STAGE/$KARAF_DIST/etc/org.apache.karaf.features.cfg
+
+    # Patch the Apache Karaf distribution with ONOS branding bundle
+    cp $M2_REPO/org/onosproject/onos-branding/$ONOS_POM_VERSION/onos-branding-*.jar \
+        $ONOS_STAGE/$KARAF_DIST/lib
+
+    # Patch in the ONOS version file
+    echo $ONOS_VERSION > $ONOS_STAGE/VERSION
+}
+
+function build_compressed_package() {
+    # Package up the ONOS tar file
+    cd $ONOS_STAGE_ROOT
+    rm -f $ONOS_TAR $ONOS_ZIP
+    COPYFILE_DISABLE=1 tar zcf $ONOS_TAR $ONOS_BITS
+
+    # Figure out whether we should build ONOS zip file and if so, build it.
+    which zip >/dev/null && [ -z "$ONOS_TAR_ONLY" ] && buildZip=true || unset buildZip
+    [ -n "$buildZip" ] && zip -rq $ONOS_ZIP $ONOS_BITS
+
+    # Report on the archives that were built and clean-up
+    [ -n "$buildZip" ] && ls -lh $ONOS_TAR $ONOS_ZIP || ls -lh $ONOS_TAR
+    rm -r $ONOS_STAGE
+}
+
+# Build a DEB package
+function build_deb() {
+    echo "This command may ask for your password to run commands as sudo,"
+    echo "  because you need to be root in order to generate a proper DEB package."
+
+    sudo rm -fr $ONOS_DEB_ROOT
+
+    mkdir -p $ONOS_DEB_ROOT/{DEBIAN,opt,etc/init}
+
+    # Copy the debian directory and fill in version
+    cp -r $ONOS_ROOT/tools/package/deb/* $ONOS_DEB_ROOT/DEBIAN/
+    sed -i'' -E "s/@ONOS_POM_VERSION/$ONOS_POM_VERSION/" $ONOS_DEB_ROOT/DEBIAN/control
+
+    cp -r $ONOS_STAGE $ONOS_DEB_ROOT/opt/onos
+    cp $ONOS_ROOT/tools/package/init/onos.conf $ONOS_DEB_ROOT/etc/init/
+
+    mkdir -p $ONOS_DEB_ROOT/opt/onos/var/
+
+    sudo chown -R root:root $ONOS_DEB_ROOT
+
+    sudo dpkg-deb --build $ONOS_DEB_ROOT > /dev/null &&
+    sudo mv $ONOS_STAGE_ROOT/deb.deb $ONOS_DEB && ls -l $ONOS_DEB
+
+    #TODO run lintian on .deb
+}
+
+# Build an RPM package
+function build_rpm() {
+    read -r -p "WARN: rpm-build utility and root priviledges are need to build the package. Do you want to continue? [Y/n] " response
+    [[ "$response" =~ ^[Nn] ]] && exit 0
+
+    sudo rm -fr $ONOS_RPM_ROOT
+
+    sudo yum -y install rpm-build
+
+    mkdir -p $ONOS_RPM_ROOT/{BUILD,RPMS,SOURCES/onos-$ONOS_RPM_VERSION/{etc/init,opt},SPECS,SRPMS}
+
+    cp -r $ONOS_STAGE $ONOS_RPM_ROOT/SOURCES/onos-$ONOS_RPM_VERSION/opt/onos
+    cp $ONOS_ROOT/tools/package/init/onos.conf $ONOS_RPM_ROOT/SOURCES/onos-$ONOS_RPM_VERSION/etc/init/
+
+    cd $ONOS_RPM_ROOT/SOURCES
+    COPYFILE_DISABLE=1 tar zcf onos-$ONOS_RPM_VERSION.tar.gz onos-$ONOS_RPM_VERSION
+
+    cp $ONOS_ROOT/tools/package/rpm/onos.spec $ONOS_RPM_ROOT/SPECS/
+    sed -i'' -E "s/@ONOS_RPM_VERSION/$ONOS_RPM_VERSION/g" $ONOS_RPM_ROOT/SPECS/onos.spec
+
+    rpmbuild --define "_topdir $ONOS_RPM_ROOT" -bb $ONOS_RPM_ROOT/SPECS/onos.spec
+
+    cp $ONOS_RPM_ROOT/RPMS/noarch/onos-$ONOS_RPM_VERSION-1.noarch.rpm $ONOS_STAGE_ROOT && ls -l $ONOS_STAGE_ROOT/onos-$ONOS_RPM_VERSION-1.noarch.rpm
+}
+
+## Script entry point
+#[ ! -d "$ONOS_ROOT" ] && echo "ONOS_ROOT is not defined" >&2 && exit 1
+#. $ONOS_ROOT/tools/build/envDefaults
+
+# Bail on any errors
+set -e
+
+## Run karaf assembly to collect artifacts for off-line installations.
+#aux=/tmp/assembly-$$.log
+#trap "rm -f $aux 2>/dev/null" EXIT
+#cd $ONOS_ROOT/tools/package/karaf-assembly && mvn clean install > $aux 2>&1
+
+## Before starting make sure the environment is clan - delete onos staging folder
+#rm -fr $ONOS_STAGE
+
+# If there are parameters check if we want to build a deb - otherwise build tar.gz
+case ${1:---tar} in
+    "--tar" | "-T") build_stage_dir
+                    build_compressed_package
+    ;;
+    "--deb" | "-D") build_stage_dir
+                    build_deb
+    ;;
+    "--rpm" | "-R") build_stage_dir
+                    build_rpm
+    ;;
+    *) echo "usage: $(basename $0) [--tar|--deb|--rpm]" >&2 && exit 1
+    ;;
+esac