m/t/launch: multi-node launches, prefixed stdout

This reinstantiates //:launch-test2, with some small fixes for usability
(prefixed stdout and GetNodes retries to handle cluster connectivity
issues as the cluster grows).

We also drive-by port //:launch-test2 and //:launch to use the new and
shiny clicontext package.

Change-Id: I62a1d827b2087f1173abf19e792a2088dc8b80bb
Reviewed-on: https://review.monogon.dev/c/monogon/+/485
Reviewed-by: Lorenz Brun <lorenz@monogon.tech>
diff --git a/metropolis/test/launch/cli/launch-multi2/BUILD.bazel b/metropolis/test/launch/cli/launch-multi2/BUILD.bazel
index 51118cf..932b57b 100644
--- a/metropolis/test/launch/cli/launch-multi2/BUILD.bazel
+++ b/metropolis/test/launch/cli/launch-multi2/BUILD.bazel
@@ -5,7 +5,10 @@
     srcs = ["main.go"],
     importpath = "source.monogon.dev/metropolis/test/launch/cli/launch-multi2",
     visibility = ["//visibility:private"],
-    deps = ["//metropolis/pkg/logbuffer:go_default_library"],
+    deps = [
+        "//metropolis/cli/pkg/context:go_default_library",
+        "//metropolis/test/launch/cluster:go_default_library",
+    ],
 )
 
 go_binary(
diff --git a/metropolis/test/launch/cli/launch-multi2/main.go b/metropolis/test/launch/cli/launch-multi2/main.go
index d6c5f05..70c3745 100644
--- a/metropolis/test/launch/cli/launch-multi2/main.go
+++ b/metropolis/test/launch/cli/launch-multi2/main.go
@@ -17,35 +17,23 @@
 package main
 
 import (
-	"fmt"
-	"io"
+	"context"
 	"log"
-	"os"
 
-	"source.monogon.dev/metropolis/pkg/logbuffer"
+	clicontext "source.monogon.dev/metropolis/cli/pkg/context"
+	"source.monogon.dev/metropolis/test/launch/cluster"
 )
 
-// prefixedStdout is a os.Stdout proxy that prefixes every line with a constant
-// prefix. This is used to show logs from two Metropolis nodes without getting
-// them confused.
-// TODO(q3k): move to logging API instead of relying on qemu stdout, and remove
-// this function.
-func prefixedStdout(prefix string) io.ReadWriter {
-	lb := logbuffer.NewLineBuffer(2048, func(l *logbuffer.Line) {
-		fmt.Fprintf(os.Stdout, "%s%s\n", prefix, l.Data)
-	})
-	// Make a ReaderWriter from LineBuffer (a Reader), by combining into an
-	// anonymous struct with a io.MultiReader() (which will always return EOF
-	// on every Read if given no underlying readers).
-	return struct {
-		io.Reader
-		io.Writer
-	}{
-		Reader: io.MultiReader(),
-		Writer: lb,
-	}
-}
-
 func main() {
-	log.Fatal("unimplemented")
+	ctx := clicontext.WithInterrupt(context.Background())
+	cl, err := cluster.LaunchCluster(ctx, cluster.ClusterOptions{
+		NumNodes: 2,
+	})
+	if err != nil {
+		log.Fatalf("LaunchCluster: %v", err)
+	}
+	log.Printf("Launch: Cluster running!")
+
+	<-ctx.Done()
+	cl.Close()
 }
diff --git a/metropolis/test/launch/cli/launch/BUILD.bazel b/metropolis/test/launch/cli/launch/BUILD.bazel
index e7e9271..cb2cbb5 100644
--- a/metropolis/test/launch/cli/launch/BUILD.bazel
+++ b/metropolis/test/launch/cli/launch/BUILD.bazel
@@ -6,6 +6,7 @@
     importpath = "source.monogon.dev/metropolis/test/launch/cli/launch",
     visibility = ["//visibility:private"],
     deps = [
+        "//metropolis/cli/pkg/context:go_default_library",
         "//metropolis/proto/api:go_default_library",
         "//metropolis/test/launch:go_default_library",
         "//metropolis/test/launch/cluster:go_default_library",
diff --git a/metropolis/test/launch/cli/launch/main.go b/metropolis/test/launch/cli/launch/main.go
index aacba06..5567379 100644
--- a/metropolis/test/launch/cli/launch/main.go
+++ b/metropolis/test/launch/cli/launch/main.go
@@ -20,23 +20,16 @@
 	"context"
 	"log"
 	"os"
-	"os/signal"
-	"syscall"
 
+	clicontext "source.monogon.dev/metropolis/cli/pkg/context"
 	apb "source.monogon.dev/metropolis/proto/api"
 	"source.monogon.dev/metropolis/test/launch"
 	"source.monogon.dev/metropolis/test/launch/cluster"
 )
 
 func main() {
-	sigs := make(chan os.Signal, 1)
-	signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
-	ctx, cancel := context.WithCancel(context.Background())
-	go func() {
-		<-sigs
-		cancel()
-	}()
-	if err := cluster.LaunchNode(ctx, cluster.NodeOptions{
+	ctx := clicontext.WithInterrupt(context.Background())
+	err := cluster.LaunchNode(ctx, cluster.NodeOptions{
 		Ports:      launch.IdentityPortMap(cluster.NodePorts),
 		SerialPort: os.Stdout,
 		NodeParameters: &apb.NodeParameters{
@@ -44,10 +37,8 @@
 				ClusterBootstrap: cluster.InsecureClusterBootstrap,
 			},
 		},
-	}); err != nil {
-		if err == ctx.Err() {
-			return
-		}
-		log.Fatalf("Failed to execute: %v\n", err)
+	})
+	if err != nil {
+		log.Fatalf("LaunchNode: %v", err)
 	}
 }