You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/agent/hypervisor/backup.go

130 lines
5.3 KiB
Go

package hypervisor
import (
"context"
"errors"
"strings"
"time"
"git.luoli.live/root/virt/agent/objecttransfer"
"git.luoli.live/root/virt/agent/operation"
)
const maxBackupObjectBytes = int64(5 << 40)
const backupLocalCleanupTimeout = 2 * time.Minute
func backupTemplateID(backupKey string) string { return "backup-" + backupKey }
func ValidateBackupRequest(request BackupRequest) error {
if !operationIdentifierPattern.MatchString(request.OperationID) ||
!catalogIDPattern.MatchString(request.BackupKey) ||
!displayNamePattern.MatchString(request.Name) ||
(request.ConsistencyMode != "filesystem" && request.ConsistencyMode != "crash") {
return NewError(ErrInvalid, "invalid_backup", "backup operation id, key, name, or consistency is invalid", nil)
}
if request.ObjectUpload.MaximumSizeBytes > maxBackupObjectBytes {
return NewError(ErrInvalid, "backup_object_too_large", "backup object size limit is too large", nil)
}
if err := objecttransfer.ValidateMultipartPlan(request.ObjectUpload); err != nil {
return NewError(ErrInvalid, "invalid_multipart_plan", "multipart upload plan is invalid", err)
}
return nil
}
func ValidateCleanupBackupWorkspaceRequest(request CleanupBackupWorkspaceRequest) error {
if !operationIdentifierPattern.MatchString(request.OperationID) ||
!operationIdentifierPattern.MatchString(request.PriorOperationID) ||
request.OperationID == request.PriorOperationID ||
(request.CleanupPlanID != "" &&
!sha256Pattern.MatchString(strings.ToLower(request.CleanupPlanID))) {
return NewError(
ErrInvalid, "invalid_backup_workspace_cleanup",
"backup workspace cleanup identity is invalid", nil,
)
}
return nil
}
func ValidateRestoreBackupRequest(request RestoreBackupRequest) error {
if !operationIdentifierPattern.MatchString(request.OperationID) ||
!catalogIDPattern.MatchString(request.BackupKey) ||
request.Debian12NetworkFix == nil {
return NewError(ErrInvalid, "invalid_backup_restore", "backup restore operation id or key is invalid", nil)
}
if request.Source != "" || request.TemplateID != "" || request.TemplateRevision != 0 ||
request.TemplateGenerationToken != "" || request.GuestSSHPublicKey != "" ||
request.RootPassword != "" || request.StartupScript != "" || request.Hostname != "" {
return NewError(ErrInvalid, "invalid_backup_restore", "backup restore cannot replace the source image or guest identity", nil)
}
if request.SourceSizeBytes > maxBackupObjectBytes {
return NewError(ErrInvalid, "backup_object_too_large", "backup object is too large", nil)
}
if err := objecttransfer.ValidateDownload(request.SourceURL, request.SourceSizeBytes, request.SourceSHA256); err != nil {
return NewError(ErrInvalid, "invalid_backup_object", "backup object identity is invalid", err)
}
// Reuse the complete create request validation for resource, network,
// hostname, policy, UUID, and derived-domain constraints. The placeholders
// satisfy only template/credential fields that are intentionally replaced
// by an existing backup disk and credential-preserving NoCloud seed.
candidate := request.CreateVMRequest
candidate.Source = "cloud"
candidate.TemplateID = backupTemplateID(request.BackupKey)
candidate.TemplateRevision = 1
candidate.TemplateGenerationToken = "backup_restore_validation"
candidate.RootPassword = "Restore1!Validation"
if err := ValidateCreateVM(candidate); err != nil {
return err
}
return nil
}
func ValidateRecoverBackupRequest(request RecoverBackupRequest) error {
if !operationIdentifierPattern.MatchString(request.OperationID) ||
!catalogIDPattern.MatchString(request.BackupKey) {
return NewError(ErrInvalid, "invalid_backup_recovery", "backup recovery operation id or key is invalid", nil)
}
if request.SourceSizeBytes > maxBackupObjectBytes {
return NewError(ErrInvalid, "backup_object_too_large", "backup object is too large", nil)
}
if err := objecttransfer.ValidateDownload(request.SourceURL, request.SourceSizeBytes, request.SourceSHA256); err != nil {
return NewError(ErrInvalid, "invalid_backup_object", "backup object identity is invalid", err)
}
return nil
}
func translateObjectTransferError(err error) error {
if err == nil {
return nil
}
var transferErr *objecttransfer.Error
if !errors.As(err, &transferErr) {
return NewError(ErrUnavailable, "object_transfer_failed", "backup object transfer failed", err)
}
code := strings.TrimSpace(transferErr.Code)
if code == "" {
code = "object_transfer_failed"
}
translated := NewError(ErrUnavailable, code, transferErr.Message, transferErr)
if objecttransfer.IsAmbiguous(err) {
return operation.MarkUncertain(translated)
}
return translated
}
func classifyBackupUploadFailure(transferErr, cleanupErr error) error {
if cleanupErr != nil {
return operation.MarkUncertain(errors.Join(transferErr, cleanupErr))
}
// The object transfer was rejected deterministically and the complete local
// snapshot rollback was confirmed. No durable backup effect remains, so a
// new operation must not be fenced behind an inconclusive result.
return operation.MarkNotApplied(transferErr)
}
func backupLocalCleanupContext(parent context.Context) (context.Context, context.CancelFunc) {
// Remote cancellation stops data transfer, but must not prevent the local
// bounded rollback which releases a system-disk-sized temporary snapshot.
return context.WithTimeout(context.WithoutCancel(parent), backupLocalCleanupTimeout)
}