Improvements for init.d, upstart, and systemd-based systems

1. Change ownership of /onos/apps

onos-service needs write access to onos/apps/foo in order to activate
an app. This also means that ONOS itself could also activate/deactivate,
modify, or reinstall apps, which seems a bit dodgy but is probably

2. Fix sudo command line

The -b option was in the wrong place, breaking sudo on systems where
we use sudo to start onos (e.g. older debian or centos.)

3. Redirect stderr of 'type daemon' command

We want to detect whether the 'daemon' function/script is available
in init.d enviroments that support it, and we do so using the type
command. Previously we didn't redirect stderr, so this resulted
in a confusing error message being sent to stderr of whoever is
invoking the script.

4. onos.conf has changed to be more consistent with onos.initd

Previously onos.conf ignored $ONOS_GROUP and had a slightly different

5. onos.service has been added for systemd-based systems

This initial version of onos.service calls /etc/init.d/onos to start
and stop ONOS. In the future we may be able to call onos-service
directly, but we will need to make sure that permissions are set up
correctly so that onos-service can activate apps and so that ONOS
itself can write its log files.

6. A README has been added

7. Update the onos-install and onos-uninstall scripts

Related Jira issue: ONOS-5550

Change-Id: Ie72775f1d0a4082af9c5ea9b13999c427c15ffe0
diff --git a/tools/package/init/onos.conf b/tools/package/init/onos.conf
index ea9657e..d130afe 100644
--- a/tools/package/init/onos.conf
+++ b/tools/package/init/onos.conf
@@ -1,6 +1,8 @@
 description  "Open Network Operating System"
 author       "ON.Lab"
+# onos.conf: ONOS service configuration for upstart-based systems
 start on (net-device-up
           and local-filesystems
           and runlevel [2345])
@@ -15,13 +17,13 @@
 pre-start script
     [ -f /opt/onos/options ] && . /opt/onos/options
-    # Ensure that the environment is initialized
-    [ -d /opt/onos ] && mkdir /opt/onos/var 2>/dev/null && chown $ONOS_USER.$ONOS_USER /opt/onos/var
-    [ -d /opt/onos ] && mkdir /opt/onos/config 2>/dev/null && chown $ONOS_USER.$ONOS_USER /opt/onos/config
-    # TODO make karaf version configurable
-    [ -d /opt/onos ] && [ ! -h /opt/onos/log ] \
-         && ln -s /opt/onos/apache-karaf-3.0.5/data/log /opt/onos/log || :
+    # Fix permissions and symlinks
+    mkdir -p /opt/onos/var 2>/dev/null && chown $ONOS_USER:$ONOS_GROUP /opt/onos/var
+    mkdir -p /opt/onos/config 2>/dev/null && chown $ONOS_USER:$ONOS_GROUP /opt/onos/config
+    mkdir -p /opt/onos/apps 2>/dev/null && chown -R $ONOS_USER:$ONOS_GROUP /opt/onos/apps
+    [ ! -h $ONOS_OME/karaf ] && ln -s /opt/onos/apache-karaf* $ONOS_HOME/karaf || :
+    [ ! -h $ONOS_HOME/log ]  && ln -s $ONOS_HOME/karaf/data/log $ONOS_HOME/log || :
 end script
 pre-stop script