Skip to content

Commit

Permalink
Merge pull request #96 from legal90/improve-shared-network-check
Browse files Browse the repository at this point in the history
Improve the check if Shared network adapter is connected
  • Loading branch information
legal90 authored Oct 13, 2020
2 parents 865d31d + 3d6550c commit 98cd20c
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions parallels_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ var (
reMachineNotFound = regexp.MustCompile(`Failed to get VM config: The virtual machine could not be found..*`)
reParallelsVersion = regexp.MustCompile(`.* (\d+\.\d+\.\d+).*`)
reParallelsEdition = regexp.MustCompile(`edition="(.+)"`)
reSharedAdapterIP = regexp.MustCompile(`\s*IPv4 address:\s*(\d+\.\d+\.\d+\.\d+)`)
reSharedFolder = regexp.MustCompile(`\s*(.+) \(\+\) path='(.+)' mode=.+`)

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'")
errMachineExist = errors.New("machine already exists")
errMachineNotExist = errors.New("machine does not exist")
errSharedNetworkNotConnected = errors.New("Your Mac host is not connected to Shared network. Please, ensure this option is set: 'Parallels Desktop' -> 'Preferences' -> 'Network' -> 'Shared' -> 'Connect Mac to this network'")

v10, _ = version.NewVersion("10.0.0")
v11, _ = version.NewVersion("11.0.0")
Expand Down Expand Up @@ -407,13 +408,9 @@ func (d *Driver) PreCreateCheck() error {
}

// Check whether the host is connected to Shared network
ok, err := isSharedConnected()
if err != nil {
if err := checkSharedNetworkConnected(); err != nil {
return err
}
if !ok {
return errSharedNotConnected
}

// Downloading boot2docker to cache should be done here to make sure
// that a download failure will not leave a machine half created.
Expand Down Expand Up @@ -523,13 +520,9 @@ 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 {
if err := checkSharedNetworkConnected(); err != nil {
return err
}
if !ok {
return errSharedNotConnected
}

s, err := d.GetState()
if err != nil {
Expand Down Expand Up @@ -787,12 +780,35 @@ func getParallelsEdition() (string, error) {
}

// Checks whether the host is connected to Shared network
func isSharedConnected() (bool, error) {
func checkSharedNetworkConnected() error {
stdout, _, err := prlsrvctlOutErr("net", "info", "Shared")
if err != nil {
return false, err
return err
}

// Parse the IPv4 of Shared network adapter
res := reSharedAdapterIP.FindStringSubmatch(string(stdout))
if res == nil {
return errSharedNetworkNotConnected
}

sharedNetworkIP := net.ParseIP(res[1])
log.Debugf("IP address of Shared network adapter: %s", sharedNetworkIP)

hostAddrs, err := net.InterfaceAddrs()
if err != nil {
return err
}
log.Debugf("All host interface addressess: %v", hostAddrs)

// Check if the there is an interface with the Shared network adapter's IP assigned
for _, netAddr := range hostAddrs {
ipAddr := netAddr.(*net.IPNet).IP
if ipAddr.Equal(sharedNetworkIP) {
log.Debugf("Parallels Shared network adapter is connected")
return nil
}
}

reSharedIsConnected := regexp.MustCompile(`Bound To:.*`)
return reSharedIsConnected.MatchString(stdout), nil
return errSharedNetworkNotConnected
}

0 comments on commit 98cd20c

Please sign in to comment.