Release build for bazel

Change-Id: I3de06814f530cef888163a84ce049fd08f491156
diff --git a/tools/build/onos-build-and-upload b/tools/build/onos-build-and-upload
index 97657c9..1d66973 100755
--- a/tools/build/onos-build-and-upload
+++ b/tools/build/onos-build-and-upload
@@ -3,6 +3,8 @@
 # Builds the release bits and uploads them.
 # -----------------------------------------------------------------------------
 
+set -e -o pipefail
+
 VERSION=$1
 NEXT_VERSION=$2
 BRANCH=$3
@@ -20,11 +22,9 @@
     # upload docs
     onos-upload-docs ${WIKI_USER}
 
-    # upload release bits
-    onos-upload-bits
-
     # spot check that uploaded artifacts are correct
-    check-uploaded-maven-artifacts  $VERSION $ONOS_ROOT https://oss.sonatype.org/content/groups/staging
+    # TODO - implement this for bazel
+    # check-uploaded-maven-artifacts  $VERSION $ONOS_ROOT https://oss.sonatype.org/content/groups/staging
 
     # set the version strings to the next version
     onos-snapshot ${NEXT_VERSION}
diff --git a/tools/build/onos-change-version b/tools/build/onos-change-version
index 9477e0f..94a5665 100755
--- a/tools/build/onos-change-version
+++ b/tools/build/onos-change-version
@@ -52,4 +52,8 @@
 sed -i".VERBACK" -E "s/ONOS_VERSION.*/ONOS_VERSION = '$NEW_VERSION'/" onos.defs
 sed -i".VERBACK" -E "s/ONOS_VERSION=.*/ONOS_VERSION='$NEW_VERSION'/" tools/build/onos-prepare-sonar
 
+# Version the bazel artifacts
+sed -i".VERBACK" -E "s/ONOS_VERSION.*/ONOS_VERSION = \"$NEW_VERSION\"/" tools/build/bazel/variables.bzl
+sed -i".VERBACK" -E "s/ONOS_VERSION.*/ONOS_VERSION = \"$NEW_VERSION\"/" WORKSPACE
+
 find . -name "*.VERBACK" | xargs rm -f
diff --git a/tools/build/onos-prepare-release b/tools/build/onos-prepare-release
index 9869ac3..3ca9326 100755
--- a/tools/build/onos-prepare-release
+++ b/tools/build/onos-prepare-release
@@ -3,8 +3,7 @@
 # Creates a tempdir for release and checks out the code
 # --------------------------------------------------------
 
-set -e
-set -x
+set -eu -o pipefail
 
 GERRIT_USER=${GERRIT_USER:-$USER}
 BRANCH=${2:-master}
@@ -31,14 +30,15 @@
 git tag -l | grep -q "$ONOS_VERSION\$" &&
   { echo "ERROR: Version already exists"; exit -1; }
 
-# Copy local buck configuration to new tree
-cp $ONOS_ROOT/.buckconfig.local $DIR/
-
 export ONOS_ROOT=$DIR
 # This is a hack to remove symlinks from the ONOS_ROOT path. To be removed when
 # the protobuf buck rules can handle symlinks
 ONOS_ROOT=$(pushd $ONOS_ROOT >/dev/null && pwd -P && popd >/dev/null)
 . $ONOS_ROOT/tools/build/envDefaults
+
+# cause any accidental invocation of buck to fail
+ln -s /tmp/no/such/directory $DIR/buck-out
+
 exec bash $COMMAND
 
 # TODO on exit, print "leaving directory"
diff --git a/tools/build/onos-release b/tools/build/onos-release
index 83d4506..ae638fa 100755
--- a/tools/build/onos-release
+++ b/tools/build/onos-release
@@ -22,8 +22,6 @@
 
 cd $ONOS_ROOT
 
-# TODO: Create a new branch for this activity?
-
 # Change the version
 onos-change-version $NEW_VERSION
 export ONOS_VERSION=$NEW_VERSION
@@ -32,31 +30,29 @@
 onos-validate-change-version
 
 # Build ONOS & deploy to staging repo using the release profile.
-onos-buck build onos
-
-if [ $dryRun -eq 0 ]; then
-    time onos-buck-publish
-else
-    time onos-buck-publish-local
-fi
+bazel build onos
 
 # Build ONOS docs
-onos-buck build //docs:internal //docs:external
+bazel build //docs:internal //docs:external
 
-# Package test tar.gz
-onos-buck build //:onos-test
+if [ $dryRun -eq 0 ]; then
+    publish_url="oss.sonatype.org"
+else
+    publish_url=""
+fi
 
-# Package admin tar.gz
-onos-buck build //:onos-admin
+# Create artifact catalog
+onos-publish-catalog publishing-catalog
+
+# publish artifacts
+onos-upload-artifacts.py publishing-catalog ${publish_url}
 
 # Build ONOS archetypes & deploy to staging repo using the release profile.
 # Note that release of the staging repository is a separate manual step.
 if [ $dryRun -eq 0 ]; then
-    export MVN_CLI_OPTS=-Pstaging
     pushd tools/package/archetypes/
-    mvn -B -Pstaging clean install && onos-archetypes-test && mvn -Pstaging -Prelease clean deploy
+    mvn -B -Pstaging clean install && onos-archetypes-test -Pstaging && mvn -Pstaging -Prelease clean deploy
     popd
