From 89aeb34199f07ec9499de75cc3c304c7b70d4d3b Mon Sep 17 00:00:00 2001 From: Anjan Nath Date: Tue, 31 Oct 2023 15:00:26 +0530 Subject: [PATCH] cache: add win32-background-launcher to the cached binaries on a released crc version the win32-background-laucher will be installed in the install location of crc by the msi, but while using a non installer version this'll help to download the win32-background-launcher binary by running `crc setup` this is also helpful for testing different versions of the background launcher during development --- pkg/crc/cache/cache_windows.go | 26 +++++++++++++++++++ .../preflight_daemon_task_check_windows.go | 20 +++++++++++--- pkg/crc/preflight/preflight_windows.go | 11 ++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 pkg/crc/cache/cache_windows.go diff --git a/pkg/crc/cache/cache_windows.go b/pkg/crc/cache/cache_windows.go new file mode 100644 index 0000000000..811694b487 --- /dev/null +++ b/pkg/crc/cache/cache_windows.go @@ -0,0 +1,26 @@ +package cache + +import ( + "fmt" + "strings" + + "github.com/crc-org/crc/v2/pkg/crc/constants" + "github.com/crc-org/crc/v2/pkg/crc/version" + "github.com/crc-org/crc/v2/pkg/os/windows/powershell" +) + +func NewWin32BackgroundLauncherCache() *Cache { + url := constants.GetCRCWindowsBackgroundLauncherDownloadURL() + version := version.GetWin32BackgroundLauncherVersion() + return newCache(constants.Win32BackgroundLauncherPath(), + url, + version, + func(executable string) (string, error) { + stdOut, stdErr, err := powershell.Execute(fmt.Sprintf(`(Get-Item '%s').VersionInfo.FileVersion`, executable)) + if err != nil { + return "", fmt.Errorf("unable to get version: %s: %w", stdErr, err) + } + return strings.TrimSpace(stdOut), nil + }, + ) +} diff --git a/pkg/crc/preflight/preflight_daemon_task_check_windows.go b/pkg/crc/preflight/preflight_daemon_task_check_windows.go index 7012c0b32f..7cade5c551 100644 --- a/pkg/crc/preflight/preflight_daemon_task_check_windows.go +++ b/pkg/crc/preflight/preflight_daemon_task_check_windows.go @@ -9,6 +9,7 @@ import ( "os/user" "strings" + "github.com/crc-org/crc/v2/pkg/crc/cache" "github.com/crc-org/crc/v2/pkg/crc/constants" "github.com/crc-org/crc/v2/pkg/crc/logging" "github.com/crc-org/crc/v2/pkg/crc/version" @@ -188,9 +189,22 @@ func killDaemonProcessIfRunning() error { return nil } -func removeDaemonPoshScript() error { - if crcos.FileExists(daemonPoshScriptPath) { - return os.Remove(daemonPoshScriptPath) +func checkWin32BackgroundLauncherInstalled() error { + c := cache.NewWin32BackgroundLauncherCache() + return c.CheckVersion() +} + +func fixWin32BackgroundLauncherInstalled() error { + c := cache.NewWin32BackgroundLauncherCache() + return c.EnsureIsCached() +} + +func removeWin32BackgroundLauncher() error { + if version.IsInstaller() { + return nil + } + if crcos.FileExists(constants.Win32BackgroundLauncherPath()) { + return os.Remove(constants.Win32BackgroundLauncherPath()) } return nil } diff --git a/pkg/crc/preflight/preflight_windows.go b/pkg/crc/preflight/preflight_windows.go index 21c167b5df..e74a94fa19 100644 --- a/pkg/crc/preflight/preflight_windows.go +++ b/pkg/crc/preflight/preflight_windows.go @@ -99,6 +99,17 @@ var daemonTaskChecks = []Check{ labels: labels{Os: Windows}, }, + { + configKeySuffix: "check-background-launcher-install", + checkDescription: "Checking if the win32 background launcher is installed", + check: checkWin32BackgroundLauncherInstalled, + fixDescription: "Installing the win32 background launcher", + fix: fixWin32BackgroundLauncherInstalled, + cleanupDescription: "Removing the win32 background launcher", + cleanup: removeWin32BackgroundLauncher, + + labels: labels{Os: Windows}, + }, { configKeySuffix: "check-daemon-task-install", checkDescription: "Checking if the daemon task is installed",