Exemple #1
0
func (cloug *Cloug) VmCreate(vm *lobster.VirtualMachine, options *lobster.VMIVmCreateOptions) (string, error) {
	tmpl := compute.Instance{
		Name:      vm.Name,
		Region:    cloug.config.Region,
		NetworkID: cloug.config.NetworkID,
		Image:     compute.Image{ID: options.ImageIdentification},
		Flavor: compute.Flavor{
			ID:         vm.Plan.Identification,
			NumCores:   vm.Plan.Cpu,
			DiskGB:     vm.Plan.Storage,
			MemoryMB:   vm.Plan.Ram,
			TransferGB: vm.Plan.Bandwidth,
		},
	}

	if options.SSHKey.Key != "" {
		tmpl.PublicKey = compute.PublicKey{
			Key: []byte(options.SSHKey.Key),
		}
	}

	tmpl.Details = make(map[string]string)
	for k, v := range vm.Plan.Metadata {
		tmpl.Details[k] = v
	}

	instance, err := cloug.service.CreateInstance(&tmpl)

	if err != nil {
		return "", err
	}

	if instance.Username != "" {
		vm.SetMetadata("username", instance.Username)
	}
	if instance.Password != "" {
		vm.SetMetadata("password", instance.Password)
	}

	return instance.ID, nil
}
Exemple #2
0
func (vt *Vultr) CreateInstance(instance *compute.Instance) (*compute.Instance, error) {
	if instance.PublicKey.ID == "" && len(instance.PublicKey.Key) > 0 {
		return common.KeypairServiceCreateWrapper(vt, vt, instance)
	}

	imageID, err := common.GetMatchingImageID(vt, &instance.Image)
	if err != nil {
		return nil, err
	}
	flavorID, err := common.GetMatchingFlavorID(vt, &instance.Flavor)
	if err != nil {
		return nil, err
	}

	flavorIDInt, err := strconv.Atoi(flavorID)
	if err != nil {
		return nil, fmt.Errorf("invalid flavor ID: %s", flavorID)
	}

	name := instance.Name
	if name == "" {
		name = DEFAULT_NAME
	}

	serverOptions := &vultr.ServerOptions{
		PrivateNetworking:    instance.Detail("private_networking", "yes") == "yes",
		IPV6:                 instance.Detail("ipv6", "yes") == "yes",
		AutoBackups:          instance.Detail("auto_backups", "no") == "yes",
		DontNotifyOnActivate: instance.Detail("dont_notify_on_activate", "no") == "yes",
	}

	imageParts := strings.SplitN(imageID, ":", 2)
	if len(imageParts) != 2 {
		return nil, fmt.Errorf("malformed image ID: missing colon")
	}
	if imageParts[0] == "iso" {
		customOSID, err := vt.findOSByName("Custom")
		if err != nil {
			return nil, fmt.Errorf("failed to get custom OS for creation from ISO: %v", err)
		}
		serverOptions.OS = customOSID
		serverOptions.ISO, _ = strconv.Atoi(imageParts[1])
	} else if imageParts[0] == "os" {
		serverOptions.OS, _ = strconv.Atoi(imageParts[1])
	} else if imageParts[0] == "snapshot" {
		snapshotOSID, err := vt.findOSByName("Snapshot")
		if err != nil {
			return nil, fmt.Errorf("failed to get snapshot OS for creation from snapshot: %v", err)
		}
		serverOptions.OS = snapshotOSID
		serverOptions.Snapshot = imageParts[1]
	} else if imageParts[0] == "app" {
		appOSID, err := vt.findOSByName("Application")
		if err != nil {
			return nil, fmt.Errorf("failed to get application OS for creation from application: %v", err)
		}
		serverOptions.OS = appOSID
		serverOptions.Application, _ = strconv.Atoi(imageParts[1])
	} else {
		return nil, fmt.Errorf("invalid image type " + imageParts[0])
	}

	if instance.PublicKey.ID != "" {
		serverOptions.SSHKey = instance.PublicKey.ID
	}

	region := DEFAULT_REGION
	if instance.Region != "" {
		region = instance.Region
	}
	regionID, err := vt.findRegion(region)
	if err != nil {
		return nil, fmt.Errorf("failed to identify region ID: %v", err)
	}

	server, err := vt.client.CreateServer(name, regionID, flavorIDInt, serverOptions)
	if err != nil {
		return nil, err
	} else {
		return &compute.Instance{
			ID: server.ID,
		}, nil
	}
}