forked from canonical/lxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.go
125 lines (97 loc) · 2.36 KB
/
sort.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
package simplestreams
import (
"github.com/lxc/lxd/shared/api"
"github.com/lxc/lxd/shared/osarch"
)
var nativeName, _ = osarch.ArchitectureGetLocal()
type sortedImages []api.Image
func (a sortedImages) Len() int {
return len(a)
}
func (a sortedImages) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a sortedImages) Less(i, j int) bool {
// When sorting images, group by:
// - Operating system (os)
// - Release (release)
// - Variant (variant)
// - Serial number / date (serial)
// - Architecture (architecture)
for _, prop := range []string{"os", "release", "variant", "serial", "architecture"} {
if a[i].Properties[prop] == a[j].Properties[prop] {
continue
}
if a[i].Properties[prop] == "" {
return false
}
if a[i].Properties[prop] == "" {
return true
}
if prop == "serial" {
return a[i].Properties[prop] > a[j].Properties[prop]
}
return a[i].Properties[prop] < a[j].Properties[prop]
}
if a[i].Properties["type"] != a[j].Properties["type"] {
iScore := 0
jScore := 0
// Image types in order of preference for LXD hosts.
for score, pref := range []string{"squashfs", "root.tar.xz", "disk-kvm.img", "uefi1.img", "disk1.img"} {
if a[i].Properties["type"] == pref {
iScore = score
}
if a[j].Properties["type"] == pref {
jScore = score
}
}
return iScore < jScore
}
return false
}
type sortedAliases []extendedAlias
func (a sortedAliases) Len() int {
return len(a)
}
func (a sortedAliases) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a sortedAliases) Less(i, j int) bool {
// Check functions.
isNative := func(arch string) bool {
return nativeName == arch
}
isPersonality := func(arch string) bool {
archID, err := osarch.ArchitectureId(nativeName)
if err != nil {
return false
}
personalities, err := osarch.ArchitecturePersonalities(archID)
if err != nil {
return false
}
for _, personality := range personalities {
personalityName, err := osarch.ArchitectureName(personality)
if err != nil {
return false
}
if personalityName == arch {
return true
}
}
return false
}
// Same thing.
if a[i].Architecture == a[j].Architecture {
return false
}
// Look for native.
if isNative(a[i].Architecture) {
return true
}
// Look for personality.
if isPersonality(a[i].Architecture) && !isNative(a[j].Architecture) {
return true
}
return false
}