Skip to content

Commit

Permalink
Add vmware driver which supports Fusion and Workstation
Browse files Browse the repository at this point in the history
VMware Fusion is available on MacOS. VMware Workstation is available on
both Linux and Windows.

vmwarefusion is still supported, but it only works for fusion. It
should be deprecated in future.
  • Loading branch information
Yongkun Anfernee Gui authored and anfernee committed Nov 9, 2017
1 parent 4d10509 commit 1130b3a
Show file tree
Hide file tree
Showing 13 changed files with 1,309 additions and 2 deletions.
775 changes: 775 additions & 0 deletions pkg/drivers/vmware/driver.go

Large diffs are not rendered by default.

133 changes: 133 additions & 0 deletions pkg/drivers/vmware/driver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
Copyright 2017 The Kubernetes Authors All rights reserved.
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 vmware

import (
"io/ioutil"
"log"
"os"
"testing"

"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/state"
)

var skip = !check(vmrunbin) || !check(vdiskmanbin)

func check(path string) bool {
_, err := os.Stat(path)
if err != nil {
log.Printf("%q is missing", path)
return false
}

return true
}

func TestSetConfigFromFlags(t *testing.T) {
driver := NewDriver("default", "path")

checkFlags := &drivers.CheckDriverOptions{
FlagsValues: map[string]interface{}{},
CreateFlags: driver.GetCreateFlags(),
}

err := driver.SetConfigFromFlags(checkFlags)
if err != nil {
t.Fatal(err)
}

if len(checkFlags.InvalidFlags) != 0 {
t.Fatalf("expect len(checkFlags.InvalidFlags) == 0; got %d", len(checkFlags.InvalidFlags))
}
}

func TestDriver(t *testing.T) {
if skip {
t.Skip()
}

path, err := ioutil.TempDir("", "vmware-driver-test")
if err != nil {
t.Fatal(err)
}

defer os.RemoveAll(path)

driver := NewDriver("default", path)

checkFlags := &drivers.CheckDriverOptions{
FlagsValues: map[string]interface{}{},
CreateFlags: driver.GetCreateFlags(),
}

err = driver.SetConfigFromFlags(checkFlags)
if err != nil {
t.Fatal(err)
}

driver.(*Driver).Boot2DockerURL = "https://github.com/boot2docker/boot2docker/releases/download/v17.10.0-ce-rc2/boot2docker.iso"

err = driver.Create()
if err != nil {
t.Fatal(err)
}

defer driver.Remove()

st, err := driver.GetState()
if err != nil {
t.Fatal(err)
}
if st != state.Running {
t.Fatalf("expect state == Running; got %s", st.String())
}

ip, err := driver.GetIP()
if err != nil {
t.Fatal(err)
}
if ip == "" {
t.Fatal("expect ip non-zero; got ''")
}

username := driver.GetSSHUsername()
if username == "" {
t.Fatal("expect username non-zero; got ''")
}

key := driver.GetSSHKeyPath()
if key == "" {
t.Fatal("expect key non-zero; got ''")
}

port, err := driver.GetSSHPort()
if err != nil {
t.Fatal(err)
}
if port == 0 {
t.Fatal("expect port not 0; got 0")
}

host, err := driver.GetSSHHostname()
if err != nil {
t.Fatal(err)
}
if host == "" {
t.Fatal("expect host non-zero; got ''")
}
}
94 changes: 94 additions & 0 deletions pkg/drivers/vmware/vmrun.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
Copyright 2017 The Kubernetes Authors All rights reserved.
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.
*/

/*
* Copyright 2017 VMware, Inc. All rights reserved. Licensed under the Apache v2 License.
*/

package vmware

