mirror of
https://github.com/mudler/LocalAI.git
synced 2026-07-03 04:46:54 -04:00
* fix(backends): make backend install ops idempotent unless forced POST /backends/apply hardcoded force=true through LocalBackendManager.InstallBackend, so applying an already-installed backend re-downloaded and re-extracted the whole artifact every time. API clients that ensure a backend exists at startup paid a full OCI image pull on every boot. Backend install ops now default to non-forced — an installed, runnable backend short-circuits (the orphaned-meta reinstall path in InstallBackendFromGallery is preserved) — and reinstall stays available: - ManagementOp gains a Force field; the local manager passes it through instead of hardcoding true. - /backends/apply accepts an optional "force" boolean in the body. - The React UI install route keeps forcing, since its button doubles as the explicit "Reinstall backend" action. Distributed installs already behaved this way (workers skip when the binary exists unless force is set); this aligns single-node behavior. Assisted-by: Claude:claude-fable-5 [Claude Code] Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(backends): don't force-reinstall LOCALAI_EXTERNAL_BACKENDS on boot The startup loop for LOCALAI_EXTERNAL_BACKENDS runs InstallExternalBackend for each listed backend on every boot, and its gallery-name path hardcoded force=true — so every start re-downloaded and re-extracted each listed backend's OCI image even when it was installed and runnable. Supervising apps that list several backends paid several full OCI pulls per launch. Give InstallExternalBackend an explicit force parameter (it only affects the gallery-name fallback; URI installs always write) and pass: - false from the boot loop and `local-ai backends install` (idempotent ensure — `backends upgrade` is the refresh path), - op.Force from the local manager's external-URI op, - the request's force on the worker install path and true on its upgrade path (behavior unchanged). Assisted-by: Claude:claude-fable-5 [Claude Code] Signed-off-by: Ettore Di Giacinto <mudler@localai.io> --------- Signed-off-by: Ettore Di Giacinto <mudler@localai.io> Co-authored-by: Ettore Di Giacinto <mudler@localai.io>
218 lines
7.6 KiB
Go
218 lines
7.6 KiB
Go
package galleryop
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/mudler/LocalAI/core/config"
|
|
"github.com/mudler/LocalAI/core/gallery"
|
|
"github.com/mudler/LocalAI/core/services/messaging"
|
|
"github.com/mudler/LocalAI/pkg/downloader"
|
|
"github.com/mudler/LocalAI/pkg/model"
|
|
"github.com/mudler/LocalAI/pkg/system"
|
|
|
|
"github.com/mudler/LocalAI/pkg/utils"
|
|
"github.com/mudler/xlog"
|
|
)
|
|
|
|
func (g *GalleryService) backendHandler(op *ManagementOp[gallery.GalleryBackend, any], systemState *system.SystemState) error {
|
|
utils.ResetDownloadTimers()
|
|
|
|
// Dedup check in distributed mode — skip if another instance is already processing this element
|
|
if g.galleryStore != nil && op.GalleryElementName != "" {
|
|
dup, err := g.galleryStore.FindDuplicate(op.GalleryElementName)
|
|
if err == nil && dup != nil && dup.ID != op.ID {
|
|
g.UpdateStatus(op.ID, &OpStatus{
|
|
Processed: true,
|
|
Message: fmt.Sprintf("already being processed by another instance (op %s)", dup.ID),
|
|
})
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// Check if already cancelled
|
|
if op.Context != nil {
|
|
select {
|
|
case <-op.Context.Done():
|
|
g.UpdateStatus(op.ID, &OpStatus{
|
|
Cancelled: true,
|
|
Processed: true,
|
|
Message: "cancelled",
|
|
GalleryElementName: op.GalleryElementName,
|
|
})
|
|
return op.Context.Err()
|
|
default:
|
|
}
|
|
}
|
|
|
|
g.UpdateStatus(op.ID, &OpStatus{Message: fmt.Sprintf("processing backend: %s", op.GalleryElementName), Progress: 0, Cancellable: true})
|
|
|
|
// displayDownload displays the download progress
|
|
progressCallback := func(fileName string, current string, total string, percentage float64) {
|
|
// Check for cancellation during progress updates
|
|
if op.Context != nil {
|
|
select {
|
|
case <-op.Context.Done():
|
|
return
|
|
default:
|
|
}
|
|
}
|
|
g.UpdateStatus(op.ID, &OpStatus{Message: fmt.Sprintf(processingMessage, fileName, total, current), FileName: fileName, Progress: percentage, TotalFileSize: total, DownloadedFileSize: current, Cancellable: true})
|
|
utils.DisplayDownloadFunction(fileName, current, total, percentage)
|
|
}
|
|
|
|
ctx := op.Context
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
|
|
var err error
|
|
if op.Upgrade {
|
|
err = g.backendManager.UpgradeBackend(ctx, op.ID, op.GalleryElementName, progressCallback)
|
|
} else if op.Delete {
|
|
err = g.backendManager.DeleteBackend(op.GalleryElementName)
|
|
} else {
|
|
err = g.backendManager.InstallBackend(ctx, op, progressCallback)
|
|
// Update GalleryElementName for status tracking if a name was derived
|
|
if op.ExternalName != "" {
|
|
op.GalleryElementName = op.ExternalName
|
|
}
|
|
}
|
|
if err != nil {
|
|
// Check if error is due to cancellation
|
|
if op.Context != nil && errors.Is(err, op.Context.Err()) {
|
|
g.UpdateStatus(op.ID, &OpStatus{
|
|
Cancelled: true,
|
|
Processed: true,
|
|
Message: "cancelled",
|
|
GalleryElementName: op.GalleryElementName,
|
|
})
|
|
return err
|
|
}
|
|
if errors.Is(err, ErrWorkerStillInstalling) {
|
|
// Soft failure: at least one worker timed out replying but is
|
|
// still running the install in the background. Mark the op as
|
|
// processed with a non-error message so the admin UI shows a
|
|
// yellow in-progress state rather than red. The reconciler's
|
|
// next pass will reconcile the actual outcome via backend.list.
|
|
xlog.Info("worker still installing in background", "backend", op.GalleryElementName, "error", err)
|
|
g.UpdateStatus(op.ID, &OpStatus{
|
|
Processed: true,
|
|
GalleryElementName: op.GalleryElementName,
|
|
Message: fmt.Sprintf("backend %s: worker still installing in background; reconciler will confirm completion (%v)", op.GalleryElementName, err),
|
|
Cancellable: false,
|
|
})
|
|
return nil
|
|
}
|
|
xlog.Error("error installing backend", "error", err, "backend", op.GalleryElementName)
|
|
if !op.Delete {
|
|
// If we didn't install the backend, we need to make sure we don't have a leftover directory
|
|
gallery.DeleteBackendFromSystem(systemState, op.GalleryElementName)
|
|
}
|
|
return err
|
|
}
|
|
|
|
// Tell peer replicas that the backend set has changed. UpgradeChecker
|
|
// caches upgrade-available bits for 6 hours, so without this peers would
|
|
// keep advertising an upgrade for a backend that already moved.
|
|
opName := "install"
|
|
switch {
|
|
case op.Delete:
|
|
opName = "delete"
|
|
case op.Upgrade:
|
|
opName = "upgrade"
|
|
}
|
|
g.publishCacheInvalidate(messaging.SubjectCacheInvalidateBackends, messaging.CacheInvalidateEvent{
|
|
Element: op.GalleryElementName,
|
|
Op: opName,
|
|
})
|
|
|
|
g.UpdateStatus(op.ID,
|
|
&OpStatus{
|
|
Deletion: op.Delete,
|
|
Processed: true,
|
|
GalleryElementName: op.GalleryElementName,
|
|
Message: "completed",
|
|
Progress: 100,
|
|
Cancellable: false})
|
|
return nil
|
|
}
|
|
|
|
// InstallExternalBackend installs a backend from an external source (OCI image, URL, or path).
|
|
// This method contains the logic to detect the input type and call the appropriate installation function.
|
|
// It can be used by both CLI and Web UI for installing backends from external sources.
|
|
//
|
|
// force applies only to the gallery-name fallback: a URI install (dir/OCI/file)
|
|
// always writes, but a bare gallery name is an "ensure installed" — the
|
|
// LOCALAI_EXTERNAL_BACKENDS boot loop runs it on every start and must not
|
|
// re-download an installed, runnable backend.
|
|
func InstallExternalBackend(ctx context.Context, galleries []config.Gallery, systemState *system.SystemState, modelLoader *model.ModelLoader, downloadStatus func(string, string, string, float64), backend, name, alias string, force, requireIntegrity bool) error {
|
|
uri := downloader.URI(backend)
|
|
switch {
|
|
case uri.LooksLikeDir():
|
|
if name == "" { // infer it from the path
|
|
name = filepath.Base(backend)
|
|
}
|
|
xlog.Info("Installing backend from path", "backend", backend, "name", name)
|
|
if err := gallery.InstallBackend(ctx, systemState, modelLoader, &gallery.GalleryBackend{
|
|
Metadata: gallery.Metadata{
|
|
Name: name,
|
|
},
|
|
Alias: alias,
|
|
URI: backend,
|
|
}, downloadStatus, requireIntegrity); err != nil {
|
|
return fmt.Errorf("error installing backend %s: %w", backend, err)
|
|
}
|
|
case uri.LooksLikeOCI() && !uri.LooksLikeOCIFile():
|
|
if name == "" {
|
|
return fmt.Errorf("specifying a name is required for OCI images")
|
|
}
|
|
xlog.Info("Installing backend from OCI image", "backend", backend, "name", name)
|
|
if err := gallery.InstallBackend(ctx, systemState, modelLoader, &gallery.GalleryBackend{
|
|
Metadata: gallery.Metadata{
|
|
Name: name,
|
|
},
|
|
Alias: alias,
|
|
URI: backend,
|
|
}, downloadStatus, requireIntegrity); err != nil {
|
|
return fmt.Errorf("error installing backend %s: %w", backend, err)
|
|
}
|
|
case uri.LooksLikeOCIFile():
|
|
derivedName, err := uri.FilenameFromUrl()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get filename from URL: %w", err)
|
|
}
|
|
// strip extension if any
|
|
derivedName = strings.TrimSuffix(derivedName, filepath.Ext(derivedName))
|
|
// Use provided name if available, otherwise use derived name
|
|
if name == "" {
|
|
name = derivedName
|
|
}
|
|
|
|
xlog.Info("Installing backend from OCI image", "backend", backend, "name", name)
|
|
if err := gallery.InstallBackend(ctx, systemState, modelLoader, &gallery.GalleryBackend{
|
|
Metadata: gallery.Metadata{
|
|
Name: name,
|
|
},
|
|
Alias: alias,
|
|
URI: backend,
|
|
}, downloadStatus, requireIntegrity); err != nil {
|
|
return fmt.Errorf("error installing backend %s: %w", backend, err)
|
|
}
|
|
default:
|
|
// Treat as gallery backend name
|
|
if name != "" || alias != "" {
|
|
return fmt.Errorf("specifying a name or alias is not supported for gallery backends")
|
|
}
|
|
err := gallery.InstallBackendFromGallery(ctx, galleries, systemState, modelLoader, backend, downloadStatus, force, requireIntegrity)
|
|
if err != nil {
|
|
return fmt.Errorf("error installing backend %s: %w", backend, err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|