Lorenz Brun | 52f7f29 | 2020-06-24 16:42:02 +0200 | [diff] [blame] | 1 | // Copyright 2020 The Monogon Project Authors. |
| 2 | // |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | package main |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "log" |
| 22 | "os" |
| 23 | "os/signal" |
| 24 | "syscall" |
| 25 | "time" |
| 26 | |
| 27 | grpcretry "github.com/grpc-ecosystem/go-grpc-middleware/retry" |
| 28 | "google.golang.org/grpc" |
| 29 | |
Serge Bazanski | 77cb6c5 | 2020-12-19 00:09:22 +0100 | [diff] [blame] | 30 | common "git.monogon.dev/source/nexantic.git/metropolis/node" |
| 31 | apb "git.monogon.dev/source/nexantic.git/metropolis/proto/api" |
| 32 | "git.monogon.dev/source/nexantic.git/metropolis/test/launch" |
Lorenz Brun | 52f7f29 | 2020-06-24 16:42:02 +0200 | [diff] [blame] | 33 | ) |
| 34 | |
| 35 | func main() { |
| 36 | sigs := make(chan os.Signal, 1) |
| 37 | signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) |
| 38 | ctx, cancel := context.WithCancel(context.Background()) |
| 39 | go func() { |
| 40 | <-sigs |
| 41 | cancel() |
| 42 | }() |
| 43 | sw0, vm0, err := launch.NewSocketPair() |
| 44 | if err != nil { |
| 45 | log.Fatalf("Failed to create network pipe: %v\n", err) |
| 46 | } |
| 47 | sw1, vm1, err := launch.NewSocketPair() |
| 48 | if err != nil { |
| 49 | log.Fatalf("Failed to create network pipe: %v\n", err) |
| 50 | } |
| 51 | |
| 52 | go func() { |
| 53 | if err := launch.Launch(ctx, launch.Options{ConnectToSocket: vm0, SerialPort: os.Stdout}); err != nil { |
| 54 | log.Fatalf("Failed to launch vm0: %v", err) |
| 55 | } |
| 56 | }() |
| 57 | nanoswitchPortMap := make(launch.PortMap) |
| 58 | identityPorts := []uint16{ |
| 59 | common.ExternalServicePort, |
| 60 | common.DebugServicePort, |
| 61 | common.KubernetesAPIPort, |
| 62 | } |
| 63 | for _, port := range identityPorts { |
| 64 | nanoswitchPortMap[port] = port |
| 65 | } |
| 66 | go func() { |
| 67 | opts := []grpcretry.CallOption{ |
| 68 | grpcretry.WithBackoff(grpcretry.BackoffExponential(100 * time.Millisecond)), |
| 69 | } |
Serge Bazanski | 57b4375 | 2020-07-13 19:17:48 +0200 | [diff] [blame] | 70 | conn, err := nanoswitchPortMap.DialGRPC(common.DebugServicePort, grpc.WithInsecure(), |
Lorenz Brun | 52f7f29 | 2020-06-24 16:42:02 +0200 | [diff] [blame] | 71 | grpc.WithUnaryInterceptor(grpcretry.UnaryClientInterceptor(opts...))) |
| 72 | if err != nil { |
| 73 | panic(err) |
| 74 | } |
| 75 | defer conn.Close() |
Serge Bazanski | 57b4375 | 2020-07-13 19:17:48 +0200 | [diff] [blame] | 76 | debug := apb.NewNodeDebugServiceClient(conn) |
| 77 | res, err := debug.GetGoldenTicket(ctx, &apb.GetGoldenTicketRequest{ |
| 78 | // HACK: this is assigned by DHCP, and we assume that everything goes well. |
| 79 | ExternalIp: "10.1.0.3", |
| 80 | }, grpcretry.WithMax(10)) |
| 81 | if err != nil { |
| 82 | log.Fatalf("Failed to get golden ticket: %v", err) |
| 83 | } |
| 84 | |
| 85 | ec := &apb.EnrolmentConfig{ |
| 86 | GoldenTicket: res.Ticket, |
| 87 | } |
| 88 | |
| 89 | if err := launch.Launch(ctx, launch.Options{ConnectToSocket: vm1, EnrolmentConfig: ec, SerialPort: os.Stdout}); err != nil { |
| 90 | log.Fatalf("Failed to launch vm1: %v", err) |
| 91 | } |
Lorenz Brun | 52f7f29 | 2020-06-24 16:42:02 +0200 | [diff] [blame] | 92 | }() |
| 93 | if err := launch.RunMicroVM(ctx, &launch.MicroVMOptions{ |
| 94 | SerialPort: os.Stdout, |
Serge Bazanski | 77cb6c5 | 2020-12-19 00:09:22 +0100 | [diff] [blame] | 95 | KernelPath: "metropolis/test/ktest/linux-testing.elf", |
| 96 | InitramfsPath: "metropolis/test/nanoswitch/initramfs.lz4", |
Lorenz Brun | 52f7f29 | 2020-06-24 16:42:02 +0200 | [diff] [blame] | 97 | ExtraNetworkInterfaces: []*os.File{sw0, sw1}, |
| 98 | PortMap: nanoswitchPortMap, |
| 99 | }); err != nil { |
| 100 | log.Fatalf("Failed to launch nanoswitch: %v", err) |
| 101 | } |
| 102 | } |