示例#1
0
func (v *vm) Create(attr vms.MachineAttributes) (vms.Machine, error) {
	if waitNextCall {
		time.Sleep(time.Duration(waitDelay) * time.Millisecond)
		waitNextCall = false
		waitDelay = 0
	}
	if failNextCall {
		failNextCall = false
		return nil, failError
	}
	if nilNextCall {
		nilNextCall = false
		return nil, nil
	}
	if attr.Type == nil {
		attr.Type = &defaultMachineType
	}

	t, ok := attr.Type.(*machineType)
	if !ok {
		return nil, errors.New("VM Type not supported")
	}

	new_machine := machine{
		id:          uuid.NewV4().String(),
		name:        attr.Name,
		machineType: *t,
		status:      vms.StatusUp,
	}
	allMachines = append(allMachines, &new_machine)

	return &new_machine, nil
}
示例#2
0
func (v *vm) Create(attr vms.MachineAttributes) (vms.Machine, error) {
	h := sha1.New()
	h.Write([]byte(uuid.NewV4().String()))
	id := hex.EncodeToString(h.Sum(nil))[0:14]

	_, err := os.Stat(path.Join(v.storageDir, "vm", id))
	if err == nil {
		return nil, errors.New("A machine with the same id exists already")
	}

	if !os.IsNotExist(err) {
		log.WithFields(log.Fields{
			"VM": id,
		}).Error(err)
		return nil, errors.New("Unable to check machine id uniqueness")
	}

	if attr.Type == nil {
		attr.Type = defaultType
	}

	t, ok := attr.Type.(*machineType)
	if !ok {
		return nil, errors.New("VM Type not supported")
	}

	iso, err := v.downloadWindowsISO()
	if err != nil {
		return nil, err
	}

	hdd, err := v.createMachineStorage(id, t)
	if err != nil {
		return nil, err
	}

	installISO, err := v.createInstallDisk(id, attr.Password)
	if err != nil {
		return nil, err
	}

	_, err = v.createVmxForSetup(id, attr.Name, t, hdd, iso, installISO)
	if err != nil {
		return nil, err
	}

	m := machine{
		id:         id,
		storageDir: v.storageDir,
	}

	return &m, nil
}