| 1 | |
| 2 | |
| 3 | |
| 4 | package client |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "encoding/json" |
| 9 | "errors" |
| 10 | "net" |
| 11 | "time" |
| 12 | |
| 13 | "google.golang.org/protobuf/types/known/emptypb" |
| 14 | |
| 15 | "github.com/lima-vm/lima/v2/pkg/driver" |
| 16 | pb "github.com/lima-vm/lima/v2/pkg/driver/external" |
| 17 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 18 | ) |
| 19 | |
| 20 | func (d *DriverClient) Validate(ctx context.Context) error { |
| 21 | d.logger.Debug("Validating driver for the given config") |
| 22 | |
| 23 | ctx, cancel := context.WithTimeout(ctx, 10*time.Second) |
| 24 | defer cancel() |
| 25 | _, err := d.DriverSvc.Validate(ctx, &emptypb.Empty{}) |
| 26 | if err != nil { |
| 27 | d.logger.WithError(err).Error("Validation failed") |
| 28 | return err |
| 29 | } |
| 30 | |
| 31 | d.logger.Debug("Driver validated successfully") |
| 32 | return nil |
| 33 | } |
| 34 | |
| 35 | func (d *DriverClient) Create(ctx context.Context) error { |
| 36 | d.logger.Debug("Initializing driver instance") |
| 37 | |
| 38 | _, err := d.DriverSvc.Create(ctx, &emptypb.Empty{}) |
| 39 | if err != nil { |
| 40 | d.logger.WithError(err).Error("Initialization failed") |
| 41 | return err |
| 42 | } |
| 43 | |
| 44 | d.logger.Debug("Driver instance initialized successfully") |
| 45 | return nil |
| 46 | } |
| 47 | |
| 48 | func (d *DriverClient) CreateDisk(ctx context.Context) error { |
| 49 | d.logger.Debug("Creating disk for the instance") |
| 50 | |
| 51 | _, err := d.DriverSvc.CreateDisk(ctx, &emptypb.Empty{}) |
| 52 | if err != nil { |
| 53 | d.logger.WithError(err).Error("Disk creation failed") |
| 54 | return err |
| 55 | } |
| 56 | |
| 57 | d.logger.Debug("Disk created successfully") |
| 58 | return nil |
| 59 | } |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | func (d *DriverClient) Start(ctx context.Context) (chan error, error) { |
| 65 | d.logger.Debug("Starting driver instance") |
| 66 | |
| 67 | stream, err := d.DriverSvc.Start(ctx, &emptypb.Empty{}) |
| 68 | if err != nil { |
| 69 | d.logger.WithError(err).Error("Failed to start driver instance") |
| 70 | return nil, err |
| 71 | } |
| 72 | |
| 73 | |
| 74 | |
| 75 | initialResp, err := stream.Recv() |
| 76 | if err != nil { |
| 77 | d.logger.WithError(err).Error("Error receiving initial response from driver start") |
| 78 | return nil, err |
| 79 | } |
| 80 | if !initialResp.Success { |
| 81 | return nil, errors.New(initialResp.Error) |
| 82 | } |
| 83 | |
| 84 | go func() { |
| 85 | <-ctx.Done() |
| 86 | if closeErr := stream.CloseSend(); closeErr != nil { |
| 87 | d.logger.WithError(closeErr).Warn("Failed to close stream") |
| 88 | } |
| 89 | }() |
| 90 | |
| 91 | errCh := make(chan error, 1) |
| 92 | go func() { |
| 93 | for { |
| 94 | respStream, err := stream.Recv() |
| 95 | if err != nil { |
| 96 | d.logger.Infof("Error receiving response from driver: %v", err) |
| 97 | return |
| 98 | } |
| 99 | d.logger.Debugf("Received response: %v", respStream) |
| 100 | if !respStream.Success { |
| 101 | errCh <- errors.New(respStream.Error) |
| 102 | } else { |
| 103 | close(errCh) |
| 104 | return |
| 105 | } |
| 106 | } |
| 107 | }() |
| 108 | |
| 109 | d.logger.Debug("Driver instance started successfully") |
| 110 | return errCh, nil |
| 111 | } |
| 112 | |
| 113 | func (d *DriverClient) Stop(ctx context.Context) error { |
| 114 | d.logger.Debug("Stopping driver instance") |
| 115 | |
| 116 | _, err := d.DriverSvc.Stop(ctx, &emptypb.Empty{}) |
| 117 | if err != nil { |
| 118 | d.logger.WithError(err).Error("Failed to stop driver instance") |
| 119 | return err |
| 120 | } |
| 121 | |
| 122 | d.logger.Debug("Driver instance stopped successfully") |
| 123 | return nil |
| 124 | } |
| 125 | |
| 126 | func (d *DriverClient) Delete(ctx context.Context) error { |
| 127 | d.logger.Debug("Deleting driver instance") |
| 128 | |
| 129 | _, err := d.DriverSvc.Delete(ctx, &emptypb.Empty{}) |
| 130 | if err != nil { |
| 131 | d.logger.WithError(err).Error("Failed to deleted driver instance") |
| 132 | return err |
| 133 | } |
| 134 | |
| 135 | d.logger.Debug("Driver instance deleted successfully") |
| 136 | return nil |
| 137 | } |
| 138 | |
| 139 | func (d *DriverClient) RunGUI(ctx context.Context) error { |
| 140 | d.logger.Debug("Running GUI for the driver instance") |
| 141 | |
| 142 | ctx, cancel := context.WithTimeout(ctx, 10*time.Second) |
| 143 | defer cancel() |
| 144 | |
| 145 | _, err := d.DriverSvc.RunGUI(ctx, &emptypb.Empty{}) |
| 146 | if err != nil { |
| 147 | d.logger.WithError(err).Error("Failed to run GUI") |
| 148 | return err |
| 149 | } |
| 150 | |
| 151 | d.logger.Debug("GUI started successfully") |
| 152 | return nil |
| 153 | } |
| 154 | |
| 155 | func (d *DriverClient) ChangeDisplayPassword(ctx context.Context, password string) error { |
| 156 | d.logger.Debug("Changing display password for the driver instance") |
| 157 | |
| 158 | _, err := d.DriverSvc.ChangeDisplayPassword(ctx, &pb.ChangeDisplayPasswordRequest{ |
| 159 | Password: password, |
| 160 | }) |
| 161 | if err != nil { |
| 162 | d.logger.WithError(err).Error("Failed to change display password") |
| 163 | return err |
| 164 | } |
| 165 | |
| 166 | d.logger.Debug("Display password changed successfully") |
| 167 | return nil |
| 168 | } |
| 169 | |
| 170 | func (d *DriverClient) DisplayConnection(ctx context.Context) (string, error) { |
| 171 | d.logger.Debug("Getting display connection for the driver instance") |
| 172 | |
| 173 | resp, err := d.DriverSvc.GetDisplayConnection(ctx, &emptypb.Empty{}) |
| 174 | if err != nil { |
| 175 | d.logger.WithError(err).Error("Failed to get display connection") |
| 176 | return "", err |
| 177 | } |
| 178 | |
| 179 | d.logger.Debugf("Display connection retrieved: %s", resp.Connection) |
| 180 | return resp.Connection, nil |
| 181 | } |
| 182 | |
| 183 | func (d *DriverClient) CreateSnapshot(ctx context.Context, tag string) error { |
| 184 | d.logger.Debugf("Creating snapshot with tag: %s", tag) |
| 185 | |
| 186 | _, err := d.DriverSvc.CreateSnapshot(ctx, &pb.CreateSnapshotRequest{ |
| 187 | Tag: tag, |
| 188 | }) |
| 189 | if err != nil { |
| 190 | d.logger.WithError(err).Error("Failed to create snapshot") |
| 191 | return err |
| 192 | } |
| 193 | |
| 194 | d.logger.Debugf("Snapshot '%s' created successfully", tag) |
| 195 | return nil |
| 196 | } |
| 197 | |
| 198 | func (d *DriverClient) ApplySnapshot(ctx context.Context, tag string) error { |
| 199 | d.logger.Debugf("Applying snapshot with tag: %s", tag) |
| 200 | |
| 201 | _, err := d.DriverSvc.ApplySnapshot(ctx, &pb.ApplySnapshotRequest{ |
| 202 | Tag: tag, |
| 203 | }) |
| 204 | if err != nil { |
| 205 | d.logger.WithError(err).Error("Failed to apply snapshot") |
| 206 | return err |
| 207 | } |
| 208 | |
| 209 | d.logger.Debugf("Snapshot '%s' applied successfully", tag) |
| 210 | return nil |
| 211 | } |
| 212 | |
| 213 | func (d *DriverClient) DeleteSnapshot(ctx context.Context, tag string) error { |
| 214 | d.logger.Debugf("Deleting snapshot with tag: %s", tag) |
| 215 | |
| 216 | _, err := d.DriverSvc.DeleteSnapshot(ctx, &pb.DeleteSnapshotRequest{ |
| 217 | Tag: tag, |
| 218 | }) |
| 219 | if err != nil { |
| 220 | d.logger.WithError(err).Error("Failed to delete snapshot") |
| 221 | return err |
| 222 | } |
| 223 | |
| 224 | d.logger.Debugf("Snapshot '%s' deleted successfully", tag) |
| 225 | return nil |
| 226 | } |
| 227 | |
| 228 | func (d *DriverClient) ListSnapshots(ctx context.Context) (string, error) { |
| 229 | d.logger.Debug("Listing snapshots") |
| 230 | |
| 231 | resp, err := d.DriverSvc.ListSnapshots(ctx, &emptypb.Empty{}) |
| 232 | if err != nil { |
| 233 | d.logger.WithError(err).Error("Failed to list snapshots") |
| 234 | return "", err |
| 235 | } |
| 236 | |
| 237 | d.logger.Debugf("Snapshots listed successfully: %s", resp.Snapshots) |
| 238 | return resp.Snapshots, nil |
| 239 | } |
| 240 | |
| 241 | func (d *DriverClient) ForwardGuestAgent(ctx context.Context) bool { |
| 242 | d.logger.Debug("Checking if guest agent needs to be forwarded") |
| 243 | |
| 244 | ctx, cancel := context.WithTimeout(ctx, 10*time.Second) |
| 245 | defer cancel() |
| 246 | |
| 247 | resp, err := d.DriverSvc.ForwardGuestAgent(ctx, &emptypb.Empty{}) |
| 248 | if err != nil { |
| 249 | d.logger.WithError(err).Error("Failed to check guest agent forwarding") |
| 250 | return false |
| 251 | } |
| 252 | |
| 253 | return resp.ShouldForward |
| 254 | } |
| 255 | |
| 256 | func (d *DriverClient) GuestAgentConn(ctx context.Context) (net.Conn, string, error) { |
| 257 | d.logger.Info("Getting guest agent connection") |
| 258 | _, err := d.DriverSvc.GuestAgentConn(ctx, &emptypb.Empty{}) |
| 259 | if err != nil { |
| 260 | d.logger.WithError(err).Error("Failed to get guest agent connection") |
| 261 | return nil, "", err |
| 262 | } |
| 263 | |
| 264 | return nil, "", nil |
| 265 | } |
| 266 | |
| 267 | func (d *DriverClient) Info(ctx context.Context) driver.Info { |
| 268 | d.logger.Debug("Getting driver info") |
| 269 | |
| 270 | ctx, cancel := context.WithTimeout(ctx, 10*time.Second) |
| 271 | defer cancel() |
| 272 | |
| 273 | resp, err := d.DriverSvc.Info(ctx, &emptypb.Empty{}) |
| 274 | if err != nil { |
| 275 | d.logger.WithError(err).Error("Failed to get driver info") |
| 276 | return driver.Info{} |
| 277 | } |
| 278 | |
| 279 | var info driver.Info |
| 280 | if err := json.Unmarshal(resp.InfoJson, &info); err != nil { |
| 281 | d.logger.WithError(err).Error("Failed to unmarshal driver info") |
| 282 | return driver.Info{} |
| 283 | } |
| 284 | |
| 285 | d.logger.Debugf("Driver info retrieved: %+v", info) |
| 286 | return info |
| 287 | } |
| 288 | |
| 289 | func (d *DriverClient) Configure(ctx context.Context, inst *limatype.Instance) (*driver.ConfiguredDriver, error) { |
| 290 | d.logger.Debugf("Setting config for instance %s with SSH local port %d", inst.Name, inst.SSHLocalPort) |
| 291 | |
| 292 | instJSON, err := inst.MarshalJSON() |
| 293 | if err != nil { |
| 294 | d.logger.WithError(err).Error("Failed to marshal instance config") |
| 295 | return nil, err |
| 296 | } |
| 297 | |
| 298 | ctx, cancel := context.WithTimeout(ctx, 10*time.Second) |
| 299 | defer cancel() |
| 300 | |
| 301 | resp, err := d.DriverSvc.Configure(ctx, &pb.SetConfigRequest{ |
| 302 | InstanceConfigJson: instJSON, |
| 303 | }) |
| 304 | if err != nil { |
| 305 | d.logger.WithError(err).Error("Failed to set config") |
| 306 | return nil, err |
| 307 | } |
| 308 | |
| 309 | if err := inst.UnmarshalJSON(resp.InstanceConfigJson); err != nil { |
| 310 | d.logger.WithError(err).Error("Failed to unmarshal updated instance config") |
| 311 | return nil, err |
| 312 | } |
| 313 | |
| 314 | d.logger.Debugf("Config set successfully for instance %s", inst.Name) |
| 315 | return &driver.ConfiguredDriver{ |
| 316 | Driver: d, |
| 317 | }, nil |
| 318 | } |
| 319 | |
| 320 | func (d *DriverClient) InspectStatus(_ context.Context, _ *limatype.Instance) string { |
| 321 | return "" |
| 322 | } |
| 323 | |
| 324 | func (d *DriverClient) SSHAddress(ctx context.Context) (string, error) { |
| 325 | d.logger.Debug("Getting SSH address for the driver instance") |
| 326 | |
| 327 | resp, err := d.DriverSvc.SSHAddress(ctx, &emptypb.Empty{}) |
| 328 | if err != nil { |
| 329 | d.logger.WithError(err).Error("Failed to get SSH address") |
| 330 | return "", err |
| 331 | } |
| 332 | |
| 333 | d.logger.Debugf("SSH address retrieved: %s", resp.Address) |
| 334 | return resp.Address, nil |
| 335 | } |
| 336 | |
| 337 | func (d *DriverClient) BootScripts(ctx context.Context) (map[string][]byte, error) { |
| 338 | d.logger.Debug("Getting boot scripts for the driver instance") |
| 339 | resp, err := d.DriverSvc.BootScripts(ctx, &emptypb.Empty{}) |
| 340 | if err != nil { |
| 341 | d.logger.WithError(err).Error("Failed to get boot scripts") |
| 342 | return nil, err |
| 343 | } |
| 344 | |
| 345 | d.logger.Debugf("Boot scripts retrieved successfully: %d scripts", len(resp.Scripts)) |
| 346 | return resp.Scripts, nil |
| 347 | } |
| 348 | |
| 349 | func (d *DriverClient) AdditionalSetupForSSH(ctx context.Context) error { |
| 350 | d.logger.Debug("Performing additional setup for SSH connection") |
| 351 | |
| 352 | _, err := d.DriverSvc.AdditionalSetupForSSH(ctx, &emptypb.Empty{}) |
| 353 | if err != nil { |
| 354 | d.logger.WithError(err).Error("Failed to perform additional setup for SSH") |
| 355 | return err |
| 356 | } |
| 357 | |
| 358 | d.logger.Debug("Additional setup for SSH completed successfully") |
| 359 | return nil |
| 360 | } |