summaryrefslogtreecommitdiffstats
path: root/stream/stream_utils.go
blob: b1ca955641ec06ea8ee780bdaef0310598ce99b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package stream

import "fmt"

// FormatPrefix describes a stream+architecture combination, intended for prepending to error messages
func (st *Stream) FormatPrefix(archname string) string {
	return fmt.Sprintf("%s/%s", st.Stream, archname)
}

// GetArchitecture loads the architecture-specific builds from a stream,
// with a useful descriptive error message if the architecture is not found.
func (st *Stream) GetArchitecture(archname string) (*Arch, error) {
	archdata, ok := st.Architectures[archname]
	if !ok {
		return nil, fmt.Errorf("stream:%s does not have architecture '%s'", st.Stream, archname)
	}
	return &archdata, nil
}

// GetAliyunRegionImage returns the release data (Image ID and release ID) for a particular
// architecture and region.
func (st *Stream) GetAliyunRegionImage(archname, region string) (*SingleImage, error) {
	starch, err := st.GetArchitecture(archname)
	if err != nil {
		return nil, err
	}
	aliyunimages := starch.Images.Aliyun
	if aliyunimages == nil {
		return nil, fmt.Errorf("%s: No Aliyun images", st.FormatPrefix(archname))
	}
	var regionVal SingleImage
	var ok bool
	if regionVal, ok = aliyunimages.Regions[region]; !ok {
		return nil, fmt.Errorf("%s: No Aliyun images in region %s", st.FormatPrefix(archname), region)
	}

	return &regionVal, nil
}

// GetAliyunImage returns the Aliyun image for a particular architecture and region.
func (st *Stream) GetAliyunImage(archname, region string) (string, error) {
	regionVal, err := st.GetAliyunRegionImage(archname, region)
	if err != nil {
		return "", err
	}
	return regionVal.Image, nil
}

// GetAwsRegionImage returns the release data (AMI and release ID) for a particular
// architecture and region.
func (st *Stream) GetAwsRegionImage(archname, region string) (*SingleImage, error) {
	starch, err := st.GetArchitecture(archname)
	if err != nil {
		return nil, err
	}
	awsimages := starch.Images.Aws
	if awsimages == nil {
		return nil, fmt.Errorf("%s: No AWS images", st.FormatPrefix(archname))
	}
	var regionVal SingleImage
	var ok bool
	if regionVal, ok = awsimages.Regions[region]; !ok {
		return nil, fmt.Errorf("%s: No AWS images in region %s", st.FormatPrefix(archname), region)
	}

	return &regionVal, nil
}

// GetAMI returns the AWS machine image for a particular architecture and region.
func (st *Stream) GetAMI(archname, region string) (string, error) {
	regionVal, err := st.GetAwsRegionImage(archname, region)
	if err != nil {
		return "", err
	}
	return regionVal.Image, nil
}

// QueryDisk finds the singleton disk artifact for a given format and architecture.
func (st *Stream) QueryDisk(architectureName, artifactName, formatName string) (*Artifact, error) {
	arch, err := st.GetArchitecture(architectureName)
	if err != nil {
		return nil, err
	}
	artifacts := arch.Artifacts[artifactName]
	if artifacts.Release == "" {
		return nil, fmt.Errorf("%s: artifact '%s' not found", st.FormatPrefix(architectureName), artifactName)
	}
	format := artifacts.Formats[formatName]
	if format.Disk == nil {
		return nil, fmt.Errorf("%s: artifact '%s' format '%s' disk not found", st.FormatPrefix(architectureName), artifactName, formatName)
	}

	return format.Disk, nil
}