diff --git a/motor/providers/gcpinstancesnapshot/provider.go b/motor/providers/gcpinstancesnapshot/provider.go index 29a0f74b22..313198b829 100644 --- a/motor/providers/gcpinstancesnapshot/provider.go +++ b/motor/providers/gcpinstancesnapshot/provider.go @@ -183,12 +183,6 @@ func New(pCfg *providers.Config) (*Provider, error) { return nil, errors.New("invalid target type") } - // attach created disk to the scanner instance - err = sc.attachDisk(scanner.projectID, scanner.zone, scanner.instanceName, mi.diskUrl, mi.deviceName) - if err != nil { - return nil, err - } - errorHandler := func() { // use different err variable to ensure it does not overshadow the real error dErr := sc.detachDisk(scanner.projectID, scanner.zone, scanner.instanceName, mi.deviceName) @@ -202,6 +196,14 @@ func New(pCfg *providers.Config) (*Provider, error) { } } + // attach created disk to the scanner instance + log.Debug().Str("device-name", mi.deviceName).Msg("attach created disk to the scanner instance") + err = sc.attachDisk(scanner.projectID, scanner.zone, scanner.instanceName, mi.diskUrl, mi.deviceName) + if err != nil { + errorHandler() + return nil, err + } + // mount volume shell := []string{"sh", "-c"} volumeMounter := snapshot.NewVolumeMounter(shell) diff --git a/motor/providers/gcpinstancesnapshot/snapshot.go b/motor/providers/gcpinstancesnapshot/snapshot.go index 569c2f7d0e..a017abd77d 100644 --- a/motor/providers/gcpinstancesnapshot/snapshot.go +++ b/motor/providers/gcpinstancesnapshot/snapshot.go @@ -2,6 +2,7 @@ package gcpinstancesnapshot import ( "context" + "encoding/json" "fmt" "net/http" "net/url" @@ -203,7 +204,8 @@ func (sc *SnapshotCreator) createDisk(disk *compute.Disk, projectID, zone, diskN } if operation.Status == "DONE" { if operation.Error != nil { - return clonedDiskUrl, fmt.Errorf("operation failed: %+v", operation.Error.Errors) + data, _ := json.Marshal(operation.Error.Errors) + return clonedDiskUrl, fmt.Errorf("could not create disk: " + string(data)) } clonedDiskUrl = operation.TargetLink break @@ -235,7 +237,7 @@ func (sc *SnapshotCreator) cloneDisk(sourceDisk, projectID, zone, diskName strin return sc.createDisk(disk, projectID, zone, diskName) } -// attachDisk attaches a disk to an instanc +// attachDisk attaches a disk to an instance func (sc *SnapshotCreator) attachDisk(projectID, zone, instanceName, sourceDiskUrl, deviceName string) error { ctx := context.Background() @@ -253,6 +255,7 @@ func (sc *SnapshotCreator) attachDisk(projectID, zone, instanceName, sourceDiskU // attach the disk to the instance op, err := computeService.Instances.AttachDisk(projectID, zone, instanceName, attachedDisk).Context(ctx).Do() if err != nil { + log.Debug().Err(err).Str("device-name", deviceName).Msg("could not attach disk") return err } @@ -260,11 +263,13 @@ func (sc *SnapshotCreator) attachDisk(projectID, zone, instanceName, sourceDiskU for { operation, err := computeService.ZoneOperations.Get(projectID, zone, op.Name).Context(ctx).Do() if err != nil { + log.Debug().Err(err).Str("device-name", deviceName).Msg("could not find operation for disk attachment") return err } if operation.Status == "DONE" { if operation.Error != nil { - return fmt.Errorf("operation failed: %+v", operation.Error.Errors) + data, _ := json.Marshal(operation.Error.Errors) + return fmt.Errorf("disk attachment failed: " + string(data)) } break }