Adding onos packaging buck rule and script

Change-Id: I0c909e6bf45197061f81f1705de05561c4c91d8d
diff --git a/.buckconfig b/.buckconfig
index ab7460b..2d6f94a 100644
--- a/.buckconfig
+++ b/.buckconfig
@@ -7,6 +7,7 @@
 
 [alias]
     onos = //:onos
+    package = //tools/package:onos-package
 
 [download]
   maven_repo = https://repo1.maven.org/maven2
diff --git a/tools/package/BUCK b/tools/package/BUCK
new file mode 100644
index 0000000..19c7db1
--- /dev/null
+++ b/tools/package/BUCK
@@ -0,0 +1,105 @@
+KARAF = '//lib:apache-karaf'
+BRANDING = '//tools/package/branding:onos-tools-package-branding'
+
+FEATURES = [
+  '//features:onos-thirdparty-base',
+  '//features:onos-thirdparty-web',
+  '//features:onos-api',
+  '//features:onos-core',
+  '//features:onos-incubator',
+  '//features:onos-rest',
+  '//features:onos-gui',
+  '//features:onos-cli',
+#'//features:onos-security',
+]
+
+#FIXME this list is currently mirrored from the root Buck file
+APPS = [
+  #Drivers
+  '//drivers/default:onos-drivers-default-oar',
+
+  # Providers
+  '//providers/bgp:onos-providers-bgp-oar',
+  '//providers/host:onos-providers-host-oar',
+  '//providers/lldp:onos-providers-lldp-oar',
+  '//providers/netcfghost:onos-providers-netcfghost-oar',
+  '//providers/netcfglinks:onos-providers-netcfglinks-oar',
+  '//providers/netconf:onos-providers-netconf-oar',
+  '//providers/openflow/message:onos-providers-openflow-message-oar',
+  '//providers/ovsdb:onos-providers-ovsdb-oar',
+  '//providers/pcep:onos-providers-pcep-oar',
+  '//providers/null:onos-providers-null-oar',
+  '//providers/openflow/base:onos-providers-openflow-base-oar',
+  '//providers/rest:onos-providers-rest-oar',
+  '//providers/isis:onos-providers-isis-oar',
+
+  # Apps
+  '//apps/dhcp:onos-apps-dhcp-oar',
+  '//apps/fwd:onos-apps-fwd-oar',
+  '//apps/aaa:onos-apps-aaa-oar',
+  '//apps/acl:onos-apps-acl-oar',
+  '//apps/bgprouter:onos-apps-bgprouter-oar',
+  '//apps/proxyarp:onos-apps-proxyarp-oar',
+  '//apps/segmentrouting:onos-apps-segmentrouting-oar',
+  '//apps/gangliametrics:onos-apps-gangliametrics-oar',
+  '//apps/graphitemetrics:onos-apps-graphitemetrics-oar',
+  '//apps/igmp:onos-apps-igmp-oar',
+  '//apps/cordmcast:onos-apps-cordmcast-oar',
+  '//apps/olt:onos-apps-olt-oar',
+  '//apps/influxdbmetrics:onos-apps-influxdbmetrics-oar',
+  '//apps/metrics:onos-apps-metrics-oar',
+  '//apps/mfwd:onos-apps-mfwd-oar',
+  '//apps/mlb:onos-apps-mlb-oar',
+  '//apps/openstackinterface:onos-apps-openstackinterface-oar',
+  '//apps/openstacknetworking:onos-apps-openstacknetworking-oar',
+  '//apps/openstacknetworking/openstackrouting:onos-apps-openstacknetworking-openstackrouting-oar',
+  '//apps/openstacknetworking/openstackswitching:onos-apps-openstacknetworking-openstackswitching-oar',
+  '//apps/mobility:onos-apps-mobility-oar',
+  '//apps/optical:onos-apps-optical-oar',
+  '//apps/pathpainter:onos-apps-pathpainter-oar',
+  '//apps/pcep-api:onos-apps-pcep-api-oar',
+  '//apps/pim:onos-apps-pim-oar',
+  '//apps/reactive-routing:onos-apps-reactive-routing-oar',
+  '//apps/sdnip:onos-apps-sdnip-oar',
+  '//apps/virtualbng:onos-apps-virtualbng-oar',
+  '//apps/cordvtn:onos-apps-cordvtn-oar',
+  '//apps/vpls:onos-apps-vpls-oar',
+  '//apps/vrouter:onos-apps-vrouter-oar',
+  '//apps/vtn:onos-apps-vtn-oar',
+  '//apps/faultmanagement:onos-apps-faultmanagement-oar',
+  '//apps/openstacknode:onos-apps-openstacknode-oar',
+  '//apps/cpman/app:onos-apps-cpman-app-oar',
+  '//apps/xosclient:onos-apps-xosclient-oar',
+]
+
+#TODO move to buck-tools
+export_file (
+  name = 'onos-prep-karaf',
+)
+
+genrule(
+  name = 'onos-karaf',
+  srcs = glob(['bin/*', 'etc/*', 'init/*']),
+  bash = '$(location :onos-prep-karaf) $OUT $(location %s) %s $(location %s)'\
+            % (KARAF, ONOS_VERSION, BRANDING),
+  out = 'karaf.zip',
+)
+
+compile_features(
+  name = 'onos-features',
+  features = FEATURES,
+)
+
+staged_repos = ['$(location %s-repo)' % f for f in FEATURES]
+staged_apps = ['$(location %s)' % a for a in APPS]
+
+# feature_coords = 'foo:bar:1.3'
+sources = [ '$(location :onos-features)', ]
+sources += staged_repos + staged_apps
+
+genrule(
+  name = 'onos-package',
+  out = 'onos.zip',
+  bash = 'cp $(location :onos-karaf) $OUT && $(exe //buck-tools:onos-stage) $OUT ' + ' '.join(sources),
+  visibility = [ 'PUBLIC' ],
+)
\ No newline at end of file
diff --git a/tools/package/onos-prep-karaf b/tools/package/onos-prep-karaf
new file mode 100755
index 0000000..c592648
--- /dev/null
+++ b/tools/package/onos-prep-karaf
@@ -0,0 +1,91 @@
+#!/bin/bash
+# -----------------------------------------------------------------------------
+# Packages ONOS distributable into onos.tar.gz, onos.zip or a deb file
+# -----------------------------------------------------------------------------
+
+set -e
+
+OUT=$1
+KARAF_TAR=$2
+ONOS_VERSION=$3
+BRANDING=$4
+
+# Unroll the Apache Karaf bits, prune them and make ONOS top-level directories.
+tar xf $KARAF_TAR
+
+# Unroll the Apache Karaf bits, prune them and make ONOS top-level directories.
+KARAF_DIR=$(ls -d apache*)
+rm -rf $KARAF_DIR/demos
+
+# Patch the log-file size in place to increase it to 10 MB
+perl -pi.old -e "s/maxFileSize=1MB/maxFileSize=10MB/g" \
+    $KARAF_DIR/etc/org.ops4j.pax.logging.cfg
+
+# Patch-in proper Karaf version into the startup script
+sed -i '' "s/apache-karaf-\$KARAF_VERSION/$KARAF_DIR/g" bin/onos-service
+sed -i '' "s/apache-karaf-\$KARAF_VERSION/$KARAF_DIR/g" bin/onos-client
+mv bin/onos-client bin/onos
+chmod a+x bin/onos-service bin/onos
+
+# Stage the ONOS admin scripts and patch in Karaf service wrapper extras
+cp -r bin $KARAF_DIR
+cp -r init $KARAF_DIR
+cp -r etc $KARAF_DIR
+
+export BOOT_FEATURES="webconsole,onos-api,onos-core,onos-incubator,onos-cli,onos-rest,onos-gui"
+#FIXME
+#[ "$ONOS_SECURITY_MODE" = true ] && enable_security_mode
+
+# Patch the Apache Karaf distribution file to add ONOS features repository
+perl -pi.old -e "s|^(featuresRepositories=.*)|\1,mvn:org.onosproject/onos-features/$ONOS_VERSION/xml/features|" \
+    $KARAF_DIR/etc/org.apache.karaf.features.cfg
+
+# Patch the Apache Karaf distribution file to load default ONOS boot features
+perl -pi.old -e "s|^(featuresBoot=.*)|\1,$BOOT_FEATURES|" \
+    $KARAF_DIR/etc/org.apache.karaf.features.cfg
+
+# Patch the Apache Karaf distribution with ONOS branding bundle
+cp $BRANDING $KARAF_DIR/lib
+
+zip -q -0 -r $OUT $KARAF_DIR
+
+#FIXME
+# 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
+
+# copy in features and repos
+# Patch in the ONOS version file
+#echo $ONOS_VERSION > $ONOS_STAGE/VERSION
+
+
+#function enable_security_mode() {
+#    echo "Enabling security mode ONOS..."
+#
+#    # SM-ONOS step 1: downgrade felix config admin
+#    FELIX_CFG_ADMIN=${FELIX_CFG_ADMIN:-~/Downloads/org.apache.felix.configadmin-1.6.0.jar}
+#    if [ ! -f $FELIX_CFG_ADMIN ]; then
+#        echo "Downloading $FELIX_CFG_ADMIN..."
+#        curl -sL http://archive.apache.org/dist/felix/org.apache.felix.configadmin-1.6.0.jar > $FELIX_CFG_ADMIN
+#    fi
+#    [ ! -f $FELIX_CFG_ADMIN ] && \
+#        echo "Felix config admin not found: $FELIX_CFG_ADMIN" && exit 1
+#
+#    mkdir -p $ONOS_STAGE/$KARAF_DIST/system/org/apache/felix/org.apache.felix.configadmin/1.6.0
+#    cp $FELIX_CFG_ADMIN $ONOS_STAGE/$KARAF_DIST/system/org/apache/felix/org.apache.felix.configadmin/1.6.0
+#    perl -pi.old -e "s|org.apache.felix.configadmin/1.8.0|org.apache.felix.configadmin/1.6.0|g" \
+#        $ONOS_STAGE/$KARAF_DIST/etc/startup.properties
+#
+#    # SM-ONOS step 2: stage ONOS Felix framework security (this is already done by karaf assembly); end
+#
+#    # SM-ONOS step 3.1: configure karaf
+#    perl -pi.old -e "s|#java.security.policy|java.security.policy|" \
+#        $ONOS_STAGE/$KARAF_DIST/etc/system.properties
+#    perl -pi.old -e "s|#org.osgi.framework.security|org.osgi.framework.security|" \
+#        $ONOS_STAGE/$KARAF_DIST/etc/system.properties
+#
+#    # SM-ONOS step 3.2: update featuresBoot
+#    export BOOT_FEATURES="onos-security,$BOOT_FEATURES"
+#}
+