Streamd v0.2.0: rename, modules panel, stable/beta channels
All checks were successful
release / build-and-upload (push) Successful in 7m5s

- Rename product/host binary to streamd (drm compat copy still shipped)
- Go module path streamd; STREAMD_* env aliases; agent UA streamd-agent
- Panel Modules store + agent pull sync (modstore / modsync)
- Rolling stable + beta download tags; README/docs channel curl blocks
- Release notes in docs/releases/v0.2.0.md attached by Forgejo Actions
This commit is contained in:
404errordeveloper 2026-10-10 15:58:26 +02:00
parent 066e018749
commit da8e1123ec
140 changed files with 2949 additions and 851 deletions

View file

@ -0,0 +1,622 @@
// Package modstore keeps provider modules under the streamd data directory.
// Operators upload release-style zips; agents pull package.zip + module.yaml.
package modstore
import (
"archive/zip"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"runtime"
"sort"
"strings"
"time"
)
const (
MaxPackageBytes = 100 << 20 // 100 MiB
manifestName = "manifest.json"
packageName = "package.zip"
yamlName = "module.yaml"
)
var NameRe = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_-]{0,63}$`)
// Info is one module's metadata for the API and agent sync.
type Info struct {
Name string `json:"name"`
HasYAML bool `json:"has_yaml"`
YAMLSHA256 string `json:"yaml_sha256,omitempty"`
HasPackage bool `json:"has_package"`
PackageSHA256 string `json:"package_sha256,omitempty"`
Arches []string `json:"arches,omitempty"`
SizeBytes int64 `json:"size_bytes,omitempty"`
UploadedAt string `json:"uploaded_at,omitempty"`
YAMLUpdatedAt string `json:"yaml_updated_at,omitempty"`
}
type Manifest struct {
Name string `json:"name"`
YAMLSHA256 string `json:"yaml_sha256,omitempty"`
PackageSHA256 string `json:"package_sha256,omitempty"`
Arches []string `json:"arches,omitempty"`
SizeBytes int64 `json:"size_bytes,omitempty"`
UploadedAt string `json:"uploaded_at,omitempty"`
YAMLUpdatedAt string `json:"yaml_updated_at,omitempty"`
}
// Store is the on-disk module library under <data>/modules.
type Store struct {
Root string
}
func New(root string) *Store {
return &Store{Root: root}
}
func (s *Store) modulesDir() string {
return s.Root
}
func (s *Store) modDir(name string) string {
return filepath.Join(s.Root, name)
}
func (s *Store) Ensure() error {
return os.MkdirAll(s.Root, 0o755)
}
// List returns every module directory under the store.
func (s *Store) List() ([]Info, error) {
ents, err := os.ReadDir(s.Root)
if err != nil {
if os.IsNotExist(err) {
return []Info{}, nil
}
return nil, err
}
out := make([]Info, 0, len(ents))
for _, e := range ents {
if !e.IsDir() || strings.HasPrefix(e.Name(), ".") {
continue
}
if !NameRe.MatchString(e.Name()) {
continue
}
out = append(out, s.Info(e.Name()))
}
sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name })
return out, nil
}
// Info loads manifest + checks for yaml/package files.
func (s *Store) Info(name string) Info {
info := Info{Name: name}
if !NameRe.MatchString(name) {
return info
}
dir := s.modDir(name)
m, err := s.readManifest(dir)
if err == nil {
info.YAMLSHA256 = m.YAMLSHA256
info.PackageSHA256 = m.PackageSHA256
info.Arches = append([]string(nil), m.Arches...)
info.SizeBytes = m.SizeBytes
info.UploadedAt = m.UploadedAt
info.YAMLUpdatedAt = m.YAMLUpdatedAt
info.HasYAML = m.YAMLSHA256 != ""
info.HasPackage = m.PackageSHA256 != ""
}
// Prefer live file hashes when present so a hand-edited yaml still shows.
if b, err := os.ReadFile(filepath.Join(dir, yamlName)); err == nil {
info.HasYAML = true
sum := sha256.Sum256(b)
info.YAMLSHA256 = hex.EncodeToString(sum[:])
}
if st, err := os.Stat(filepath.Join(dir, packageName)); err == nil && !st.IsDir() {
info.HasPackage = true
if info.PackageSHA256 == "" {
if sum, err := fileSHA256(filepath.Join(dir, packageName)); err == nil {
info.PackageSHA256 = sum
}
}
if info.SizeBytes == 0 {
info.SizeBytes = st.Size()
}
}
return info
}
// ReadYAML returns module.yaml bytes.
func (s *Store) ReadYAML(name string) ([]byte, error) {
if !NameRe.MatchString(name) {
return nil, fmt.Errorf("invalid module name")
}
b, err := os.ReadFile(filepath.Join(s.modDir(name), yamlName))
if err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("module %s: no module.yaml", name)
}
return nil, err
}
return b, nil
}
// WriteYAML atomically replaces module.yaml and updates the manifest.
func (s *Store) WriteYAML(name string, yamlBytes []byte) (Info, error) {
if !NameRe.MatchString(name) {
return Info{}, fmt.Errorf("invalid module name")
}
if len(strings.TrimSpace(string(yamlBytes))) == 0 {
return Info{}, fmt.Errorf("empty module.yaml")
}
dir := s.modDir(name)
if err := os.MkdirAll(dir, 0o755); err != nil {
return Info{}, err
}
dest := filepath.Join(dir, yamlName)
if err := atomicWrite(dest, yamlBytes, 0o644); err != nil {
return Info{}, err
}
sum := sha256.Sum256(yamlBytes)
now := time.Now().UTC().Format(time.RFC3339)
m, _ := s.readManifest(dir)
m.Name = name
m.YAMLSHA256 = hex.EncodeToString(sum[:])
m.YAMLUpdatedAt = now
if m.UploadedAt == "" {
m.UploadedAt = now
}
if err := s.writeManifest(dir, m); err != nil {
return Info{}, err
}
return s.Info(name), nil
}
// OpenPackage returns a ReadCloser for package.zip plus size.
func (s *Store) OpenPackage(name string) (*os.File, int64, error) {
if !NameRe.MatchString(name) {
return nil, 0, fmt.Errorf("invalid module name")
}
path := filepath.Join(s.modDir(name), packageName)
f, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
return nil, 0, fmt.Errorf("module %s: no package.zip", name)
}
return nil, 0, err
}
st, err := f.Stat()
if err != nil {
_ = f.Close()
return nil, 0, err
}
return f, st.Size(), nil
}
// Delete removes the module directory.
func (s *Store) Delete(name string) error {
if !NameRe.MatchString(name) {
return fmt.Errorf("invalid module name")
}
dir := s.modDir(name)
if _, err := os.Stat(dir); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("module %s not found", name)
}
return err
}
return os.RemoveAll(dir)
}
// InstallZip stores zipBytes as package.zip, extracts into the module dir,
// promotes the host-arch binary, and writes the manifest.
// nameHint may be empty; the zip layout is used to infer the name.
func (s *Store) InstallZip(zipBytes []byte, nameHint string) (Info, error) {
if len(zipBytes) == 0 {
return Info{}, fmt.Errorf("empty zip")
}
if int64(len(zipBytes)) > MaxPackageBytes {
return Info{}, fmt.Errorf("zip exceeds %d byte limit", MaxPackageBytes)
}
name, entries, err := inspectZip(zipBytes, nameHint)
if err != nil {
return Info{}, err
}
if !NameRe.MatchString(name) {
return Info{}, fmt.Errorf("invalid module name %q", name)
}
if err := s.Ensure(); err != nil {
return Info{}, err
}
dir := s.modDir(name)
tmpDir, err := os.MkdirTemp(s.Root, "."+name+".tmp-*")
if err != nil {
return Info{}, err
}
defer os.RemoveAll(tmpDir)
if err := extractZipEntries(zipBytes, entries, tmpDir); err != nil {
return Info{}, err
}
if err := promoteHostBinary(tmpDir, name); err != nil {
return Info{}, err
}
yamlPath := filepath.Join(tmpDir, yamlName)
if _, err := os.Stat(yamlPath); err != nil {
return Info{}, fmt.Errorf("zip missing module.yaml")
}
arches := detectArches(tmpDir, name)
pkgSum := sha256.Sum256(zipBytes)
yamlRaw, err := os.ReadFile(yamlPath)
if err != nil {
return Info{}, err
}
yamlSum := sha256.Sum256(yamlRaw)
now := time.Now().UTC().Format(time.RFC3339)
m := Manifest{
Name: name,
YAMLSHA256: hex.EncodeToString(yamlSum[:]),
PackageSHA256: hex.EncodeToString(pkgSum[:]),
Arches: arches,
SizeBytes: int64(len(zipBytes)),
UploadedAt: now,
YAMLUpdatedAt: now,
}
if err := s.writeManifest(tmpDir, m); err != nil {
return Info{}, err
}
if err := atomicWrite(filepath.Join(tmpDir, packageName), zipBytes, 0o644); err != nil {
return Info{}, err
}
// Replace the live module dir.
backup := dir + ".bak"
_ = os.RemoveAll(backup)
if _, err := os.Stat(dir); err == nil {
if err := os.Rename(dir, backup); err != nil {
return Info{}, err
}
}
if err := os.Rename(tmpDir, dir); err != nil {
_ = os.Rename(backup, dir)
return Info{}, err
}
_ = os.RemoveAll(backup)
return s.Info(name), nil
}
type zipEntry struct {
// Rel is the path relative to the module root inside the zip (forward slashes).
Rel string
File *zip.File
}
func inspectZip(zipBytes []byte, nameHint string) (name string, entries []zipEntry, err error) {
zr, err := zip.NewReader(newBytesReaderAt(zipBytes), int64(len(zipBytes)))
if err != nil {
return "", nil, fmt.Errorf("open zip: %w", err)
}
return inspectZipReader(zr, nameHint)
}
type bytesReaderAt struct{ b []byte }
func newBytesReaderAt(b []byte) *bytesReaderAt { return &bytesReaderAt{b: b} }
func (r *bytesReaderAt) ReadAt(p []byte, off int64) (int, error) {
if off < 0 {
return 0, errors.New("negative offset")
}
if off >= int64(len(r.b)) {
return 0, io.EOF
}
n := copy(p, r.b[off:])
if n < len(p) {
return n, io.EOF
}
return n, nil
}
func inspectZipReader(zr *zip.Reader, nameHint string) (string, []zipEntry, error) {
hint := strings.TrimSpace(nameHint)
if hint != "" && !NameRe.MatchString(hint) {
return "", nil, fmt.Errorf("invalid module name %q", hint)
}
prefix := ""
topDirs := map[string]struct{}{}
hasRootFile := false
for _, f := range zr.File {
name := strings.ReplaceAll(f.Name, "\\", "/")
name = strings.TrimPrefix(name, "./")
if name == "" || strings.HasPrefix(name, "__MACOSX/") || strings.HasSuffix(name, ".DS_Store") {
continue
}
parts := strings.Split(name, "/")
if len(parts) > 0 && parts[0] != "" {
if f.FileInfo().IsDir() || len(parts) > 1 {
topDirs[parts[0]] = struct{}{}
}
if len(parts) == 1 && !f.FileInfo().IsDir() {
hasRootFile = true
}
}
}
switch {
case hint != "":
prefix = ""
// If every entry is under hint/, strip it.
allUnder := true
for _, f := range zr.File {
n := strings.ReplaceAll(f.Name, "\\", "/")
n = strings.TrimPrefix(n, "./")
if n == "" || strings.HasPrefix(n, "__MACOSX/") {
continue
}
if n != hint && !strings.HasPrefix(n, hint+"/") {
allUnder = false
break
}
}
if allUnder {
prefix = hint + "/"
}
case hasRootFile:
// Flat zip: module.yaml at root. Name must be provided or inferred from binary.
prefix = ""
case len(topDirs) == 1:
for d := range topDirs {
prefix = d + "/"
if hint == "" {
hint = d
}
}
default:
return "", nil, fmt.Errorf("cannot infer module name from zip; pass name= or use a single top-level folder")
}
if hint == "" {
// Try to infer from .bin/<arch>/<name> or .bin/<name>.
for _, f := range zr.File {
n := strings.ReplaceAll(f.Name, "\\", "/")
n = strings.TrimPrefix(n, "./")
if prefix != "" {
if !strings.HasPrefix(n, prefix) {
continue
}
n = strings.TrimPrefix(n, prefix)
}
base := filepath.Base(n)
base = strings.TrimSuffix(base, ".exe")
if strings.HasPrefix(n, ".bin/") && NameRe.MatchString(base) && base != ".bin" {
hint = base
break
}
}
}
if hint == "" {
return "", nil, fmt.Errorf("cannot infer module name from zip; pass name=")
}
if !NameRe.MatchString(hint) {
return "", nil, fmt.Errorf("invalid module name %q", hint)
}
var entries []zipEntry
for _, f := range zr.File {
n := strings.ReplaceAll(f.Name, "\\", "/")
n = strings.TrimPrefix(n, "./")
if n == "" || strings.HasPrefix(n, "__MACOSX/") || strings.HasSuffix(n, ".DS_Store") {
continue
}
rel := n
if prefix != "" {
if !strings.HasPrefix(n, prefix) {
continue
}
rel = strings.TrimPrefix(n, prefix)
}
if rel == "" {
continue
}
if strings.Contains(rel, "..") {
return "", nil, fmt.Errorf("zip slip: %s", f.Name)
}
entries = append(entries, zipEntry{Rel: rel, File: f})
}
if len(entries) == 0 {
return "", nil, fmt.Errorf("zip has no files for module %s", hint)
}
return hint, entries, nil
}
func extractZipEntries(zipBytes []byte, entries []zipEntry, destDir string) error {
_ = zipBytes // entries already hold openable *zip.File from the same buffer
for _, e := range entries {
target := filepath.Join(destDir, filepath.FromSlash(e.Rel))
// Zip-slip: ensure target stays under destDir.
if !isUnder(destDir, target) {
return fmt.Errorf("zip slip: %s", e.Rel)
}
if e.File.FileInfo().IsDir() || strings.HasSuffix(e.Rel, "/") {
if err := os.MkdirAll(target, 0o755); err != nil {
return err
}
continue
}
if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
return err
}
rc, err := e.File.Open()
if err != nil {
return err
}
mode := e.File.Mode()
if mode&0o111 != 0 {
mode = 0o755
} else {
mode = 0o644
}
err = copyToFile(target, rc, mode)
_ = rc.Close()
if err != nil {
return err
}
}
return nil
}
func isUnder(root, path string) bool {
absRoot, err1 := filepath.Abs(root)
absPath, err2 := filepath.Abs(path)
if err1 != nil || err2 != nil {
return false
}
rel, err := filepath.Rel(absRoot, absPath)
if err != nil {
return false
}
return rel == "." || (!strings.HasPrefix(rel, "..") && !filepath.IsAbs(rel))
}
func copyToFile(dest string, r io.Reader, mode os.FileMode) error {
tmp := dest + ".partial"
f, err := os.OpenFile(tmp, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, mode)
if err != nil {
return err
}
_, copyErr := io.Copy(f, r)
closeErr := f.Close()
if copyErr != nil {
_ = os.Remove(tmp)
return copyErr
}
if closeErr != nil {
_ = os.Remove(tmp)
return closeErr
}
_ = os.Remove(dest)
return os.Rename(tmp, dest)
}
func promoteHostBinary(modDir, name string) error {
want := name
if runtime.GOOS == "windows" {
want += ".exe"
}
dest := filepath.Join(modDir, ".bin", want)
if fileExists(dest) {
return nil
}
archDir := filepath.Join(modDir, ".bin", runtime.GOOS+"-"+runtime.GOARCH)
src := filepath.Join(archDir, want)
if !fileExists(src) {
// Soft: describe may fail later; zip install still succeeds.
return nil
}
if err := os.MkdirAll(filepath.Dir(dest), 0o755); err != nil {
return err
}
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
return copyToFile(dest, in, 0o755)
}
func detectArches(modDir, name string) []string {
binRoot := filepath.Join(modDir, ".bin")
ents, err := os.ReadDir(binRoot)
if err != nil {
return nil
}
seen := map[string]struct{}{}
var arches []string
add := func(a string) {
if _, ok := seen[a]; ok {
return
}
seen[a] = struct{}{}
arches = append(arches, a)
}
for _, e := range ents {
if e.IsDir() {
sub := e.Name() // e.g. windows-amd64
if strings.Contains(sub, "-") {
add(sub)
}
continue
}
base := strings.TrimSuffix(e.Name(), ".exe")
if base == name {
add(runtime.GOOS + "-" + runtime.GOARCH)
}
}
sort.Strings(arches)
return arches
}
func (s *Store) readManifest(dir string) (Manifest, error) {
var m Manifest
b, err := os.ReadFile(filepath.Join(dir, manifestName))
if err != nil {
return m, err
}
if err := json.Unmarshal(b, &m); err != nil {
return m, err
}
return m, nil
}
func (s *Store) writeManifest(dir string, m Manifest) error {
b, err := json.MarshalIndent(m, "", " ")
if err != nil {
return err
}
b = append(b, '\n')
return atomicWrite(filepath.Join(dir, manifestName), b, 0o644)
}
func atomicWrite(path string, data []byte, mode os.FileMode) error {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
}
tmp := path + ".tmp"
if err := os.WriteFile(tmp, data, mode); err != nil {
return err
}
if err := os.Rename(tmp, path); err != nil {
_ = os.Remove(tmp)
return err
}
return nil
}
func fileSHA256(path string) (string, error) {
f, err := os.Open(path)
if err != nil {
return "", err
}
defer f.Close()
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
func fileExists(p string) bool {
st, err := os.Stat(p)
return err == nil && !st.IsDir()
}