import (
"bytes"
"errors"
"fmt"
"io"
"os"
"os/exec"
"strings"

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

var (
vmrunbin = setVmwareCmd("vmrun")
vdiskmanbin = setVmwareCmd("vmware-vdiskmanager")
)

var (
ErrMachineExist = errors.New("machine already exists")
ErrMachineNotExist = errors.New("machine does not exist")
ErrVMRUNNotFound = errors.New("VMRUN not found")
)

func init() {
// vmrun with nogui on VMware Fusion through at least 8.0.1 doesn't work right
// if the umask is set to not allow world-readable permissions
SetUmask()
}

func isMachineDebugEnabled() bool {
return os.Getenv("MACHINE_DEBUG") != ""
}

func vmrun(args ...string) (string, string, error) {
cmd := exec.Command(vmrunbin, args...)

var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout, cmd.Stderr = &stdout, &stderr

if isMachineDebugEnabled() {
cmd.Stdout = io.MultiWriter(os.Stdout, cmd.Stdout)
cmd.Stderr = io.MultiWriter(os.Stderr, cmd.Stderr)
}

log.Debugf("executing: %v %v", vmrunbin, strings.Join(args, " "))

err := cmd.Run()
if err != nil {
if ee, ok := err.(*exec.Error); ok && ee == exec.ErrNotFound {
err = ErrVMRUNNotFound
}
}

return stdout.String(), stderr.String(), err
}

// Make a vmdk disk image with the given size (in MB).
func vdiskmanager(dest string, size int) error {
cmd := exec.Command(vdiskmanbin, "-c", "-t", "0", "-s", fmt.Sprintf("%dMB", size), "-a", "lsilogic", dest)
if isMachineDebugEnabled() {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}

if stdout := cmd.Run(); stdout != nil {
if ee, ok := stdout.(*exec.Error); ok && ee == exec.ErrNotFound {
return ErrVMRUNNotFound
}
}
return nil
}
40 changes: 40 additions & 0 deletions pkg/drivers/vmware/vmware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// +build !darwin,!linux,!windows

/*
Copyright 2017 The Kubernetes Authors All rights reserved.
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 vmware

import "github.com/docker/machine/libmachine/drivers"

func NewDriver(hostName, storePath string) drivers.Driver {
return drivers.NewDriverNotSupported("vmware", hostName, storePath)
}

func DhcpConfigFiles() string {
return ""
}

func DhcpLeaseFiles() string {
return ""
}

func SetUmask() {
}

func setVmwareCmd(cmd string) string {
return ""
}
53 changes: 53 additions & 0 deletions pkg/drivers/vmware/vmware_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright 2017 The Kubernetes Authors All rights reserved.
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 vmware

import (
"os"
"os/exec"
"path/filepath"
"syscall"
)

func DhcpConfigFiles() string {
return "/Library/Preferences/VMware Fusion/vmnet*/dhcpd.conf"
}

func DhcpLeaseFiles() string {
return "/var/db/vmware/*.leases"
}

func SetUmask() {
_ = syscall.Umask(022)
}

// detect the vmrun and vmware-vdiskmanager cmds' path if needed
func setVmwareCmd(cmd string) string {
if path, err := exec.LookPath(cmd); err == nil {
return path
}
for _, fp := range []string{
"/Applications/VMware Fusion.app/Contents/Library/",
} {
p := filepath.Join(fp, cmd)
_, err := os.Stat(p)
if err == nil {
return p
}
}
return cmd
}
42 changes: 42 additions & 0 deletions pkg/drivers/vmware/vmware_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright 2017 The Kubernetes Authors All rights reserved.
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 vmware

import (
"os/exec"
"syscall"
)

func DhcpConfigFiles() string {
return "/etc/vmware/vmnet*/dhcpd/dhcpd.conf"
}

func DhcpLeaseFiles() string {
return "/etc/vmware/vmnet*/dhcpd/dhcpd.leases"
}

func SetUmask() {
_ = syscall.Umask(022)
}

// detect the vmrun and vmware-vdiskmanager cmds' path if needed
func setVmwareCmd(cmd string) string {
if path, err := exec.LookPath(cmd); err == nil {
return path
}
return cmd
}
Loading

0 comments on commit 1130b3a

Please sign in to comment.