Adding rpm generation functionalities in onos-package

Change-Id: Iced5950e6f509700dcce5f419edec52a90a1ab83
diff --git a/tools/build/envDefaults b/tools/build/envDefaults
index 494abdd..57c1f67 100644
--- a/tools/build/envDefaults
+++ b/tools/build/envDefaults
@@ -21,12 +21,15 @@
 export ONOS_POM_VERSION="1.3.0-SNAPSHOT"
 export ONOS_VERSION=${ONOS_VERSION:-1.3.0.$BUILD_NUMBER}
 
-# ONOS production bits (onos.tar.gz & onos.zip) staging environment
+# ONOS production bits (onos.tar.gz, onos.zip, onos.deb, onos.rpm) staging environment
 export ONOS_BITS=onos-${ONOS_VERSION%~*}
 export ONOS_STAGE_ROOT=${ONOS_STAGE_ROOT:-/tmp}
 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 ONOS_ZIP=$ONOS_STAGE.zip
 
diff --git a/tools/build/onos-package b/tools/build/onos-package
index 51207c5..269f959 100755
--- a/tools/build/onos-package
+++ b/tools/build/onos-package
@@ -90,17 +90,15 @@
 }
 
 # Build a DEB package
-function build_deb() { 
+function build_deb() {
     echo "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
-    mkdir -p $ONOS_DEB_ROOT/opt/
-    mkdir -p $ONOS_DEB_ROOT/etc/init
+    mkdir -p $ONOS_DEB_ROOT/{DEBIAN,opt,etc/init}
 
     {
-        echo "Package: onos"
+        echo "Package: ONOS"
         echo "Architecture: all"
         echo "Maintainer: ONOS Project"
         echo "Depends: debconf (>= 0.5.00), default-jre-headless (>= 1.8) | openjdk-8-jre | oracle-java8-installer"
@@ -116,11 +114,74 @@
     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
 }
 
+# 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
+    case $response in
+        [nN][oO])
+            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/debian/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
+
+            {
+                echo "Name: ONOS"
+                echo "Version: $ONOS_RPM_VERSION"
+                echo "Release: 1"
+                echo "Summary: Open Networking Operating System (ONOS)"
+                echo "Vendor: ONOS Project"
+                echo "Packager: ONOS Project"
+                echo "Group: Applications/Engineering"
+                echo "Requires: jre >= 1:8"
+                echo "License: Apache 2.0"
+                echo "URL: http://www.onosproject.org"
+                echo "Source0: ONOS-$ONOS_RPM_VERSION.tar.gz"
+                echo "BuildArch: noarch"
+                echo "BuildRoot: %{_tmppath}/%{name}-buildroot"
+                echo "%description"
+                echo "Open Network Operating System (ONOS) is an opensource SDN controller."
+                echo -e "\n"
+                echo "%prep"
+                echo "%setup -q"
+                echo -e "\n"
+                echo "%install"
+                echo "mkdir -p %{buildroot}"
+                echo "cp -R * %{buildroot}"
+                echo -e "\n"
+                echo "%clean"
+                echo "rm -rf %{buildroot}"
+                echo -e "\n"
+                echo "%files"
+                echo "%defattr(-,root,root,-)"
+                echo "/etc/init/onos.conf"
+                echo "/opt/onos/"
+                echo -e "\n"
+                echo "%post"
+                echo "echo ONOS successfully installed at /opt/onos"
+            } > $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
+            ;;
+    esac
+}
+
 # Script entry point
 [ ! -d "$ONOS_ROOT" ] && echo "ONOS_ROOT is not defined" >&2 && exit 1
 . $ONOS_ROOT/tools/build/envDefaults
@@ -131,14 +192,17 @@
 # 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 
+# If there are parameters check if we want to build a deb - otherwise build tar.gz
 case ${1:---tar} in
-    "--tar") build_stage_dir
-             build_compressed_package
+    "--tar" | "-T") build_stage_dir
+                    build_compressed_package
     ;;
-    "--deb") build_stage_dir
-             build_deb
+    "--deb" | "-D") build_stage_dir
+                    build_deb
     ;;
-    *) echo "usage: $(basename $0) [--tar|--deb]" >&2 && exit 1
+    "--rpm" | "-R") build_stage_dir
+                    build_rpm
+    ;;
+    *) echo "usage: $(basename $0) [--tar|--deb|--rpm]" >&2 && exit 1
     ;;
 esac