paths.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. exe, err := os.Executable()
  36. if err != nil {
  37. slog.Warn("error discovering executable directory", "error", err)
  38. AppDir = filepath.Join(localAppData, "Programs", "Ollama")
  39. } else {
  40. AppDir = filepath.Dir(exe)
  41. }
  42. // Make sure we have PATH set correctly for any spawned children
  43. paths := strings.Split(os.Getenv("PATH"), ";")
  44. // Start with whatever we find in the PATH/LD_LIBRARY_PATH
  45. found := false
  46. for _, path := range paths {
  47. d, err := filepath.Abs(path)
  48. if err != nil {
  49. continue
  50. }
  51. if strings.EqualFold(AppDir, d) {
  52. found = true
  53. }
  54. }
  55. if !found {
  56. paths = append(paths, AppDir)
  57. pathVal := strings.Join(paths, ";")
  58. slog.Debug("setting PATH=" + pathVal)
  59. err := os.Setenv("PATH", pathVal)
  60. if err != nil {
  61. slog.Error(fmt.Sprintf("failed to update PATH: %s", err))
  62. }
  63. }
  64. // Make sure our logging dir exists
  65. _, err = os.Stat(AppDataDir)
  66. if errors.Is(err, os.ErrNotExist) {
  67. if err := os.MkdirAll(AppDataDir, 0o755); err != nil {
  68. slog.Error(fmt.Sprintf("create ollama dir %s: %v", AppDataDir, err))
  69. }
  70. }
  71. } else if runtime.GOOS == "darwin" {
  72. // TODO
  73. AppName += ".app"
  74. // } else if runtime.GOOS == "linux" {
  75. // TODO
  76. }
  77. }