Skip to content

Commit

Permalink
Preflight: Move crc binary symlink check from linux to non-win file
Browse files Browse the repository at this point in the history
This same code is going to use with mac os when lanuchd plist file
is created so this patch makes it shareable.
  • Loading branch information
praveenkumar committed Mar 31, 2022
1 parent 889b132 commit 197df63
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 75 deletions.
47 changes: 0 additions & 47 deletions pkg/crc/preflight/preflight_checks_linux.go
Expand Up @@ -700,50 +700,3 @@ func getCPUFlags() (string, error) {
}
return flags, nil
}

func runtimeExecutablePath() (string, error) {
path, err := exec.LookPath(os.Args[0])
if err != nil {
// os.Args[0] is not in $PATH, crc must have been started by specifying the path to its binary
path = os.Args[0]
}
path, err = filepath.Abs(path)
if err != nil {
return "", err
}
return filepath.EvalSymlinks(path)
}

func checkCrcSymlink() error {
runtimePath, err := runtimeExecutablePath()
if err != nil {
return err
}
symlinkPath, err := filepath.EvalSymlinks(constants.CrcSymlinkPath)
if err != nil {
return err
}
if symlinkPath != runtimePath {
return fmt.Errorf("%s points to %s, not to %s", constants.CrcSymlinkPath, symlinkPath, runtimePath)
}

return nil
}

func fixCrcSymlink() error {
_ = os.Remove(constants.CrcSymlinkPath)

runtimePath, err := runtimeExecutablePath()
if err != nil {
return err
}
logging.Debugf("symlinking %s to %s", runtimePath, constants.CrcSymlinkPath)
return os.Symlink(runtimePath, constants.CrcSymlinkPath)
}

func removeCrcSymlink() error {
if crcos.FileExists(constants.CrcSymlinkPath) {
return os.Remove(constants.CrcSymlinkPath)
}
return nil
}
59 changes: 59 additions & 0 deletions pkg/crc/preflight/preflight_checks_nonwin.go
Expand Up @@ -6,6 +6,7 @@ package preflight
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"syscall"
Expand Down Expand Up @@ -70,6 +71,17 @@ func genericPreflightChecks(preset crcpreset.Preset) []Check {
fixDescription: fmt.Sprintf("crc requires at least %s to run", units.HumanSize(float64(constants.GetDefaultMemory(preset)*1024*1024))),
flags: NoFix,

labels: None,
},
{
configKeySuffix: "check-crc-symlink",
checkDescription: "Checking if crc executable symlink exists",
check: checkCrcSymlink,
fixDescription: "Creating symlink for crc executable",
fix: fixCrcSymlink,
cleanupDescription: "Removing crc executable symlink",
cleanup: removeCrcSymlink,

labels: None,
},
}
Expand Down Expand Up @@ -186,3 +198,50 @@ func checkSupportedCPUArch() error {
}
return nil
}

func runtimeExecutablePath() (string, error) {
path, err := exec.LookPath(os.Args[0])
if err != nil {
// os.Args[0] is not in $PATH, crc must have been started by specifying the path to its binary
path = os.Args[0]
}
path, err = filepath.Abs(path)
if err != nil {
return "", err
}
return filepath.EvalSymlinks(path)
}

func checkCrcSymlink() error {
runtimePath, err := runtimeExecutablePath()
if err != nil {
return err
}
symlinkPath, err := filepath.EvalSymlinks(constants.CrcSymlinkPath)
if err != nil {
return err
}
if symlinkPath != runtimePath {
return fmt.Errorf("%s points to %s, not to %s", constants.CrcSymlinkPath, symlinkPath, runtimePath)
}

return nil
}

func fixCrcSymlink() error {
_ = os.Remove(constants.CrcSymlinkPath)

runtimePath, err := runtimeExecutablePath()
if err != nil {
return err
}
logging.Debugf("symlinking %s to %s", runtimePath, constants.CrcSymlinkPath)
return os.Symlink(runtimePath, constants.CrcSymlinkPath)
}

