- Runtime: list and fetch runtimes from this repo's GitHub Releases (stable by default, candidate on request), with SHA256 check and resume. CLI gains `runtime available` and `runtime fetch`; the host page gets an online install row and a "receive candidates" switch. - First launch: pick a data folder (non-system drives first, USB never recommended); the choice is recorded in %APPDATA%\reims-go\location.json. Existing installs in the default folder are adopted without asking. - pack-runtime.ps1: also builds the matching source package, records every patch with its SHA256 and the channel, and refuses undeclared changes in the build trees. - patches: declare reims-vgpu-cargo-lock.patch (Cargo.lock source lines). - UI: sidebar and VM page restyle, new design tokens. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
330 lines
9.3 KiB
Go
330 lines
9.3 KiB
Go
package runtime
|
|
|
|
// Online install: runtime packages are GitHub Release assets of the reims-go
|
|
// repository (decided 2026-11-28, AGENTS.md). A runtime release is tagged
|
|
// "runtime-<commit>" and carries:
|
|
//
|
|
// reims-runtime-win-x64-<commit>.tar.zst
|
|
// reims-runtime-src-<commit>.tar.zst (matching sources, spec §7)
|
|
// SHA256SUMS.txt
|
|
//
|
|
// A GitHub prerelease is the candidate channel; a full release is stable
|
|
// (spec §6). The client lists stable only unless candidate is asked for.
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
)
|
|
|
|
const (
|
|
DefaultReleasesRepo = "Nwflower/reims-go"
|
|
DefaultAPIBase = "https://api.github.com"
|
|
runtimeTagPrefix = "runtime-"
|
|
sumsAsset = "SHA256SUMS.txt"
|
|
)
|
|
|
|
const (
|
|
ChannelStable = "stable"
|
|
ChannelCandidate = "candidate"
|
|
)
|
|
|
|
// Source is where runtime releases are listed. REIMS_GO_RELEASES_REPO
|
|
// overrides the repository (for forks and testing).
|
|
type Source struct {
|
|
APIBase string
|
|
Repo string
|
|
Client *http.Client
|
|
}
|
|
|
|
func DefaultSource() Source {
|
|
repo := os.Getenv("REIMS_GO_RELEASES_REPO")
|
|
if repo == "" {
|
|
repo = DefaultReleasesRepo
|
|
}
|
|
return Source{APIBase: DefaultAPIBase, Repo: repo, Client: &http.Client{Timeout: 0}}
|
|
}
|
|
|
|
// Remote is a runtime package published as a release.
|
|
type Remote struct {
|
|
Commit string `json:"commit"`
|
|
Tag string `json:"tag"`
|
|
Channel string `json:"channel"`
|
|
PublishedAt string `json:"published_at"`
|
|
PageURL string `json:"page_url"`
|
|
ArchiveName string `json:"archive_name"`
|
|
ArchiveURL string `json:"archive_url"`
|
|
ArchiveSize int64 `json:"archive_size"`
|
|
SumsURL string `json:"sums_url"`
|
|
SourceName string `json:"source_name,omitempty"`
|
|
SourceURL string `json:"source_url,omitempty"`
|
|
}
|
|
|
|
type ghRelease struct {
|
|
TagName string `json:"tag_name"`
|
|
Draft bool `json:"draft"`
|
|
Prerelease bool `json:"prerelease"`
|
|
PublishedAt string `json:"published_at"`
|
|
HTMLURL string `json:"html_url"`
|
|
Assets []ghAsset `json:"assets"`
|
|
}
|
|
|
|
type ghAsset struct {
|
|
Name string `json:"name"`
|
|
Size int64 `json:"size"`
|
|
URL string `json:"browser_download_url"`
|
|
}
|
|
|
|
func (s Source) client() *http.Client {
|
|
if s.Client != nil {
|
|
return s.Client
|
|
}
|
|
return http.DefaultClient
|
|
}
|
|
|
|
func newRequest(ctx context.Context, url string) (*http.Request, error) {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// A generic client name; nothing about the user (GitHub requires a User-Agent).
|
|
req.Header.Set("User-Agent", "reims-go")
|
|
return req, nil
|
|
}
|
|
|
|
// ListRemote lists published runtime packages, newest first. Candidates are
|
|
// included only when includeCandidate is set.
|
|
func (s Source) ListRemote(ctx context.Context, includeCandidate bool) ([]Remote, error) {
|
|
url := fmt.Sprintf("%s/repos/%s/releases?per_page=100", strings.TrimRight(s.APIBase, "/"), s.Repo)
|
|
req, err := newRequest(ctx, url)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("Accept", "application/vnd.github+json")
|
|
resp, err := s.client().Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("list runtime releases: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusTooManyRequests {
|
|
return nil, fmt.Errorf("GitHub API rate limit reached; try again later (HTTP %d)", resp.StatusCode)
|
|
}
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("list runtime releases: HTTP %d", resp.StatusCode)
|
|
}
|
|
var rels []ghRelease
|
|
if err := json.NewDecoder(resp.Body).Decode(&rels); err != nil {
|
|
return nil, fmt.Errorf("read runtime releases: %w", err)
|
|
}
|
|
var out []Remote
|
|
for _, r := range rels {
|
|
if r.Draft || !strings.HasPrefix(r.TagName, runtimeTagPrefix) {
|
|
continue
|
|
}
|
|
commit := strings.TrimPrefix(r.TagName, runtimeTagPrefix)
|
|
rem := Remote{Commit: commit, Tag: r.TagName, PublishedAt: r.PublishedAt, PageURL: r.HTMLURL, Channel: ChannelStable}
|
|
if r.Prerelease {
|
|
rem.Channel = ChannelCandidate
|
|
}
|
|
for _, a := range r.Assets {
|
|
switch {
|
|
case a.Name == "reims-runtime-win-x64-"+commit+".tar.zst":
|
|
rem.ArchiveName, rem.ArchiveURL, rem.ArchiveSize = a.Name, a.URL, a.Size
|
|
case a.Name == sumsAsset:
|
|
rem.SumsURL = a.URL
|
|
case strings.HasPrefix(a.Name, "reims-runtime-src-"):
|
|
rem.SourceName, rem.SourceURL = a.Name, a.URL
|
|
}
|
|
}
|
|
// A release without its sums or sources is incomplete: never offer it.
|
|
if rem.ArchiveURL == "" || rem.SumsURL == "" || rem.SourceURL == "" {
|
|
continue
|
|
}
|
|
if rem.Channel == ChannelCandidate && !includeCandidate {
|
|
continue
|
|
}
|
|
out = append(out, rem)
|
|
}
|
|
sort.SliceStable(out, func(i, j int) bool { return out[i].PublishedAt > out[j].PublishedAt })
|
|
return out, nil
|
|
}
|
|
|
|
// Pick finds a release by commit prefix, or the newest for "" / "latest".
|
|
func Pick(list []Remote, want string) (*Remote, error) {
|
|
want = strings.TrimSpace(want)
|
|
if len(list) == 0 {
|
|
return nil, errors.New("no runtime release is published for this channel")
|
|
}
|
|
if want == "" || strings.EqualFold(want, "latest") {
|
|
return &list[0], nil
|
|
}
|
|
for i := range list {
|
|
if strings.HasPrefix(list[i].Commit, want) || strings.HasPrefix(want, list[i].Commit) {
|
|
return &list[i], nil
|
|
}
|
|
}
|
|
return nil, fmt.Errorf("no runtime release for %q", want)
|
|
}
|
|
|
|
// Progress reports bytes of the archive download.
|
|
type Progress func(done, total int64)
|
|
|
|
// Download fetches r's archive into dir, continuing a partial download, and
|
|
// checks it against the release's SHA256SUMS.txt. It returns the archive path.
|
|
func (s Source) Download(ctx context.Context, r Remote, dir string, onProgress Progress) (string, error) {
|
|
if onProgress == nil {
|
|
onProgress = func(int64, int64) {}
|
|
}
|
|
want, err := s.expectedSum(ctx, r)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
return "", err
|
|
}
|
|
final := filepath.Join(dir, r.ArchiveName)
|
|
if got, err := fileSHA256(final); err == nil && strings.EqualFold(got, want) {
|
|
onProgress(r.ArchiveSize, r.ArchiveSize)
|
|
return final, nil
|
|
}
|
|
part := final + ".part"
|
|
|
|
var have int64
|
|
if st, err := os.Stat(part); err == nil {
|
|
have = st.Size()
|
|
}
|
|
req, err := newRequest(ctx, r.ArchiveURL)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if have > 0 {
|
|
req.Header.Set("Range", "bytes="+strconv.FormatInt(have, 10)+"-")
|
|
}
|
|
resp, err := s.client().Do(req)
|
|
if err != nil {
|
|
return "", fmt.Errorf("download %s: %w", r.ArchiveName, err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
flags := os.O_CREATE | os.O_WRONLY
|
|
switch resp.StatusCode {
|
|
case http.StatusPartialContent:
|
|
flags |= os.O_APPEND
|
|
case http.StatusOK:
|
|
have = 0 // server ignored the range: start over
|
|
flags |= os.O_TRUNC
|
|
case http.StatusRequestedRangeNotSatisfiable:
|
|
os.Remove(part)
|
|
return s.Download(ctx, r, dir, onProgress)
|
|
default:
|
|
return "", fmt.Errorf("download %s: HTTP %d", r.ArchiveName, resp.StatusCode)
|
|
}
|
|
total := r.ArchiveSize
|
|
if total <= 0 && resp.ContentLength > 0 {
|
|
total = have + resp.ContentLength
|
|
}
|
|
|
|
f, err := os.OpenFile(part, flags, 0o644)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
done := have
|
|
onProgress(done, total)
|
|
buf := make([]byte, 256*1024)
|
|
last := time.Now()
|
|
for {
|
|
n, rerr := resp.Body.Read(buf)
|
|
if n > 0 {
|
|
if _, werr := f.Write(buf[:n]); werr != nil {
|
|
f.Close()
|
|
return "", werr
|
|
}
|
|
done += int64(n)
|
|
if time.Since(last) > 100*time.Millisecond {
|
|
onProgress(done, total)
|
|
last = time.Now()
|
|
}
|
|
}
|
|
if rerr == io.EOF {
|
|
break
|
|
}
|
|
if rerr != nil {
|
|
f.Close()
|
|
return "", fmt.Errorf("download %s: %w", r.ArchiveName, rerr) // .part kept for resume
|
|
}
|
|
}
|
|
if err := f.Close(); err != nil {
|
|
return "", err
|
|
}
|
|
onProgress(done, total)
|
|
|
|
got, err := fileSHA256(part)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if !strings.EqualFold(got, want) {
|
|
os.Remove(part)
|
|
return "", fmt.Errorf("%s: SHA256 mismatch (expected %s, got %s); the download was discarded", r.ArchiveName, want, got)
|
|
}
|
|
if err := os.Rename(part, final); err != nil {
|
|
return "", err
|
|
}
|
|
return final, nil
|
|
}
|
|
|
|
// expectedSum reads r's archive hash from the release's SHA256SUMS.txt
|
|
// ("<hex> <name>" lines, as sha256sum writes them).
|
|
func (s Source) expectedSum(ctx context.Context, r Remote) (string, error) {
|
|
req, err := newRequest(ctx, r.SumsURL)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
resp, err := s.client().Do(req)
|
|
if err != nil {
|
|
return "", fmt.Errorf("download %s: %w", sumsAsset, err)
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
return "", fmt.Errorf("download %s: HTTP %d", sumsAsset, resp.StatusCode)
|
|
}
|
|
sc := bufio.NewScanner(io.LimitReader(resp.Body, 1<<20))
|
|
for sc.Scan() {
|
|
fields := strings.Fields(sc.Text())
|
|
if len(fields) == 2 && strings.TrimPrefix(fields[1], "*") == r.ArchiveName && len(fields[0]) == 64 {
|
|
return strings.ToLower(fields[0]), nil
|
|
}
|
|
}
|
|
return "", fmt.Errorf("%s has no entry for %s", sumsAsset, r.ArchiveName)
|
|
}
|
|
|
|
// DownloadsDir is where runtime archives are downloaded before install.
|
|
func DownloadsDir(cfg *config.Config) string {
|
|
return filepath.Join(cfg.DataRoot, "downloads")
|
|
}
|
|
|
|
// IsInstalled reports whether commit is already unpacked under runtimes/.
|
|
func IsInstalled(cfg *config.Config, commit string) bool {
|
|
list, err := List(cfg)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
for _, it := range list {
|
|
if strings.HasPrefix(it.Commit, commit) || strings.HasPrefix(commit, it.Commit) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|