-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform.go
183 lines (141 loc) · 4.19 KB
/
platform.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package tug
import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
"regexp"
"sort"
"strings"
)
// TugBuilderName denotes the name of the buildx builder.
const TugBuilderName = "tug"
// TugNodeName denotes the name of the buildx node.
const TugNodeName = "tug0"
// TugBuilderPattern denotes the pattern used to search for the tug builder within the buildx builder list.
var TugBuilderPattern = regexp.MustCompile(`^tug\W+`)
// DefaultPlatformsPattern denotes the pattern used to extract supported buildx platforms.
var DefaultPlatformsPattern = regexp.MustCompile(`Platforms:\W+(?P<platforms>.+)$`)
// PlatformPattern denotes the pattern used to extract operating system and architecture variants from buildx platform strings.
var PlatformPattern = regexp.MustCompile(`(?P<os>[^/]+)/(?P<arch>.+)$`)
// Platform models a targetable Docker image platform.
type Platform struct {
// Os denotes a buildx operating system, e.g. "linux".
Os string
// Arch denotes a buildx architecture, e.g. "arm64".
Arch string
}
// ParsePlatform extracts metadata from a buildx platform string.
func ParsePlatform(s string) (*Platform, error) {
match := PlatformPattern.FindStringSubmatch(s)
if len(match) < 3 {
return nil, fmt.Errorf("invalid buildx platform: %v", s)
}
return &Platform{Os: match[1], Arch: match[2]}, nil
}
// Format renders a buildx platform string.
func (o Platform) Format() string {
return fmt.Sprintf("%s/%s", o.Os, o.Arch)
}
// Platforms models a slice of platform(s).
type Platforms []Platform
// Len calculates the number of elements in a Platforms collection,
// in service of sorting.
func (o Platforms) Len() int {
return len(o)
}
// Swap reverse the order of two elements in a Platforms collection,
// in service of sorting.
func (o Platforms) Swap(i, j int) {
o[i], o[j] = o[j], o[i]
}
// Less returns whether the elements a Platforms collection,
// identified by their indices,
// are in ascending order or not,
// in service of sorting.
func (o Platforms) Less(i int, j int) bool {
return o[i].Format() < o[j].Format()
}
// EnsureTugBuilder prepares the tug buildx builder.
func EnsureTugBuilder(debug bool) error {
cmd := exec.Command("docker")
cmd.Args = []string{"docker", "buildx", "create", "--bootstrap", "--name", TugBuilderName, "--node", TugNodeName}
cmd.Env = os.Environ()
cmd.Stderr = os.Stderr
if debug {
log.Printf("Command: %v", cmd)
}
return cmd.Run()
}
// AvailablePlatforms initializes tug and reports the available buildx platforms.
func AvailablePlatforms(debug bool) ([]Platform, error) {
if err := EnsureTugBuilder(debug); err != nil {
return nil, err
}
cmd := exec.Command("docker")
cmd.Args = []string{"docker", "buildx", "inspect", TugBuilderName}
cmd.Env = os.Environ()
cmd.Stderr = os.Stderr
if debug {
log.Printf("Command: %v", cmd)
}
stdoutChild, err := cmd.StdoutPipe()
if err != nil {
return []Platform{}, err
}
if err2 := cmd.Start(); err2 != nil {
return []Platform{}, err
}
scanner := bufio.NewScanner(stdoutChild)
var platforms []Platform
for scanner.Scan() {
line := scanner.Text()
match := DefaultPlatformsPattern.FindStringSubmatch(line)
if len(match) < 2 {
continue
}
platformsText := match[1]
platformPairsText := strings.Split(platformsText, ", ")
for _, platformPairText := range platformPairsText {
platform, err2 := ParsePlatform(platformPairText)
if err2 != nil {
return platforms, err
}
platforms = append(platforms, *platform)
}
}
if err2 := cmd.Wait(); err2 != nil {
return platforms, err
}
if platforms == nil {
return platforms, fmt.Errorf("no platforms detected")
}
sort.Sort(Platforms(platforms))
return platforms, nil
}
// NichePlatforms may be disabled by default.
var NichePlatforms = []Platform{
{
"linux",
"mips64",
},
}
// DisableNichePlatforms filters out some exceedingly niche platforms,
// which may not have associated base image entries on Docker Hub.
func DisableNichePlatforms(platforms []Platform) []Platform {
var out []Platform
for _, platform := range platforms {
var foundNiche bool
for _, niche := range NichePlatforms {
if platform == niche {
foundNiche = true
break
}
}
if !foundNiche {
out = append(out, platform)
}
}
return out
}