Example #1
0
func (conn VcConn) VMCreate(isoPath string) error {
	log.Infof("Creating virtual machine %s of vCenter %s... ",
		conn.driver.MachineName, conn.driver.IP)

	args := []string{"vm.create"}
	args = conn.AppendConnectionString(args)
	args = append(args, fmt.Sprintf("--net=%s", conn.driver.Network))
	args = append(args, fmt.Sprintf("--dc=%s", conn.driver.Datacenter))
	args = append(args, fmt.Sprintf("--ds=%s", conn.driver.Datastore))
	args = append(args, fmt.Sprintf("--iso=%s", isoPath))
	memory := strconv.Itoa(conn.driver.Memory)
	args = append(args, fmt.Sprintf("--m=%s", memory))
	cpu := strconv.Itoa(conn.driver.CPU)
	args = append(args, fmt.Sprintf("--c=%s", cpu))
	args = append(args, "--disk.controller=pvscsi")
	args = append(args, "--net.adapter=vmxnet3")
	args = append(args, "--on=false")
	if conn.driver.Pool != "" {
		args = append(args, fmt.Sprintf("--pool=%s", conn.driver.Pool))
	}
	if conn.driver.HostIP != "" {
		args = append(args, fmt.Sprintf("--host.ip=%s", conn.driver.HostIP))
	}
	args = append(args, conn.driver.MachineName)
	_, stderr, err := govcOutErr(args...)

	if stderr == "" && err == nil {
		return nil
	}
	return errors.NewVMError("create", conn.driver.MachineName, stderr)
}
Example #2
0
func (conn VcConn) VMInfo() (string, error) {
	args := []string{"vm.info"}
	args = conn.AppendConnectionString(args)
	args = append(args, fmt.Sprintf("--dc=%s", conn.driver.Datacenter))
	args = append(args, conn.driver.MachineName)

	stdout, stderr, err := govcOutErr(args...)
	if strings.Contains(stdout, "Name") && stderr == "" && err == nil {
		return stdout, nil
	}
	return "", errors.NewVMError("find", conn.driver.MachineName, "VM not found")
}
Example #3
0
func (conn VcConn) VMFetchIP() (string, error) {
	args := []string{"vm.ip"}
	args = conn.AppendConnectionString(args)
	args = append(args, fmt.Sprintf("--dc=%s", conn.driver.Datacenter))
	args = append(args, conn.driver.MachineName)
	stdout, stderr, err := govcOutErr(args...)

	if stderr == "" && err == nil {
		return stdout, nil
	}
	return "", errors.NewVMError("fetching IP", conn.driver.MachineName, stderr)
}
Example #4
0
func (conn VcConn) VMAttachNetwork() error {
	args := []string{"vm.network.add"}
	args = conn.AppendConnectionString(args)
	args = append(args, fmt.Sprintf("--dc=%s", conn.driver.Datacenter))
	args = append(args, fmt.Sprintf("--vm=%s", conn.driver.MachineName))
	args = append(args, fmt.Sprintf("--net=%s", conn.driver.Network))

	_, stderr, err := govcOutErr(args...)
	if stderr == "" && err == nil {
		return nil
	}
	return errors.NewVMError("add network", conn.driver.MachineName, stderr)
}
Example #5
0
func (conn VcConn) VMDestroy() error {
	log.Infof("Deleting virtual machine %s of vCenter %s... ",
		conn.driver.MachineName, conn.driver.IP)

	args := []string{"vm.destroy"}
	args = conn.AppendConnectionString(args)
	args = append(args, fmt.Sprintf("--dc=%s", conn.driver.Datacenter))
	args = append(args, conn.driver.MachineName)
	_, stderr, err := govcOutErr(args...)

	if stderr == "" && err == nil {
		return nil
	}
	return errors.NewVMError("delete", conn.driver.MachineName, stderr)
}
Example #6
0
func (conn VcConn) VMDiskCreate() error {
	args := []string{"vm.disk.create"}
	args = conn.AppendConnectionString(args)
	args = append(args, fmt.Sprintf("--dc=%s", conn.driver.Datacenter))
	args = append(args, fmt.Sprintf("--vm=%s", conn.driver.MachineName))
	args = append(args, fmt.Sprintf("--ds=%s", conn.driver.Datastore))
	args = append(args, fmt.Sprintf("--name=%s/%s", conn.driver.MachineName, conn.driver.MachineName))
	diskSize := strconv.Itoa(conn.driver.DiskSize)
	args = append(args, fmt.Sprintf("--size=%sMiB", diskSize))

	_, stderr, err := govcOutErr(args...)
	if stderr == "" && err == nil {
		return nil
	}
	return errors.NewVMError("add network", conn.driver.MachineName, stderr)
}
Example #7
0
func (conn VcConn) VMShutdown() error {
	log.Infof("Powering off virtual machine %s of vCenter %s... ",
		conn.driver.MachineName, conn.driver.IP)

	args := []string{"vm.power"}
	args = conn.AppendConnectionString(args)
	args = append(args, fmt.Sprintf("--dc=%s", conn.driver.Datacenter))
	args = append(args, "-s")
	args = append(args, conn.driver.MachineName)
	_, stderr, err := govcOutErr(args...)

	if stderr == "" && err == nil {
		return nil
	}
	return errors.NewVMError("power on", conn.driver.MachineName, stderr)
}