Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

close stream, check size, and fix backingstore and other minor size assignement #478

Merged
merged 2 commits into from
Dec 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions libvirt/cloudinit_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ func readIso9660File(file os.FileInfo) ([]byte, error) {
// pointer when you are done.
func downloadISO(virConn *libvirt.Connect, volume libvirt.StorageVol) (*os.File, error) {
// get Volume info (required to get size later)
var bytesCopied int64

info, err := volume.GetInfo()
if err != nil {
return nil, fmt.Errorf("Error retrieving info for volume: %s", err)
Expand All @@ -320,18 +322,36 @@ func downloadISO(virConn *libvirt.Connect, volume libvirt.StorageVol) (*os.File,
if err != nil {
return tmpFile, err
}
defer stream.Finish()

volume.Download(stream, 0, info.Capacity, 0)
defer func() {
stream.Free()
}()

err = volume.Download(stream, 0, info.Capacity, 0)
if err != nil {
stream.Abort()
return tmpFile, fmt.Errorf("Error by downloading content to libvirt volume:%s", err)
}
sio := NewStreamIO(*stream)

n, err := io.Copy(tmpFile, sio)
bytesCopied, err = io.Copy(tmpFile, sio)
if err != nil {
return tmpFile, fmt.Errorf("Error while copying remote volume to local disk: %s", err)
}

if uint64(bytesCopied) != info.Capacity {
stream.Abort()
return tmpFile, fmt.Errorf("Error while copying remote volume to local disk, bytesCopied %d != %d volume.size", bytesCopied, info.Capacity)
}

err = stream.Finish()
if err != nil {
stream.Abort()
return tmpFile, fmt.Errorf("Error by terminating libvirt stream %s", err)
}

tmpFile.Seek(0, 0)
log.Printf("%d bytes downloaded", n)
log.Printf("%d bytes downloaded", bytesCopied)

return tmpFile, nil
}
11 changes: 5 additions & 6 deletions libvirt/resource_libvirt_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,14 @@ func resourceLibvirtVolumeCreate(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("Can't retrieve volume %s: %v", baseVolumeName.(string), err)
}
}

if baseVolume != nil {
backingStoreDef, err := newDefBackingStoreFromLibvirt(baseVolume)
if err != nil {
return fmt.Errorf("Could not retrieve backing store definition: %s", err.Error())
}

// does the backing store have some size information?, check at least that it is not smaller than the backing store
volumeDef.Capacity.Value = uint64(d.Get("size").(int))
if _, ok := d.GetOk("size"); ok {
// does the backing store have some size information?, check at least that it is not smaller than the backing store
volumeDef.Capacity.Value = uint64(d.Get("size").(int))
backingStoreVolumeDef, err := newDefVolumeFromLibvirt(baseVolume)
if err != nil {
return err
Expand All @@ -215,8 +213,9 @@ func resourceLibvirtVolumeCreate(d *schema.ResourceData, meta interface{}) error
volumeDef.BackingStore = &backingStoreDef
}
}

volumeDef.Capacity.Value = uint64(d.Get("size").(int))
if _, ok := d.GetOk("size"); ok {
volumeDef.Capacity.Value = uint64(d.Get("size").(int))
}
data, err := xmlMarshallIndented(volumeDef)
if err != nil {
return fmt.Errorf("Error serializing libvirt volume: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion libvirt/resource_libvirt_volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func TestAccLibvirtVolume_UniqueName(t *testing.T) {
Steps: []resource.TestStep{
{
Config: config,
ExpectError: regexp.MustCompile(`storage volume '` + randomVolumeName + `' already exists`),
ExpectError: regexp.MustCompile(`storage volume '` + randomVolumeName + `' exists already`),
},
},
})
Expand Down
5 changes: 0 additions & 5 deletions libvirt/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,3 @@ func (sio *StreamIO) Read(p []byte) (int, error) {
func (sio *StreamIO) Write(p []byte) (int, error) {
return sio.Stream.Send(p)
}

// Close closes the stream
func (sio *StreamIO) Close() error {
return sio.Stream.Finish()
}
17 changes: 12 additions & 5 deletions libvirt/utils_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,6 @@ func newCopier(virConn *libvirt.Connect, volume *libvirt.StorageVol, size uint64
}

defer func() {
if uint64(bytesCopied) != size {
stream.Abort()
} else {
stream.Finish()
}
stream.Free()
}()

Expand All @@ -255,12 +250,24 @@ func newCopier(virConn *libvirt.Connect, volume *libvirt.StorageVol, size uint64
// if we get unexpected EOF this mean that connection was closed suddently from server side
// the problem is not on the plugin but on server hosting currupted images
if err == io.ErrUnexpectedEOF {
stream.Abort()
return fmt.Errorf("Error: transfer was unexpectedly closed from the server while downloading. Please try again later or check the server hosting sources")
}
if err != nil {
stream.Abort()
return fmt.Errorf("Error while copying source to volume %s", err)
}

log.Printf("%d bytes uploaded\n", bytesCopied)
if uint64(bytesCopied) != size {
stream.Abort()
return fmt.Errorf("Error during volume Upload. BytesCopied: %d != %d volume.size", bytesCopied, size)
}

if err := stream.Finish(); err != nil {
stream.Abort()
return fmt.Errorf("Error by terminating libvirt stream %s", err)
}
return nil
}
return copier
Expand Down