sensorlog_lite: Use global mounttable for syncgroup publish.

Local measure device mounttable mounts itself into the global mounttable.
This prevents syncgroups from breaking when the local mounttable IP:port
changes. Both mounttables are used for sync rendesvouz.
Workaround for neighbourhood join and sync.

Change-Id: I2ff917bf8fd4253c11fff5f063efbb2661b32a3a
diff --git a/go/src/v.io/x/sensorlog/measured/measured.go b/go/src/v.io/x/sensorlog/measured/measured.go
index 105eabb..4c369c6 100644
--- a/go/src/v.io/x/sensorlog/measured/measured.go
+++ b/go/src/v.io/x/sensorlog/measured/measured.go
@@ -30,6 +30,7 @@
 	// Flags below are only applied the first time measured is run against a Syncbase service with the same devid.
 	flagAdmin     = flag.String("admin", "", "Blessing of admin user allowed to join the syncgroup. Must be specified.")
 	flagPublishSb = flag.String("publish-sb", "", "Syncbase service to publish the syncgroup at. Must be absolute. Must be specified. The syncgroup is published as '"+config.SyncgroupName("<publish-sb>", "<devid>")+"'.")
+	flagPublishMt = flag.String("publish-mt", "", "Additional mounttable to use for sync rendezvous in addition to namespace roots. Optional.")
 )
 
 func main() {
@@ -53,6 +54,9 @@
 		return 1
 	}
 	publishMts := v23.GetNamespace(ctx).Roots()
+	if *flagPublishMt != "" {
+		publishMts = append(publishMts, *flagPublishMt)
+	}
 
 	db, err := sbutil.CreateOrOpenDB(ctx, *flagSbService, sbmodel.MeasuredTables)
 	if err != nil {
diff --git a/go/src/v.io/x/sensorlog/scripts/run_cli_syncbased.sh b/go/src/v.io/x/sensorlog/scripts/run_cli_syncbased.sh
index 0e3d43a..8c5b07a 100755
--- a/go/src/v.io/x/sensorlog/scripts/run_cli_syncbased.sh
+++ b/go/src/v.io/x/sensorlog/scripts/run_cli_syncbased.sh
@@ -15,7 +15,7 @@
   local -r PREFIX="${SL_PREFIX:-sl/client}"
   local -r DEVID="${SL_DEVID:-$(gen_uuid)}"
   local -r NAME="${PREFIX}/${DEVID}"
-  local -r IPADDR_PORT="${SL_IPADDR_PORT:-$(dig $(hostname) +short):8707}"
+  local -r IPADDR_PORT="${SL_IPADDR_PORT:-$(dig $(hostname) +short):8202}"
   local -r TMPDIR="${SL_TMPDIR:-sltmp}/${NAME}"
 
   mkdir -p "${TMPDIR}"
diff --git a/go/src/v.io/x/sensorlog/scripts/run_measured.sh b/go/src/v.io/x/sensorlog/scripts/run_measured.sh
index 5590274..f034099 100755
--- a/go/src/v.io/x/sensorlog/scripts/run_measured.sh
+++ b/go/src/v.io/x/sensorlog/scripts/run_measured.sh
@@ -5,9 +5,14 @@
 
 # Starts an instance of measured and required services.
 #
-# mounttabled is started locally with syncbased. measured connects to it
-# and bootstraps the syncgroup joinable at:
-# /$IPADDR_PORT/$PREFIX/$DEVID/syncbased.
+# mounttabled is started locally at IPADDR:PORT, mounting itself to the global
+# mounttable at $GLOBAL_MT/users/$USER/$PREFIX/$DEVID. syncbased is started in
+# the local mounttable at $PREFIX/$DEVID/syncbased.
+# measured is started, creating a syncgroup published at the local syncbased as
+# reached through the global mounttable:
+# $GLOBAL_MT/users/$USER/$PREFIX/$DEVID/$PREFIX/$DEVID/syncbased
+# Once the syncgroup is joined, sync is configured to use either the global or
+# the local mounttable (unless the local mounttable IPADDR:PORT changes).
 # measured drops most permissions on prefixes in the syncgroup. Full admin
 # permissions are granted to $ADMIN (as a blessing extension of the same
 # default blessing as the one running this script).
@@ -17,20 +22,24 @@
 source "${JIRI_ROOT}/experimental/projects/sensorlog_lite/src/v.io/x/sensorlog_lite/scripts/runner_lib.sh"
 
 # Must be run with V23_CREDENTIALS set or through the agent.
-# Optional environment variables: SL_PREFIX, SL_DEVID, SL_ADMIN, SL_IPADDR_PORT, SL_TMPDIR
+# Optional environment variables: SL_PREFIX, SL_DEVID, SL_ADMIN, SL_IPADDR_PORT, SL_USER, SL_GLOBAL_MT, SL_TMPDIR
 function main() {
   local -r PREFIX="${SL_PREFIX:-sl/measured}"
   local -r DEVID="${SL_DEVID:-$(gen_uuid)}"
   local -r ADMIN="${SL_ADMIN:-sl/client}"
   local -r NAME="${PREFIX}/${DEVID}"
   local -r IPADDR_PORT="${SL_IPADDR_PORT:-$(dig $(hostname) +short):8707}"
+  local -r USER="${SL_USER:-$(get_user_email)}"
+  local -r GLOBAL_MT="${SL_GLOBAL_MT:-/ns.dev.v.io:8101}"
   local -r TMPDIR="${SL_TMPDIR:-sltmp}/${NAME}"
 
   mkdir -p "${TMPDIR}"
   trap "kill_child_processes; exit 1" ERR EXIT
-  run_mounttabled "${NAME}" "${IPADDR_PORT}"
+  run_mounttabled "${NAME}" "${IPADDR_PORT}" "${GLOBAL_MT}/users/${USER}"
   run_syncbased "/${IPADDR_PORT}" "${NAME}" "${TMPDIR}"
-  run_measured "/${IPADDR_PORT}" "${NAME}" "${DEVID}" "${ADMIN}"
+  run_measured "/${IPADDR_PORT}" "${NAME}" "${DEVID}" "${ADMIN}" \
+      "${GLOBAL_MT}/users/${USER}/${NAME}/${NAME}/syncbased" \
+      "${GLOBAL_MT}/users/${USER}"
   # Wait for signal.
   while true; do
     sleep 10
diff --git a/go/src/v.io/x/sensorlog/scripts/runner_lib.sh b/go/src/v.io/x/sensorlog/scripts/runner_lib.sh
index 16c76d7..b4e69f2 100644
--- a/go/src/v.io/x/sensorlog/scripts/runner_lib.sh
+++ b/go/src/v.io/x/sensorlog/scripts/runner_lib.sh
@@ -39,14 +39,26 @@
 }
 export -f get_blessing_root
 
-# Starts mounttabled at IPADDR:PORT.
-# run_mounttabled NAME IPADDR:PORT
+# Extracts email address from the blessing obtained by get_blessing_root.
+function get_user_email() {
+  get_blessing_root | tr "${BLESSING_CHAIN_SEPARATOR}" '\n' | grep '@' | head -n 1
+}
+export -f get_user_email
+
+# Starts mounttabled at IPADDR:PORT. If $GLOBAL_MOUNT is provided, the
+# mounttable mounts itself under $GLOBAL_MOUNT/$NAME.
+# run_mounttabled NAME IPADDR:PORT [GLOBAL_MOUNT]
 function run_mounttabled() {
   local -r NAME="$1"
   local -r IPADDR_PORT="$2"
+  local GLOBAL_MOUNT="${3:-}"
+  if [[ -n "${GLOBAL_MOUNT}" ]]; then
+    GLOBAL_MOUNT="${GLOBAL_MOUNT}/${NAME}"
+  fi
   # TODO(ivanpi): Lock down mounttable permissions.
   "${JIRI_ROOT}"/release/go/bin/vbecome -name="$(name_to_blessing "${NAME}/mounttabled")" \
     "${JIRI_ROOT}"/release/go/bin/mounttabled -v23.tcp.address "${IPADDR_PORT}" \
+    -name="${GLOBAL_MOUNT}" \
     &
   sleep 1
 }
@@ -76,19 +88,24 @@
 }
 export -f run_syncbased
 
