Skip to content

Commit e16614c

Browse files
author
Yongkun Anfernee Gui
committed
Make driver config separate package
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.
1 parent 7f08a90 commit e16614c

File tree

2 files changed

+130
-93
lines changed

2 files changed

+130
-93
lines changed

pkg/drivers/vmware/config/config.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
/*
18+
* Copyright 2017 VMware, Inc. All rights reserved. Licensed under the Apache v2 License.
19+
*/
20+
21+
package config
22+
23+
import (
24+
"github.com/docker/machine/libmachine/drivers"
25+
"github.com/docker/machine/libmachine/mcnflag"
26+
)
27+
28+
const (
29+
defaultSSHUser = "docker"
30+
defaultSSHPass = "tcuser"
31+
defaultDiskSize = 20000
32+
defaultCPU = 1
33+
defaultMemory = 1024
34+
)
35+
36+
// Config specifies the configuration of driver VMware
37+
type Config struct {
38+
*drivers.BaseDriver
39+
40+
Memory int
41+
DiskSize int
42+
CPU int
43+
ISO string
44+
Boot2DockerURL string
45+
46+
SSHPassword string
47+
ConfigDriveISO string
48+
ConfigDriveURL string
49+
NoShare bool
50+
}
51+
52+
// NewConfig creates a new Config
53+
func NewConfig(hostname, storePath string) *Config {
54+
return &Config{
55+
CPU: defaultCPU,
56+
Memory: defaultMemory,
57+
DiskSize: defaultDiskSize,
58+
SSHPassword: defaultSSHPass,
59+
BaseDriver: &drivers.BaseDriver{
60+
SSHUser: defaultSSHUser,
61+
MachineName: hostname,
62+
StorePath: storePath,
63+
},
64+
}
65+
}
66+
67+
// GetCreateFlags registers the flags this driver adds to
68+
// "docker hosts create"
69+
func (c *Config) GetCreateFlags() []mcnflag.Flag {
70+
return []mcnflag.Flag{
71+
mcnflag.StringFlag{
72+
EnvVar: "VMWARE_BOOT2DOCKER_URL",
73+
Name: "vmware-boot2docker-url",
74+
Usage: "URL for boot2docker image",
75+
Value: "",
76+
},
77+
mcnflag.StringFlag{
78+
EnvVar: "VMWARE_CONFIGDRIVE_URL",
79+
Name: "vmware-configdrive-url",
80+
Usage: "URL for cloud-init configdrive",
81+
Value: "",
82+
},
83+
mcnflag.IntFlag{
84+
EnvVar: "VMWARE_CPU_COUNT",
85+
Name: "vmware-cpu-count",
86+
Usage: "number of CPUs for the machine (-1 to use the number of CPUs available)",
87+
Value: defaultCPU,
88+
},
89+
mcnflag.IntFlag{
90+
EnvVar: "VMWARE_MEMORY_SIZE",
91+
Name: "vmware-memory-size",
92+
Usage: "size of memory for host VM (in MB)",
93+
Value: defaultMemory,
94+
},
95+
mcnflag.IntFlag{
96+
EnvVar: "VMWARE_DISK_SIZE",
97+
Name: "vmware-disk-size",
98+
Usage: "size of disk for host VM (in MB)",
99+
Value: defaultDiskSize,
100+
},
101+
mcnflag.StringFlag{
102+
EnvVar: "VMWARE_SSH_USER",
103+
Name: "vmware-ssh-user",
104+
Usage: "SSH user",
105+
Value: defaultSSHUser,
106+
},
107+
mcnflag.StringFlag{
108+
EnvVar: "VMWARE_SSH_PASSWORD",
109+
Name: "vmware-ssh-password",
110+
Usage: "SSH password",
111+
Value: defaultSSHPass,
112+
},
113+
mcnflag.BoolFlag{
114+
EnvVar: "VMWARE_NO_SHARE",
115+
Name: "vmware-no-share",
116+
Usage: "Disable the mount of your home directory",
117+
},
118+
}
119+
}

pkg/drivers/vmware/driver.go

Lines changed: 11 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -36,108 +36,26 @@ import (
3636

3737
"github.com/docker/machine/libmachine/drivers"
3838
"github.com/docker/machine/libmachine/log"
39-
"github.com/docker/machine/libmachine/mcnflag"
4039
"github.com/docker/machine/libmachine/mcnutils"
4140
"github.com/docker/machine/libmachine/ssh"
4241
"github.com/docker/machine/libmachine/state"
42+
"github.com/machine-drivers/docker-machine-driver-vmware/pkg/drivers/vmware/config"
4343
cryptossh "golang.org/x/crypto/ssh"
4444
)
4545

4646
const (
47-
B2DUser = "docker"
48-
B2DPass = "tcuser"
4947
isoFilename = "boot2docker.iso"
5048
isoConfigDrive = "configdrive.iso"
5149
)
5250

