| 1 | |
| 2 | |
| 3 | |
| 4 | package server |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "encoding/json" |
| 9 | "maps" |
| 10 | "net" |
| 11 | "path/filepath" |
| 12 | |
| 13 | "google.golang.org/grpc/codes" |
| 14 | "google.golang.org/grpc/status" |
| 15 | "google.golang.org/protobuf/types/known/emptypb" |
| 16 | |
| 17 | "github.com/lima-vm/lima/v2/pkg/bicopy" |
| 18 | pb "github.com/lima-vm/lima/v2/pkg/driver/external" |
| 19 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 20 | "github.com/lima-vm/lima/v2/pkg/limatype/filenames" |
| 21 | ) |
| 22 | |
| 23 | func (s *DriverServer) Start(_ *emptypb.Empty, stream pb.Driver_StartServer) error { |
| 24 | s.logger.Debug("Received Start request") |
| 25 | errChan, err := s.driver.Start(stream.Context()) |
| 26 | if err != nil { |
| 27 | s.logger.WithError(err).Error("Start failed") |
| 28 | if sendErr := stream.Send(&pb.StartResponse{Success: false, Error: err.Error()}); sendErr != nil { |
| 29 | s.logger.WithError(sendErr).Error("Failed to send error response") |
| 30 | return status.Errorf(codes.Internal, "failed to send error response: %v", sendErr) |
| 31 | } |
| 32 | return status.Errorf(codes.Internal, "failed to start driver: %v", err) |
| 33 | } |
| 34 | |
| 35 | |
| 36 | |
| 37 | if err := stream.Send(&pb.StartResponse{Success: true}); err != nil { |
| 38 | s.logger.WithError(err).Error("Failed to send success response") |
| 39 | return status.Errorf(codes.Internal, "failed to send success response: %v", err) |
| 40 | } |
| 41 | |
| 42 | for { |
| 43 | select { |
| 44 | case err, ok := <-errChan: |
| 45 | if !ok { |
| 46 | s.logger.Debug("Start error channel closed") |
| 47 | if err := stream.Send(&pb.StartResponse{Success: true}); err != nil { |
| 48 | s.logger.WithError(err).Error("Failed to send success response") |
| 49 | return status.Errorf(codes.Internal, "failed to send success response: %v", err) |
| 50 | } |
| 51 | return nil |
| 52 | } |
| 53 | if err != nil { |
| 54 | s.logger.WithError(err).Error("Error during Start") |
| 55 | if err := stream.Send(&pb.StartResponse{Error: err.Error(), Success: false}); err != nil { |
| 56 | s.logger.WithError(err).Error("Failed to send response") |
| 57 | return status.Errorf(codes.Internal, "failed to send error response: %v", err) |
| 58 | } |
| 59 | } |
| 60 | case <-stream.Context().Done(): |
| 61 | s.logger.Debug("Stream context done, stopping Start") |
| 62 | return nil |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func (s *DriverServer) Configure(ctx context.Context, req *pb.SetConfigRequest) (*pb.SetConfigResponse, error) { |
| 68 | s.logger.Debugf("Received SetConfig request") |
| 69 | var inst limatype.Instance |
| 70 | |
| 71 | if err := inst.UnmarshalJSON(req.InstanceConfigJson); err != nil { |
| 72 | s.logger.WithError(err).Error("Failed to unmarshal InstanceConfigJson") |
| 73 | return nil, err |
| 74 | } |
| 75 | |
| 76 | _, err := s.driver.Configure(ctx, &inst) |
| 77 | if err != nil { |
| 78 | s.logger.WithError(err).Error("Failed to configure driver") |
| 79 | return nil, err |
| 80 | } |
| 81 | |
| 82 | jsonConfig, err := inst.MarshalJSON() |
| 83 | if err != nil { |
| 84 | s.logger.WithError(err).Error("Failed to marshal instance config") |
| 85 | return nil, status.Errorf(codes.Internal, "failed to marshal instance config: %v", err) |
| 86 | } |
| 87 | |
| 88 | s.logger.Debug("Configure succeeded") |
| 89 | return &pb.SetConfigResponse{InstanceConfigJson: jsonConfig}, nil |
| 90 | } |
| 91 | |
| 92 | func (s *DriverServer) GuestAgentConn(ctx context.Context, _ *emptypb.Empty) (*emptypb.Empty, error) { |
| 93 | s.logger.Debug("Received GuestAgentConn request") |
| 94 | conn, connType, err := s.driver.GuestAgentConn(ctx) |
| 95 | if err != nil { |
| 96 | s.logger.WithError(err).Error("GuestAgentConn failed") |
| 97 | return nil, err |
| 98 | } |
| 99 | |
| 100 | if connType != "unix" { |
| 101 | proxySocketPath := filepath.Join(s.driver.Info(ctx).InstanceDir, filenames.GuestAgentSock) |
| 102 | |
| 103 | var lc net.ListenConfig |
| 104 | listener, err := lc.Listen(ctx, "unix", proxySocketPath) |
| 105 | if err != nil { |
| 106 | s.logger.WithError(err).Error("Failed to create proxy socket") |
| 107 | return nil, err |
| 108 | } |
| 109 | |
| 110 | go func() { |
| 111 | defer listener.Close() |
| 112 | defer conn.Close() |
| 113 | |
| 114 | proxyConn, err := listener.Accept() |
| 115 | if err != nil { |
| 116 | s.logger.WithError(err).Error("Failed to accept proxy connection") |
| 117 | return |
| 118 | } |
| 119 | |
| 120 | bicopy.Bicopy(conn, proxyConn, nil) |
| 121 | }() |
| 122 | } |
| 123 | |
| 124 | return &emptypb.Empty{}, nil |
| 125 | } |
| 126 | |
| 127 | func (s *DriverServer) Info(ctx context.Context, _ *emptypb.Empty) (*pb.InfoResponse, error) { |
| 128 | s.logger.Debug("Received GetInfo request") |
| 129 | info := s.driver.Info(ctx) |
| 130 | |
| 131 | infoJSON, err := json.Marshal(info) |
| 132 | if err != nil { |
| 133 | s.logger.WithError(err).Error("Failed to marshal driver info") |
| 134 | return nil, status.Errorf(codes.Internal, "failed to marshal driver info: %v", err) |
| 135 | } |
| 136 | |
| 137 | return &pb.InfoResponse{ |
| 138 | InfoJson: infoJSON, |
| 139 | }, nil |
| 140 | } |
| 141 | |
| 142 | func (s *DriverServer) Validate(ctx context.Context, empty *emptypb.Empty) (*emptypb.Empty, error) { |
| 143 | s.logger.Debugf("Received Validate request") |
| 144 | err := s.driver.Validate(ctx) |
| 145 | if err != nil { |
| 146 | s.logger.WithError(err).Error("Validation failed") |
| 147 | return empty, err |
| 148 | } |
| 149 | s.logger.Debug("Validation succeeded") |
| 150 | return empty, nil |
| 151 | } |
| 152 | |
| 153 | func (s *DriverServer) Create(ctx context.Context, empty *emptypb.Empty) (*emptypb.Empty, error) { |
| 154 | s.logger.Debug("Received Initialize request") |
| 155 | err := s.driver.Create(ctx) |
| 156 | if err != nil { |
| 157 | s.logger.WithError(err).Error("Initialization failed") |
| 158 | return empty, err |
| 159 | } |
| 160 | s.logger.Debug("Initialization succeeded") |
| 161 | return empty, nil |
| 162 | } |
| 163 | |
| 164 | func (s *DriverServer) CreateDisk(ctx context.Context, empty *emptypb.Empty) (*emptypb.Empty, error) { |
| 165 | s.logger.Debug("Received CreateDisk request") |
| 166 | err := s.driver.CreateDisk(ctx) |
| 167 | if err != nil { |
| 168 | s.logger.WithError(err).Error("CreateDisk failed") |
| 169 | return empty, err |
| 170 | } |
| 171 | s.logger.Debug("CreateDisk succeeded") |
| 172 | return empty, nil |
| 173 | } |
| 174 | |
| 175 | func (s *DriverServer) Stop(ctx context.Context, empty *emptypb.Empty) (*emptypb.Empty, error) { |
| 176 | s.logger.Debug("Received Stop request") |
| 177 | err := s.driver.Stop(ctx) |
| 178 | if err != nil { |
| 179 | s.logger.WithError(err).Error("Stop failed") |
| 180 | return empty, err |
| 181 | } |
| 182 | s.logger.Debug("Stop succeeded") |
| 183 | return empty, nil |
| 184 | } |
| 185 | |
| 186 | func (s *DriverServer) RunGUI(ctx context.Context, empty *emptypb.Empty) (*emptypb.Empty, error) { |
| 187 | s.logger.Debug("Received RunGUI request") |
| 188 | err := s.driver.RunGUI(ctx) |
| 189 | if err != nil { |
| 190 | s.logger.WithError(err).Error("RunGUI failed") |
| 191 | return empty, err |
| 192 | } |
| 193 | s.logger.Debug("RunGUI succeeded") |
| 194 | return empty, nil |
| 195 | } |
| 196 | |
| 197 | func (s *DriverServer) Delete(ctx context.Context, empty *emptypb.Empty) (*emptypb.Empty, error) { |
| 198 | s.logger.Debug("Received Delete request") |
| 199 | err := s.driver.Delete(ctx) |
| 200 | if err != nil { |
| 201 | s.logger.WithError(err).Error("Delete failed") |
| 202 | return empty, err |
| 203 | } |
| 204 | s.logger.Debug("Delete succeeded") |
| 205 | return empty, nil |
| 206 | } |
| 207 | |
| 208 | func (s *DriverServer) BootScripts(ctx context.Context, _ *emptypb.Empty) (*pb.BootScriptsResponse, error) { |
| 209 | s.logger.Debug("Received BootScripts request") |
| 210 | scripts, err := s.driver.BootScripts(ctx) |
| 211 | if err != nil { |
| 212 | s.logger.WithError(err).Error("BootScripts failed") |
| 213 | return nil, err |
| 214 | } |
| 215 | |
| 216 | resp := &pb.BootScriptsResponse{ |
| 217 | Scripts: make(map[string][]byte), |
| 218 | } |
| 219 | |
| 220 | maps.Copy(resp.Scripts, scripts) |
| 221 | |
| 222 | s.logger.Debugf("BootScripts succeeded with %d scripts", len(resp.Scripts)) |
| 223 | return resp, nil |
| 224 | } |
| 225 | |
| 226 | func (s *DriverServer) SSHAddress(ctx context.Context, _ *emptypb.Empty) (*pb.SSHAddressResponse, error) { |
| 227 | s.logger.Debug("Received SSHAddress request") |
| 228 | address, err := s.driver.SSHAddress(ctx) |
| 229 | if err != nil { |
| 230 | s.logger.WithError(err).Error("SSHAddress failed") |
| 231 | return nil, err |
| 232 | } |
| 233 | |
| 234 | s.logger.Debugf("SSHAddress succeeded with address: %s", address) |
| 235 | return &pb.SSHAddressResponse{Address: address}, nil |
| 236 | } |
| 237 | |
| 238 | func (s *DriverServer) ChangeDisplayPassword(ctx context.Context, req *pb.ChangeDisplayPasswordRequest) (*emptypb.Empty, error) { |
| 239 | s.logger.Debug("Received ChangeDisplayPassword request") |
| 240 | err := s.driver.ChangeDisplayPassword(ctx, req.Password) |
| 241 | if err != nil { |
| 242 | s.logger.WithError(err).Error("ChangeDisplayPassword failed") |
| 243 | return &emptypb.Empty{}, err |
| 244 | } |
| 245 | s.logger.Debug("ChangeDisplayPassword succeeded") |
| 246 | return &emptypb.Empty{}, nil |
| 247 | } |
| 248 | |
| 249 | func (s *DriverServer) GetDisplayConnection(ctx context.Context, _ *emptypb.Empty) (*pb.GetDisplayConnectionResponse, error) { |
| 250 | s.logger.Debug("Received GetDisplayConnection request") |
| 251 | conn, err := s.driver.DisplayConnection(ctx) |
| 252 | if err != nil { |
| 253 | s.logger.WithError(err).Error("GetDisplayConnection failed") |
| 254 | return nil, err |
| 255 | } |
| 256 | s.logger.Debug("GetDisplayConnection succeeded") |
| 257 | return &pb.GetDisplayConnectionResponse{Connection: conn}, nil |
| 258 | } |
| 259 | |
| 260 | func (s *DriverServer) CreateSnapshot(ctx context.Context, req *pb.CreateSnapshotRequest) (*emptypb.Empty, error) { |
| 261 | s.logger.Debugf("Received CreateSnapshot request with tag: %s", req.Tag) |
| 262 | err := s.driver.CreateSnapshot(ctx, req.Tag) |
| 263 | if err != nil { |
| 264 | s.logger.WithError(err).Error("CreateSnapshot failed") |
| 265 | return &emptypb.Empty{}, err |
| 266 | } |
| 267 | s.logger.Debug("CreateSnapshot succeeded") |
| 268 | return &emptypb.Empty{}, nil |
| 269 | } |
| 270 | |
| 271 | func (s *DriverServer) ApplySnapshot(ctx context.Context, req *pb.ApplySnapshotRequest) (*emptypb.Empty, error) { |
| 272 | s.logger.Debugf("Received ApplySnapshot request with tag: %s", req.Tag) |
| 273 | err := s.driver.ApplySnapshot(ctx, req.Tag) |
| 274 | if err != nil { |
| 275 | s.logger.WithError(err).Error("ApplySnapshot failed") |
| 276 | return &emptypb.Empty{}, err |
| 277 | } |
| 278 | s.logger.Debug("ApplySnapshot succeeded") |
| 279 | return &emptypb.Empty{}, nil |
| 280 | } |
| 281 | |
| 282 | func (s *DriverServer) DeleteSnapshot(ctx context.Context, req *pb.DeleteSnapshotRequest) (*emptypb.Empty, error) { |
| 283 | s.logger.Debugf("Received DeleteSnapshot request with tag: %s", req.Tag) |
| 284 | err := s.driver.DeleteSnapshot(ctx, req.Tag) |
| 285 | if err != nil { |
| 286 | s.logger.WithError(err).Error("DeleteSnapshot failed") |
| 287 | return &emptypb.Empty{}, err |
| 288 | } |
| 289 | s.logger.Debug("DeleteSnapshot succeeded") |
| 290 | return &emptypb.Empty{}, nil |
| 291 | } |
| 292 | |
| 293 | func (s *DriverServer) ListSnapshots(ctx context.Context, _ *emptypb.Empty) (*pb.ListSnapshotsResponse, error) { |
| 294 | s.logger.Debug("Received ListSnapshots request") |
| 295 | snapshots, err := s.driver.ListSnapshots(ctx) |
| 296 | if err != nil { |
| 297 | s.logger.WithError(err).Error("ListSnapshots failed") |
| 298 | return nil, err |
| 299 | } |
| 300 | s.logger.Debug("ListSnapshots succeeded") |
| 301 | return &pb.ListSnapshotsResponse{Snapshots: snapshots}, nil |
| 302 | } |
| 303 | |
| 304 | func (s *DriverServer) ForwardGuestAgent(ctx context.Context, _ *emptypb.Empty) (*pb.ForwardGuestAgentResponse, error) { |
| 305 | s.logger.Debug("Received ForwardGuestAgent request") |
| 306 | return &pb.ForwardGuestAgentResponse{ShouldForward: s.driver.ForwardGuestAgent(ctx)}, nil |
| 307 | } |
| 308 | |
| 309 | func (s *DriverServer) AdditionalSetupForSSH(ctx context.Context, _ *emptypb.Empty) (*emptypb.Empty, error) { |
| 310 | s.logger.Debug("Received AdditionalSetupForSSH request") |
| 311 | err := s.driver.AdditionalSetupForSSH(ctx) |
| 312 | if err != nil { |
| 313 | s.logger.WithError(err).Error("AdditionalSetupForSSH failed") |
| 314 | return &emptypb.Empty{}, err |
| 315 | } |
| 316 | s.logger.Debug("AdditionalSetupForSSH succeeded") |
| 317 | return &emptypb.Empty{}, nil |
| 318 | } |