paths.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package lifecycle
  2. import (
  3. "errors"
  4. "fmt"
  5. "log/slog"
  6. "os"
  7. "path/filepath"
  8. "runtime"
  9. "strings"
  10. )
  11. var (
  12. AppName = "ollama app"
  13. CLIName = "ollama"
  14. AppDir = "/opt/Ollama"
  15. AppDataDir = "/opt/Ollama"
  16. // TODO - should there be a distinct log dir?
  17. UpdateStageDir = "/tmp"
  18. AppLogFile = "/tmp/ollama_app.log"
  19. ServerLogFile = "/tmp/ollama.log"
  20. UpgradeLogFile = "/tmp/ollama_update.log"
  21. Installer = "OllamaSetup.exe"
  22. LogRotationCount = 5
  23. )
  24. func init() {
  25. if runtime.GOOS == "windows" {
  26. AppName += ".exe"
  27. CLIName += ".exe"
  28. // Logs, configs, downloads go to LOCALAPPDATA
  29. localAppData := os.Getenv("LOCALAPPDATA")
  30. AppDataDir = filepath.Join(localAppData, "Ollama")
  31. UpdateStageDir = filepath.Join(AppDataDir, "updates")
  32. AppLogFile = filepath.Join(AppDataDir, "app.log")
  33. ServerLogFile = filepath.Join(AppDataDir, "server.log")
  34. UpgradeLogFile = filepath.Join(AppDataDir, "upgrade.log")
  35. // Executables are stored in APPDATA
  36. AppDir = filepath.Join(localAppData, "Programs", "Ollama")
  37. // Make sure we have PATH set correctly for any spawned children
  38. paths := strings.Split(os.Getenv("PATH"), ";")
  39. // Start with whatever we find in the PATH/LD_LIBRARY_PATH
  40. found := false
  41. for _, path := range paths {
  42. d, err := filepath.Abs(path)
  43. if err != nil {
  44. continue
  45. }
  46. if strings.EqualFold(AppDir, d) {
  47. found = true
  48. }
  49. }
  50. if !found {
  51. paths = append(paths, AppDir)
  52. pathVal := strings.Join(paths, ";")
  53. slog.Debug("setting PATH=" + pathVal)
  54. err := os.Setenv("PATH", pathVal)
  55. if err != nil {
  56. slog.Error(fmt.Sprintf("failed to update PATH: %s", err))
  57. }
  58. }
  59. // Make sure our logging dir exists
  60. _, err := os.Stat(AppDataDir)
  61. if errors.Is(err, os.ErrNotExist) {
  62. if err := os.MkdirAll(AppDataDir, 0o755); err != nil {
  63. slog.Error(fmt.Sprintf("create ollama dir %s: %v", AppDataDir, err))
  64. }
  65. }
  66. } else if runtime.GOOS == "darwin" {
  67. // TODO
  68. AppName += ".app"
  69. // } else if runtime.GOOS == "linux" {
  70. // TODO
  71. }
  72. }