Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 1 | package mgmt |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "time" |
| 6 | |
| 7 | "golang.org/x/sys/unix" |
| 8 | "google.golang.org/grpc/codes" |
| 9 | "google.golang.org/grpc/status" |
| 10 | |
| 11 | apb "source.monogon.dev/metropolis/proto/api" |
| 12 | ) |
| 13 | |
| 14 | 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] | 15 | ok := s.updateMutex.TryLock() |
| 16 | if ok { |
| 17 | defer s.updateMutex.Unlock() |
| 18 | } else { |
| 19 | return nil, status.Error(codes.Aborted, "another UpdateNode RPC is in progress on this node") |
| 20 | } |
| 21 | if req.ActivationMode == apb.ActivationMode_ACTIVATION_INVALID { |
| 22 | return nil, status.Errorf(codes.InvalidArgument, "activation_mode needs to be explicitly specified") |
| 23 | } |
| 24 | 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] | 25 | return nil, status.Errorf(codes.Unavailable, "error installing update: %v", err) |
| 26 | } |
Lorenz Brun | d14be0e | 2023-07-31 16:46:14 +0200 | [diff] [blame] | 27 | if req.ActivationMode != apb.ActivationMode_ACTIVATION_NONE { |
Tim Windelschmidt | 45d6f18 | 2023-08-07 13:19:41 +0000 | [diff] [blame] | 28 | |
| 29 | methodString, method := "reboot", unix.LINUX_REBOOT_CMD_RESTART |
| 30 | if req.ActivationMode == apb.ActivationMode_ACTIVATION_KEXEC { |
| 31 | methodString = "kexec" |
| 32 | method = unix.LINUX_REBOOT_CMD_KEXEC |
| 33 | } |
| 34 | |
| 35 | s.LogTree.MustLeveledFor("update").Infof("activating update with method: %s", methodString) |
| 36 | |
Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 37 | go func() { |
Tim Windelschmidt | 45d6f18 | 2023-08-07 13:19:41 +0000 | [diff] [blame] | 38 | // TODO(#253): Tell Supervisor to shut down gracefully and reboot |
Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 39 | time.Sleep(10 * time.Second) |
Tim Windelschmidt | 45d6f18 | 2023-08-07 13:19:41 +0000 | [diff] [blame] | 40 | s.LogTree.MustLeveledFor("update").Info("activating now...") |
Lorenz Brun | b80b844 | 2023-08-03 17:40:17 +0200 | [diff] [blame] | 41 | unix.Unmount(s.UpdateService.ESPPath, 0) |
Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 42 | unix.Sync() |
Tim Windelschmidt | 45d6f18 | 2023-08-07 13:19:41 +0000 | [diff] [blame] | 43 | unix.Reboot(method) |
Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 44 | }() |
| 45 | } |
Lorenz Brun | d14be0e | 2023-07-31 16:46:14 +0200 | [diff] [blame] | 46 | |
Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 47 | return &apb.UpdateNodeResponse{}, nil |
| 48 | } |