-    unset MVN_CLI_OPTS
 else
     pushd tools/package/archetypes/
     mvn -B clean install && onos-archetypes-test
@@ -68,5 +64,3 @@
     git commit -a -m"Tagging $NEW_VERSION"
     git tag -sm"Tagging $NEW_VERSION" $NEW_VERSION #signed tag
 fi
-
-# TODO: push?
diff --git a/tools/build/onos-upload-artifacts.py b/tools/build/onos-upload-artifacts.py
index 0cd6c3e..074c14d 100755
--- a/tools/build/onos-upload-artifacts.py
+++ b/tools/build/onos-upload-artifacts.py
@@ -20,6 +20,7 @@
 import hashlib
 import requests, os
 import xml.etree.ElementTree, shutil
+import time
 
 SONATYPE_USER=os.environ.get("SONATYPE_USER")
 SONATYPE_PASSWORD=os.environ.get("SONATYPE_PASSWORD")
@@ -42,6 +43,8 @@
 </promoteRequest>
 '''
 
+CLOSE_RETRY_ATTEMPTS = 12 * 2
+
 def hashlib_compute(hash, input_file, output_file):
     with open(input_file, 'rb') as f:
         for block in iter(lambda: f.read(100000), b''):
@@ -106,6 +109,31 @@
     r = requests.post(url, close_request, headers=headers, auth=(SONATYPE_USER, SONATYPE_PASSWORD))
 
 
+def wait_for_staging_repo(description, repo_id):
+    base_url = "https://" + destination_repo_url + "/service/local/staging/profiles" + "/" + SONATYPE_PROFILE
+    if repo_id is None:
+        return
+    close_request = CLOSE_REPO_REQUEST_TEMPLATE.replace("%(description)", description).replace("%(repo_id)", repo_id)
+    url = base_url + "/finish"
+    headers = {'Content-Type': 'application/xml'}
+    repo_query_url = "https://oss.sonatype.org/service/local/staging/repository/" + repo_id
+
+    attempt = 1
+    print ("waiting for repo to close...")
+    while True:
+        r = requests.get(repo_query_url, close_request, headers=headers, auth=(SONATYPE_USER, SONATYPE_PASSWORD))
+        root = xml.etree.ElementTree.fromstring(r.text)
+        transitioning = root.find("transitioning").text
+        if transitioning != "true":
+            break
+        if attempt == CLOSE_RETRY_ATTEMPTS:
+            print ("Unable to close repo")
+            sys.exit(1)
+        attempt = attempt + 1
+        time.sleep(5)
+    print ("Repo closed successfully")
+
+
 def stage_file(file, repo_id, dest):
     filename_in_repo = os.path.dirname(dest) + "/" + os.path.basename(file)
     if destination_repo_url is not None:
@@ -188,4 +216,5 @@
         dest = s[1]
         upload_file(src, dest)
     close_staging_repo(repo_id=repo_id, description=description)
+    wait_for_staging_repo(repo_id=repo_id, description=description)
     shutil.rmtree(tempdir)
diff --git a/tools/build/onos-upload-docs b/tools/build/onos-upload-docs
index ed9c0f5..ef39330 100755
--- a/tools/build/onos-upload-docs
+++ b/tools/build/onos-upload-docs
@@ -11,10 +11,10 @@
 user=${1:-${WIKI_USER:-$USER}}
 remote=$user@api.onosproject.org
 
-docs=$(onos-buck build //docs:external --show-output 2>/dev/null | tail -1 | cut -d\  -f2)
+docs="bazel-bin/docs/external.jar"
 
 ONOS_VERSION_STRING=$ONOS_VERSION
-if echo $ONOS_VERSION_STRING | grep '-b'; then
+if echo $ONOS_VERSION_STRING | grep -- '-b'; then
     echo "ONOS version $ONOS_VERSION_STRING is a beta. Skipping"
     exit 0
 fi
@@ -32,7 +32,7 @@
     replace="0"
 fi
 
-if echo $ONOS_VERSION_STRING | grep '-'; then
+if echo $ONOS_VERSION_STRING | grep -- '-'; then
     echo "ONOS version $ONOS_VERSION_STRING is a beta or RC. Not replacing default document set."
     replace="0"
 fi
diff --git a/tools/test/bin/onos-archetypes-test b/tools/test/bin/onos-archetypes-test
index 6bc3e15..42515cc 100755
--- a/tools/test/bin/onos-archetypes-test
+++ b/tools/test/bin/onos-archetypes-test
@@ -25,10 +25,10 @@
 for an in api bundle cli rest ui uitab uitopo; do
     cd $AROOT
     rm -fr $AROOT/foo-$an
-    mvn archetype:generate $MVN_CLI_OPTS $ARCHETYPE_OPTS \
+    mvn archetype:generate "$@" $ARCHETYPE_OPTS \
         -DarchetypeArtifactId=onos-$an-archetype \
         -DgroupId=org.foo -DartifactId=foo-$an -Dversion=1.0 \
         -Dpackage=org.foo.$an -DinteractiveMode=false
     cd $AROOT/foo-$an
-    mvn $MVN_CLI_OPTS clean install
+    mvn "$@" clean install
 done