LoadFromBackup loads from a mounted instance's backup file. If applyProfiles is false, then the profiles property will be cleared to prevent profile enrichment from DB. Then the expanded config and expanded devices from the backup file will be applied to the local config and local devices respective
(s *state.State, projectName string, instancePath string, applyProfiles bool)
| 393 | // Then the expanded config and expanded devices from the backup file will be applied to the local config and |
| 394 | // local devices respectively. This is done to allow an expanded instance to be returned without needing the DB. |
| 395 | func LoadFromBackup(s *state.State, projectName string, instancePath string, applyProfiles bool) (Instance, error) { |
| 396 | var inst Instance |
| 397 | |
| 398 | backupYamlPath := filepath.Join(instancePath, "backup.yaml") |
| 399 | backupConf, err := backup.ParseConfigYamlFile(backupYamlPath) |
| 400 | if err != nil { |
| 401 | return nil, fmt.Errorf("Failed parsing instance backup file from %q: %w", backupYamlPath, err) |
| 402 | } |
| 403 | |
| 404 | instDBArgs, err := backup.ConfigToInstanceDBArgs(s, backupConf, projectName, applyProfiles) |
| 405 | if err != nil { |
| 406 | return nil, err |
| 407 | } |
| 408 | |
| 409 | if !applyProfiles { |
| 410 | // Stop instance.Load() from expanding profile config from DB, and apply expanded config from |
| 411 | // backup file to local config. This way we can still see the devices even if DB not available. |
| 412 | instDBArgs.Config = backupConf.Container.ExpandedConfig |
| 413 | instDBArgs.Devices = deviceConfig.NewDevices(backupConf.Container.ExpandedDevices) |
| 414 | } |
| 415 | |
| 416 | var p *api.Project |
| 417 | err = s.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error { |
| 418 | proj, err := cluster.GetProject(ctx, tx.Tx(), projectName) |
| 419 | if err != nil { |
| 420 | return err |
| 421 | } |
| 422 | |
| 423 | p, err = proj.ToAPI(ctx, tx.Tx()) |
| 424 | if err != nil { |
| 425 | return err |
| 426 | } |
| 427 | |
| 428 | return nil |
| 429 | }) |
| 430 | if err != nil { |
| 431 | return nil, err |
| 432 | } |
| 433 | |
| 434 | inst, err = Load(s, *instDBArgs, *p) |
| 435 | if err != nil { |
| 436 | return nil, fmt.Errorf("Failed loading instance from backup file %q: %w", backupYamlPath, err) |
| 437 | } |
| 438 | |
| 439 | return inst, nil |
| 440 | } |
| 441 | |
| 442 | // DeviceNextInterfaceHWAddr generates a random MAC address. |
| 443 | func DeviceNextInterfaceHWAddr(pattern string) (string, error) { |
no test coverage detected
searching dependent graphs…