Skip to content

Commit

Permalink
Windows: Remove an existing task as part of fixDaemonTaskInstalled
Browse files Browse the repository at this point in the history
This patch should resolve following flow
- User installed crc-2.1.0 version and during setup task installed with
  version 2.1.0
- New version 2.2.0 arrive and user try to do setup then it fails with
  following error
  ```
  DEBU expected crcDaemon task to be on version '2.1.0' but got '2.0.1
  '
  INFO Installing the daemon task

  failed to register crcDaemon task, exit status 1:
  Register-ScheduledTask : Cannot create a file when that file already
  exists.
  At line:1 char:43
  + ... yContinue'; Register-ScheduledTask -Xml '<?xml version="1.0"
    encoding ...
    +
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ResourceExists:
	  (PS_ScheduledTask:Root/Microsoft/...S_ScheduledTask)
	  [Register-ScheduledTask], CimException
	      + FullyQualifiedErrorId : HRESULT
		0x800700b7,Register-ScheduledTask
  ```

This patch make sure if the older version is running and matches with
errOlderVersion then remove the existing task.
  • Loading branch information
praveenkumar committed Apr 22, 2022
1 parent 4de2dce commit 300b2a8
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pkg/crc/preflight/preflight_daemon_task_check_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package preflight
import (
"bytes"
"encoding/xml"
"errors"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -43,6 +44,7 @@ var (
</Actions>
</Task>
`
errOlderVersion = fmt.Errorf("expected %s task to be on version '%s'", constants.DaemonTaskName, version.GetCRCVersion())
)

func genDaemonTaskInstallTemplate(crcVersion, daemonCommand string) (string, error) {
Expand Down Expand Up @@ -70,6 +72,10 @@ func checkIfDaemonTaskInstalled() error {
}

func fixDaemonTaskInstalled() error {
// Remove older task if exist
if err := removeDaemonTask(); err != nil {
return err
}
// prepare the task script
binPath, err := os.Executable()
if err != nil {
Expand Down Expand Up @@ -100,7 +106,7 @@ func removeDaemonTask() error {
return err
}
}
if err := checkIfDaemonTaskInstalled(); err == nil {
if err := checkIfDaemonTaskInstalled(); err == nil || errors.Is(err, errOlderVersion) {
_, stderr, err := powershell.Execute("Unregister-ScheduledTask", "-TaskName", constants.DaemonTaskName, "-Confirm:$false")
if err != nil {
logging.Debugf("unable to unregister the %s task: %v : %s", constants.DaemonTaskName, err, stderr)
Expand Down Expand Up @@ -145,7 +151,7 @@ func checkIfOlderTask() error {
return fmt.Errorf("%s task is not running: %v : %s", constants.DaemonTaskName, err, stderr)
}
if strings.TrimSpace(stdout) != version.GetCRCVersion() {
return fmt.Errorf("expected %s task to be on version '%s' but got '%s'", constants.DaemonTaskName, version.GetCRCVersion(), stdout)
return fmt.Errorf("%w but got '%s'", errOlderVersion, stdout)
}
return nil
}

0 comments on commit 300b2a8

Please sign in to comment.