updater_windows.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package lifecycle
  2. import (
  3. "context"
  4. "fmt"
  5. "log/slog"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. )
  10. func DoUpgrade(cancel context.CancelFunc, done chan int) error {
  11. files, err := filepath.Glob(filepath.Join(UpdateStageDir, "*", "*.exe")) // TODO generalize for multiplatform
  12. if err != nil {
  13. return fmt.Errorf("failed to lookup downloads: %s", err)
  14. }
  15. if len(files) == 0 {
  16. return fmt.Errorf("no update downloads found")
  17. } else if len(files) > 1 {
  18. // Shouldn't happen
  19. slog.Warn(fmt.Sprintf("multiple downloads found, using first one %v", files))
  20. }
  21. installerExe := files[0]
  22. slog.Info("starting upgrade with " + installerExe)
  23. slog.Info("upgrade log file " + UpgradeLogFile)
  24. // When running in debug mode, we'll be "verbose" and let the installer pop up and prompt
  25. installArgs := []string{
  26. "/CLOSEAPPLICATIONS", // Quit the tray app if it's still running
  27. "/LOG=" + filepath.Base(UpgradeLogFile), // Only relative seems reliable, so set pwd
  28. "/FORCECLOSEAPPLICATIONS", // Force close the tray app - might be needed
  29. }
  30. // make the upgrade as quiet as possible (no GUI, no prompts)
  31. installArgs = append(installArgs,
  32. "/SP", // Skip the "This will install... Do you wish to continue" prompt
  33. "/SUPPRESSMSGBOXES",
  34. "/SILENT",
  35. "/VERYSILENT",
  36. )
  37. // Safeguard in case we have requests in flight that need to drain...
  38. slog.Info("Waiting for server to shutdown")
  39. cancel()
  40. if done != nil {
  41. <-done
  42. } else {
  43. // Shouldn't happen
  44. slog.Warn("done chan was nil, not actually waiting")
  45. }
  46. slog.Debug(fmt.Sprintf("starting installer: %s %v", installerExe, installArgs))
  47. os.Chdir(filepath.Dir(UpgradeLogFile)) //nolint:errcheck
  48. cmd := exec.Command(installerExe, installArgs...)
  49. if err := cmd.Start(); err != nil {
  50. return fmt.Errorf("unable to start ollama app %w", err)
  51. }
  52. if cmd.Process != nil {
  53. err = cmd.Process.Release()
  54. if err != nil {
  55. slog.Error(fmt.Sprintf("failed to release server process: %s", err))
  56. }
  57. } else {
  58. // TODO - some details about why it didn't start, or is this a pedantic error case?
  59. return fmt.Errorf("installer process did not start")
  60. }
  61. // TODO should we linger for a moment and check to make sure it's actually running by checking the pid?
  62. slog.Info("Installer started in background, exiting")
  63. os.Exit(0)
  64. // Not reached
  65. return nil
  66. }