Skip to content

Commit

Permalink
Issue minishift#1368 Add PowerShell wrapper and check if HyperV avail…
Browse files Browse the repository at this point in the history
…able
  • Loading branch information
gbraad committed Sep 13, 2017
1 parent 6059e3f commit 5cfcaf0
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 11 deletions.
30 changes: 24 additions & 6 deletions cmd/minishift/cmd/start_preflight.go
Expand Up @@ -26,7 +26,8 @@ import (

"github.com/docker/machine/libmachine/drivers"
configCmd "github.com/minishift/minishift/cmd/minishift/cmd/config"
miniutil "github.com/minishift/minishift/pkg/minishift/util"
"github.com/minishift/minishift/pkg/minishift/shell/powershell"
miniUtil "github.com/minishift/minishift/pkg/minishift/util"

"github.com/minishift/minishift/pkg/util/os/atexit"
"github.com/spf13/viper"
Expand All @@ -38,28 +39,30 @@ const (

// preflightChecksAfterStartingHost is executed before the startHost function.
func preflightChecksBeforeStartingHost() {
driverErrorMessage := "See the 'Setting Up the Driver Plug-in' topic for more information"

switch viper.GetString(configCmd.VmDriver.Name) {
case "xhyve":
preflightCheckSucceedsOrFails(
configCmd.SkipCheckXHyveDriver.Name,
checkXhyveDriver,
"Checking if xhyve driver is installed",
false, configCmd.WarnCheckXHyveDriver.Name,
"See the 'Setting Up the Driver Plug-in' topic for more information")
driverErrorMessage)
case "kvm":
preflightCheckSucceedsOrFails(
configCmd.SkipCheckKVMDriver.Name,
checkKvmDriver,
"Checking if KVM driver is installed",
false, configCmd.WarnCheckXHyveDriver.Name,
"See the 'Setting Up the Driver Plug-in' topic for more information")
driverErrorMessage)
case "hyperv":
preflightCheckSucceedsOrFails(
configCmd.SkipCheckHyperVDriver.Name,
checkHypervDriver,
"Checking if Hyper-V driver is configured",
false, configCmd.WarnCheckHyperVDriver.Name,
"Hyper-V virtual switch is not set")
driverErrorMessage)
}
}

Expand Down Expand Up @@ -232,6 +235,21 @@ func checkHypervDriver() bool {
if switchEnv == "" {
return false
}

posh := powershell.New()

checkIfHyperVInstalled := `@(Get-Command Get-VM).ModuleName`
stdOut, _ := posh.Execute(checkIfHyperVInstalled)
if !strings.Contains(stdOut, "Hyper-V") {
return false
}

checkIfMemberOfHyperVAdmins := `@([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole("Hyper-V Administrators")`
stdOut, _ = posh.Execute(checkIfMemberOfHyperVAdmins)
if !strings.Contains(stdOut, "True") {
return false
}

return true
}

Expand Down Expand Up @@ -268,7 +286,7 @@ func checkIPConnectivity(driver drivers.Driver) bool {
}

fmt.Printf("\n Pinging %s ... ", ipToPing)
return miniutil.IsIPReachable(driver, ipToPing, false)
return miniUtil.IsIPReachable(driver, ipToPing, false)
}

// checkHttpConnectivity allows to test outside connectivity and possible proxy support
Expand All @@ -279,7 +297,7 @@ func checkHttpConnectivity(driver drivers.Driver) bool {
}

fmt.Printf("\n Retrieving %s ... ", urlToRetrieve)
return miniutil.IsRetrievable(driver, urlToRetrieve, false)
return miniUtil.IsRetrievable(driver, urlToRetrieve, false)
}

// checkStorageMounted checks if the peristent storage volume, storageDisk, is
Expand Down
14 changes: 9 additions & 5 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions glide.yaml
Expand Up @@ -61,6 +61,10 @@ import:
- package: github.com/DATA-DOG/godog
version: v0.6.2
- package: github.com/elazarl/goproxy
- package: github.com/gorillalabs/go-powershell
version: 3bc7a60b1df80a5766820c4a53d59668f4553f75
- package: github.com/juju/errors
version: c7d06af17c68cd34c835053720b21f6549d9b0ee
# The following repositories need to be specified explicitly, since the versions referenced by the openshift/origin dependency
# refer to forks of these projects within the openshift organizaton
# This needs to be manually updated after upgrading the openshift/origin dependnecy
Expand Down
42 changes: 42 additions & 0 deletions pkg/minishift/shell/powershell/powershell.go
@@ -0,0 +1,42 @@
/*
Copyright (C) 2017 Red Hat, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package powershell

import (
ps "github.com/gorillalabs/go-powershell"
"github.com/gorillalabs/go-powershell/backend"
)

type PowerShell struct {
powerShell ps.Shell
}

func New() *PowerShell {
return &PowerShell{
powerShell: createPowerShell(),
}
}
func (p *PowerShell) Execute(command string) (stdOut string, stdErr string) {
stdOut, stdErr, _ = p.powerShell.Execute(command)
return
}

func createPowerShell() ps.Shell {
back := &backend.Local{}
shell, _ := ps.New(back)
return shell
}

0 comments on commit 5cfcaf0

Please sign in to comment.