5351
// Driver for VMware
5452
type Driver struct {
55-
*drivers.BaseDriver
56-
Memory int
57-
DiskSize int
58-
CPU int
59-
ISO string
60-
Boot2DockerURL string
61-
62-
SSHPassword string
63-
ConfigDriveISO string
64-
ConfigDriveURL string
65-
NoShare bool
53+
*config.Config
6654
}
6755

68-
const (
69-
defaultSSHUser = B2DUser
70-
defaultSSHPass = B2DPass
71-
defaultDiskSize = 20000
72-
defaultCPU = 1
73-
defaultMemory = 1024
74-
)
75-
76-
// GetCreateFlags registers the flags this driver adds to
77-
// "docker hosts create"
78-
func (d *Driver) GetCreateFlags() []mcnflag.Flag {
79-
return []mcnflag.Flag{
80-
mcnflag.StringFlag{
81-
EnvVar: "VMWARE_BOOT2DOCKER_URL",
82-
Name: "vmware-boot2docker-url",
83-
Usage: "URL for boot2docker image",
84-
Value: "",
85-
},
86-
mcnflag.StringFlag{
87-
EnvVar: "VMWARE_CONFIGDRIVE_URL",
88-
Name: "vmware-configdrive-url",
89-
Usage: "URL for cloud-init configdrive",
90-
Value: "",
91-
},
92-
mcnflag.IntFlag{
93-
EnvVar: "VMWARE_CPU_COUNT",
94-
Name: "vmware-cpu-count",
95-
Usage: "number of CPUs for the machine (-1 to use the number of CPUs available)",
96-
Value: defaultCPU,
97-
},
98-
mcnflag.IntFlag{
99-
EnvVar: "VMWARE_MEMORY_SIZE",
100-
Name: "vmware-memory-size",
101-
Usage: "size of memory for host VM (in MB)",
102-
Value: defaultMemory,
103-
},
104-
mcnflag.IntFlag{
105-
EnvVar: "VMWARE_DISK_SIZE",
106-
Name: "vmware-disk-size",
107-
Usage: "size of disk for host VM (in MB)",
108-
Value: defaultDiskSize,
109-
},
110-
mcnflag.StringFlag{
111-
EnvVar: "VMWARE_SSH_USER",
112-
Name: "vmware-ssh-user",
113-
Usage: "SSH user",
114-
Value: defaultSSHUser,
115-
},
116-
mcnflag.StringFlag{
117-
EnvVar: "VMWARE_SSH_PASSWORD",
118-
Name: "vmware-ssh-password",
119-
Usage: "SSH password",
120-
Value: defaultSSHPass,
121-
},
122-
mcnflag.BoolFlag{
123-
EnvVar: "VMWARE_NO_SHARE",
124-
Name: "vmware-no-share",
125-
Usage: "Disable the mount of your home directory",
126-
},
127-
}
128-
}
129-
130-
func NewDriver(hostName, storePath string) drivers.Driver {
56+
func NewDriver(hostname, storePath string) drivers.Driver {
13157
return &Driver{
132-
CPU: defaultCPU,
133-
Memory: defaultMemory,
134-
DiskSize: defaultDiskSize,
135-
SSHPassword: defaultSSHPass,
136-
BaseDriver: &drivers.BaseDriver{
137-
SSHUser: defaultSSHUser,
138-
MachineName: hostName,
139-
StorePath: storePath,
140-
},
58+
Config: config.NewConfig(hostname, storePath),
14159
}
14260
}
14361

@@ -378,29 +296,29 @@ func (d *Driver) Start() error {
378296
}
379297

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

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

386304
// Expand tar file.
387-
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\"")
305+
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\"")
388306

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

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

395313
shareName, hostDir, shareDir := getShareDriveAndName()
396314
if hostDir != "" && !d.NoShare {
397315
if _, err := os.Stat(hostDir); err != nil && !os.IsNotExist(err) {
398316
return err
399317
} else if !os.IsNotExist(err) {
400318
// add shared folder, create mountpoint and mount it.
401-
vmrun("-gu", B2DUser, "-gp", B2DPass, "addSharedFolder", d.vmxPath(), shareName, hostDir)
319+
vmrun("-gu", d.SSHUser, "-gp", d.SSHPassword, "addSharedFolder", d.vmxPath(), shareName, hostDir)
402320
command := mountCommand(shareName, shareDir)
403-
vmrun("-gu", B2DUser, "-gp", B2DPass, "runScriptInGuest", d.vmxPath(), "/bin/sh", command)
321+
vmrun("-gu", d.SSHUser, "-gp", d.SSHPassword, "runScriptInGuest", d.vmxPath(), "/bin/sh", command)
404322
}
405323
}
406324
return nil

0 commit comments

Comments
 (0)