github.com/openshift/installer@v1.4.17/pkg/destroy/baremetal/baremetal.go (about) 1 package baremetal 2 3 import ( 4 "fmt" 5 "net/url" 6 7 libvirt "github.com/digitalocean/go-libvirt" 8 "github.com/sirupsen/logrus" 9 10 "github.com/openshift/installer/pkg/destroy/providers" 11 "github.com/openshift/installer/pkg/types" 12 ) 13 14 // ClusterUninstaller holds the various options for the cluster we want to delete. 15 type ClusterUninstaller struct { 16 InfraID string 17 LibvirtURI string 18 BootstrapProvisioningIP string 19 Logger logrus.FieldLogger 20 } 21 22 // Run is the entrypoint to start the uninstall process. 23 func (o *ClusterUninstaller) Run() (*types.ClusterQuota, error) { 24 o.Logger.Debug("Deleting bare metal resources") 25 26 uri, err := url.Parse(o.LibvirtURI) 27 if err != nil { 28 return nil, err 29 } 30 31 virt, err := libvirt.ConnectToURI(uri) 32 if err != nil { 33 return nil, err 34 } 35 defer func() { 36 if err := virt.Disconnect(); err != nil { 37 if o.Logger != nil { 38 o.Logger.Warn("failed to disconnect from libvirt", err) 39 } 40 } 41 }() 42 43 err = o.deleteStoragePool(virt) 44 if err != nil { 45 return nil, fmt.Errorf("failed to clean baremetal bootstrap storage pool: %w", err) 46 } 47 48 return nil, nil 49 } 50 51 // New returns bare metal Uninstaller from ClusterMetadata. 52 func New(logger logrus.FieldLogger, metadata *types.ClusterMetadata) (providers.Destroyer, error) { 53 return &ClusterUninstaller{ 54 InfraID: metadata.InfraID, 55 LibvirtURI: metadata.ClusterPlatformMetadata.BareMetal.LibvirtURI, 56 BootstrapProvisioningIP: metadata.ClusterPlatformMetadata.BareMetal.BootstrapProvisioningIP, 57 Logger: logger, 58 }, nil 59 } 60 61 // deleteStoragePool destroys, deletes and undefines any storagePool left behind during the creation 62 // of the bootstrap VM 63 func (o *ClusterUninstaller) deleteStoragePool(virt *libvirt.Libvirt) error { 64 o.Logger.Debug("Deleting baremetal bootstrap volumes") 65 66 pName := o.InfraID + "-bootstrap" 67 pool, err := virt.StoragePoolLookupByName(pName) 68 if err != nil { 69 o.Logger.Warnf("Unable to get storage pool %s: %s", pName, err) 70 return nil 71 } 72 73 // delete vols 74 vols, err := virt.StoragePoolListVolumes(pool, 0) 75 if err != nil { 76 o.Logger.Warnf("Unable to get volumes in storage pool %s: %s", pName, err) 77 return nil 78 } 79 80 for _, vName := range vols { 81 vol, err := virt.StorageVolLookupByName(pool, vName) 82 if err != nil { 83 o.Logger.Warnf("Unable to get volume %s in storage pool %s: %s", vName, pName, err) 84 return nil 85 } 86 if err := virt.StorageVolDelete(vol, 0); err != nil { 87 o.Logger.Warnf("Unable to delete volume %s in storage pool %s: %s", vName, pName, err) 88 return nil 89 } 90 o.Logger.WithField("volume", vName).Info("Deleted volume") 91 } 92 93 if err := virt.StoragePoolDestroy(pool); err != nil { 94 o.Logger.Warnf("Unable to destroy storage pool %s: %s", pName, err) 95 } 96 97 if err := virt.StoragePoolDelete(pool, 0); err != nil { 98 o.Logger.Warnf("Unable to delete storage pool %s: %s", pName, err) 99 } 100 101 if err := virt.StoragePoolUndefine(pool); err != nil { 102 o.Logger.Warnf("Unable to undefine storage pool %s: %s", pName, err) 103 } 104 o.Logger.WithField("pool", pName).Info("Deleted pool") 105 106 return nil 107 }