Skip to content

Commit

Permalink
preflight: Don't use errors.Newf in preflight checks
Browse files Browse the repository at this point in the history
The generic preflight code will log errors when one is returned from a
check/fix method, so we don't need to use errors.Newf for that.
Moreover, the preflight methods were using a mix of fmt.Errorf and
errors.Newf. This commit makes this use consistent.
  • Loading branch information
cfergeau authored and praveenkumar committed Feb 14, 2020
1 parent 6f457dc commit c2102d5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 30 deletions.
25 changes: 12 additions & 13 deletions pkg/crc/preflight/preflight_checks_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package preflight

import (
"bytes"
"errors"
"fmt"
"golang.org/x/sys/unix"
"io/ioutil"
Expand Down Expand Up @@ -49,34 +48,34 @@ func checkVirtualizationEnabled() error {
out, err := ioutil.ReadFile("/proc/cpuinfo")
if err != nil {
logging.Debugf("Failed to read /proc/cpuinfo: %v", err)
return errors.New("Failed to read /proc/cpuinfo")
return fmt.Errorf("Failed to read /proc/cpuinfo")
}
re := regexp.MustCompile(`flags.*:.*`)

flags := re.FindString(string(out))
if flags == "" {
return errors.New("Could not find cpu flags from /proc/cpuinfo")
return fmt.Errorf("Could not find cpu flags from /proc/cpuinfo")
}

re = regexp.MustCompile(`(vmx|svm)`)

cputype := re.FindString(flags)
if cputype == "" {
return errors.New("Virtualization is not available for you CPU")
return fmt.Errorf("Virtualization is not available for you CPU")
}
logging.Debug("CPU virtualization flags are good")
return nil
}

func fixVirtualizationEnabled() error {
return errors.New("You need to enable virtualization in BIOS")
return fmt.Errorf("You need to enable virtualization in BIOS")
}

func checkKvmEnabled() error {
logging.Debug("Checking if /dev/kvm exists")
// Check if /dev/kvm exists
if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) {
return errors.New("kvm kernel module is not loaded")
return fmt.Errorf("kvm kernel module is not loaded")
}
logging.Debug("/dev/kvm was found")
return nil
Expand All @@ -100,7 +99,7 @@ func checkLibvirtInstalled() error {
logging.Debug("Checking if 'virsh' is available")
path, err := exec.LookPath("virsh")
if err != nil {
return errors.New("Libvirt cli virsh was not found in path")
return fmt.Errorf("Libvirt cli virsh was not found in path")
}
logging.Debug("'virsh' was found in ", path)
return nil
Expand Down Expand Up @@ -128,7 +127,7 @@ func checkLibvirtEnabled() error {
return fmt.Errorf("Error checking if libvirtd service is enabled")
}
if strings.TrimSpace(stdOut) != "enabled" {
return errors.New("libvirtd.service is not enabled")
return fmt.Errorf("libvirtd.service is not enabled")
}
logging.Debug("libvirtd.service is already enabled")
return nil
Expand Down Expand Up @@ -224,7 +223,7 @@ func checkLibvirtServiceRunning() error {
return fmt.Errorf("Failed to check if libvirtd service is active")
}
if strings.TrimSpace(stdOut) != "active" {
return errors.New("libvirtd.service is not running")
return fmt.Errorf("libvirtd.service is not running")
}
logging.Debug("libvirtd.service is already running")
return nil
Expand Down Expand Up @@ -311,7 +310,7 @@ func checkLibvirtCrcNetworkAvailable() error {
logging.Debug("Checking if libvirt 'crc' network exists")
_, _, err := crcos.RunWithDefaultLocale("virsh", "--connect", "qemu:///system", "net-info", "crc")
if err != nil {
return errors.New("Libvirt network crc not found")
return fmt.Errorf("Libvirt network crc not found")
}

return checkLibvirtCrcNetworkDefinition()
Expand Down Expand Up @@ -414,7 +413,7 @@ func checkLibvirtCrcNetworkActive() error {
return nil
}
}
return errors.New("Libvirt crc network is not active")
return fmt.Errorf("Libvirt crc network is not active")
}

func fixLibvirtCrcNetworkActive() error {
Expand Down Expand Up @@ -518,7 +517,7 @@ func checkNetworkManagerInstalled() error {
logging.Debug("Checking if 'nmcli' is available")
path, err := exec.LookPath("nmcli")
if err != nil {
return errors.New("NetworkManager cli nmcli was not found in path")
return fmt.Errorf("NetworkManager cli nmcli was not found in path")
}
logging.Debug("'nmcli' was found in ", path)
return nil
Expand All @@ -539,7 +538,7 @@ func checkNetworkManagerIsRunning() error {
return fmt.Errorf("%v : %s", err, stdErr)
}
if strings.TrimSpace(stdOut) != "active" {
return errors.New("NetworkManager.service is not running")
return fmt.Errorf("NetworkManager.service is not running")
}
logging.Debug("NetworkManager.service is already running")
return nil
Expand Down
33 changes: 16 additions & 17 deletions pkg/crc/preflight/preflight_checks_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strconv"
"strings"

"github.com/code-ready/crc/pkg/crc/errors"
"github.com/code-ready/crc/pkg/crc/logging"

winnet "github.com/code-ready/crc/pkg/os/windows/network"
Expand All @@ -26,17 +25,17 @@ func checkVersionOfWindowsUpdate() error {
stdOut, _, err := powershell.Execute(windowsReleaseId)
if err != nil {
logging.Debug(err.Error())
return errors.New("Failed to get Windows release id")
return fmt.Errorf("Failed to get Windows release id")
}
yourWindowsReleaseId, err := strconv.Atoi(strings.TrimSpace(stdOut))

if err != nil {
logging.Debug(err.Error())
return errors.Newf("Failed to parse Windows release id: %s", stdOut)
return fmt.Errorf("Failed to parse Windows release id: %s", stdOut)
}

if yourWindowsReleaseId < minimumWindowsReleaseId {
return errors.Newf("Please update Windows. Currently %d is the minimum release needed to run. You are running %d", minimumWindowsReleaseId, yourWindowsReleaseId)
return fmt.Errorf("Please update Windows. Currently %d is the minimum release needed to run. You are running %d", minimumWindowsReleaseId, yourWindowsReleaseId)
}
return nil
}
Expand All @@ -47,20 +46,20 @@ func checkHyperVInstalled() error {
stdOut, _, err := powershell.Execute(checkHypervisorPresent)
if err != nil {
logging.Debug(err.Error())
return errors.New("Failed checking if Hyper-V is installed")
return fmt.Errorf("Failed checking if Hyper-V is installed")
}
if !strings.Contains(stdOut, "True") {
return errors.New("Hyper-V not installed")
return fmt.Errorf("Hyper-V not installed")
}

checkVmmsExists := `@(Get-Service vmms).Status`
_, stdErr, err := powershell.Execute(checkVmmsExists)
if err != nil {
logging.Debug(err.Error())
return errors.New("Failed checking if Hyper-V management service exists")
return fmt.Errorf("Failed checking if Hyper-V management service exists")
}
if strings.Contains(stdErr, "Get-Service") {
return errors.New("Hyper-V management service not available")
return fmt.Errorf("Hyper-V management service not available")
}

return nil
Expand All @@ -73,7 +72,7 @@ func fixHyperVInstalled() error {

if err != nil {
logging.Debug(err.Error())
return errors.New("Error occurred installing Hyper-V")
return fmt.Errorf("Error occurred installing Hyper-V")
}

// We do need to error out as a restart might be needed (unfortunately no output redirect possible)
Expand All @@ -87,10 +86,10 @@ func checkHyperVServiceRunning() error {
stdOut, _, err := powershell.Execute(checkVmmsRunning)
if err != nil {
logging.Debug(err.Error())
return errors.New("Failed checking if Hyper-V is running")
return fmt.Errorf("Failed checking if Hyper-V is running")
}
if strings.TrimSpace(stdOut) != "Running" {
return errors.New("Hyper-V Virtual Machine Management service not running")
return fmt.Errorf("Hyper-V Virtual Machine Management service not running")
}

return nil
Expand All @@ -102,7 +101,7 @@ func fixHyperVServiceRunning() error {

if err != nil {
logging.Debug(err.Error())
return errors.New("Error occurred enabling Hyper-V service")
return fmt.Errorf("Error occurred enabling Hyper-V service")
}

return nil
Expand All @@ -118,10 +117,10 @@ func checkIfUserPartOfHyperVAdmins() error {
stdOut, _, err := powershell.Execute(checkIfMemberOfHyperVAdmins)
if err != nil {
logging.Debug(err.Error())
return errors.New("Failed checking if user is part of hyperv admins group")
return fmt.Errorf("Failed checking if user is part of hyperv admins group")
}
if !strings.Contains(stdOut, "True") {
return errors.New("User is not a member of the Hyper-V administrators group")
return fmt.Errorf("User is not a member of the Hyper-V administrators group")
}

return nil
Expand All @@ -131,7 +130,7 @@ func fixUserPartOfHyperVAdmins() error {
outGroupName, _, err := powershell.Execute(`(New-Object System.Security.Principal.SecurityIdentifier("S-1-5-32-578")).Translate([System.Security.Principal.NTAccount]).Value`)
if err != nil {
logging.Debug(err.Error())
return errors.New("Unable to get group name")
return fmt.Errorf("Unable to get group name")
}
groupName := strings.TrimSpace(strings.Replace(strings.TrimSpace(outGroupName), "BUILTIN\\", "", -1))

Expand All @@ -141,7 +140,7 @@ func fixUserPartOfHyperVAdmins() error {
_, _, err = powershell.ExecuteAsAdmin("add user to hyperv admins group", netCmdArgs)
if err != nil {
logging.Debug(err.Error())
return errors.New("Unable to get user name")
return fmt.Errorf("Unable to get user name")
}

return nil
Expand All @@ -157,7 +156,7 @@ func checkIfHyperVVirtualSwitchExists() error {
return nil
}

return errors.New("Virtual Switch not found")
return fmt.Errorf("Virtual Switch not found")
}

func checkIfRunningAsNormalUser() error {
Expand Down

0 comments on commit c2102d5

Please sign in to comment.