Skip to content

Commit

Permalink
Merge pull request #56 from Parallels/check-shared-netw
Browse files Browse the repository at this point in the history
Ensure that the host is connected to Shared network
  • Loading branch information
legal90 authored Jul 6, 2016
2 parents 5649bae + 683fada commit 37869d2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 46 deletions.
73 changes: 50 additions & 23 deletions parallels.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package parallels

import (
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
Expand Down Expand Up @@ -32,6 +34,16 @@ const (
defaultDiskSize = 20000
)

var (
reMachineNotFound = regexp.MustCompile(`Failed to get VM config: The virtual machine could not be found..*`)
reMajorVersion = regexp.MustCompile(`prlctl version (\d+)\.\d+\.\d+.*`)
reParallelsEdition = regexp.MustCompile(`edition="(.+)"`)

errMachineExist = errors.New("machine already exists")
errMachineNotExist = errors.New("machine does not exist")
errSharedNotConnected = errors.New("Your Mac host is not connected to Shared network. Please, enable this option: 'Parallels Desktop' -> 'Preferences' -> 'Network' -> 'Shared' -> 'Connect Mac to this network'")
)

// Driver for Parallels Desktop
type Driver struct {
*drivers.BaseDriver
Expand Down Expand Up @@ -77,7 +89,7 @@ func (d *Driver) Create() error {

log.Infof("Creating Parallels Desktop VM...")

ver, err := d.getParallelsVersion()
ver, err := getParallelsVersion()
if err != nil {
return err
}
Expand Down Expand Up @@ -259,15 +271,6 @@ func (d *Driver) GetSSHHostname() (string, error) {
return d.GetIP()
}

// GetSSHUsername returns username for use with ssh
func (d *Driver) GetSSHUsername() string {
if d.SSHUser == "" {
d.SSHUser = "docker"
}

return d.SSHUser
}

// GetURL returns a Docker compatible host URL for connecting to this host
// e.g. tcp://1.2.3.4:2376
func (d *Driver) GetURL() (string, error) {
Expand All @@ -286,7 +289,7 @@ func (d *Driver) GetState() (state.State, error) {
stdout, stderr, err := prlctlOutErr("list", d.MachineName, "--output", "status", "--no-header")
if err != nil {
if reMachineNotFound.FindString(stderr) != "" {
return state.Error, ErrMachineNotExist
return state.Error, errMachineNotExist
}
return state.Error, err
}
Expand Down Expand Up @@ -319,11 +322,8 @@ func (d *Driver) PreCreateCheck() error {
}

// Check Parallels Desktop version
ver, err := d.getParallelsVersion()
ver, err := getParallelsVersion()
if err != nil {
if err == ErrPrlctlNotFound {
return fmt.Errorf("Could not detect `prlctl` binary! Make sure Parallels Desktop Pro or Business edition is installed")
}
return err
}

Expand All @@ -339,7 +339,7 @@ func (d *Driver) PreCreateCheck() error {
}

// Check Parallels Desktop edition
edit, err := d.getParallelsEdition()
edit, err := getParallelsEdition()
if err != nil {
return err
}
Expand Down Expand Up @@ -367,7 +367,7 @@ func (d *Driver) PreCreateCheck() error {
func (d *Driver) Remove() error {
s, err := d.GetState()
if err != nil {
if err == ErrMachineNotExist {
if err == errMachineNotExist {
log.Infof("machine does not exist, assuming it has been removed already")
return nil
}
Expand Down Expand Up @@ -442,6 +442,15 @@ func (d *Driver) SetConfigFromFlags(opts drivers.DriverOptions) error {

// Start a host
func (d *Driver) Start() error {
// Check whether the host is connected to Shared network
ok, err := isSharedConnected()
if err != nil {
return err
}
if !ok {
return errSharedNotConnected
}

s, err := d.GetState()
if err != nil {
return err
Expand Down Expand Up @@ -630,8 +639,19 @@ func (d *Driver) generateDiskImage(size int) error {
return nil
}

// Detect Parallels Desktop major version
func (d *Driver) getParallelsVersion() (int, error) {
func (d *Driver) publicSSHKeyPath() string {
return d.GetSSHKeyPath() + ".pub"
}

func detectCmdInPath(cmd string) string {
if path, err := exec.LookPath(cmd); err == nil {
return path
}
return cmd
}

// Detects Parallels Desktop major version
func getParallelsVersion() (int, error) {
stdout, _, err := prlctlOutErr("--version")
if err != nil {
return 0, err
Expand All @@ -651,8 +671,8 @@ func (d *Driver) getParallelsVersion() (int, error) {
return majorVer, nil
}

// Detect Parallels Desktop edition
func (d *Driver) getParallelsEdition() (string, error) {
// Detects Parallels Desktop edition
func getParallelsEdition() (string, error) {
stdout, _, err := prlsrvctlOutErr("info", "--license")
if err != nil {
return "", err
Expand All @@ -667,6 +687,13 @@ func (d *Driver) getParallelsEdition() (string, error) {
return res[1], nil
}

func (d *Driver) publicSSHKeyPath() string {
return d.GetSSHKeyPath() + ".pub"
// Checks whether the host is connected to Shared network
func isSharedConnected() (bool, error) {
stdout, _, err := prlsrvctlOutErr("net", "info", "Shared")
if err != nil {
return false, err
}

reSharedIsConnected := regexp.MustCompile(`Bound To:.*`)
return reSharedIsConnected.MatchString(stdout), nil
}
34 changes: 11 additions & 23 deletions prlctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,19 @@ import (
"errors"
"os"
"os/exec"
"regexp"
"strings"

"github.com/docker/machine/libmachine/log"
)

// TODO check these
var (
reVMNameUUID = regexp.MustCompile(`"(.+)" {([0-9a-f-]+)}`)
reVMInfoLine = regexp.MustCompile(`(?:"(.+)"|(.+))=(?:"(.*)"|(.*))`)
reColonLine = regexp.MustCompile(`(.+):\s+(.*)`)
reMachineNotFound = regexp.MustCompile(`Failed to get VM config: The virtual machine could not be found..*`)
reMajorVersion = regexp.MustCompile(`prlctl version (\d+)\.\d+\.\d+.*`)
reParallelsEdition = regexp.MustCompile(`edition="(.+)"`)
)
prlctlCmd = detectCmdInPath("prlctl")
prlsrvctlCmd = detectCmdInPath("prlsrvctl")
prldisktoolCmd = detectCmdInPath("prl_disk_tool")

var (
ErrMachineExist = errors.New("machine already exists")
ErrMachineNotExist = errors.New("machine does not exist")
ErrPrlctlNotFound = errors.New("prlctl not found")
ErrPrlsrvctlNotFound = errors.New("prlsrvctl not found")
ErrPrldisktoolNotFound = errors.New("prl_disk_tool not found")
prlctlCmd = "prlctl"
prlsrvctlCmd = "prlsrvctl"
prldisktoolCmd = "prl_disk_tool"
errPrlctlNotFound = errors.New("Could not detect `prlctl` binary! Make sure Parallels Desktop Pro or Business edition is installed")
errPrlsrvctlNotFound = errors.New("Could not detect `prlsrvctl` binary! Make sure Parallels Desktop Pro or Business edition is installed")
errPrldisktoolNotFound = errors.New("Could not detect `prl_disk_tool` binary! Make sure Parallels Desktop Pro or Business edition is installed")
)

func runCmd(cmdName string, args []string, notFound error) (string, string, error) {
Expand All @@ -54,24 +42,24 @@ func runCmd(cmdName string, args []string, notFound error) (string, string, erro
}

func prlctl(args ...string) error {
_, _, err := runCmd(prlctlCmd, args, ErrPrlctlNotFound)
_, _, err := runCmd(prlctlCmd, args, errPrlctlNotFound)
return err
}

func prlctlOutErr(args ...string) (string, string, error) {
return runCmd(prlctlCmd, args, ErrPrlctlNotFound)
return runCmd(prlctlCmd, args, errPrlctlNotFound)
}

func prlsrvctl(args ...string) error {
_, _, err := runCmd(prlsrvctlCmd, args, ErrPrlsrvctlNotFound)
_, _, err := runCmd(prlsrvctlCmd, args, errPrlsrvctlNotFound)
return err
}

func prlsrvctlOutErr(args ...string) (string, string, error) {
return runCmd(prlsrvctlCmd, args, ErrPrlsrvctlNotFound)
return runCmd(prlsrvctlCmd, args, errPrlsrvctlNotFound)
}

func prldisktool(args ...string) error {
_, _, err := runCmd(prldisktoolCmd, args, ErrPrldisktoolNotFound)
_, _, err := runCmd(prldisktoolCmd, args, errPrldisktoolNotFound)
return err
}

0 comments on commit 37869d2

Please sign in to comment.