-
Notifications
You must be signed in to change notification settings - Fork 20
/
template.go
164 lines (138 loc) · 3.8 KB
/
template.go
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package cmd
import (
"fmt"
"log"
"regexp"
"sort"
"strconv"
"strings"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
// templateCmd represents the template command
var templateCmd = &cobra.Command{
Use: "template",
Short: "Templates details",
}
func getTemplateByName(zoneID *egoscale.UUID, name string) (*egoscale.Template, error) {
// Find by name, then by ID
template := &egoscale.Template{
IsFeatured: true,
ZoneID: zoneID,
}
id, errUUID := egoscale.ParseUUID(name)
if errUUID != nil {
template.Name = name
} else {
template.ID = id
}
if err := cs.GetWithContext(gContext, template); err == nil {
return template, err
}
// attempts a fuzzy search
sortedTemplates, err := findTemplates(zoneID, name)
if err != nil {
return nil, err
}
if len(sortedTemplates) > 1 {
return nil, fmt.Errorf("more than one templates found")
}
if len(sortedTemplates) == 0 {
return nil, fmt.Errorf("template %q not found", name)
}
return &sortedTemplates[0], nil
}
func findTemplates(zoneID *egoscale.UUID, filters ...string) ([]egoscale.Template, error) {
allOS := make(map[string]*egoscale.Template)
reLinux := regexp.MustCompile(`^Linux (?P<name>.+?) (?P<version>[0-9]+(\.[0-9]+)?)`)
reVersion := regexp.MustCompile(`(?P<version>[0-9]+(\.[0-9]+)?)`)
req := &egoscale.ListTemplates{
TemplateFilter: "featured",
ZoneID: zoneID,
Keyword: strings.Join(filters, " "),
}
var err error
cs.PaginateWithContext(gContext, req, func(i interface{}, e error) bool {
if e != nil {
err = e
return false
}
template := i.(*egoscale.Template)
size := template.Size >> 30 // Size in GiB
if strings.HasPrefix(template.Name, "Linux") {
m := reSubMatchMap(reLinux, template.DisplayText)
if len(m) > 0 {
if size > 10 {
// Skipping big, legacy images
return true
}
version, errParse := strconv.ParseFloat(m["version"], 64)
if errParse != nil {
log.Printf("Malformed Linux version. got %q in %q", m["version"], template.Name)
return true
}
res := fmt.Sprintf("%.5f", 10000-version)
// fix Container Linux sorting
name := strings.Replace(m["name"], "stable ", "", 1)
key := fmt.Sprintf("Linux %s %s", name, res)
allOS[key] = template
return true
}
// skip
log.Printf("Malformed Linux. %q", template.DisplayText)
return true
}
if strings.HasPrefix(template.Name, "Windows Server") || strings.HasPrefix(template.Name, "OpenBSD") {
m := reSubMatchMap(reVersion, template.DisplayText)
if len(m) > 0 {
version, errParse := strconv.ParseFloat(m["version"], 64)
if errParse != nil {
log.Printf("Malformed Windows/OpenBSD version. %q", template.Name)
return true
}
key := fmt.Sprintf("%s %.5f %5d", template.Name[:7], 10000-version, size)
allOS[key] = template
return true
}
log.Printf("Malformed Windows/OpenBSD. %q", template.DisplayText)
return true
}
// In doubt, use it directly
allOS[template.Name] = template
return true
})
if err != nil {
return nil, err
}
keys := make([]string, 0, len(allOS))
for k := range allOS {
keys = append(keys, k)
}
sort.Strings(keys)
reDate := regexp.MustCompile(`.* \((?P<date>.*)\)$`)
templates := make([]egoscale.Template, len(keys))
for i, k := range keys {
t := allOS[k]
m := reSubMatchMap(reDate, t.DisplayText)
if m["date"] != "" {
t.Created = m["date"]
} else if len(t.Created) > 10 {
t.Created = t.Created[0:10]
}
templates[i] = *t
}
return templates, nil
}
func reSubMatchMap(r *regexp.Regexp, str string) map[string]string {
match := r.FindStringSubmatch(str)
subMatchMap := make(map[string]string)
for i, name := range r.SubexpNames() {
if i != 0 && len(match) > 0 {
subMatchMap[name] = match[i]
}
}
return subMatchMap
}
func init() {
RootCmd.AddCommand(templateCmd)
}