74 lines
2.6 KiB
Go
74 lines
2.6 KiB
Go
package app
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/Nwflower/reims-go/internal/driver"
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
"github.com/Nwflower/reims-go/internal/vmstart"
|
|
)
|
|
|
|
// ListSnapshots returns all driver-owned restore points for a VM, newest first.
|
|
func (a *App) ListSnapshots(name string) ([]driver.DiskSnapshot, error) {
|
|
return a.ListSnapshotsWithDriver("", name)
|
|
}
|
|
func (a *App) ListSnapshotsWithDriver(driverID, name string) ([]driver.DiskSnapshot, error) {
|
|
a.mu.Lock()
|
|
svc := a.lifecycleSvc
|
|
a.mu.Unlock()
|
|
if svc == nil {
|
|
return nil, errors.New("driver lifecycle is not initialized")
|
|
}
|
|
return svc.ListSnapshots(a.baseCtx(), driver.VMRef{DriverID: driverID, Name: name})
|
|
}
|
|
|
|
func (a *App) CreateSnapshot(name, snapName string) error {
|
|
return a.CreateSnapshotWithDriver("", name, snapName)
|
|
}
|
|
func (a *App) CreateSnapshotWithDriver(driverID, name, snapName string) error {
|
|
return a.runSnapshotOpWithDriver(driverID, name, "create", snapName, "Created restore point")
|
|
}
|
|
func (a *App) RestoreSnapshot(name, snapName string) error {
|
|
return a.RestoreSnapshotWithDriver("", name, snapName)
|
|
}
|
|
func (a *App) RestoreSnapshotWithDriver(driverID, name, snapName string) error {
|
|
return a.runSnapshotOpWithDriver(driverID, name, "restore", snapName, "Restored restore point")
|
|
}
|
|
func (a *App) DeleteSnapshot(name, snapName string) error {
|
|
return a.DeleteSnapshotWithDriver("", name, snapName)
|
|
}
|
|
func (a *App) DeleteSnapshotWithDriver(driverID, name, snapName string) error {
|
|
return a.runSnapshotOpWithDriver(driverID, name, "delete", snapName, "Deleted restore point")
|
|
}
|
|
|
|
func (a *App) runSnapshotOpWithDriver(driverID, name, operation, snapName, verb string) error {
|
|
loc := a.locale()
|
|
cleanSnap := strings.TrimSpace(snapName)
|
|
if cleanSnap == "" {
|
|
return i18n.NewCodedError(loc, "E_SNAPSHOT_NAME_EMPTY", "err.snapshot.name_empty", nil, nil)
|
|
}
|
|
if len([]rune(cleanSnap)) > 64 {
|
|
return i18n.NewCodedError(loc, "E_SNAPSHOT_NAME_TOO_LONG", "err.snapshot.name_too_long", nil, nil)
|
|
}
|
|
if _, err := driver.ValidateSnapshotName(cleanSnap); err != nil {
|
|
return i18n.NewCodedError(loc, "E_SNAPSHOT_NAME_INVALID", "err.snapshot.name_invalid", nil, err)
|
|
}
|
|
a.mu.Lock()
|
|
svc := a.lifecycleSvc
|
|
a.mu.Unlock()
|
|
if svc == nil {
|
|
return errors.New("driver lifecycle is not initialized")
|
|
}
|
|
ref := driver.VMRef{DriverID: driverID, Name: name}
|
|
w := vmstart.LineWriter(func(line string) { a.emitVMLogForDriver(driverID, name, line) })
|
|
if err := svc.RunSnapshot(a.baseCtx(), ref, operation, cleanSnap, w, w); err != nil {
|
|
return err
|
|
}
|
|
a.emitVMLogForDriver(driverID, name, fmt.Sprintf("%s %q", verb, cleanSnap))
|
|
a.wakePoll()
|
|
a.emitLifecycleStateChanged()
|
|
return nil
|
|
}
|