// Stop tries to start all associated VMs func Stop( buffer *bytes.Buffer, vmrun vmware.VmrunWrapper, config *util.LCMConfiguration) error { // Get all existing clones clones, err := getClones(vmrun, config) if err != nil { return err } util.TryWrite2Columns(buffer, 20, "Clones", fmt.Sprint(len(clones))) util.TryWriteln(buffer, "") for _, clone := range clones { if !clone.running { continue } err := vmrun.Stop(clone.path, true) if err != nil { return err } vmName := tryVMNameExtraction(clone.path) util.TryWrite2Columns(buffer, 20, " Stopped Clone", vmName) } util.TryWriteln(buffer, "") return nil }
// Deletes up to <<use>> clones func deleteUpTo( vmrun vmware.VmrunWrapper, config *util.LCMConfiguration, clones []*virtualMachine, use int) ([]string, error) { // First order the clones by status clonesOrdered := orderClonesByRunning(clones) toDelete := int(math.Min( float64(len(clonesOrdered)), math.Abs(float64(len(clones)-use)))) var deletedClones []string // Then shutdown target clones for i := 0; i < toDelete; i++ { clone := clones[i] // First try to stop the clone if it is running if clone.running { // First try a soft stop err := vmrun.Stop(clone.path, false) if err != nil { // then try a hard stop err = vmrun.Stop(clone.path, true) if err != nil { return nil, err } } } // Run Vmrun delete err := vmrun.Delete(clone.path) if err != nil { return nil, err } // Try to find the vmName vmxMatch := vmxName.FindStringSubmatch(clone.path) if len(vmxMatch) != 2 { deletedClones = append(deletedClones, clone.path) } else { vmName := strings.TrimSuffix(vmxMatch[1], ".vmx") deletedClones = append(deletedClones, vmName) } } return deletedClones, nil }