mirror of
https://github.com/mudler/LocalAI.git
synced 2025-05-20 02:24:59 +00:00
fix: use rice when embedding large binaries (#5309)
* fix(embed): use go-rice for large backend assets Golang embed FS has a hard limit that we might exceed when providing many binary alternatives. Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * simplify golang deps Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * chore(tests): switch to testcontainers and print logs Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(tests): do not build a test binary Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * small fixup Signed-off-by: Ettore Di Giacinto <mudler@localai.io> --------- Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
This commit is contained in:
parent
7ebd7b2454
commit
21bdfe5fa4
14 changed files with 180 additions and 122 deletions
|
@ -1,35 +1,37 @@
|
|||
package assets
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
rice "github.com/GeertJohan/go.rice"
|
||||
"github.com/mudler/LocalAI/pkg/library"
|
||||
)
|
||||
|
||||
const backendAssetsDir = "backend-assets"
|
||||
|
||||
func ResolvePath(dir string, paths ...string) string {
|
||||
return filepath.Join(append([]string{dir, "backend-assets"}, paths...)...)
|
||||
return filepath.Join(append([]string{dir, backendAssetsDir}, paths...)...)
|
||||
}
|
||||
|
||||
func ExtractFiles(content embed.FS, extractDir string) error {
|
||||
// Create the target directory if it doesn't exist
|
||||
err := os.MkdirAll(extractDir, 0750)
|
||||
func ExtractFiles(content *rice.Box, extractDir string) error {
|
||||
// Create the target directory with backend-assets subdirectory
|
||||
backendAssetsDir := filepath.Join(extractDir, backendAssetsDir)
|
||||
err := os.MkdirAll(backendAssetsDir, 0750)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create directory: %v", err)
|
||||
}
|
||||
|
||||
// Walk through the embedded FS and extract files
|
||||
err = fs.WalkDir(content, ".", func(path string, d fs.DirEntry, err error) error {
|
||||
// Walk through the rice box and extract files
|
||||
err = content.Walk("", func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Reconstruct the directory structure in the target directory
|
||||
targetFile := filepath.Join(extractDir, path)
|
||||
if d.IsDir() {
|
||||
targetFile := filepath.Join(backendAssetsDir, path)
|
||||
if info.IsDir() {
|
||||
// Create the directory in the target directory
|
||||
err := os.MkdirAll(targetFile, 0750)
|
||||
if err != nil {
|
||||
|
@ -38,8 +40,8 @@ func ExtractFiles(content embed.FS, extractDir string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// Read the file from the embedded FS
|
||||
fileData, err := content.ReadFile(path)
|
||||
// Read the file from the rice box
|
||||
fileData, err := content.Bytes(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read file: %v", err)
|
||||
}
|
||||
|
@ -56,7 +58,7 @@ func ExtractFiles(content embed.FS, extractDir string) error {
|
|||
// If there is a lib directory, set LD_LIBRARY_PATH to include it
|
||||
// we might use this mechanism to carry over e.g. Nvidia CUDA libraries
|
||||
// from the embedded FS to the target directory
|
||||
library.LoadExtractedLibs(extractDir)
|
||||
library.LoadExtractedLibs(backendAssetsDir)
|
||||
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -1,19 +1,19 @@
|
|||
package assets
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"io/fs"
|
||||
"os"
|
||||
|
||||
rice "github.com/GeertJohan/go.rice"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func ListFiles(content embed.FS) (files []string) {
|
||||
err := fs.WalkDir(content, ".", func(path string, d fs.DirEntry, err error) error {
|
||||
func ListFiles(content *rice.Box) (files []string) {
|
||||
err := content.Walk("", func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if d.IsDir() {
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,7 @@ func ListFiles(content embed.FS) (files []string) {
|
|||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("error walking the embedded filesystem")
|
||||
log.Error().Err(err).Msg("error walking the rice box")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ func LoadExtractedLibs(dir string) error {
|
|||
}
|
||||
|
||||
var err error = nil
|
||||
for _, libDir := range []string{filepath.Join(dir, "backend-assets", "lib"), filepath.Join(dir, "lib")} {
|
||||
for _, libDir := range []string{filepath.Join(dir, "lib"), filepath.Join(dir, "lib")} {
|
||||
err = errors.Join(err, LoadExternal(libDir))
|
||||
}
|
||||
return err
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue