updater_windows.go 2.3 KB

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