|
| 1 | +// Copyright (c) 2023 Gitpod GmbH. All rights reserved. |
| 2 | +// Licensed under the GNU Affero General Public License (AGPL). |
| 3 | +// See License.AGPL.txt in the project root for license information. |
| 4 | + |
| 5 | +package selfupdate |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "crypto" |
| 10 | + "encoding/hex" |
| 11 | + "encoding/json" |
| 12 | + "errors" |
| 13 | + "fmt" |
| 14 | + "log/slog" |
| 15 | + "net/http" |
| 16 | + "net/url" |
| 17 | + "os" |
| 18 | + "path/filepath" |
| 19 | + "regexp" |
| 20 | + "runtime" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/Masterminds/semver/v3" |
| 24 | + "github.com/gitpod-io/local-app/pkg/config" |
| 25 | + "github.com/gitpod-io/local-app/pkg/constants" |
| 26 | + "github.com/inconshreveable/go-update" |
| 27 | + "github.com/opencontainers/go-digest" |
| 28 | +) |
| 29 | + |
| 30 | +// Manifest is the manifest of a selfupdate |
| 31 | +type Manifest struct { |
| 32 | + Version *semver.Version `json:"version"` |
| 33 | + Binaries []Binary `json:"binaries"` |
| 34 | +} |
| 35 | + |
| 36 | +// Binary describes a single executable binary |
| 37 | +type Binary struct { |
| 38 | + // URL is added when the manifest is downloaded. |
| 39 | + URL string `json:"-"` |
| 40 | + |
| 41 | + Filename string `json:"filename"` |
| 42 | + OS string `json:"os"` |
| 43 | + Arch string `json:"arch"` |
| 44 | + Digest digest.Digest `json:"digest"` |
| 45 | +} |
| 46 | + |
| 47 | +type FilenameParserFunc func(filename string) (os, arch string, ok bool) |
| 48 | + |
| 49 | +var regexDefaultFilenamePattern = regexp.MustCompile(`^.*-(linux|darwin|windows)-(amd64|arm64)(\.exe)?$`) |
| 50 | + |
| 51 | +func DefaultFilenameParser(filename string) (os, arch string, ok bool) { |
| 52 | + matches := regexDefaultFilenamePattern.FindStringSubmatch(filename) |
| 53 | + if matches == nil { |
| 54 | + return "", "", false |
| 55 | + } |
| 56 | + |
| 57 | + return matches[1], matches[2], true |
| 58 | +} |
| 59 | + |
| 60 | +// GenerateManifest generates a manifest for the given location |
| 61 | +// by scanning the location for binaries following the naming convention |
| 62 | +func GenerateManifest(version *semver.Version, loc string, filenameParser FilenameParserFunc) (*Manifest, error) { |
| 63 | + files, err := os.ReadDir(loc) |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + |
| 68 | + var binaries []Binary |
| 69 | + for _, f := range files { |
| 70 | + goos, arch, ok := filenameParser(f.Name()) |
| 71 | + if !ok { |
| 72 | + continue |
| 73 | + } |
| 74 | + |
| 75 | + fd, err := os.Open(filepath.Join(loc, f.Name())) |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + dgst, err := digest.FromReader(fd) |
| 80 | + fd.Close() |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + |
| 85 | + binaries = append(binaries, Binary{ |
| 86 | + Filename: f.Name(), |
| 87 | + OS: goos, |
| 88 | + Arch: arch, |
| 89 | + Digest: dgst, |
| 90 | + }) |
| 91 | + } |
| 92 | + |
| 93 | + return &Manifest{ |
| 94 | + Version: version, |
| 95 | + Binaries: binaries, |
| 96 | + }, nil |
| 97 | +} |
| 98 | + |
| 99 | +// DownloadManifest downloads a manifest from the given URL. |
| 100 | +// Expects the manifest to be at <baseURL>/manifest.json. |
| 101 | +func DownloadManifest(ctx context.Context, baseURL string) (res *Manifest, err error) { |
| 102 | + defer func() { |
| 103 | + if err != nil { |
| 104 | + err = fmt.Errorf("download manifest from %s: %w", baseURL, err) |
| 105 | + } |
| 106 | + }() |
| 107 | + |
| 108 | + murl, err := url.Parse(baseURL) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + originalPath := murl.Path |
| 113 | + murl.Path = filepath.Join(murl.Path, "manifest.json") |
| 114 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, murl.String(), nil) |
| 115 | + if err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + resp, err := http.DefaultClient.Do(req) |
| 119 | + if err != nil { |
| 120 | + return nil, err |
| 121 | + } |
| 122 | + defer resp.Body.Close() |
| 123 | + |
| 124 | + var mf Manifest |
| 125 | + err = json.NewDecoder(resp.Body).Decode(&mf) |
| 126 | + if err != nil { |
| 127 | + return nil, err |
| 128 | + } |
| 129 | + for i := range mf.Binaries { |
| 130 | + murl.Path = filepath.Join(originalPath, mf.Binaries[i].Filename) |
| 131 | + mf.Binaries[i].URL = murl.String() |
| 132 | + } |
| 133 | + |
| 134 | + return &mf, nil |
| 135 | +} |
| 136 | + |
| 137 | +// NeedsUpdate checks if the current version is outdated |
| 138 | +func NeedsUpdate(current *semver.Version, manifest *Manifest) bool { |
| 139 | + return manifest.Version.GreaterThan(current) |
| 140 | +} |
| 141 | + |
| 142 | +// ReplaceSelf replaces the current binary with the one from the manifest, no matter the version |
| 143 | +// If there is no matching binary in the manifest, this function returns ErrNoBinaryAvailable. |
| 144 | +func ReplaceSelf(ctx context.Context, manifest *Manifest) error { |
| 145 | + var binary *Binary |
| 146 | + for _, b := range manifest.Binaries { |
| 147 | + if b.OS != runtime.GOOS || b.Arch != runtime.GOARCH { |
| 148 | + continue |
| 149 | + } |
| 150 | + |
| 151 | + binary = &b |
| 152 | + break |
| 153 | + } |
| 154 | + if binary == nil { |
| 155 | + return ErrNoBinaryAvailable |
| 156 | + } |
| 157 | + |
| 158 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, binary.URL, nil) |
| 159 | + if err != nil { |
| 160 | + return err |
| 161 | + } |
| 162 | + resp, err := http.DefaultClient.Do(req) |
| 163 | + if err != nil { |
| 164 | + return err |
| 165 | + } |
| 166 | + defer resp.Body.Close() |
| 167 | + |
| 168 | + dgst, _ := hex.DecodeString(binary.Digest.Hex()) |
| 169 | + return update.Apply(resp.Body, update.Options{ |
| 170 | + Checksum: dgst, |
| 171 | + Hash: crypto.SHA256, |
| 172 | + TargetMode: 0755, |
| 173 | + }) |
| 174 | +} |
| 175 | + |
| 176 | +var ErrNoBinaryAvailable = errors.New("no binary available for this platform") |
| 177 | + |
| 178 | +// Autoupdate checks if there is a newer version available and updates the binary if so |
| 179 | +// actually updates. This function returns immediately and runs the update in the background. |
| 180 | +// The returned function can be used to wait for the update to finish. |
| 181 | +func Autoupdate(ctx context.Context, cfg *config.Config) func() { |
| 182 | + if !cfg.Autoupdate { |
| 183 | + return func() {} |
| 184 | + } |
| 185 | + |
| 186 | + done := make(chan struct{}) |
| 187 | + go func() { |
| 188 | + defer close(done) |
| 189 | + |
| 190 | + gpctx, _ := cfg.GetActiveContext() |
| 191 | + if gpctx == nil { |
| 192 | + slog.Debug("no active context - autoupdate disabled") |
| 193 | + return |
| 194 | + } |
| 195 | + |
| 196 | + var err error |
| 197 | + defer func() { |
| 198 | + if err != nil { |
| 199 | + slog.Debug("autoupdate failed", "err", err) |
| 200 | + } |
| 201 | + }() |
| 202 | + |
| 203 | + mfctx, cancel := context.WithTimeout(ctx, 1*time.Second) |
| 204 | + defer cancel() |
| 205 | + baseURL := *gpctx.Host.URL |
| 206 | + baseURL.Path = "/static/bin" |
| 207 | + mf, err := DownloadManifest(mfctx, baseURL.String()) |
| 208 | + if err != nil { |
| 209 | + return |
| 210 | + } |
| 211 | + |
| 212 | + if !NeedsUpdate(constants.Version, mf) { |
| 213 | + slog.Debug("no update available", "current", constants.Version, "latest", mf.Version) |
| 214 | + return |
| 215 | + } |
| 216 | + |
| 217 | + dlctx, cancel := context.WithTimeout(ctx, 5*time.Minute) |
| 218 | + defer cancel() |
| 219 | + slog.Debug("attempting to autoupdate", "current", constants.Version, "latest", mf.Version) |
| 220 | + err = ReplaceSelf(dlctx, mf) |
| 221 | + }() |
| 222 | + |
| 223 | + return func() { |
| 224 | + select { |
| 225 | + case <-done: |
| 226 | + return |
| 227 | + case <-time.After(5 * time.Second): |
| 228 | + slog.Warn("autoupdate is still running - press Ctrl+C to abort") |
| 229 | + } |
| 230 | + } |
| 231 | +} |
0 commit comments