mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 16:27:33 -10:00
Lima's download of the cloud image neither retries nor times out: one cut in its 900 MB fails base's build, and a stall hangs it. devvm now downloads the pinned image itself through fetch.Resumable (ubuntu.Download), into images/ubuntu/<sha256>.img (Dirs.UbuntuImage), resuming across cuts, stalls and runs, and gives Lima that file. Lima 2.3 checks a local file against the digest before it copies it. - Before downloading, it takes the image from Lima's own download cache when that holds it with the pin's SHA-256: copied into a new file of devvm's own, mode 600, hashed as it's written, capped, stopped by the context, and kept only once it checks out. Lima's cache is never changed, and a base whose dated release Ubuntu may have removed still builds again. - One devvm downloads at a time (images/locks/ubuntu-image), another waiting under ubuntu_image_waiting and looking again under the lock (TestUbuntuImageAfterWait, which fails without that second look). - The download goes through the company proxy and trusts the user's CAs (Lima.Roots, from trust.Pool, which internal/cli passes through the registry), as do the Finder's lookups (TestNewTrustsRoots, and the registry's TestOverrides). An untrusted certificate is ubuntu_untrusted, naming the CAs' folder, one from a trusted CA that's for another host, out of date or not valid yet ubuntu_bad_certificate (check the clock and what answers for the host), and any other that fails to verify (a SHA-1 signature, a CA's name constraints, a key for clients alone) ubuntu_certificate_invalid, naming Go's reason, for the lookups (the listing, the checksums, their signature: TestLookupCertificates) and the download alike (TestLocalImageFailsFor), and a release Ubuntu removed (404, 410) ubuntu_release_gone, which names devvm rebuild base; none is retried. ubuntu_download_failed no longer starts with devvm. - fetch.Untrusted, fetch.BadCertificate (an x509.HostnameError, or a CertificateInvalidError that's Expired) and fetch.Unverified (any failure to verify, with its reason, so every error the other two name), beside fetch.Gone, are the one place that tells a certificate's failures apart: Transient asks Unverified, the lookups and the download ask all three, host mise asks Untrusted, and a forbidigo rule keeps x509's and tls's verification errors in internal/fetch (TestCertificateClasses, TestTransient, testdata/lint, the owners doc's row). - An https address, or a test build's file:// DEVVM_UBUNTU_URL, read from disk the same way; anything else is refused. The CLI tests' sandbox has a stand-in of this host's image in the store. - Base's record keeps the image's address, and the key is as it was: it hashes the pin (release, file, digest), not the location. - devvm gc --caches frees devvm's copies that no record names and that aren't pinned now, unchanged for an hour, a stopped .part and a killed import's .import included, and those of a kept image once its .img is there. A pin it can't read fails it. - Of Lima's cache, it frees only an entry that devvm's own state shows a build of devvm's left: a base record names its address, devvm has its own copy of the image, and the installed Lima's own templates (share/lima/templates beside limactl, Lima.Templates) don't name it. An entry no record names may be the user's own Lima's, so it stays, and none is freed when the templates can't be read, with a line in the run log naming the folder and why. A test build's DEVVM_LIMA_TEMPLATES stands in for the templates folder. - ubuntu.ParseImage reads a record's image as Image.String writes it, and ubuntu.IsSHA256 is the one check of a cloud image's SHA-256, in place of Lima's own regex. - devvm space shows the copies as a cloud images row, freed by gc --caches, kept as state, as a release Ubuntu removed can't be downloaded again. 0.1.0's frozen state runs the new commands (fixture_v010_cloud_image): devvm looks in Lima's cache under the SHA-256 of the address 0.1.0's base record keeps and never changes the entry, base builds from devvm's own copy, and gc --caches keeps that copy, frees Lima's entry the record names and devvm's own stale copies, and keeps an older entry no record names. Docs: images.md (the download, its failures, the import, gc's rules), space.md, backends/lima.md, settings.md (the lookups and the download trust your CAs), troubleshooting.md, overview.md, testing.md, tests.md, structure.md's internal/fetch row; the CHANGELOG's two Fixed lines, which later commits make highlights, What to do 5 and 6, and the release page's Ubuntu's cloud image in devvm's folder. New messages: downloading_ubuntu, importing_ubuntu, ubuntu_image_waiting, ubuntu_download_failed, ubuntu_release_gone, ubuntu_bad_certificate, ubuntu_certificate_invalid, ubuntu_image_checksum, ubuntu_untrusted. Lima.LocalImage is the seam, Lima.ImageClient the client. Co-Authored-By: Claude Opus 5.5 <[email protected]>
216 lines
7.3 KiB
Go
216 lines
7.3 KiB
Go
package lima
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"crypto/x509"
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/devvmhq/devvm/internal/backend"
|
|
"github.com/devvmhq/devvm/internal/fetch"
|
|
"github.com/devvmhq/devvm/internal/msg"
|
|
"github.com/devvmhq/devvm/internal/platform"
|
|
"github.com/devvmhq/devvm/internal/ubuntu"
|
|
)
|
|
|
|
// localImage is the pinned cloud image img as a file of devvm's own
|
|
// (Dirs.UbuntuImage), which Lima is given in place of its address: devvm
|
|
// downloads it itself (ubuntu.Download), resuming across cuts, stalls and
|
|
// runs, where Lima's own download neither retries nor times out. One
|
|
// devvm downloads at a time (UbuntuImageLock). Before downloading, it
|
|
// takes the image from Lima's own cache when that has it (fromLimaCache):
|
|
// what a build of base that had Lima download the image left there, which
|
|
// Ubuntu may no longer have. Lima still checks the file against the
|
|
// digest, and base's record keeps the image's address (createFromUbuntu).
|
|
// The address is https, or a test build's DEVVM_UBUNTU_URL on disk
|
|
// (file://), which goes the same way; anything else is refused.
|
|
func (l *Lima) localImage(ctx context.Context, img ubuntu.Image) (string, error) {
|
|
if !strings.HasPrefix(img.URL, "https://") && !strings.HasPrefix(img.URL, "file://") {
|
|
return "", fmt.Errorf("lima: Ubuntu's cloud image %s isn't an https address", img.URL)
|
|
}
|
|
sum, ok := strings.CutPrefix(img.Digest, "sha256:")
|
|
if !ok || !ubuntu.IsSHA256(sum) {
|
|
return "", fmt.Errorf("lima: the pinned image's digest %q isn't a SHA-256", img.Digest)
|
|
}
|
|
path := l.Dirs.UbuntuImage(sum)
|
|
if regular(path) {
|
|
return path, nil
|
|
}
|
|
for _, dir := range []string{l.Dirs.UbuntuImages(), l.Dirs.ImageLocks()} {
|
|
if err := os.MkdirAll(dir, 0o700); err != nil {
|
|
return "", err
|
|
}
|
|
}
|
|
unlock, err := platform.LockFile(ctx, l.Dirs.UbuntuImageLock(), msg.Waiting(l.Status, msg.UbuntuImageWaiting()))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer unlock()
|
|
if regular(path) {
|
|
return path, nil // another devvm downloaded it meanwhile
|
|
}
|
|
rel := "release-" + ubuntu.ReleaseDate(img.URL)
|
|
if ok, err := l.fromLimaCache(ctx, img, sum, path, rel); err != nil {
|
|
return "", err
|
|
} else if ok {
|
|
return path, nil
|
|
}
|
|
if err := l.trust(); err != nil {
|
|
return "", err
|
|
}
|
|
stop := l.Status(msg.DownloadingUbuntu(rel))
|
|
err = ubuntu.Download(ctx, l.ImageClient, img, path, l.LogWriter())
|
|
stop()
|
|
m, isMsg := errors.AsType[msg.M](err)
|
|
reason, unverified := fetch.Unverified(err)
|
|
switch {
|
|
case err == nil:
|
|
return path, nil
|
|
case ctx.Err() != nil:
|
|
return "", ctx.Err()
|
|
case errors.Is(err, fetch.ErrChecksum):
|
|
return "", msg.UbuntuImageChecksum(rel, img.URL)
|
|
case errors.Is(err, fetch.ErrProxy) && isMsg:
|
|
return "", m // the company proxy's own refusal, such as its password unsaved
|
|
case errors.Is(err, fetch.ErrWrite):
|
|
return "", fmt.Errorf("lima: saving Ubuntu's cloud image in %s: %w", l.Dirs.UbuntuImages(), err)
|
|
case fetch.Untrusted(err):
|
|
// A network that inspects TLS, whose CA this machine doesn't have.
|
|
return "", msg.UbuntuUntrusted(l.Dirs.TrustedCAs(), img.URL).Because(err)
|
|
case fetch.BadCertificate(err):
|
|
// A CA it trusts, on a certificate for another host or out of
|
|
// date: no CA to add, and trying again gets the same.
|
|
return "", msg.UbuntuBadCertificate(img.URL).Because(err)
|
|
case unverified:
|
|
// Any other: only its reason names the cause.
|
|
return "", msg.UbuntuCertificateInvalid(reason, img.URL).Because(err)
|
|
case fetch.Gone(err):
|
|
// Ubuntu removed the release: trying again gets the same answer.
|
|
return "", msg.UbuntuReleaseGone(rel, img.URL).Because(err)
|
|
}
|
|
_, _ = fmt.Fprintln(l.LogWriter(), err)
|
|
return "", backend.Logged(msg.UbuntuDownloadFailed(rel, img.URL).Because(err))
|
|
}
|
|
|
|
// trust makes the clients that find and download Ubuntu's cloud image
|
|
// trust Roots, the user's CAs with this machine's own, once a run, before
|
|
// their first request; with Roots nil, this machine's alone.
|
|
func (l *Lima) trust() error {
|
|
if l.trusted == nil {
|
|
return nil
|
|
}
|
|
return l.trusted()
|
|
}
|
|
|
|
// trustRoots makes clients trust the CAs roots returns, which may be
|
|
// none (nil): trust's one run.
|
|
func trustRoots(roots func() (*x509.CertPool, error), clients ...*fetch.Client) error {
|
|
if roots == nil {
|
|
return nil
|
|
}
|
|
pool, err := roots()
|
|
if err != nil || pool == nil {
|
|
return err
|
|
}
|
|
for _, c := range clients {
|
|
c.Trust(pool)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// fromLimaCache takes img out of Lima's download cache into path when Lima
|
|
// has it: copied into a new file of devvm's own, hashed as it's written,
|
|
// and kept only once its SHA-256 is the pin's (sum). Lima's cache stays
|
|
// as it is. False when it has no such entry, or one that doesn't check
|
|
// out or can't be copied (said in the run log), which the download then
|
|
// replaces; the context's error once ctx ends.
|
|
func (l *Lima) fromLimaCache(ctx context.Context, img ubuntu.Image, sum, path, rel string) (bool, error) {
|
|
dir, err := l.downloadCache()
|
|
if err != nil {
|
|
return false, nil // no cache folder: nothing to take
|
|
}
|
|
name := sha256.Sum256([]byte(img.URL))
|
|
src := filepath.Join(dir, hex.EncodeToString(name[:]), "data")
|
|
if fi, err := os.Lstat(src); err != nil || !fi.Mode().IsRegular() {
|
|
return false, nil // no entry of Lima's for the image
|
|
}
|
|
defer l.Status(msg.ImportingUbuntu(rel))()
|
|
tmp := path + ".import"
|
|
_ = os.Remove(tmp) // what a killed import left: devvm's own
|
|
got, err := copyHashed(ctx, src, tmp, ubuntu.ImageCap)
|
|
if err == nil && got == sum {
|
|
err = os.Rename(tmp, path)
|
|
if err == nil {
|
|
return true, nil
|
|
}
|
|
}
|
|
_ = os.Remove(tmp)
|
|
if ctx.Err() != nil {
|
|
return false, ctx.Err()
|
|
}
|
|
if err == nil {
|
|
err = fmt.Errorf("its SHA-256 is %s, not the pin's", got)
|
|
}
|
|
_, _ = fmt.Fprintf(l.LogWriter(), "lima: not taking Ubuntu's cloud image from Lima's cache (%s): %v\n", src, err)
|
|
return false, nil
|
|
}
|
|
|
|
// copyHashed copies the regular file src to a new file dst, mode 600, no
|
|
// more than limit bytes, returning the SHA-256 of what it wrote: the
|
|
// bytes dst holds, whatever src holds by then. It stops once ctx ends.
|
|
func copyHashed(ctx context.Context, src, dst string, limit int64) (string, error) {
|
|
in, err := os.Open(src) //nolint:gosec // Lima's cache, read only
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer func() { _ = in.Close() }()
|
|
fi, err := in.Stat()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if !fi.Mode().IsRegular() {
|
|
return "", fmt.Errorf("%s isn't a regular file", src)
|
|
}
|
|
out, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600) //nolint:gosec // devvm's own new file, checked before it's kept
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
h := sha256.New()
|
|
n, err := io.Copy(io.MultiWriter(out, h), io.LimitReader(ctxReader{ctx, in}, limit+1))
|
|
if cerr := out.Close(); err == nil {
|
|
err = cerr
|
|
}
|
|
if err == nil && n > limit {
|
|
err = fmt.Errorf("%s: %w: more than %d bytes", src, fetch.ErrTooLarge, limit)
|
|
}
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return hex.EncodeToString(h.Sum(nil)), nil
|
|
}
|
|
|
|
// ctxReader reads r until ctx ends, then fails with its error.
|
|
type ctxReader struct {
|
|
ctx context.Context //nolint:containedctx // a copy's own, which ends it
|
|
r io.Reader
|
|
}
|
|
|
|
func (c ctxReader) Read(b []byte) (int, error) {
|
|
if err := c.ctx.Err(); err != nil {
|
|
return 0, err
|
|
}
|
|
return c.r.Read(b)
|
|
}
|
|
|
|
// regular reports whether path is a regular file.
|
|
func regular(path string) bool {
|
|
fi, err := os.Lstat(path)
|
|
return err == nil && fi.Mode().IsRegular()
|
|
}
|