blob: 69b87443c68434d14635dfcc83399ea0243a54a4 [file] [log] [blame]
Lorenz Brun35fcf032023-06-29 04:15:58 +02001package mgmt
2
3import (
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
14func (s *Service) UpdateNode(ctx context.Context, req *apb.UpdateNodeRequest) (*apb.UpdateNodeResponse, error) {
Lorenz Brund14be0e2023-07-31 16:46:14 +020015 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 Brun35fcf032023-06-29 04:15:58 +020025 return nil, status.Errorf(codes.Unavailable, "error installing update: %v", err)
26 }
Lorenz Brund14be0e2023-07-31 16:46:14 +020027 if req.ActivationMode != apb.ActivationMode_ACTIVATION_NONE {
Tim Windelschmidt45d6f182023-08-07 13:19:41 +000028
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 Brun35fcf032023-06-29 04:15:58 +020037 go func() {
Tim Windelschmidt45d6f182023-08-07 13:19:41 +000038 // TODO(#253): Tell Supervisor to shut down gracefully and reboot
Lorenz Brun35fcf032023-06-29 04:15:58 +020039 time.Sleep(10 * time.Second)
Tim Windelschmidt45d6f182023-08-07 13:19:41 +000040 s.LogTree.MustLeveledFor("update").Info("activating now...")
Lorenz Brunb80b8442023-08-03 17:40:17 +020041 unix.Unmount(s.UpdateService.ESPPath, 0)
Lorenz Brun35fcf032023-06-29 04:15:58 +020042 unix.Sync()
Tim Windelschmidt45d6f182023-08-07 13:19:41 +000043 unix.Reboot(method)
Lorenz Brun35fcf032023-06-29 04:15:58 +020044 }()
45 }
Lorenz Brund14be0e2023-07-31 16:46:14 +020046
Lorenz Brun35fcf032023-06-29 04:15:58 +020047 return &apb.UpdateNodeResponse{}, nil
48}