Skip to content

Commit

Permalink
Make driver config separate package
Browse files Browse the repository at this point in the history
This makes it possible for user of this driver to load config only
without having a hard dependency on the driver itself. For instance,
when the driver is a standalone binary, the user of this driver only
needs to know how to configure this driver, like minikube's non-builtin
drivers.
  • Loading branch information
Yongkun Anfernee Gui committed Mar 13, 2018
1 parent 7f08a90 commit e16614c
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 93 deletions.
119 changes: 119 additions & 0 deletions pkg/drivers/vmware/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
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 config

import (
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/mcnflag"
)

const (
defaultSSHUser = "docker"
defaultSSHPass = "tcuser"
defaultDiskSize = 20000
defaultCPU = 1
defaultMemory = 1024
)

// Config specifies the configuration of driver VMware
type Config struct {
*drivers.BaseDriver

Memory int
DiskSize int
CPU int
ISO string
Boot2DockerURL string

SSHPassword string
ConfigDriveISO string
ConfigDriveURL string
NoShare bool
}

// NewConfig creates a new Config
func NewConfig(hostname, storePath string) *Config {
return &Config{
CPU: defaultCPU,
Memory: defaultMemory,
DiskSize: defaultDiskSize,
SSHPassword: defaultSSHPass,
BaseDriver: &drivers.BaseDriver{
SSHUser: defaultSSHUser,
MachineName: hostname,
StorePath: storePath,
},
}
}

// GetCreateFlags registers the flags this driver adds to
// "docker hosts create"
func (c *Config) GetCreateFlags() []mcnflag.Flag {
return []mcnflag.Flag{
mcnflag.StringFlag{
EnvVar: "VMWARE_BOOT2DOCKER_URL",
Name: "vmware-boot2docker-url",
Usage: "URL for boot2docker image",
Value: "",
},
mcnflag.StringFlag{
EnvVar: "VMWARE_CONFIGDRIVE_URL",
Name: "vmware-configdrive-url",
Usage: "URL for cloud-init configdrive",
Value: "",
},
mcnflag.IntFlag{
EnvVar: "VMWARE_CPU_COUNT",
Name: "vmware-cpu-count",
Usage: "number of CPUs for the machine (-1 to use the number of CPUs available)",
Value: defaultCPU,
},
mcnflag.IntFlag{
EnvVar: "VMWARE_MEMORY_SIZE",
Name: "vmware-memory-size",
Usage: "size of memory for host VM (in MB)",
Value: defaultMemory,
},
mcnflag.IntFlag{
EnvVar: "VMWARE_DISK_SIZE",
Name: "vmware-disk-size",
Usage: "size of disk for host VM (in MB)",
Value: defaultDiskSize,
},
mcnflag.StringFlag{
EnvVar: "VMWARE_SSH_USER",
Name: "vmware-ssh-user",
Usage: "SSH user",
Value: defaultSSHUser,
},
mcnflag.StringFlag{
EnvVar: "VMWARE_SSH_PASSWORD",
Name: "vmware-ssh-password",
Usage: "SSH password",
Value: defaultSSHPass,
},
mcnflag.BoolFlag{
EnvVar: "VMWARE_NO_SHARE",
Name: "vmware-no-share",
Usage: "Disable the mount of your home directory",
},
}
}
104 changes: 11 additions & 93 deletions pkg/drivers/vmware/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,108 +36,26 @@ import (

"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/mcnflag"
"github.com/docker/machine/libmachine/mcnutils"
"github.com/docker/machine/libmachine/ssh"
"github.com/docker/machine/libmachine/state"
"github.com/machine-drivers/docker-machine-driver-vmware/pkg/drivers/vmware/config"
cryptossh "golang.org/x/crypto/ssh"
)

const (
B2DUser = "docker"
B2DPass = "tcuser"
isoFilename = "boot2docker.iso"
isoConfigDrive = "configdrive.iso"
)

// Driver for VMware
type Driver struct {
*drivers.BaseDriver
Memory int
DiskSize int
CPU int
ISO string
Boot2DockerURL string

SSHPassword string
ConfigDriveISO string
ConfigDriveURL string
NoShare bool
*config.Config
}

const (
defaultSSHUser = B2DUser
defaultSSHPass = B2DPass
defaultDiskSize = 20000
defaultCPU = 1
defaultMemory = 1024
)

