blob: 1e61ef1f0f111964380be6b7ebd458143bd751b7 [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
2// SPDX-License-Identifier: Apache-2.0
3
Lorenz Brun35fcf032023-06-29 04:15:58 +02004package mgmt
5
6import (
7 "context"
Lorenz Brun35fcf032023-06-29 04:15:58 +02008
9 "golang.org/x/sys/unix"
10 "google.golang.org/grpc/codes"
11 "google.golang.org/grpc/status"
12
13 apb "source.monogon.dev/metropolis/proto/api"
14)
15
16func (s *Service) UpdateNode(ctx context.Context, req *apb.UpdateNodeRequest) (*apb.UpdateNodeResponse, error) {
Lorenz Brund14be0e2023-07-31 16:46:14 +020017 ok := s.updateMutex.TryLock()
18 if ok {
19 defer s.updateMutex.Unlock()
20 } else {
21 return nil, status.Error(codes.Aborted, "another UpdateNode RPC is in progress on this node")
22 }
Tim Windelschmidta10d0cb2025-01-13 14:44:15 +010023 if req.ActivationMode == apb.ActivationMode_ACTIVATION_MODE_INVALID {
Lorenz Brund14be0e2023-07-31 16:46:14 +020024 return nil, status.Errorf(codes.InvalidArgument, "activation_mode needs to be explicitly specified")
25 }
Jan Schär62cecde2025-04-16 15:24:04 +000026 if err := s.UpdateService.InstallImage(ctx, req.OsImage, req.ActivationMode == apb.ActivationMode_ACTIVATION_MODE_KEXEC); err != nil {
Lorenz Brun35fcf032023-06-29 04:15:58 +020027 return nil, status.Errorf(codes.Unavailable, "error installing update: %v", err)
28 }
Tim Windelschmidta10d0cb2025-01-13 14:44:15 +010029 if req.ActivationMode != apb.ActivationMode_ACTIVATION_MODE_NONE {
Tim Windelschmidt45d6f182023-08-07 13:19:41 +000030
31 methodString, method := "reboot", unix.LINUX_REBOOT_CMD_RESTART
Tim Windelschmidta10d0cb2025-01-13 14:44:15 +010032 if req.ActivationMode == apb.ActivationMode_ACTIVATION_MODE_KEXEC {
Tim Windelschmidt45d6f182023-08-07 13:19:41 +000033 methodString = "kexec"
34 method = unix.LINUX_REBOOT_CMD_KEXEC
35 }
36
37 s.LogTree.MustLeveledFor("update").Infof("activating update with method: %s", methodString)
Lorenz Bruna036c4e2024-09-10 19:11:57 +020038 s.initiateReboot(method)
Lorenz Brun35fcf032023-06-29 04:15:58 +020039 }
Lorenz Brund14be0e2023-07-31 16:46:14 +020040
Lorenz Brun35fcf032023-06-29 04:15:58 +020041 return &apb.UpdateNodeResponse{}, nil
42}