ollama/server/manifest.go

165 lines
2.9 KiB
Go
Raw Normal View History

2024-04-17 21:54:14 +00:00
package server
import (
"crypto/sha256"
"encoding/json"
"errors"
2024-04-17 21:54:14 +00:00
"fmt"
"io"
2024-05-06 23:34:13 +00:00
"log/slog"
2024-04-17 21:54:14 +00:00
"os"
"path/filepath"
"github.com/ollama/ollama/types/model"
)
type Manifest struct {
ManifestV2
filepath string
2024-05-06 23:34:13 +00:00
fi os.FileInfo
digest string
2024-04-17 21:54:14 +00:00
}
func (m *Manifest) Size() (size int64) {
for _, layer := range append(m.Layers, m.Config) {
size += layer.Size
}
return
}
func (m *Manifest) Remove() error {
if err := os.Remove(m.filepath); err != nil {
return err
}
manifests, err := GetManifestPath()
if err != nil {
return err
}
return PruneDirectory(manifests)
}
func (m *Manifest) RemoveLayers() error {
for _, layer := range append(m.Layers, m.Config) {
if err := layer.Remove(); errors.Is(err, os.ErrNotExist) {
slog.Debug("layer does not exist", "digest", layer.Digest)
} else if err != nil {
return err
}
}
return nil
}
func ParseNamedManifest(n model.Name) (*Manifest, error) {
if !n.IsFullyQualified() {
return nil, model.Unqualified(n)
2024-04-17 21:54:14 +00:00
}
manifests, err := GetManifestPath()
if err != nil {
return nil, err
}
p := filepath.Join(manifests, n.Filepath())
var m ManifestV2
f, err := os.Open(p)
2024-04-17 21:54:14 +00:00
if err != nil {
return nil, err
}
defer f.Close()
2024-04-17 21:54:14 +00:00
2024-05-06 23:34:13 +00:00
fi, err := f.Stat()
if err != nil {
return nil, err
}
2024-04-17 21:54:14 +00:00
sha256sum := sha256.New()
if err := json.NewDecoder(io.TeeReader(f, sha256sum)).Decode(&m); err != nil {
2024-04-17 21:54:14 +00:00
return nil, err
}
return &Manifest{
ManifestV2: m,
filepath: p,
2024-05-06 23:34:13 +00:00
fi: fi,
digest: fmt.Sprintf("%x", sha256sum.Sum(nil)),
2024-04-17 21:54:14 +00:00
}, nil
}
func WriteManifest(name model.Name, config *Layer, layers []*Layer) error {
manifests, err := GetManifestPath()
if err != nil {
return err
2024-04-17 21:54:14 +00:00
}
p := filepath.Join(manifests, name.Filepath())
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
2024-04-17 21:54:14 +00:00
return err
}
f, err := os.Create(p)
2024-04-17 21:54:14 +00:00
if err != nil {
return err
}
defer f.Close()
2024-04-17 21:54:14 +00:00
m := ManifestV2{
SchemaVersion: 2,
MediaType: "application/vnd.docker.distribution.manifest.v2+json",
Config: config,
Layers: layers,
2024-04-17 21:54:14 +00:00
}
return json.NewEncoder(f).Encode(m)
2024-04-17 21:54:14 +00:00
}
func Manifests() (map[model.Name]*Manifest, error) {
manifests, err := GetManifestPath()
if err != nil {
return nil, err
}
// TODO(mxyng): use something less brittle
2024-05-08 22:56:40 +00:00
matches, err := filepath.Glob(filepath.Join(manifests, "*", "*", "*", "*"))
if err != nil {
return nil, err
}
ms := make(map[model.Name]*Manifest)
for _, match := range matches {
2024-05-09 17:00:18 +00:00
fi, err := os.Stat(match)
if err != nil {
return nil, err
}
2024-05-09 17:00:18 +00:00
if !fi.IsDir() {
rel, err := filepath.Rel(manifests, match)
if err != nil {
slog.Warn("bad filepath", "path", match, "error", err)
continue
}
n := model.ParseNameFromFilepath(rel)
if !n.IsValid() {
slog.Warn("bad manifest name", "path", rel, "error", err)
continue
}
m, err := ParseNamedManifest(n)
if err != nil {
2024-05-06 23:34:13 +00:00
slog.Warn("bad manifest", "name", n, "error", err)
continue
}
ms[n] = m
}
}
return ms, nil
}