// GetCreateFlags registers the flags this driver adds to
// "docker hosts create"
func (d *Driver) GetCreateFlags() []mcnflag.Flag {
return []mcnflag.Flag{
mcnflag.StringFlag{
EnvVar: "VMWARE_BOOT2DOCKER_URL",
Name: "vmware-boot2docker-url",
Usage: "URL for boot2docker image",
Value: "",
},
mcnflag.StringFlag{
EnvVar: "VMWARE_CONFIGDRIVE_URL",
Name: "vmware-configdrive-url",
Usage: "URL for cloud-init configdrive",
Value: "",
},
mcnflag.IntFlag{
EnvVar: "VMWARE_CPU_COUNT",
Name: "vmware-cpu-count",
Usage: "number of CPUs for the machine (-1 to use the number of CPUs available)",
Value: defaultCPU,
},
mcnflag.IntFlag{
EnvVar: "VMWARE_MEMORY_SIZE",
Name: "vmware-memory-size",
Usage: "size of memory for host VM (in MB)",
Value: defaultMemory,
},
mcnflag.IntFlag{
EnvVar: "VMWARE_DISK_SIZE",
Name: "vmware-disk-size",
Usage: "size of disk for host VM (in MB)",
Value: defaultDiskSize,
},
mcnflag.StringFlag{
EnvVar: "VMWARE_SSH_USER",
Name: "vmware-ssh-user",
Usage: "SSH user",
Value: defaultSSHUser,
},
mcnflag.StringFlag{
EnvVar: "VMWARE_SSH_PASSWORD",
Name: "vmware-ssh-password",
Usage: "SSH password",
Value: defaultSSHPass,
},
mcnflag.BoolFlag{
EnvVar: "VMWARE_NO_SHARE",
Name: "vmware-no-share",
Usage: "Disable the mount of your home directory",
},
}
}

func NewDriver(hostName, storePath string) drivers.Driver {
func NewDriver(hostname, storePath string) drivers.Driver {
return &Driver{
CPU: defaultCPU,
Memory: defaultMemory,
DiskSize: defaultDiskSize,
SSHPassword: defaultSSHPass,
BaseDriver: &drivers.BaseDriver{
SSHUser: defaultSSHUser,
MachineName: hostName,
StorePath: storePath,
},
Config: config.NewConfig(hostname, storePath),
}
}

Expand Down Expand Up @@ -378,29 +296,29 @@ func (d *Driver) Start() error {
}

// Test if /var/lib/boot2docker exists
vmrun("-gu", B2DUser, "-gp", B2DPass, "directoryExistsInGuest", d.vmxPath(), "/var/lib/boot2docker")
vmrun("-gu", d.SSHUser, "-gp", d.SSHPassword, "directoryExistsInGuest", d.vmxPath(), "/var/lib/boot2docker")

// Copy SSH keys bundle
vmrun("-gu", B2DUser, "-gp", B2DPass, "CopyFileFromHostToGuest", d.vmxPath(), d.ResolveStorePath("userdata.tar"), "/home/docker/userdata.tar")
vmrun("-gu", d.SSHUser, "-gp", d.SSHPassword, "CopyFileFromHostToGuest", d.vmxPath(), d.ResolveStorePath("userdata.tar"), "/home/docker/userdata.tar")

// Expand tar file.
vmrun("-gu", B2DUser, "-gp", B2DPass, "runScriptInGuest", d.vmxPath(), "/bin/sh", "sudo sh -c \"tar xvf /home/docker/userdata.tar -C /home/docker > /var/log/userdata.log 2>&1 && chown -R docker:staff /home/docker\"")
vmrun("-gu", d.SSHUser, "-gp", d.SSHPassword, "runScriptInGuest", d.vmxPath(), "/bin/sh", "sudo sh -c \"tar xvf /home/docker/userdata.tar -C /home/docker > /var/log/userdata.log 2>&1 && chown -R docker:staff /home/docker\"")

// copy to /var/lib/boot2docker
vmrun("-gu", B2DUser, "-gp", B2DPass, "runScriptInGuest", d.vmxPath(), "/bin/sh", "sudo /bin/mv /home/docker/userdata.tar /var/lib/boot2docker/userdata.tar")
vmrun("-gu", d.SSHUser, "-gp", d.SSHPassword, "runScriptInGuest", d.vmxPath(), "/bin/sh", "sudo /bin/mv /home/docker/userdata.tar /var/lib/boot2docker/userdata.tar")

// Enable Shared Folders
vmrun("-gu", B2DUser, "-gp", B2DPass, "enableSharedFolders", d.vmxPath())
vmrun("-gu", d.SSHUser, "-gp", d.SSHPassword, "enableSharedFolders", d.vmxPath())

shareName, hostDir, shareDir := getShareDriveAndName()
if hostDir != "" && !d.NoShare {
if _, err := os.Stat(hostDir); err != nil && !os.IsNotExist(err) {
return err
} else if !os.IsNotExist(err) {
// add shared folder, create mountpoint and mount it.
vmrun("-gu", B2DUser, "-gp", B2DPass, "addSharedFolder", d.vmxPath(), shareName, hostDir)
vmrun("-gu", d.SSHUser, "-gp", d.SSHPassword, "addSharedFolder", d.vmxPath(), shareName, hostDir)
command := mountCommand(shareName, shareDir)
vmrun("-gu", B2DUser, "-gp", B2DPass, "runScriptInGuest", d.vmxPath(), "/bin/sh", command)
vmrun("-gu", d.SSHUser, "-gp", d.SSHPassword, "runScriptInGuest", d.vmxPath(), "/bin/sh", command)
}
}
return nil
Expand Down

0 comments on commit e16614c

Please sign in to comment.