-# Starts measured publishing the syncgroup at the local mounttable. Expects a
-# syncbase instance to have been started at $MT with the same $NAME.
-# run_measured MT NAME DEVID ADMIN
+# Starts measured that uses $PUBLISH_SB to publish the syncgroup. Expects a
+# syncbase instance to have been started at $MT with the same $NAME. If
+# $PUBLISH_MT is provided, the syncgroup is advertised there in addition to
+# the local mounttable.
+# run_measured MT NAME DEVID ADMIN PUBLISH_SB [PUBLISH_MT]
 function run_measured() {
   local -r MT="$1"
   local -r NAME="$2"
   local -r DEVID="$3"
   local -r ADMIN="$4"
+  local -r PUBLISH_SB="$5"
+  local -r PUBLISH_MT="${6:-}"
   local -r DEF_BLESSING_ADMIN="$(name_to_blessing "$(get_blessing_root)/${ADMIN}")"
   "${JIRI_ROOT}"/release/go/bin/vbecome -name="$(name_to_blessing "${NAME}")" \
     "${JIRI_ROOT}"/experimental/projects/sensorlog_lite/bin/measured -v23.namespace.root "${MT}" \
     -service "${NAME}/syncbased" -devid="${DEVID}" -admin="${DEF_BLESSING_ADMIN}" \
-    -publish-sb "${MT}/${NAME}/syncbased" -alsologtostderr \
+    -publish-sb "${PUBLISH_SB}" -publish-mt="${PUBLISH_MT}" \
+    -alsologtostderr \
     &
   sleep 1
 }