Beispiel #1
0
// Attempts to find a flavor in options that is similar to target.
// Returns flavor ID if successful, or empty string if no suitable flavor found.
func MatchFlavor(target *compute.Flavor, options []*compute.Flavor) string {
	for _, option := range options {
		if target.Name != "" && target.Name != option.Name {
			continue
		} else if len(option.Regions) > 0 && len(target.Regions) > 0 && !utils.IsSliceSubset(option.Regions, target.Regions) {
			continue
		} else if target.NumCores != 0 && target.NumCores != option.NumCores {
			continue
		} else if target.DiskGB != 0 && target.DiskGB != option.DiskGB {
			continue
		} else if target.MemoryMB != 0 && target.MemoryMB != option.MemoryMB {
			continue
		} else if target.TransferGB != 0 && target.TransferGB != option.TransferGB {
			continue
		}
		return option.ID
	}

	return ""
}
Beispiel #2
0
func (ln *LunaNode) FindImage(image *compute.Image) (string, error) {
	var searchTerms []string
	if image.Distribution != "" {
		searchTerms = append(searchTerms, image.Distribution)

		if image.Version != "" {
			searchTerms = append(searchTerms, image.Version)
		}
	}
	if image.Architecture != "" {
		if image.Architecture == compute.ArchAMD64 {
			searchTerms = append(searchTerms, "64-bit")
		} else if image.Architecture == compute.Archi386 {
			searchTerms = append(searchTerms, "32-bit")
		} else {
			searchTerms = append(searchTerms, string(image.Architecture))
		}
	}
	if image.Type == compute.TemplateImage {
		searchTerms = append(searchTerms, "(template)")
	} else if image.Type == compute.ISOImage {
		searchTerms = append(searchTerms, "(ISO)")
	}

	if len(searchTerms) == 0 {
		searchTerms = []string{"ubuntu", "64-bit", "(template)"}
	}

	apiImages, err := ln.ListImages()
	if err != nil {
		return "", err
	}

	var bestImage *compute.Image
	var bestID int // get highest ID, which corresponds to latest image satisfying specification

	for _, apiImage := range apiImages {
		if !apiImage.Public {
			continue
		}

		// check for search terms
		fail := false
		for _, term := range searchTerms {
			if !strings.Contains(strings.ToLower(apiImage.Name), term) {
				fail = true
				break
			}
		}
		if fail {
			continue
		}

		// verify region requirements are satisfied
		if len(image.Regions) > 0 && len(apiImage.Regions) > 0 && !utils.IsSliceSubset(apiImage.Regions, image.Regions) {
			continue
		}

		imageID, _ := strconv.Atoi(image.ID)

		if bestImage == nil || imageID > bestID {
			bestImage = apiImage
			bestID = imageID
		}
	}

	if bestImage == nil {
		return "", nil
	} else {
		return bestImage.ID, nil
	}
}