core: Change rt.New to veyron2.Init.

Change-Id: Ibdc54c066442f09a12225d7f91a2f933edcdf628
diff --git a/tools/binary/impl_test.go b/tools/binary/impl_test.go
index d0f7ca6..28b62b2 100644
--- a/tools/binary/impl_test.go
+++ b/tools/binary/impl_test.go
@@ -15,7 +15,6 @@
 	"v.io/core/veyron2/context"
 	"v.io/core/veyron2/ipc"
 	"v.io/core/veyron2/naming"
-	"v.io/core/veyron2/rt"
 	"v.io/core/veyron2/security"
 	"v.io/core/veyron2/services/mgmt/binary"
 	"v.io/core/veyron2/services/mgmt/repository"
@@ -121,12 +120,10 @@
 }
 
 func TestBinaryClient(t *testing.T) {
-	runtime, err := rt.New()
-	if err != nil {
-		t.Fatalf("Unexpected error initializing runtime: %s", err)
-	}
-	defer runtime.Cleanup()
-	gctx = runtime.NewContext()
+	var shutdown veyron2.Shutdown
+	gctx, shutdown = veyron2.Init()
+	defer shutdown()
+	var err error
 	if gctx, err = veyron2.SetPrincipal(gctx, tsecurity.NewPrincipal("test-blessing")); err != nil {
 		panic(err)
 	}
diff --git a/tools/mgmt/device/acl_test.go b/tools/mgmt/device/acl_test.go
index b242182..a146e82 100644
--- a/tools/mgmt/device/acl_test.go
+++ b/tools/mgmt/device/acl_test.go
@@ -20,6 +20,9 @@
 )
 
 func TestACLGetCommand(t *testing.T) {
+	shutdown := initTest()
+	defer shutdown()
+
 	tape := NewTape()
 	server, endpoint, err := startServer(t, gctx, tape)
 	if err != nil {
@@ -66,6 +69,9 @@
 }
 
 func TestACLSetCommand(t *testing.T) {
+	shutdown := initTest()
+	defer shutdown()
+
 	tape := NewTape()
 	server, endpoint, err := startServer(t, gctx, tape)
 	if err != nil {
diff --git a/tools/mgmt/device/impl_test.go b/tools/mgmt/device/impl_test.go
index 1e38b25..065574c 100644
--- a/tools/mgmt/device/impl_test.go
+++ b/tools/mgmt/device/impl_test.go
@@ -12,23 +12,23 @@
 
 	"v.io/core/veyron2"
 	"v.io/core/veyron2/naming"
-	"v.io/core/veyron2/rt"
 	"v.io/core/veyron2/services/mgmt/device"
 	verror "v.io/core/veyron2/verror2"
 )
 
-func init() {
-	runtime, err := rt.New()
-	if err != nil {
-		panic(err)
-	}
-	gctx = runtime.NewContext()
+func initTest() (shutdown veyron2.Shutdown) {
+	var err error
+	gctx, shutdown = veyron2.Init()
 	if gctx, err = veyron2.SetPrincipal(gctx, tsecurity.NewPrincipal("test-blessing")); err != nil {
 		panic(err)
 	}
+	return shutdown
 }
 
 func TestListCommand(t *testing.T) {
+	shutdown := initTest()
+	defer shutdown()
+
 	tape := NewTape()
 	server, endpoint, err := startServer(t, gctx, tape)
 	if err != nil {
@@ -81,6 +81,9 @@
 }
 
 func TestAddCommand(t *testing.T) {
+	shutdown := initTest()
+	defer shutdown()
+
 	tape := NewTape()
 	server, endpoint, err := startServer(t, gctx, tape)
 	if err != nil {
@@ -131,6 +134,9 @@
 }
 
 func TestRemoveCommand(t *testing.T) {
+	shutdown := initTest()
+	defer shutdown()
+
 	tape := NewTape()
 	server, endpoint, err := startServer(t, gctx, tape)
 	if err != nil {
@@ -168,6 +174,9 @@
 }
 
 func TestInstallCommand(t *testing.T) {
+	shutdown := initTest()
+	defer shutdown()
+
 	tape := NewTape()
 	server, endpoint, err := startServer(t, gctx, tape)
 	if err != nil {
@@ -258,6 +267,9 @@
 }
 
 func TestClaimCommand(t *testing.T) {
+	shutdown := initTest()
+	defer shutdown()
+
 	tape := NewTape()
 	server, endpoint, err := startServer(t, gctx, tape)
 	if err != nil {
@@ -335,6 +347,9 @@
 }
 
 func TestStartCommand(t *testing.T) {
+	shutdown := initTest()
+	defer shutdown()
+
 	tape := NewTape()
 	server, endpoint, err := startServer(t, gctx, tape)
 	if err != nil {
diff --git a/tools/mgmt/device/instance_impl_test.go b/tools/mgmt/device/instance_impl_test.go
index 97e567f..10f6702 100644
--- a/tools/mgmt/device/instance_impl_test.go
+++ b/tools/mgmt/device/instance_impl_test.go
@@ -11,6 +11,9 @@
 )
 
 func TestStopCommand(t *testing.T) {
+	shutdown := initTest()
+	defer shutdown()
+
 	tape := NewTape()
 	server, endpoint, err := startServer(t, gctx, tape)
 	if err != nil {
@@ -83,6 +86,9 @@
 }
 
 func testHelper(t *testing.T, lower, upper string) {
+	shutdown := initTest()
+	defer shutdown()
+
 	tape := NewTape()
 	server, endpoint, err := startServer(t, gctx, tape)
 	if err != nil {
diff --git a/tools/mounttable/impl_test.go b/tools/mounttable/impl_test.go
index 7fd56d3..d1aefc9 100644
--- a/tools/mounttable/impl_test.go
+++ b/tools/mounttable/impl_test.go
@@ -15,7 +15,7 @@
 	"v.io/core/veyron2/vlog"
 
 	tsecurity "v.io/core/veyron/lib/testutil/security"
-	"v.io/core/veyron/profiles"
+	_ "v.io/core/veyron/profiles"
 )
 
 type server struct {
@@ -83,7 +83,7 @@
 		t.Errorf("NewServer failed: %v", err)
 		return nil, nil, err
 	}
-	endpoints, err := server.Listen(profiles.LocalListenSpec)
+	endpoints, err := server.Listen(veyron2.GetListenSpec(ctx))
 	if err != nil {
 		t.Errorf("Listen failed: %v", err)
 		return nil, nil, err
diff --git a/tools/naming/simulator/driver.go b/tools/naming/simulator/driver.go
index 685f19a..34c6e91 100644
--- a/tools/naming/simulator/driver.go
+++ b/tools/naming/simulator/driver.go
@@ -18,7 +18,6 @@
 
 	"v.io/core/veyron2"
 	"v.io/core/veyron2/context"
-	"v.io/core/veyron2/rt"
 
 	"v.io/core/veyron/lib/expect"
 	"v.io/core/veyron/lib/modules"
@@ -96,21 +95,17 @@
 	}
 }
 
-var runtime veyron2.Runtime
 var ctx *context.T
 
 func main() {
-	var err error
-	if runtime, err = rt.New(); err != nil {
-		panic(err)
-	}
-	defer runtime.Cleanup()
-	ctx = runtime.NewContext()
+	var shutdown veyron2.Shutdown
+	ctx, shutdown = veyron2.Init()
 
 	// Subprocesses commands are run by fork/execing this binary
 	// so we must test to see if this instance is a subprocess or the
 	// the original command line instance.
 	if modules.IsModulesProcess() {
+		shutdown()
 		// Subprocess, run the requested command.
 		if err := modules.Dispatch(); err != nil {
 			fmt.Fprintf(os.Stderr, "failed: %v\n", err)
@@ -118,6 +113,7 @@
 		}
 		return
 	}
+	defer shutdown()
 
 	shell, err := modules.NewShell(ctx, nil)
 	if err != nil {
diff --git a/tools/profile/impl_test.go b/tools/profile/impl_test.go
index 891e36d..25de771 100644
--- a/tools/profile/impl_test.go
+++ b/tools/profile/impl_test.go
@@ -15,7 +15,7 @@
 	"v.io/core/veyron2/vlog"
 
 	tsecurity "v.io/core/veyron/lib/testutil/security"
-	"v.io/core/veyron/profiles"
+	_ "v.io/core/veyron/profiles"
 	"v.io/core/veyron/services/mgmt/profile"
 	"v.io/core/veyron/services/mgmt/repository"
 )
@@ -90,7 +90,7 @@
 		t.Errorf("NewServer failed: %v", err)
 		return nil, nil, err
 	}
-	endpoints, err := server.Listen(profiles.LocalListenSpec)
+	endpoints, err := server.Listen(veyron2.GetListenSpec(ctx))
 	if err != nil {
 		t.Errorf("Listen failed: %v", err)
 		return nil, nil, err
diff --git a/tools/servicerunner/main.go b/tools/servicerunner/main.go
index 09a8e37..225e19a 100644
--- a/tools/servicerunner/main.go
+++ b/tools/servicerunner/main.go
@@ -9,7 +9,7 @@
 	"strings"
 	"time"
 
-	"v.io/core/veyron2/rt"
+	"v.io/core/veyron2"
 
 	"v.io/core/veyron/lib/expect"
 	"v.io/core/veyron/lib/flags/consts"
@@ -55,20 +55,24 @@
 }
 
 func main() {
-	r, err := rt.New()
-	if err != nil {
-		panic(fmt.Sprintf("Could not initialize runtime %s", err))
-	}
-	defer r.Cleanup()
+	ctx, shutdown := veyron2.Init()
 
 	if modules.IsModulesProcess() {
+		// TODO(suharshs): This is a hack and we should find a better way to parse flags in the modules.
+		// This is needed because the modules commands call veyron2.Init and multiple runtimes cannot
+		// be initialized simultaneously.
+		// In addition the modules read their args from flag.Args() (all flags after "--") which means
+		// the flags must still be parsed before calling modules.Dispatch(). Thus moving veyron2.Init
+		// below this clause solves nothing.
+		shutdown()
 		panicOnError(modules.Dispatch())
 		return
 	}
+	defer shutdown()
 
 	vars := map[string]string{}
 
-	sh, err := modules.NewShell(r.NewContext(), nil)
+	sh, err := modules.NewShell(ctx, nil)
 	if err != nil {
 		panic(fmt.Sprintf("modules.NewShell: %s", err))
 	}
diff --git a/tools/vrpc/vrpc_test.go b/tools/vrpc/vrpc_test.go
index 573c367..e039db9 100644
--- a/tools/vrpc/vrpc_test.go
+++ b/tools/vrpc/vrpc_test.go
@@ -10,7 +10,7 @@
 	"v.io/core/veyron2/vlog"
 
 	tsecurity "v.io/core/veyron/lib/testutil/security"
-	"v.io/core/veyron/profiles"
+	_ "v.io/core/veyron/profiles"
 	"v.io/core/veyron/tools/vrpc/test_base"
 )
 
@@ -120,7 +120,7 @@
 		t.Fatalf("NewServer failed: %v", err)
 		return
 	}
-	endpoints, err := ipcServer.Listen(profiles.LocalListenSpec)
+	endpoints, err := ipcServer.Listen(veyron2.GetListenSpec(gctx))
 	if err != nil {
 		t.Fatalf("Listen failed: %v", err)
 		return
@@ -129,9 +129,9 @@
 	obj := test_base.TypeTesterServer(&server{})
 	if err := ipcServer.Serve("", obj, nil); err != nil {
 		t.Fatalf("Serve failed: %v", err)
-		return
+		return name, shutdown
 	}
-	return
+	return name, shutdown
 }
 
 func TestSignature(t *testing.T) {
diff --git a/tools/vrun/vrun.go b/tools/vrun/vrun.go
index eb34737..fb3bec2 100644
--- a/tools/vrun/vrun.go
+++ b/tools/vrun/vrun.go
@@ -14,7 +14,6 @@
 
 	"v.io/core/veyron2"
 	"v.io/core/veyron2/context"
-	"v.io/core/veyron2/rt"
 	"v.io/core/veyron2/security"
 	"v.io/core/veyron2/vlog"
 
@@ -45,13 +44,8 @@
 }
 
 func vrun(cmd *cmdline.Command, args []string) error {
-	runtime, err := rt.New()
-	if err != nil {
-		vlog.Errorf("Could not initialize runtime")
-		return err
-	}
-	defer runtime.Cleanup()
-	ctx := runtime.NewContext()
+	ctx, shutdown := veyron2.Init()
+	defer shutdown()
 
 	if len(args) == 0 {
 		return cmd.UsageErrorf("vrun: no command specified")