-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add vmware driver which supports Fusion and Workstation
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
committed
Nov 21, 2017
1 parent
10f2bb5
commit 44ef6c2
Showing
13 changed files
with
1,327 additions
and
1 deletion.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ''") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
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() { | ||
// write stdout to stderr because stdout is used for parsing sometimes | ||
cmd.Stdout = io.MultiWriter(os.Stderr, 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() { | ||
// write stdout to stderr because stdout is used for parsing sometimes | ||
cmd.Stdout = os.Stderr | ||
cmd.Stderr = os.Stderr | ||
} | ||
|
||
if stdout := cmd.Run(); stdout != nil { | ||
if ee, ok := stdout.(*exec.Error); ok && ee == exec.ErrNotFound { | ||
return ErrVMRUNNotFound | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// +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 "" | ||
} | ||
|
||
func getShareDriveAndName() (string, string, string) { | ||
return "", "", "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
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 | ||
} | ||
|
||
func getShareDriveAndName() (string, string, string) { | ||
return "Users", "/Users", "/hosthome" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
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 | ||
} | ||
|
||
func getShareDriveAndName() (string, string, string) { | ||
return "hosthome", "/home", "/hosthome" | ||
} |
Oops, something went wrong.