| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| Serge Bazanski | b40c008 | 2023-03-29 14:28:04 +0200 | [diff] [blame] | 4 | // Package mgmt implements the node-local management service, a.k.a. |
| 5 | // metropolis.proto.api.NodeManagement. |
| 6 | package mgmt |
| 7 | |
| 8 | import ( |
| 9 | "context" |
| 10 | "fmt" |
| 11 | "net" |
| Lorenz Brun | d14be0e | 2023-07-31 16:46:14 +0200 | [diff] [blame] | 12 | "sync" |
| Serge Bazanski | b40c008 | 2023-03-29 14:28:04 +0200 | [diff] [blame] | 13 | |
| 14 | "google.golang.org/grpc" |
| 15 | |
| Jan Schär | 0f8ce4c | 2025-09-04 13:27:50 +0200 | [diff] [blame^] | 16 | "source.monogon.dev/metropolis/node/allocs" |
| Serge Bazanski | b40c008 | 2023-03-29 14:28:04 +0200 | [diff] [blame] | 17 | "source.monogon.dev/metropolis/node/core/identity" |
| 18 | "source.monogon.dev/metropolis/node/core/rpc" |
| Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 19 | "source.monogon.dev/metropolis/node/core/update" |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame] | 20 | "source.monogon.dev/osbase/logtree" |
| 21 | "source.monogon.dev/osbase/supervisor" |
| Serge Bazanski | b40c008 | 2023-03-29 14:28:04 +0200 | [diff] [blame] | 22 | |
| 23 | apb "source.monogon.dev/metropolis/proto/api" |
| 24 | ) |
| 25 | |
| Serge Bazanski | e012b72 | 2023-03-29 17:49:04 +0200 | [diff] [blame] | 26 | // Service implements metropolis.proto.api.NodeManagement. |
| Serge Bazanski | b40c008 | 2023-03-29 14:28:04 +0200 | [diff] [blame] | 27 | type Service struct { |
| Serge Bazanski | e012b72 | 2023-03-29 17:49:04 +0200 | [diff] [blame] | 28 | // NodeCredentials used to set up gRPC server. |
| Serge Bazanski | b40c008 | 2023-03-29 14:28:04 +0200 | [diff] [blame] | 29 | NodeCredentials *identity.NodeCredentials |
| Serge Bazanski | e012b72 | 2023-03-29 17:49:04 +0200 | [diff] [blame] | 30 | // LogTree from which NodeManagement.Logs will be served. |
| 31 | LogTree *logtree.LogTree |
| Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 32 | // Update service handle for performing updates via the API. |
| 33 | UpdateService *update.Service |
| Lorenz Brun | d14be0e | 2023-07-31 16:46:14 +0200 | [diff] [blame] | 34 | // Serialized UpdateNode RPCs |
| 35 | updateMutex sync.Mutex |
| Serge Bazanski | e012b72 | 2023-03-29 17:49:04 +0200 | [diff] [blame] | 36 | |
| 37 | // Automatically populated on Run. |
| 38 | LogService |
| Serge Bazanski | b40c008 | 2023-03-29 14:28:04 +0200 | [diff] [blame] | 39 | } |
| 40 | |
| Serge Bazanski | e012b72 | 2023-03-29 17:49:04 +0200 | [diff] [blame] | 41 | // Run the Servie as a supervisor runnable. |
| Serge Bazanski | b40c008 | 2023-03-29 14:28:04 +0200 | [diff] [blame] | 42 | func (s *Service) Run(ctx context.Context) error { |
| Serge Bazanski | e012b72 | 2023-03-29 17:49:04 +0200 | [diff] [blame] | 43 | if s.NodeCredentials == nil { |
| 44 | return fmt.Errorf("NodeCredentials missing") |
| 45 | } |
| 46 | if s.LogTree == nil { |
| 47 | return fmt.Errorf("LogTree missing") |
| 48 | } |
| 49 | |
| 50 | s.LogService.LogTree = s.LogTree |
| 51 | |
| Serge Bazanski | b40c008 | 2023-03-29 14:28:04 +0200 | [diff] [blame] | 52 | sec := rpc.ServerSecurity{ |
| 53 | NodeCredentials: s.NodeCredentials, |
| 54 | } |
| 55 | logger := supervisor.MustSubLogger(ctx, "rpc") |
| 56 | opts := sec.GRPCOptions(logger) |
| Jan Schär | 0f8ce4c | 2025-09-04 13:27:50 +0200 | [diff] [blame^] | 57 | lis, err := net.Listen("tcp", fmt.Sprintf(":%d", allocs.PortNodeManagement)) |
| Serge Bazanski | b40c008 | 2023-03-29 14:28:04 +0200 | [diff] [blame] | 58 | if err != nil { |
| 59 | return fmt.Errorf("failed to listen on node management socket socket: %w", err) |
| 60 | } |
| 61 | defer lis.Close() |
| 62 | |
| 63 | srv := grpc.NewServer(opts...) |
| 64 | apb.RegisterNodeManagementServer(srv, s) |
| 65 | |
| 66 | runnable := supervisor.GRPCServer(srv, lis, false) |
| 67 | if err := supervisor.Run(ctx, "server", runnable); err != nil { |
| 68 | return fmt.Errorf("could not run server: %w", err) |
| 69 | } |
| 70 | supervisor.Signal(ctx, supervisor.SignalHealthy) |
| 71 | <-ctx.Done() |
| 72 | return ctx.Err() |
| 73 | } |