func removeCrcSymlink() error {
if crcos.FileExists(constants.CrcSymlinkPath) {
return os.Remove(constants.CrcSymlinkPath)
}
return nil
}
10 changes: 5 additions & 5 deletions pkg/crc/preflight/preflight_darwin_test.go
Expand Up @@ -13,13 +13,13 @@ import (
func TestCountConfigurationOptions(t *testing.T) {
cfg := config.New(config.NewEmptyInMemoryStorage())
RegisterSettings(cfg)
assert.Len(t, cfg.AllConfigs(), 12)
assert.Len(t, cfg.AllConfigs(), 13)
}

func TestCountPreflights(t *testing.T) {
assert.Len(t, getPreflightChecks(true, network.SystemNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift), 17)
assert.Len(t, getPreflightChecks(true, network.SystemNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift), 17)
assert.Len(t, getPreflightChecks(true, network.SystemNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift), 18)
assert.Len(t, getPreflightChecks(true, network.SystemNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift), 18)

assert.Len(t, getPreflightChecks(true, network.UserNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift), 16)
assert.Len(t, getPreflightChecks(true, network.UserNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift), 16)
assert.Len(t, getPreflightChecks(true, network.UserNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift), 17)
assert.Len(t, getPreflightChecks(true, network.UserNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift), 17)
}
11 changes: 0 additions & 11 deletions pkg/crc/preflight/preflight_linux.go
Expand Up @@ -20,17 +20,6 @@ import (

func libvirtPreflightChecks(distro *linux.OsRelease) []Check {
checks := []Check{
{
configKeySuffix: "check-crc-symlink",
checkDescription: "Checking if crc executable symlink exists",
check: checkCrcSymlink,
fixDescription: "Creating symlink for crc executable",
fix: fixCrcSymlink,
cleanupDescription: "Removing crc executable symlink",
cleanup: removeCrcSymlink,

labels: labels{Os: Linux},
},
{
configKeySuffix: "check-virt-enabled",
checkDescription: "Checking if Virtualization is enabled",
Expand Down
24 changes: 12 additions & 12 deletions pkg/crc/preflight/preflight_linux_test.go
Expand Up @@ -70,11 +70,11 @@ var checkListForDistros = []checkListForDistro{
{check: checkOldAdminHelperExecutableCached},
{check: checkSupportedCPUArch},
{configKeySuffix: "check-ram"},
{check: checkCrcSymlink},
{cleanup: removeCRCMachinesDir},
{cleanup: removeOldLogs},
{cleanup: cluster.ForgetPullSecret},
{cleanup: removeHostsFileEntry},
{check: checkCrcSymlink},
{check: checkVirtualizationEnabled},
{check: checkKvmEnabled},
{check: checkLibvirtInstalled},
Expand Down Expand Up @@ -109,11 +109,11 @@ var checkListForDistros = []checkListForDistro{
{check: checkOldAdminHelperExecutableCached},
{check: checkSupportedCPUArch},
{configKeySuffix: "check-ram"},
{check: checkCrcSymlink},
{cleanup: removeCRCMachinesDir},
{cleanup: removeOldLogs},
{cleanup: cluster.ForgetPullSecret},
{cleanup: removeHostsFileEntry},
{check: checkCrcSymlink},
{check: checkVirtualizationEnabled},
{check: checkKvmEnabled},
{check: checkLibvirtInstalled},
Expand Down Expand Up @@ -147,11 +147,11 @@ var checkListForDistros = []checkListForDistro{
{check: checkOldAdminHelperExecutableCached},
{check: checkSupportedCPUArch},
{configKeySuffix: "check-ram"},
{check: checkCrcSymlink},
{cleanup: removeCRCMachinesDir},
{cleanup: removeOldLogs},
{cleanup: cluster.ForgetPullSecret},
{cleanup: removeHostsFileEntry},
{check: checkCrcSymlink},
{check: checkVirtualizationEnabled},
{check: checkKvmEnabled},
{check: checkLibvirtInstalled},
Expand Down Expand Up @@ -179,11 +179,11 @@ var checkListForDistros = []checkListForDistro{
{check: checkOldAdminHelperExecutableCached},
{check: checkSupportedCPUArch},
{configKeySuffix: "check-ram"},
{check: checkCrcSymlink},
{cleanup: removeCRCMachinesDir},
{cleanup: removeOldLogs},
{cleanup: cluster.ForgetPullSecret},
{cleanup: removeHostsFileEntry},
{check: checkCrcSymlink},
{check: checkVirtualizationEnabled},
{check: checkKvmEnabled},
{check: checkLibvirtInstalled},
Expand Down Expand Up @@ -218,11 +218,11 @@ var checkListForDistros = []checkListForDistro{
{check: checkOldAdminHelperExecutableCached},
{check: checkSupportedCPUArch},
{configKeySuffix: "check-ram"},
{check: checkCrcSymlink},
{cleanup: removeCRCMachinesDir},
{cleanup: removeOldLogs},
{cleanup: cluster.ForgetPullSecret},
{cleanup: removeHostsFileEntry},
{check: checkCrcSymlink},
{check: checkVirtualizationEnabled},
{check: checkKvmEnabled},
{check: checkLibvirtInstalled},
Expand Down Expand Up @@ -256,11 +256,11 @@ var checkListForDistros = []checkListForDistro{
{check: checkOldAdminHelperExecutableCached},
{check: checkSupportedCPUArch},
{configKeySuffix: "check-ram"},
{check: checkCrcSymlink},
{cleanup: removeCRCMachinesDir},
{cleanup: removeOldLogs},
{cleanup: cluster.ForgetPullSecret},
{cleanup: removeHostsFileEntry},
{check: checkCrcSymlink},
{check: checkVirtualizationEnabled},
{check: checkKvmEnabled},
{check: checkLibvirtInstalled},
Expand Down Expand Up @@ -288,11 +288,11 @@ var checkListForDistros = []checkListForDistro{
{check: checkOldAdminHelperExecutableCached},
{check: checkSupportedCPUArch},
{configKeySuffix: "check-ram"},
{check: checkCrcSymlink},
{cleanup: removeCRCMachinesDir},
{cleanup: removeOldLogs},
{cleanup: cluster.ForgetPullSecret},
{cleanup: removeHostsFileEntry},
{check: checkCrcSymlink},
{check: checkVirtualizationEnabled},
{check: checkKvmEnabled},
{check: checkLibvirtInstalled},
Expand Down Expand Up @@ -327,11 +327,11 @@ var checkListForDistros = []checkListForDistro{
{check: checkOldAdminHelperExecutableCached},
{check: checkSupportedCPUArch},
{configKeySuffix: "check-ram"},
{check: checkCrcSymlink},
{cleanup: removeCRCMachinesDir},
{cleanup: removeOldLogs},
{cleanup: cluster.ForgetPullSecret},
{cleanup: removeHostsFileEntry},
{check: checkCrcSymlink},
{check: checkVirtualizationEnabled},
{check: checkKvmEnabled},
{check: checkLibvirtInstalled},
Expand Down Expand Up @@ -365,11 +365,11 @@ var checkListForDistros = []checkListForDistro{
{check: checkOldAdminHelperExecutableCached},
{check: checkSupportedCPUArch},
{configKeySuffix: "check-ram"},
{check: checkCrcSymlink},
{cleanup: removeCRCMachinesDir},
{cleanup: removeOldLogs},
{cleanup: cluster.ForgetPullSecret},
{cleanup: removeHostsFileEntry},
{check: checkCrcSymlink},
{check: checkVirtualizationEnabled},
{check: checkKvmEnabled},
{check: checkLibvirtInstalled},
Expand Down Expand Up @@ -397,11 +397,11 @@ var checkListForDistros = []checkListForDistro{
{check: checkOldAdminHelperExecutableCached},
{check: checkSupportedCPUArch},
{configKeySuffix: "check-ram"},
{check: checkCrcSymlink},
{cleanup: removeCRCMachinesDir},
{cleanup: removeOldLogs},
{cleanup: cluster.ForgetPullSecret},
{cleanup: removeHostsFileEntry},
{check: checkCrcSymlink},
{check: checkVirtualizationEnabled},
{check: checkKvmEnabled},
{check: checkLibvirtInstalled},
Expand Down Expand Up @@ -437,11 +437,11 @@ var checkListForDistros = []checkListForDistro{
{check: checkOldAdminHelperExecutableCached},
{check: checkSupportedCPUArch},
{configKeySuffix: "check-ram"},
{check: checkCrcSymlink},
{cleanup: removeCRCMachinesDir},
{cleanup: removeOldLogs},
{cleanup: cluster.ForgetPullSecret},
{cleanup: removeHostsFileEntry},
{check: checkCrcSymlink},
{check: checkVirtualizationEnabled},
{check: checkKvmEnabled},
{check: checkLibvirtInstalled},
Expand Down Expand Up @@ -476,11 +476,11 @@ var checkListForDistros = []checkListForDistro{
{check: checkOldAdminHelperExecutableCached},
{check: checkSupportedCPUArch},
{configKeySuffix: "check-ram"},
{check: checkCrcSymlink},
{cleanup: removeCRCMachinesDir},
{cleanup: removeOldLogs},
{cleanup: cluster.ForgetPullSecret},
{cleanup: removeHostsFileEntry},
{check: checkCrcSymlink},
{check: checkVirtualizationEnabled},
{check: checkKvmEnabled},
{check: checkLibvirtInstalled},
Expand Down

0 comments on commit 197df63

Please sign in to comment.