コード例 #1
0
ファイル: lobster.go プロジェクト: LunaNode/cloug
func (lobster *Lobster) CreateInstance(instance *compute.Instance) (*compute.Instance, error) {
	if instance.PublicKey.ID == "" && len(instance.PublicKey.Key) > 0 {
		return common.KeypairServiceCreateWrapper(lobster, lobster, instance)
	}

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

	imageIDInt, err := strconv.Atoi(imageID)
	if err != nil {
		return nil, fmt.Errorf("invalid image ID: %s", imageID)
	}
	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
	}

	var clientOptions api.VmCreateOptions
	if instance.PublicKey.ID != "" {
		clientOptions.KeyId, err = strconv.Atoi(instance.PublicKey.ID)
		if err != nil {
			return nil, fmt.Errorf("invalid key ID: %s", instance.PublicKey.ID)
		}
	}

	vmID, err := lobster.client.VmCreate(name, flavorIDInt, imageIDInt, &clientOptions)
	if err != nil {
		return nil, err
	} else {
		return &compute.Instance{
			ID: strconv.Itoa(vmID),
		}, nil
	}
}
コード例 #2
0
ファイル: vultr.go プロジェクト: LunaNode/cloug
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
	}
}
コード例 #3
0
ファイル: digitalocean.go プロジェクト: LunaNode/cloug
func (do *DigitalOcean) CreateInstance(instance *compute.Instance) (*compute.Instance, error) {
	if instance.PublicKey.ID == "" && len(instance.PublicKey.Key) > 0 {
		return common.KeypairServiceCreateWrapper(do, do, instance)
	}

	imageID, err := do.imageID(&instance.Image)
	if err != nil {
		return nil, err
	}

	flavorID, err := common.GetMatchingFlavorID(do, &instance.Flavor)
	if err != nil {
		return nil, err
	}

	password := instance.Password
	if password == "" {
		password = utils.Uid(16)
	}

	createRequest := &godo.DropletCreateRequest{
		Name:   instance.Name,
		Region: instance.Region,
		Size:   flavorID,
		Image: godo.DropletCreateImage{
			ID: imageID,
		},
		IPv6:              true,
		PrivateNetworking: true,
		UserData:          fmt.Sprintf("#cloud-config\nchpasswd:\n list: |\n  root:%s\n expire: False\n", password),
	}

	if createRequest.Name == "" {
		createRequest.Name = DEFAULT_NAME
	}

	if createRequest.Region == "" {
		createRequest.Region = DEFAULT_REGION
	}

	if instance.PublicKey.ID != "" {
		keyID, err := strconv.Atoi(instance.PublicKey.ID)
		if err != nil {
			return nil, fmt.Errorf("invalid key ID")
		}
		createRequest.SSHKeys = []godo.DropletCreateSSHKey{godo.DropletCreateSSHKey{
			ID: keyID,
		}}
	}

	droplet, _, err := do.client.Droplets.Create(createRequest)

	if err != nil {
		return nil, err
	} else {
		return &compute.Instance{
			ID:       fmt.Sprintf("%d", droplet.ID),
			Name:     droplet.Name,
			Status:   do.mapInstanceStatus(droplet.Status),
			Username: "******",
			Password: password,
		}, nil
	}
}