ONOS-2895 Updating onos-package for rpm creation

Change-Id: I5db3b2991e12f65caef6c29f29ab5ed915748cb3
diff --git a/tools/build/onos-package b/tools/build/onos-package
index f47f5b6..a355413 100755
--- a/tools/build/onos-package
+++ b/tools/build/onos-package
@@ -120,64 +120,26 @@
 # 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
+    [[ "$response" =~ ^[Nn] ]] && exit 0
 
-            sudo yum -y install rpm-build
+    sudo rm -fr $ONOS_RPM_ROOT
 
-            mkdir -p $ONOS_RPM_ROOT/{BUILD,RPMS,SOURCES/ONOS-$ONOS_RPM_VERSION/{etc/init,opt},SPECS,SRPMS}
+    sudo yum -y install rpm-build
 
-            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/
+    mkdir -p $ONOS_RPM_ROOT/{BUILD,RPMS,SOURCES/onos-$ONOS_RPM_VERSION/{etc/init,opt},SPECS,SRPMS}
 
-            cd $ONOS_RPM_ROOT/SOURCES
-            COPYFILE_DISABLE=1 tar zcf ONOS-$ONOS_RPM_VERSION.tar.gz ONOS-$ONOS_RPM_VERSION
+    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/
 
-            {
-                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
+    cd $ONOS_RPM_ROOT/SOURCES
+    COPYFILE_DISABLE=1 tar zcf onos-$ONOS_RPM_VERSION.tar.gz onos-$ONOS_RPM_VERSION
 
-            rpmbuild --define "_topdir $ONOS_RPM_ROOT" -bb $ONOS_RPM_ROOT/SPECS/onos.spec
+    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
 
-            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
+    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
diff --git a/tools/package/deb/prerm b/tools/package/deb/prerm
index 5e8ace3..2a1633d 100755
--- a/tools/package/deb/prerm
+++ b/tools/package/deb/prerm
@@ -10,7 +10,6 @@
       sleep 1
     done
     [ -z "$(ps -ef | grep karaf.jar | grep -v grep)" ] && echo 'Stopped onos service' || echo 'Failed to stop onos'
-    ps -ef | grep karaf.jar | grep -v grep
 )
 
 # Clean up onos runtime directories
diff --git a/tools/package/rpm/onos.spec b/tools/package/rpm/onos.spec
new file mode 100644
index 0000000..df0a05d
--- /dev/null
+++ b/tools/package/rpm/onos.spec
@@ -0,0 +1,71 @@
+Name:        onos
+Version:     @ONOS_RPM_VERSION
+Release:     1
+Summary:     Open Networking Operating System (ONOS)
+Vendor:      ONOS Project
+Packager:    ONOS Project
+
+Group:       Applications/Engineering
+License:     Apache 2.0
+Requires:    jre >= 1:8
+URL:         http://www.onosproject.org
+Source0:     %{name}-@ONOS_RPM_VERSION.tar.gz
+
+BuildArch: noarch
+BuildRoot: %{_tmppath}/%{name}-buildroot
+
+%description
+Open Network Operating System (ONOS) is an open source SDN controller.
+
+%prep
+%setup -q
+
+%install
+mkdir -p %{buildroot}
+cp -R * %{buildroot}
+
+%clean
+rm -rf %{buildroot}
+
+%files
+%defattr(-,root,root,-)
+/etc/init/onos.conf
+/opt/onos/
+
+%post
+ONOS_USER=sdn
+
+# Check to see if user exists, and if not, create a service account
+getent passwd $ONOS_USER >/dev/null 2&>1 || ( useradd -M $ONOS_USER && usermod -L $ONOS_USER )
+
+# Create ONOS options file
+[ ! -f /opt/onos/options ] && cat << EOF > /opt/onos/options
+export ONOS_OPTS=server
+export ONOS_USER="$ONOS_USER"
+EOF
+
+# Change permissions for onos directory
+[ -d /opt/onos ] && chown -R $ONOS_USER.$ONOS_USER /opt/onos
+
+echo ONOS successfully installed at /opt/onos
+
+%preun
+# Check if onos is running; if it is, stop onos
+[ -z "$(status onos 2>/dev/null | grep start)" ] && echo "onos is not running." || (
+    stop onos
+
+    # Wait for onos to stop up to 5 seconds
+    for i in $(seq 1 5); do
+      [ -z "$(ps -ef | grep karaf.jar | grep -v grep)" ] && break
+      sleep 1
+    done
+    [ -z "$(ps -ef | grep karaf.jar | grep -v grep)" ] && echo 'Stopped onos service' || echo 'Failed to stop onos'
+)
+
+%postun
+#TODO this should be less brute-force
+rm -rf /opt/onos
+
+%changelog
+# TODO
+