Adding Bazel packaging of admin tools and test tools.

Change-Id: I321c79264cf5f3ca0d6c7a52e300c916acbe463a
diff --git a/tools/package/BUILD b/tools/package/BUILD
index 1a65642..26638d4 100644
--- a/tools/package/BUILD
+++ b/tools/package/BUILD
@@ -4,40 +4,63 @@
 PATCHES = "@apache_karaf_patches//file"
 BRANDING = "//tools/package/branding:onos-tools-package-branding"
 
-# FIXME: This is still work in progress
+BRANDED_KARAF_REQUIREMENTS = [
+    KARAF,
+    PATCHES,
+    BRANDING,
+]
+
+# Generates auxiliary karaf.zip file; branded and augmented with ONOS runtime tools
 genrule(
     name = "onos-karaf",
-    srcs = [
-        KARAF,
-        PATCHES,
-        BRANDING,
-      ] + glob(["bin/*", "etc/*", "init/*", "runtime/bin/*"]),
+    srcs = BRANDED_KARAF_REQUIREMENTS + glob(["bin/*", "etc/*", "init/*", "runtime/bin/*"]),
     outs = ["karaf.zip"],
-    cmd = "echo $(location onos-prep-karaf) $(location karaf.zip) $(location %s) %s $(location %s) $(location %s) > $(location karaf.zip)" \
+    cmd = "$(location onos-prep-karaf) $(location karaf.zip) $(location %s) %s $(location %s) $(location %s) tools/package" \
               % (KARAF, ONOS_VERSION, BRANDING, PATCHES),
     tools = ["onos-prep-karaf"],
-    visibility = ["//visibility:public"],
 )
-#    cmd = "$(location onos-prep-karaf) $(location karaf.zip) $(location %s) %s $(location %s) $(location %s)" \
-#              % (KARAF, ONOS_VERSION), BRANDING, PATCHES),
 
+# FIXME: Move /features to under tools/package/features
 PACKAGING_REQUIREMENTS = [
     "//features:onos-features",
     ":onos-karaf",
 ]
 
-# FIXME: This is still work in progress
+# FIXME: Need to add OAR files as dependencies
+# Generates the principal distributable onos.tar.gz file
 genrule(
     name = "onos-package",
     srcs = PACKAGING_REQUIREMENTS + glob(["bin/*", "etc/*", "init/*", "config/*", "runtime/bin/*"]),
     outs = ["onos.tar.gz"],
-    cmd = "echo $(location onos_stage.py) $(location onos.tar.gz) $(location :onos-karaf) $(location //features:onos-features) $(SRCS) >$(location onos.tar.gz)",
+    cmd = "$(location onos_stage.py) $(location onos.tar.gz) %s $(location :onos-karaf) $(SRCS)" % ONOS_VERSION,
     visibility = ["//visibility:public"],
     tools = ["onos_stage.py"],
 )
-#    cmd = "$(location onos_stage.py) $(location onos.tar.gz) $(location :onos-karaf) $(SRCS)",
-#    cmd = "$(exe //buck-tools:onos-stage) $OUT " + ONOS_VERSION + " $(location :onos-karaf) " + " ".join(sources),
 
+# Generates auxiliary tar file with ONOS runtime tools
+filegroup(
+    name = "onos-runtime-tools",
+    srcs = glob(["runtime/bin/*"]),
+)
+
+# Generates the onos-admin.tar.gz file with remote admin tools
+genrule(
+    name = "onos-package-admin",
+    srcs = ["//tools/dev:onos-admin", "//tools/test:onos-admin", ":onos-runtime-tools"],
+    outs = ["onos-admin.tar.gz"],
+    cmd = "mkdir onos-admin-%s; cp $(SRCS) onos-admin-%s; tar zcf $(location onos-admin.tar.gz) onos-admin-%s"\
+          % (ONOS_VERSION, ONOS_VERSION, ONOS_VERSION),
+    visibility = ["//visibility:public"],
+)
+
+genrule(
+    name = "onos-package-test",
+    srcs = ["//tools/dev:onos-test", "//tools/test:onos-test", ":onos-runtime-tools", "//:onos-env-defaults"],
+    outs = ["onos-test.tar.gz"],
+    cmd = "mkdir onos-test-%s; cp -r tools onos-test-%s; tar zcf $(location onos-test.tar.gz) onos-test-%s"\
+          % (ONOS_VERSION, ONOS_VERSION, ONOS_VERSION),
+    visibility = ["//visibility:public"],
+)
 
 #staged_repos = ['$(location %s-repo)' % f for f in FEATURES]
 #staged_apps = ['$(location %s)' % a for a in APPS]
@@ -46,22 +69,6 @@
 #sources = [ '$(location :onos-features)', ]
 #sources += staged_repos + staged_apps
 #
-#tar_file(
-#  name = 'onos-package-runtime',
-#  srcs = glob(['runtime/bin/*']),
-#  root = 'tools/package',
-#  out = 'package-runtime.tar.gz',
-#  visibility = [ 'PUBLIC' ],
-#)
-#
-#genrule(
-#  name = 'onos-package',
-#  srcs = glob(['bin/*', 'etc/*', 'init/*', 'config/*', 'runtime/bin/*']),
-#  out = 'onos.tar.gz',
-#  bash = '$(exe //buck-tools:onos-stage) $OUT ' + ONOS_VERSION + ' $(location :onos-karaf) ' + ' '.join(sources),
-#  visibility = [ 'PUBLIC' ],
-#)
-#
 #genrule(
 #  name = 'onos-run',
 #  out = 'onos-run',
diff --git a/tools/package/onos-prep-karaf b/tools/package/onos-prep-karaf
index a466858..cb30a43 100755
--- a/tools/package/onos-prep-karaf
+++ b/tools/package/onos-prep-karaf
@@ -10,6 +10,8 @@
 ONOS_VERSION=$3
 BRANDING=$4
 KARAF_PATCHES=$5
+SANDBOX=${6:-.}
+
 #FIXME karaf version
 KARAF_VERSION="3.0.8"
 ONOS_SECURITY_MODE="false"
@@ -33,9 +35,9 @@
     $KARAF_DIR/etc/org.ops4j.pax.logging.cfg
 
 # Patch-in proper Karaf version into the startup script
-sed -i.bk "s/apache-karaf-\$KARAF_VERSION/$KARAF_DIR/g" bin/onos-service
+sed -i.bk "s/apache-karaf-\$KARAF_VERSION/$KARAF_DIR/g" $SANDBOX/bin/onos-service
 rm -f bin/*.bk
-chmod a+x bin/onos-service bin/onos
+chmod a+x $SANDBOX/bin/onos-service $SANDBOX/bin/onos
 
 export BOOT_FEATURES="standard,ssh,scr,war,webconsole,onos-api,onos-core,onos-incubator,onos-cli,onos-rest,onos-gui"
 
@@ -56,10 +58,10 @@
 mv $KARAF_DIR $PREFIX
 
 # Stage the ONOS admin scripts and patch in Karaf service wrapper extras
-cp -r bin $PREFIX
-cp -r runtime/bin/* $PREFIX/bin/
-cp -r init $PREFIX
-cp -r etc/* $PREFIX/$KARAF_DIR/etc/
+cp -r $SANDBOX/bin $PREFIX
+cp -r $SANDBOX/runtime/bin/* $PREFIX/bin/
+cp -r $SANDBOX/init $PREFIX
+cp -r $SANDBOX/etc/* $PREFIX/$KARAF_DIR/etc/
 
 if [ "$ONOS_SECURITY_MODE" = true ]
 then