2023-06-01 21:38:52 +00:00
|
|
|
package assets
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-06-13 20:59:42 +00:00
|
|
|
"runtime"
|
2023-06-01 21:38:52 +00:00
|
|
|
)
|
|
|
|
|
2024-05-14 23:17:02 +00:00
|
|
|
func ResolvePath(dir string, paths ...string) string {
|
|
|
|
return filepath.Join(append([]string{dir, "backend-assets"}, paths...)...)
|
|
|
|
}
|
|
|
|
|
2023-06-01 21:38:52 +00:00
|
|
|
func ExtractFiles(content embed.FS, extractDir string) error {
|
|
|
|
// Create the target directory if it doesn't exist
|
2024-04-25 22:47:06 +00:00
|
|
|
err := os.MkdirAll(extractDir, 0750)
|
2023-06-01 21:38:52 +00:00
|
|
|
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 {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reconstruct the directory structure in the target directory
|
|
|
|
targetFile := filepath.Join(extractDir, path)
|
|
|
|
if d.IsDir() {
|
|
|
|
// Create the directory in the target directory
|
2024-04-25 22:47:06 +00:00
|
|
|
err := os.MkdirAll(targetFile, 0750)
|
2023-06-01 21:38:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create directory: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the file from the embedded FS
|
|
|
|
fileData, err := content.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to read file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the file in the target directory
|
2024-05-14 23:17:02 +00:00
|
|
|
err = os.WriteFile(targetFile, fileData, 0700)
|
2023-06-01 21:38:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to write file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2024-06-09 13:11:37 +00:00
|
|
|
// 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
|
|
|
|
|
2024-06-13 20:59:42 +00:00
|
|
|
// Skip this if LOCALAI_SKIP_LIBRARY_PATH is set
|
|
|
|
if os.Getenv("LOCALAI_SKIP_LIBRARY_PATH") != "" {
|
2024-06-09 13:11:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-14 06:51:25 +00:00
|
|
|
|
2024-06-13 20:59:42 +00:00
|
|
|
lpathVar := "LD_LIBRARY_PATH"
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
lpathVar = "DYLD_FALLBACK_LIBRARY_PATH" // should it be DYLD_LIBRARY_PATH ?
|
|
|
|
}
|
|
|
|
|
2024-06-14 06:51:25 +00:00
|
|
|
for _, libDir := range []string{filepath.Join(extractDir, "backend-assets", "lib"), filepath.Join(extractDir, "lib")} {
|
2024-06-09 13:11:37 +00:00
|
|
|
if _, err := os.Stat(libDir); err == nil {
|
2024-06-13 20:59:42 +00:00
|
|
|
ldLibraryPath := os.Getenv(lpathVar)
|
2024-06-09 13:11:37 +00:00
|
|
|
if ldLibraryPath == "" {
|
|
|
|
ldLibraryPath = libDir
|
|
|
|
} else {
|
|
|
|
ldLibraryPath = fmt.Sprintf("%s:%s", ldLibraryPath, libDir)
|
|
|
|
}
|
2024-06-13 20:59:42 +00:00
|
|
|
os.Setenv(lpathVar, ldLibraryPath)
|
2024-06-09 13:11:37 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-01 21:38:52 +00:00
|
|
|
return err
|
|
|
|
}
|