Adding buck publish scripts

Change-Id: I7d3cbe55a9d958d2bb2cc2fb71a3293825c1bf33
diff --git a/.buckconfig b/.buckconfig
index a4b1354..57cfed6 100644
--- a/.buckconfig
+++ b/.buckconfig
@@ -2,7 +2,7 @@
   includes = //buck-tools/default.defs
 
 [plugins]
-  directory = //bucklets/plugins
+  directory = //bin/plugins
 
 [java]
   source_level = 8
diff --git a/tools/build/buck-publish-api b/tools/build/buck-publish-api
new file mode 100755
index 0000000..e319395
--- /dev/null
+++ b/tools/build/buck-publish-api
@@ -0,0 +1,56 @@
+#!/bin/bash
+
+BUCK_LOCAL_CONFIG=${BUCK_LOCAL_CONFIG:-$ONOS_ROOT/.buckconfig.local}
+MVN_REPO="https://oss.sonatype.org/content/repositories/snapshots"
+NO_BUCKD=1
+
+set -e
+set -x
+
+#FIXME if pwd != buck
+pushd buck
+
+# build buck
+scripts/create_tag.sh
+buck build buck --show-output
+
+# publish cli:main-fixed as buck-api
+cp $BUCK_LOCAL_CONFIG .
+buck publish //src/com/facebook/buck/cli:main-fixed \
+    --remote-repo=https://oss.sonatype.org/content/repositories/snapshots/ \
+    | tee ../api-publish.txt
+
+# Alternatively, we can deploy org.onosproject:buck-api with the following:
+#mvn deploy:deploy-file -DgroupId=org.onosproject -DartifactId=buck-api \
+#  -Dversion=0.1-SNAPSHOT -DgeneratePom=true -Dpackaging=jar \
+#  -DrepositoryId=snapshot -Durl=https://oss.sonatype.org/content/repositories/snapshots/ \
+#  -Dfile=buck-out/gen/src/com/facebook/buck/cli/main-fixed/main-fixed.jar
+
+#FIXME upload API to S3 for backup
+
+popd #buck
+
+SNAPSHOT_VERSION=$(cat api-publish.txt | grep "^org.onosproject:buck-api:jar" \
+                       | cut -d' ' -f1 | cut -d: -f4)
+BUCK_API_URL="$MVN_REPO/org/onosproject/buck-api/0.1-SNAPSHOT/buck-api-$SNAPSHOT_VERSION.jar"
+echo $BUCK_API_URL
+
+BUCK_API_SHA=$(shasum buck-out/gen/src/com/facebook/buck/cli/main-fixed/main-fixed.jar | cut -d' ' -f1)
+echo $BUCK_API_SHA
+
+pushd onos
+sed -i "" -E "s#url =.*#url = '$BUCK_API_URL',#" tools/build/buck-plugin/BUCK
+sed -i "" -E "s#sha1 =.*#sha1 = '$BUCK_API_SHA',#" tools/build/buck-plugin/BUCK
+popd #onos
+
+#pushd onos-yang-tools
+#FIXME update version (assume 0.1-SNAPSHOT for now)
+#popd #onos-yang-tools
+
+set +x
+
+echo
+echo "Please build and verify the ONOS Buck plugin and the Yang tools plugin:"
+echo "  cd onos; buck build //tools/build/buck-plugin:onosjar"
+echo "  cd onos-yang-tools; mvn clean package"
+echo "You should commit and push any required changes."
\ No newline at end of file
diff --git a/tools/build/buck-publish-package b/tools/build/buck-publish-package
new file mode 100755
index 0000000..909acc9
--- /dev/null
+++ b/tools/build/buck-publish-package
@@ -0,0 +1,57 @@
+#!/bin/bash
+
+DEFAULT_TAG=${TAG:-$(date +v%Y.%m.%d.01)}
+NO_BUCKD=1
+
+DOWNLOAD_BASE="http://onlab.vicci.org/onos/third-party"
+BUCK_ZIP="buck-$DEFAULT_TAG.zip"
+ZIP_STAGE="buck-bin"
+
+rm -rf $ZIP_STAGE
+
+set -e
+set -x
+
+# build plugins
+
+pushd onos
+buck build //tools/build/buck-plugin:onosjar
+popd #onos
+
+pushd onos-yang-tools
+mvn clean package
+popd #onos-yang-tools
+
+# assemble zip
+
+mkdir -p buck-bin/plugins
+
+cp buck/buck-out/gen/programs/buck.pex $ZIP_STAGE/buck
+cp onos/buck-out/gen/tools/build/buck-plugin/onosjar.jar $ZIP_STAGE/plugins/onos.jar
+cp onos-yang-tools/plugin/buck/target/onos-yang-buck-plugin-1.9-SNAPSHOT.jar $ZIP_STAGE/plugins/yang.jar
+
+BUCK_VERSION=$(buck/buck-out/gen/programs/buck.pex -V)
+echo $BUCK_VERSION > $ZIP_STAGE/.buck_version
+
+chmod 555 $ZIP_STAGE/buck
+chmod 444 $ZIP_STAGE/.buck_version
+
+pushd $ZIP_STAGE
+zip -r ../$BUCK_ZIP buck .buck_version plugins
+popd #$ZIP_STAGE
+
+# publish zip
+#FIXME check for s3 credentials
+uploadToS3.py --dest third-party/ $BUCK_ZIP
+
+# update version in onos-buck
+URL="$DOWNLOAD_BASE/$BUCK_ZIP"
+SHA=$(shasum $BUCK_ZIP | cut -d' ' -f1)
+
+sed -i "" -E "s#BUCK_URL=.*#BUCK_URL=\"$URL\"#" onos/tools/build/onos-buck
+sed -i "" -E "s#BUCK_SHA=.*#BUCK_SHA=\"$SHA\"#" onos/tools/build/onos-buck
+sed -i "" -E "s#REQUIRED_VERSION=.*#REQUIRED_VERSION=\"$BUCK_VERSION\"#" onos/tools/build/onos-buck
+
+set +x
+echo
+echo "Commit and push change to onos-buck on the onos directory."
\ No newline at end of file
diff --git a/tools/build/buck-publish-setup b/tools/build/buck-publish-setup
new file mode 100755
index 0000000..25e360a
--- /dev/null
+++ b/tools/build/buck-publish-setup
@@ -0,0 +1,24 @@
+#!/bin/bash
+
+BUCK_REF="10-11-2016"
+ONOS_REF="master"
+YANG_REF="master"
+
+set -x
+
+# change to tmpdir
+DIR=$(mktemp -d /tmp/buck-publish.XXXXX) || { echo "Failed to create temp file"; exit 1; }
+cd $DIR
+
+# checkout buck
+git clone -b $BUCK_REF https://github.com/bocon13/buck
+
+# checkout plugins
+git clone -b $ONOS_REF https://gerrit.onosproject.org/onos.git
+git clone -b $YANG_REF https://gerrit.onosproject.org/onos-yang-tools.git
+
+set +x
+
+echo
+echo "Buck publish area:"
+echo "  cd $DIR"
diff --git a/tools/build/onos-buck b/tools/build/onos-buck
index 721bf46..df5d789 100755
--- a/tools/build/onos-buck
+++ b/tools/build/onos-buck
@@ -5,9 +5,9 @@
 
 set -e
 
-BUCK_URL="https://github.com/bocon13/buck/releases/download/v2016.09.13.01/buck"
-BUCK_SHA="e72cf2e9ef719fa81fd4e0d1b620f20448c10a9d"
-REQUIRED_VERSION="buck version 0b60c3d7f8d54b2e6e6607749b748c0f240a6eb3"
+BUCK_URL="http://onlab.vicci.org/onos/third-party/buck.zip"
+BUCK_SHA="26652b4f49849517fd19abd782bd8cd6f28e11a9"
+REQUIRED_VERSION="buck version 7eb2adae2aa1e4d38ce2f5b9f907878687bf19cf"
 
 [  "-U" = "$1" ] && shift && FORCE_UPDATE=True
 
@@ -16,48 +16,24 @@
 
 if [ -n "$FORCE_UPDATE" ] || [ ! -f "buck" ] || [ "$REQUIRED_VERSION" != "$(cat .buck_version)" ]; then
     echo "Downloading Buck..."
-    rm -f .buck_version buck
-    curl -o ./buck -L $BUCK_URL
+    rm -fr .buck_version buck buck.zip plugins
+    curl -o ./buck.zip -L $BUCK_URL
     if [ -n "$(which shasum)" ]; then
-        SHA=$(shasum ./buck | cut -d' ' -f1)
+        SHA=$(shasum ./buck.zip | cut -d' ' -f1)
         [ "$SHA" != "$BUCK_SHA" ] &&
            echo "ERROR: Downloaded SHA ($SHA) does not match expected SHA ($BUCK_SHA)" &&
            exit 1
     else
         echo "SHA cannot be verified"
     fi
-    chmod 555 ./buck
-    echo $(./buck --version 2>/dev/null) > .buck_version
-    chmod 444 .buck_version
-    rm -rf ./buck-out
+    unzip buck.zip
+    rm buck.zip
+    rm -rf $ONOS_ROOT/buck-out
     printf "Successfully downloaded Buck to $ONOS_ROOT/bin/buck\n\n"
-    ONOS_BUILD_PLUGIN="true"
 fi
 popd > /dev/null
 
 BUCK=$ONOS_ROOT/bin/buck
-PLUGINS=$ONOS_ROOT/bucklets/plugins
-ONOS_PLUGIN=$PLUGINS/onosjar.jar
-
-if [ ! -f "$ONOS_PLUGIN" -o -n "$ONOS_BUILD_PLUGIN" ]; then
-    echo "Building ONOS Buck plugins..."
-
-    # Build it first
-    pluginJar=$(NO_BUCKD=1 $BUCK build //tools/build/buck-plugin:onosjar --show-output 2>/dev/null | grep onosjar.jar | cut -d\  -f2)
-
-    CHK_NEW=$(cksum $ONOS_ROOT/$pluginJar | cut -d' ' -f1-2)
-    CHK_OLD=$(cksum $ONOS_PLUGIN 2>/dev/null | cut -d' ' -f1-2)
-    if [ "$CHK_NEW" != "$CHK_OLD" ]; then
-        # diff plugins... if different, copy and restart buckd
-        # Then install it
-        mkdir -p $PLUGINS
-        cp $ONOS_ROOT/$pluginJar $PLUGINS
-        echo "Updated to the latest plugin."
-        $BUCK clean 2>/dev/null
-    else
-        echo "Plugin was already up to date."
-    fi
-fi
 
 # Finally, run the Buck command...
 $BUCK "$@"