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

Fixes the force deletion #15

Merged
merged 1 commit into from
May 23, 2024
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
2 changes: 1 addition & 1 deletion app/lima.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func StartLimaVM(vmName string, wg *sync.WaitGroup, errCh chan<- error) {
func DeleteLimaVM(vmName string, force bool, wg *sync.WaitGroup, errCh chan<- error) {
defer wg.Done()

cmd := exec.Command("limactl", "delete", "-f", vmName)
cmd := exec.Command("limactl", "delete", vmName)

if force {
// Force destroy the VMs
Expand Down
61 changes: 38 additions & 23 deletions cmd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,27 @@ var destroyCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
//fmt.Println("destroy called")

instances := lima.GetInstancesByPrefix(name)
runningInstances := lima.GetInstancesByStatus(instances, "running")
allInstances := lima.GetInstancesByPrefix(name)

if len(runningInstances) != 0 {
fmt.Println("There are running instances in the cluster, cannot destroy!")
if len(allInstances) == 0 {
fmt.Printf("No instances in the cluster %s\n", name)
return
}

stoppedInstances := lima.GetInstancesByStatus(instances, "stopped")

var wg sync.WaitGroup
errCh := make(chan error, len(stoppedInstances))

// Stop Lima VMs concurrently
for _, vmName := range stoppedInstances {
wg.Add(1)
go lima.DeleteLimaVM(vmName.Name, force, &wg, errCh)
// @TODO - Serialize properly
time.Sleep(10 * time.Second)
if force {
destroyVM(allInstances, true)
return
}

// Wait for all goroutines to finish
wg.Wait()

// Close error channel after all goroutines are done
close(errCh)
runningInstances := lima.GetInstancesByStatus(allInstances, "running")
if len(runningInstances) > 0 {
fmt.Println("There are running instances in the cluster, cannot destroy!")
return
}

// Check for any errors reported by the goroutines
for err := range errCh {
fmt.Println(err)
stoppedInstances := lima.GetInstancesByStatus(allInstances, "stopped")
if len(allInstances) == len(stoppedInstances) {
destroyVM(allInstances, false)
}
},
}
Expand All @@ -76,3 +67,27 @@ func init() {
destroyCmd.Flags().StringVarP(&name, "name", "n", "shikari", "name of the cluster")
destroyCmd.Flags().BoolVarP(&force, "force", "f", false, "force destruction of the cluster even when VMs are running")
}

func destroyVM(instances []lima.LimaVM, force bool) {
var wg sync.WaitGroup
errCh := make(chan error, len(instances))

// Stop Lima VMs concurrently
for i, vmName := range instances {
wg.Add(1)
fmt.Println("Nth GoRoutine", i)
go lima.DeleteLimaVM(vmName.Name, force, &wg, errCh)
time.Sleep(2 * time.Second) //delay the goroutine to avoid errors
}

// Wait for all goroutines to finish
wg.Wait()

// Close error channel after all goroutines are done
close(errCh)

// Check for any errors reported by the goroutines
for err := range errCh {
fmt.Println(err)
}
}