-
Notifications
You must be signed in to change notification settings - Fork 9
/
update.go
358 lines (298 loc) · 9.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package nsboot
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/spf13/viper"
"golang.org/x/mod/semver"
"google.golang.org/protobuf/encoding/protojson"
"namespacelabs.dev/foundation/internal/artifacts"
"namespacelabs.dev/foundation/internal/artifacts/download"
"namespacelabs.dev/foundation/internal/artifacts/unpack"
"namespacelabs.dev/foundation/internal/cli/version"
"namespacelabs.dev/foundation/internal/cli/versioncheck"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/console/colors"
"namespacelabs.dev/foundation/internal/fnapi"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/fnerrors/format"
"namespacelabs.dev/foundation/internal/fnfs/tarfs"
"namespacelabs.dev/foundation/internal/workspace/dirs"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/tasks"
)
const (
updatePeriod = time.Hour * 24
)
type NSPackage struct {
Command string
Path string
}
func versionsJson(command string) string {
return fmt.Sprintf("%s-version.json", command)
}
func (p NSPackage) Execute(ctx context.Context) error {
proc := exec.CommandContext(ctx, filepath.Join(string(p.Path), p.Command), os.Args[1:]...)
proc.Stdin = os.Stdin
proc.Stdout = os.Stdout
proc.Stderr = os.Stderr
proc.Env = append(os.Environ(), fmt.Sprintf("NSBOOT_VERSION=%s", formatNSBootVersion()))
return proc.Run()
}
func (p NSPackage) ExecuteAndForwardExitCode(ctx context.Context, style colors.Style) {
err := p.Execute(ctx)
if err == nil {
os.Exit(0)
} else if exiterr, ok := err.(*exec.ExitError); ok {
os.Exit(exiterr.ExitCode())
} else {
format.Format(os.Stderr, err, format.WithStyle(style))
os.Exit(3)
}
}
func checkExists(command string, cached *versionCache) (NSPackage, bool) {
if cached != nil && cached.BinaryPath != "" {
if _, err := os.Stat(cached.BinaryPath); err == nil {
return NSPackage{Command: command, Path: cached.BinaryPath}, true
}
}
return NSPackage{}, false
}
func CheckUpdate(ctx context.Context, command string, updateInline bool, currentVersion string) (*toolVersion, NSPackage, error) {
cached, needUpdate, err := loadCheckCachedMetadata(ctx, command, true)
if err != nil {
return nil, NSPackage{}, fnerrors.New("failed to load versions.json: %w", err)
}
if cached != nil {
if cached.Latest != nil && currentVersion != "" && semver.Compare(currentVersion, cached.Latest.TagName) >= 0 {
// Current version is at least as up to date as the downloaded version.
return nil, NSPackage{}, nil
}
if !needUpdate {
if ns, ok := checkExists(command, cached); ok {
return cached.Latest, ns, nil
}
}
}
if !updateInline {
return nil, NSPackage{}, nil
}
var ver *toolVersion
var ns NSPackage
ns.Command = command
updateErr := compute.Do(ctx, func(ctx context.Context) error {
var err error
ver, ns, err = performUpdate(ctx, command, cached, currentVersion, !needUpdate)
return err
})
return ver, ns, updateErr
}
func ForceUpdate(ctx context.Context, command string) error {
cached, err := loadCachedMetadata(command)
if err != nil {
return err
}
newVersion, _, err := performUpdate(ctx, command, cached, "", true)
if err != nil {
return err
}
if cached != nil && cached.Latest != nil && cached.Latest.TagName == newVersion.TagName {
fmt.Fprintf(console.Stdout(ctx), "Already up-to-date at %s.\n", newVersion.TagName)
} else {
fmt.Fprintf(console.Stdout(ctx), "Updated to version %s.\n", newVersion.TagName)
}
return nil
}
func AutoUpdateEnabled() bool {
return viper.GetBool("enable_autoupdate")
}
// Loads version cache and applies default update policy.
func loadCheckCachedMetadata(ctx context.Context, command string, silent bool) (*versionCache, bool, error) {
cache, err := loadCachedMetadata(command)
if err != nil {
return nil, false, err
}
if cache == nil {
return nil, false, err
}
needUpdate := cache.Latest != nil && (time.Since(cache.Latest.FetchedAt) > updatePeriod || cache.PendingVersion != "")
if needUpdate && !AutoUpdateEnabled() {
needUpdate = false
if !silent {
fmt.Fprintf(console.Stdout(ctx), "ns version is stale, but auto-update is disabled (see \"enable_autoupdate\" in config.json)\n")
}
}
return cache, needUpdate, nil
}
func performUpdate(ctx context.Context, command string, previous *versionCache, currentVersion string, forceUpdate bool) (*toolVersion, NSPackage, error) {
// XXX store the last time timestamp checked, else after 24h we're always checking.
newVersion, err := fetchRemoteVersion(ctx, command)
if err != nil {
return nil, NSPackage{}, fnerrors.New("failed to load an update from the update service: %w", err)
}
// If the latest version is not new than the current, just use the current binary.
if currentVersion != "" && semver.Compare(currentVersion, newVersion.TagName) >= 0 {
return nil, NSPackage{}, nil
}
values := url.Values{}
if previous != nil && previous.Latest != nil {
serialized, _ := json.Marshal(reportedExistingVersion{
TagName: previous.Latest.TagName,
SHA256: previous.Latest.SHA256,
})
values.Add("update_from", base64.RawURLEncoding.EncodeToString(serialized))
}
if forceUpdate {
values.Add("force_update", "true")
}
path, err := fetchBinary(ctx, command, newVersion, values)
if err != nil {
return nil, NSPackage{}, fnerrors.New("failed to fetch a new tarball: %w", err)
}
// Only commit to the new version once we know that we got the new binary.
if err := persistVersion(command, newVersion, path); err != nil {
return nil, NSPackage{}, fnerrors.New("failed to persist the version cache: %w", err)
}
return newVersion, NSPackage{Command: command, Path: path}, nil
}
func fetchRemoteVersion(ctx context.Context, command string) (*toolVersion, error) {
return tasks.Return(ctx, tasks.Action(command+".version-check"), func(ctx context.Context) (*toolVersion, error) {
resp, err := fnapi.GetLatestVersion(ctx, map[string]any{command: map[string]any{}})
if err != nil {
return nil, err
}
tarball := findHostTarball(resp.Tarballs)
if tarball == nil {
return nil, fnerrors.New("no tarball for host OS/architecture offered by the update service")
}
return &toolVersion{
TagName: resp.Version,
BuildTime: resp.BuildTime,
FetchedAt: time.Now(),
URL: tarball.URL,
SHA256: tarball.SHA256,
}, nil
})
}
func loadCachedMetadata(command string) (*versionCache, error) {
cacheDir, err := dirs.Cache()
if err != nil {
return nil, err
}
bs, err := os.ReadFile(filepath.Join(cacheDir, versionsJson(command)))
if err != nil {
if os.IsNotExist(err) {
// Missing the cache is okay.
return nil, nil
}
return nil, err
}
var cache versionCache
if err := json.Unmarshal(bs, &cache); err != nil {
return nil, err
}
return &cache, nil
}
func NewerVersion(command string, ver *versioncheck.Status) bool {
cached, err := loadCachedMetadata(command)
if err == nil && cached != nil && cached.Latest != nil {
return semver.Compare(ver.Version, cached.Latest.TagName) > 0
}
return false
}
func invalidate(command string, cached *versionCache, pending string) error {
dir, err := dirs.Cache()
if err != nil {
return err
}
return rewrite(dir, versionsJson(command), func(w io.Writer) error {
copy := *cached
copy.PendingVersion = pending
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
return enc.Encode(copy)
})
}
func persistVersion(command string, version *toolVersion, path string) error {
toolDir, err := dirs.Ensure(dirs.Cache())
if err != nil {
return err
}
return rewrite(toolDir, versionsJson(command), func(w io.Writer) error {
cache := versionCache{
Latest: version,
BinaryPath: path,
}
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
return enc.Encode(cache)
})
}
func rewrite(dir, filename string, make func(io.Writer) error) error {
newFile, err := os.CreateTemp(dir, filename)
if err != nil {
return fnerrors.InternalError("failed to create new %s: %w", filename, err)
}
writeErr := make(newFile)
// Always close before returning.
closeErr := newFile.Close()
if writeErr != nil {
return fnerrors.InternalError("failed to generate new %s: %w", filename, err)
}
if closeErr != nil {
return fnerrors.InternalError("failed to flush new %s: %w", filename, err)
}
if err := os.Rename(newFile.Name(), filepath.Join(dir, filename)); err != nil {
return fnerrors.InternalError("failed to update %s: %w", filename, err)
}
return nil
}
func fetchBinary(ctx context.Context, command string, version *toolVersion, values url.Values) (string, error) {
tarRef := artifacts.Reference{
URL: version.URL,
Digest: schema.Digest{
Algorithm: "sha256",
Hex: version.SHA256,
},
}
fsys := unpack.Unpack(command, tarfs.TarGunzip(download.NamespaceURL(tarRef, values)), unpack.SkipChecksumCheck())
unpacked, err := compute.GetValue(ctx, fsys)
if err != nil {
return "", err
}
return unpacked.Files, nil
}
func findHostTarball(tarballs []*fnapi.Artifact) *fnapi.Artifact {
for _, tarball := range tarballs {
goos := strings.ToLower(tarball.OS)
goarch := strings.ToLower(tarball.Arch)
if goos == runtime.GOOS && goarch == runtime.GOARCH {
return tarball
}
}
return nil
}
func formatNSBootVersion() string {
ver, err := version.Current()
if err != nil {
return fmt.Sprintf(`{"error": %q}`, err)
}
bs, err := protojson.Marshal(ver)
if err != nil {
return fmt.Sprintf(`{"error": %q}`, err)
}
return string(bs)
}