Exemplo n.º 1
0
Arquivo: power.go Projeto: ankoh/vmlcm
// 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
}
Exemplo n.º 2
0
// Use ensures that <<use>> number of clones exist
// Attention buffer may be nil
func Use(
	buffer *bytes.Buffer,
	vmrun vmware.VmrunWrapper,
	config *util.LCMConfiguration,
	use int) error {
	// Check use parameter
	if use < 0 {
		return fmt.Errorf("Parameter must be >= zero")
	}

	// First check mac addresses
	if len(config.Addresses) < use {
		return fmt.Errorf("Tried to use %d clones with only %d mac addresses",
			use, len(config.Addresses))
	}

	// Then check the number of clones
	// Get all existing clones
	clones, err := getClones(vmrun, config)
	if err != nil {
		return err
	}

	util.TryWrite2Columns(buffer, 20, "Old clones", fmt.Sprint(len(clones)))
	util.TryWrite2Columns(buffer, 20, "New clones", fmt.Sprint(use))

	// Check if number of existing clones equals the parameter (== noop)
	if len(clones) == use {
		util.TryWriteln(buffer, "")
		util.TryWrite2Columns(buffer, 20, "  Nothing to do...", ":'(")
		util.TryWriteln(buffer, "")
		return nil
	}

	// Check if clones need to be created
	if len(clones) < use {
		createdClones, err := cloneUpTo(vmrun, config, clones, use)
		if err != nil {
			return err
		}
		util.TryWriteln(buffer, "")
		for _, createdClone := range createdClones {
			util.TryWrite2Columns(buffer, 20, "  Created Clone", createdClone)
		}
		util.TryWriteln(buffer, "")
	}

	// Check if clones need to be deleted
	if len(clones) > use {
		deletedClones, err := deleteUpTo(vmrun, config, clones, use)
		if err != nil {
			return err
		}
		util.TryWriteln(buffer, "")
		for _, deletedClone := range deletedClones {
			util.TryWrite2Columns(buffer, 20, "  Deleted Clone", deletedClone)
		}
		util.TryWriteln(buffer, "")
	}

	return nil
}