Пример #1
0
func CreateVM(ctx context.Context, session *session.Session, host *object.HostSystem, name string) (*types.ManagedObjectReference, error) {
	// Create the spec config
	specconfig := test.SpecConfig(session, name)

	// Create a linux guest
	linux, err := guest.NewLinuxGuest(ctx, session, specconfig)
	if err != nil {
		return nil, err
	}

	// Find the Virtual Machine folder that we use
	folders, err := session.Datacenter.Folders(ctx)
	if err != nil {
		return nil, err
	}
	parent := folders.VmFolder

	// Create the vm
	task, err := parent.CreateVM(ctx, *linux.Spec().Spec(), session.Pool, host)
	if err != nil {
		return nil, err
	}
	info, err := task.WaitForResult(ctx, nil)
	if err != nil {
		return nil, err
	}

	moref := info.Result.(types.ManagedObjectReference)
	// Return the moRef
	return &moref, nil
}
Пример #2
0
func (h *Handle) Create(ctx context.Context, sess *session.Session, config *ContainerCreateConfig) error {
	defer trace.End(trace.Begin("Handle.Create"))

	if h.Spec != nil {
		log.Debugf("spec has already been set on handle %p during create of %s", h, config.Metadata.ID)
		return fmt.Errorf("spec already set")
	}

	// update the handle with Metadata
	h.ExecConfig = config.Metadata
	// add create time to config
	h.ExecConfig.Common.Created = time.Now().UTC().Unix()
	// configure with debug
	h.ExecConfig.Diagnostics.DebugLevel = VCHConfig.DebugLevel
	// Convert the management hostname to IP
	ips, err := net.LookupIP(ManagementHostName)
	if err != nil {
		log.Errorf("Unable to look up %s during create of %s: %s", ManagementHostName, config.Metadata.ID, err)
		return err
	}

	if len(ips) == 0 {
		log.Errorf("No IP found for %s during create of %s", ManagementHostName, config.Metadata.ID)
		return fmt.Errorf("No IP found on %s", ManagementHostName)
	}

	if len(ips) > 1 {
		log.Errorf("Multiple IPs found for %s during create of %s: %v", ManagementHostName, config.Metadata.ID, ips)
		return fmt.Errorf("Multiple IPs found on %s: %#v", ManagementHostName, ips)
	}

	URI := fmt.Sprintf("tcp://%s:%d", ips[0], serialOverLANPort)

	//FIXME: remove debug network
	backing, err := VCHConfig.DebugNetwork.EthernetCardBackingInfo(ctx)
	if err != nil {
		detail := fmt.Sprintf("unable to generate backing info for debug network - this code can be removed once network mapping/dhcp client are available: %s", err)
		log.Error(detail)
		return errors.New(detail)
	}
	uuid, err := instanceUUID(config.Metadata.ID)
	if err != nil {
		detail := fmt.Sprintf("unable to get instance UUID: %s", err)
		log.Error(detail)
		return errors.New(detail)
	}
	specconfig := &spec.VirtualMachineConfigSpecConfig{
		// FIXME: hardcoded values
		NumCPUs:  2,
		MemoryMB: 2048,

		ConnectorURI: URI,

		ID:       config.Metadata.ID,
		Name:     config.Metadata.Name,
		BiosUUID: uuid,

		ParentImageID: config.ParentImageID,
		BootMediaPath: VCHConfig.BootstrapImagePath,
		VMPathName:    fmt.Sprintf("[%s]", sess.Datastore.Name()),
		DebugNetwork:  backing,

		ImageStoreName: config.ImageStoreName,
		ImageStorePath: &VCHConfig.ImageStores[0],

		Metadata: config.Metadata,
	}
	log.Debugf("Config: %#v", specconfig)

	// Create a linux guest
	linux, err := guest.NewLinuxGuest(ctx, sess, specconfig)
	if err != nil {
		log.Errorf("Failed during linux specific spec generation during create of %s: %s", config.Metadata.ID, err)
		return err
	}

	h.SetSpec(linux.Spec())
	return nil
}
Пример #3
0
// Create returns a new handle that can be Committed to create a new container.
// At this time the config is *not* deep copied so should not be changed once passed
//
// TODO: either deep copy the configuration, or provide an alternative means of passing the data that
// avoids the need for the caller to unpack/repack the parameters
func Create(ctx context.Context, sess *session.Session, config *ContainerCreateConfig) (*Handle, error) {
	defer trace.End(trace.Begin("Handle.Create"))

	h := &Handle{
		key:         newHandleKey(),
		targetState: StateCreated,
		containerBase: containerBase{
			ExecConfig: config.Metadata,
		},
	}

	// configure with debug
	h.ExecConfig.Diagnostics.DebugLevel = Config.DebugLevel

	// Convert the management hostname to IP
	ips, err := net.LookupIP(constants.ManagementHostName)
	if err != nil {
		log.Errorf("Unable to look up %s during create of %s: %s", constants.ManagementHostName, config.Metadata.ID, err)
		return nil, err
	}

	if len(ips) == 0 {
		log.Errorf("No IP found for %s during create of %s", constants.ManagementHostName, config.Metadata.ID)
		return nil, fmt.Errorf("No IP found on %s", constants.ManagementHostName)
	}

	if len(ips) > 1 {
		log.Errorf("Multiple IPs found for %s during create of %s: %v", constants.ManagementHostName, config.Metadata.ID, ips)
		return nil, fmt.Errorf("Multiple IPs found on %s: %#v", constants.ManagementHostName, ips)
	}

	uuid, err := instanceUUID(config.Metadata.ID)
	if err != nil {
		detail := fmt.Sprintf("unable to get instance UUID: %s", err)
		log.Error(detail)
		return nil, errors.New(detail)
	}

	specconfig := &spec.VirtualMachineConfigSpecConfig{
		NumCPUs:  int32(config.Resources.NumCPUs),
		MemoryMB: config.Resources.MemoryMB,

		ID:       config.Metadata.ID,
		Name:     config.Metadata.Name,
		BiosUUID: uuid,

		ParentImageID: config.ParentImageID,
		BootMediaPath: Config.BootstrapImagePath,
		VMPathName:    fmt.Sprintf("[%s]", sess.Datastore.Name()),

		ImageStoreName: config.ImageStoreName,
		ImageStorePath: &Config.ImageStores[0],

		Metadata: config.Metadata,
	}
	log.Debugf("Config: %#v", specconfig)

	// Create a linux guest
	linux, err := guest.NewLinuxGuest(ctx, sess, specconfig)
	if err != nil {
		log.Errorf("Failed during linux specific spec generation during create of %s: %s", config.Metadata.ID, err)
		return nil, err
	}

	h.Spec = linux.Spec()

	handlesLock.Lock()
	defer handlesLock.Unlock()

	handles.Add(h.key, h)

	return h, nil
}