-
Notifications
You must be signed in to change notification settings - Fork 4
/
update.go
247 lines (218 loc) · 5.67 KB
/
update.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package main
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/jsonmessage"
"gopkg.in/yaml.v3"
)
const (
checkDataRelPath = ".cache/canon/update-data.yaml"
lockRelPath = ".cache/canon/update.lock"
)
type ImageDef struct {
Image string
Platform string
}
// MarshalYAML marshals yaml
//
//nolint:unparam
func (i ImageDef) MarshalYAML() (interface{}, error) {
return i.Image + "|" + i.Platform, nil
}
// UnmarshalYAML unmarshals yaml.
func (i *ImageDef) UnmarshalYAML(n *yaml.Node) error {
splits := strings.Split(n.Value, "|")
if len(splits) != 2 {
return errors.New(n.Value + " did not split into image and platform")
}
i.Image = splits[0]
i.Platform = splits[1]
return nil
}
type ImageCheckData map[ImageDef]time.Time
func update(images ...ImageDef) error {
lock, err := getLock()
if err != nil {
return err
}
defer func() {
checkErr(dropLock(lock))
}()
checkData, err := readCheckData()
if err != nil {
return err
}
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return err
}
for _, i := range images {
resp, err := cli.ImagePull(ctx, i.Image, image.PullOptions{Platform: i.Platform})
if err != nil {
return err
}
defer resp.Close()
err = jsonmessage.DisplayJSONMessagesStream(resp, os.Stdout, os.Stdout.Fd(), true, nil)
if err != nil {
return err
}
err = resp.Close()
if err != nil {
return err
}
checkData[i] = time.Now()
}
return checkData.write()
}
func getLock() (*os.File, error) {
home, err := os.UserHomeDir()
if err != nil {
return nil, err
}
lockFile := filepath.Join(home, lockRelPath)
if err := os.MkdirAll(filepath.Dir(lockFile), 0o755); err != nil {
return nil, err
}
file, err := os.OpenFile(lockFile, os.O_WRONLY|os.O_CREATE, 0o666)
if err != nil {
return nil, err
}
_, err = fmt.Fprintf(file, "%d", os.Getpid())
if err != nil {
return file, err
}
err = syscall.Flock(int(file.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
if errors.Is(err, syscall.EAGAIN) {
err = errors.New("another canon process is holding " + lockFile)
}
return file, err
}
func dropLock(file *os.File) error {
if err := syscall.Flock(int(file.Fd()), syscall.LOCK_UN); err != nil {
return err
}
if err := file.Close(); err != nil {
return err
}
return os.Remove(file.Name())
}
// Updates the image for the active (default or specified) profile, and (optionally) all known profiles.
func checkUpdate(curProfile *Profile, all, force bool) error {
// Used to de-dupe
imagesMap := make(map[ImageDef]bool)
lock, err := getLock()
if err != nil {
return err
}
checkData, err := readCheckData()
if err != nil {
return err
}
err = dropLock(lock)
if err != nil {
return err
}
// add current profile's image
for _, i := range checkImageDate(curProfile, checkData, force) {
if all || i.Platform == "linux/"+curProfile.Arch {
imagesMap[i] = true
}
}
if all {
for _, p := range mergedCfg {
iface, ok := p.(map[string]interface{})
if !ok {
continue
}
prof, err := newProfile(true)
if err != nil {
return err
}
// we want defaults but NOT the defaults for images
prof.ImageAMD64 = ""
prof.ImageARM64 = ""
prof.ImageARM = ""
prof.ImageARMv6 = ""
prof.Image386 = ""
prof.Image = ""
err = mapDecode(iface, prof)
if err != nil {
return err
}
for _, i := range checkImageDate(prof, checkData, force) {
imagesMap[i] = true
}
}
}
var images []ImageDef
for i := range imagesMap {
fmt.Printf("queuing update: %s|%s\n", i.Image, i.Platform)
images = append(images, i)
}
return update(images...)
}
func checkImageDate(profile *Profile, checkData ImageCheckData, force bool) []ImageDef {
var imageCandidates, images []ImageDef
// multi arch profiles
if profile.ImageAMD64 != "" {
imageCandidates = append(imageCandidates, ImageDef{Image: profile.ImageAMD64, Platform: "linux/amd64"})
}
if profile.ImageARM64 != "" {
imageCandidates = append(imageCandidates, ImageDef{Image: profile.ImageARM64, Platform: "linux/arm64"})
}
if profile.ImageARM != "" {
imageCandidates = append(imageCandidates, ImageDef{Image: profile.ImageARM, Platform: "linux/arm"})
}
if profile.ImageARMv6 != "" {
imageCandidates = append(imageCandidates, ImageDef{Image: profile.ImageARMv6, Platform: "linux/arm/v6"})
}
if profile.Image386 != "" {
imageCandidates = append(imageCandidates, ImageDef{Image: profile.Image386, Platform: "linux/386"})
}
if profile.Image != "" {
imageCandidates = append(imageCandidates, ImageDef{Image: profile.Image, Platform: "linux/" + profile.Arch})
}
for _, i := range imageCandidates {
lastUpdate, ok := checkData[i]
if !ok || force || time.Now().After(lastUpdate.Add(profile.UpdateInterval)) || profile.MinimumDate.After(lastUpdate) {
images = append(images, i)
}
}
return images
}
func readCheckData() (ImageCheckData, error) {
checkData := make(ImageCheckData)
home, err := os.UserHomeDir()
if err != nil {
return checkData, err
}
dataBytes, err := os.ReadFile(filepath.Join(home, checkDataRelPath))
if err != nil && !errors.Is(err, os.ErrNotExist) {
return checkData, err
}
return checkData, yaml.Unmarshal(dataBytes, checkData)
}
func (data *ImageCheckData) write() error {
out, err := yaml.Marshal(data)
if err != nil {
return err
}
home, err := os.UserHomeDir()
if err != nil {
return err
}
checkDataFilePath := filepath.Join(home, checkDataRelPath)
if err := os.MkdirAll(filepath.Dir(checkDataFilePath), 0o755); err != nil {
return err
}
return os.WriteFile(checkDataFilePath, out, 0o644)
}