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 { |
Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 28 | // TODO(#253): Tell Supervisor to shut down gracefully and reboot |
| 29 | go func() { |
| 30 | time.Sleep(10 * time.Second) |
| 31 | unix.Sync() |
Lorenz Brun | d14be0e | 2023-07-31 16:46:14 +0200 | [diff] [blame^] | 32 | if req.ActivationMode == apb.ActivationMode_ACTIVATION_KEXEC { |
| 33 | unix.Reboot(unix.LINUX_REBOOT_CMD_KEXEC) |
| 34 | } else { |
| 35 | unix.Reboot(unix.LINUX_REBOOT_CMD_RESTART) |
| 36 | } |
Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 37 | }() |
| 38 | } |
Lorenz Brun | d14be0e | 2023-07-31 16:46:14 +0200 | [diff] [blame^] | 39 | |
Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 40 | return &apb.UpdateNodeResponse{}, nil |
| 41 | } |