| Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 1 | package mgmt |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "time" |
| 6 | |
| Lorenz Brun | 1e90c6d | 2024-02-19 22:21:01 +0100 | [diff] [blame] | 7 | "github.com/vishvananda/netlink" |
| Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 8 | "golang.org/x/sys/unix" |
| 9 | "google.golang.org/grpc/codes" |
| 10 | "google.golang.org/grpc/status" |
| 11 | |
| 12 | apb "source.monogon.dev/metropolis/proto/api" |
| 13 | ) |
| 14 | |
| 15 | func (s *Service) UpdateNode(ctx context.Context, req *apb.UpdateNodeRequest) (*apb.UpdateNodeResponse, error) { |
| Lorenz Brun | d14be0e | 2023-07-31 16:46:14 +0200 | [diff] [blame] | 16 | ok := s.updateMutex.TryLock() |
| 17 | if ok { |
| 18 | defer s.updateMutex.Unlock() |
| 19 | } else { |
| 20 | return nil, status.Error(codes.Aborted, "another UpdateNode RPC is in progress on this node") |
| 21 | } |
| 22 | if req.ActivationMode == apb.ActivationMode_ACTIVATION_INVALID { |
| 23 | return nil, status.Errorf(codes.InvalidArgument, "activation_mode needs to be explicitly specified") |
| 24 | } |
| 25 | if err := s.UpdateService.InstallBundle(ctx, req.BundleUrl, req.ActivationMode == apb.ActivationMode_ACTIVATION_KEXEC); err != nil { |
| Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 26 | return nil, status.Errorf(codes.Unavailable, "error installing update: %v", err) |
| 27 | } |
| Lorenz Brun | d14be0e | 2023-07-31 16:46:14 +0200 | [diff] [blame] | 28 | if req.ActivationMode != apb.ActivationMode_ACTIVATION_NONE { |
| Tim Windelschmidt | 45d6f18 | 2023-08-07 13:19:41 +0000 | [diff] [blame] | 29 | |
| 30 | methodString, method := "reboot", unix.LINUX_REBOOT_CMD_RESTART |
| 31 | if req.ActivationMode == apb.ActivationMode_ACTIVATION_KEXEC { |
| 32 | methodString = "kexec" |
| 33 | method = unix.LINUX_REBOOT_CMD_KEXEC |
| 34 | } |
| 35 | |
| 36 | s.LogTree.MustLeveledFor("update").Infof("activating update with method: %s", methodString) |
| 37 | |
| Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 38 | go func() { |
| Tim Windelschmidt | 45d6f18 | 2023-08-07 13:19:41 +0000 | [diff] [blame] | 39 | // TODO(#253): Tell Supervisor to shut down gracefully and reboot |
| Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 40 | time.Sleep(10 * time.Second) |
| Tim Windelschmidt | 45d6f18 | 2023-08-07 13:19:41 +0000 | [diff] [blame] | 41 | s.LogTree.MustLeveledFor("update").Info("activating now...") |
| Lorenz Brun | b80b844 | 2023-08-03 17:40:17 +0200 | [diff] [blame] | 42 | unix.Unmount(s.UpdateService.ESPPath, 0) |
| Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 43 | unix.Sync() |
| Lorenz Brun | 1e90c6d | 2024-02-19 22:21:01 +0100 | [diff] [blame] | 44 | disableNetworkInterfaces() |
| Tim Windelschmidt | 45d6f18 | 2023-08-07 13:19:41 +0000 | [diff] [blame] | 45 | unix.Reboot(method) |
| Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 46 | }() |
| 47 | } |
| Lorenz Brun | d14be0e | 2023-07-31 16:46:14 +0200 | [diff] [blame] | 48 | |
| Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 49 | return &apb.UpdateNodeResponse{}, nil |
| 50 | } |
| Lorenz Brun | 1e90c6d | 2024-02-19 22:21:01 +0100 | [diff] [blame] | 51 | |
| 52 | // For kexec it's recommended to disable all physical network interfaces |
| 53 | // before doing it. This function doesn't return any errors as it's best- |
| 54 | // effort anyways as we cannot reliably log the error anymore. |
| 55 | func disableNetworkInterfaces() { |
| 56 | links, err := netlink.LinkList() |
| 57 | if err != nil { |
| 58 | return |
| 59 | } |
| 60 | for _, link := range links { |
| 61 | d, ok := link.(*netlink.Device) |
| 62 | if !ok { |
| 63 | continue |
| 64 | } |
| 65 | if err := netlink.LinkSetDown(d); err != nil { |
| 66 | continue |
| 67 | } |
| 68 | } |
| 69 | } |