github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/providers/virtualbox/delete_instance.go (about) 1 package virtualbox 2 3 import ( 4 "github.com/sirupsen/logrus" 5 "github.com/emc-advanced-dev/pkg/errors" 6 "github.com/solo-io/unik/pkg/providers/virtualbox/virtualboxclient" 7 "github.com/solo-io/unik/pkg/types" 8 ) 9 10 func (p *VirtualboxProvider) DeleteInstance(id string, force bool) error { 11 instance, err := p.GetInstance(id) 12 if err != nil { 13 return errors.New("retrieving instance "+id, err) 14 } 15 if instance.State == types.InstanceState_Running { 16 if force { 17 if err := p.StopInstance(instance.Id); err != nil { 18 return errors.New("stopping instance for deletion", err) 19 } 20 } else { 21 return errors.New("instance "+instance.Id+" is still running. try again with --force or power off instance first", err) 22 } 23 } 24 image, err := p.GetImage(instance.ImageId) 25 if err != nil { 26 return errors.New("getting image for instance", err) 27 } 28 29 for controllerPort, deviceMapping := range image.RunSpec.DeviceMappings { 30 if deviceMapping.MountPoint != "/" { 31 logrus.Debugf("using storage controller %s", image.RunSpec.StorageDriver) 32 if err := virtualboxclient.DetachDisk(instance.Id, controllerPort, image.RunSpec.StorageDriver); err != nil { 33 return errors.New("detaching scsi volume from instance", err) 34 } 35 } 36 } 37 if err := virtualboxclient.DestroyVm(instance.Id); err != nil { 38 return errors.New("destroying vm", err) 39 } 40 return p.state.RemoveInstance(instance) 41 }