2024-04-17 21:54:14 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
2024-08-01 21:52:15 +00:00
|
|
|
"encoding/hex"
|
2024-04-17 21:54:14 +00:00
|
|
|
"encoding/json"
|
2024-06-10 18:15:03 +00:00
|
|
|
"errors"
|
2024-08-15 17:29:14 +00:00
|
|
|
"fmt"
|
2024-04-17 21:54:14 +00:00
|
|
|
"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 {
|
2024-08-07 21:22:17 +00:00
|
|
|
SchemaVersion int `json:"schemaVersion"`
|
|
|
|
MediaType string `json:"mediaType"`
|
|
|
|
Config Layer `json:"config"`
|
|
|
|
Layers []Layer `json:"layers"`
|
2024-04-18 00:23:19 +00:00
|
|
|
|
|
|
|
filepath string
|
2024-05-06 23:34:13 +00:00
|
|
|
fi os.FileInfo
|
2024-04-18 00:23:19 +00:00
|
|
|
digest string
|
2024-04-17 21:54:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manifest) Size() (size int64) {
|
2024-08-07 21:22:17 +00:00
|
|
|
for _, layer := range append(m.Layers, m.Config) {
|
2024-04-17 21:54:14 +00:00
|
|
|
size += layer.Size
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-18 00:23:19 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-05-08 21:36:08 +00:00
|
|
|
func (m *Manifest) RemoveLayers() error {
|
2024-08-07 21:22:17 +00:00
|
|
|
for _, layer := range append(m.Layers, m.Config) {
|
2024-08-06 00:13:52 +00:00
|
|
|
if layer.Digest != "" {
|
|
|
|
if err := layer.Remove(); errors.Is(err, os.ErrNotExist) {
|
|
|
|
slog.Debug("layer does not exist", "digest", layer.Digest)
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-08 21:36:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-18 00:23:19 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-04-18 00:23:19 +00:00
|
|
|
p := filepath.Join(manifests, n.Filepath())
|
|
|
|
|
2024-06-10 15:47:13 +00:00
|
|
|
var m Manifest
|
2024-04-18 00:23:19 +00:00
|
|
|
f, err := os.Open(p)
|
2024-04-17 21:54:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-18 00:23:19 +00:00
|
|
|
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()
|
2024-04-18 00:23:19 +00:00
|
|
|
if err := json.NewDecoder(io.TeeReader(f, sha256sum)).Decode(&m); err != nil {
|
2024-04-17 21:54:14 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-06-10 15:47:13 +00:00
|
|
|
m.filepath = p
|
|
|
|
m.fi = fi
|
2024-08-01 21:52:15 +00:00
|
|
|
m.digest = hex.EncodeToString(sha256sum.Sum(nil))
|
2024-06-10 15:47:13 +00:00
|
|
|
|
|
|
|
return &m, nil
|
2024-04-17 21:54:14 +00:00
|
|
|
}
|
|
|
|
|
2024-08-07 21:22:17 +00:00
|
|
|
func WriteManifest(name model.Name, config Layer, layers []Layer) error {
|
2024-05-08 21:36:08 +00:00
|
|
|
manifests, err := GetManifestPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-04-17 21:54:14 +00:00
|
|
|
}
|
|
|
|
|
2024-05-08 21:36:08 +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
|
|
|
|
}
|
|
|
|
|
2024-05-08 21:36:08 +00:00
|
|
|
f, err := os.Create(p)
|
2024-04-17 21:54:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-08 21:36:08 +00:00
|
|
|
defer f.Close()
|
2024-04-17 21:54:14 +00:00
|
|
|
|
2024-06-10 15:47:13 +00:00
|
|
|
m := Manifest{
|
2024-05-08 21:36:08 +00:00
|
|
|
SchemaVersion: 2,
|
|
|
|
MediaType: "application/vnd.docker.distribution.manifest.v2+json",
|
2024-08-07 21:22:17 +00:00
|
|
|
Config: config,
|
2024-05-08 21:36:08 +00:00
|
|
|
Layers: layers,
|
2024-04-17 21:54:14 +00:00
|
|
|
}
|
|
|
|
|
2024-05-08 21:36:08 +00:00
|
|
|
return json.NewEncoder(f).Encode(m)
|
2024-04-17 21:54:14 +00:00
|
|
|
}
|
2024-04-18 00:23:19 +00:00
|
|
|
|
2024-11-05 22:21:45 +00:00
|
|
|
func Manifests(continueOnError bool) (map[model.Name]*Manifest, error) {
|
2024-04-18 00:23:19 +00:00
|
|
|
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, "*", "*", "*", "*"))
|
2024-04-18 00:23:19 +00:00
|
|
|
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)
|
2024-04-18 00:23:19 +00:00
|
|
|
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 {
|
2024-11-05 22:21:45 +00:00
|
|
|
if !continueOnError {
|
|
|
|
return nil, fmt.Errorf("%s %w", match, err)
|
|
|
|
}
|
2024-05-09 17:00:18 +00:00
|
|
|
slog.Warn("bad filepath", "path", match, "error", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
n := model.ParseNameFromFilepath(rel)
|
|
|
|
if !n.IsValid() {
|
2024-11-05 22:21:45 +00:00
|
|
|
if !continueOnError {
|
|
|
|
return nil, fmt.Errorf("%s %w", rel, err)
|
|
|
|
}
|
2024-08-14 23:36:07 +00:00
|
|
|
slog.Warn("bad manifest name", "path", rel)
|
2024-05-09 17:00:18 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-04-18 00:23:19 +00:00
|
|
|
m, err := ParseNamedManifest(n)
|
2024-11-05 22:21:45 +00:00
|
|
|
if err != nil {
|
|
|
|
if !continueOnError {
|
|
|
|
return nil, fmt.Errorf("%s %w", n, err)
|
|
|
|
}
|
2024-05-06 23:34:13 +00:00
|
|
|
slog.Warn("bad manifest", "name", n, "error", err)
|
|
|
|
continue
|
2024-04-18 00:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ms[n] = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ms, nil
|
|
|
|
}
|