Skip to content

Commit aa3a4e4

Browse files
committed
Simplify code
Signed-off-by: David Gageot <david@gageot.net>
1 parent 2311fd6 commit aa3a4e4

File tree

3 files changed

+34
-110
lines changed

3 files changed

+34
-110
lines changed

docs/drivers/hyper-v.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,16 @@ Administrator level account to create and manage Hyper-V machines.
2424
2525
Options:
2626

27-
- `--hyperv-boot2docker-url`: The URL of the boot2docker ISO. Defaults to the latest available version.
28-
- `--hyperv-boot2docker-location`: Location of a local boot2docker iso to use. Overrides the URL option below.
29-
- `--hyperv-virtual-switch`: Name of the virtual switch to use. Defaults to first found.
27+
- `--hyperv-boot2docker-url`: The URL of the boot2docker ISO.
28+
- `--hyperv-virtual-switch`: Name of the virtual switch to use.
3029
- `--hyperv-disk-size`: Size of disk for the host in MB.
31-
- `--hyperv-memory`: Size of memory for the host in MB. By default, the machine is setup to use dynamic memory.
30+
- `--hyperv-memory`: Size of memory for the host in MB.
3231

3332
Environment variables and default values:
3433

3534
| CLI option | Environment variable | Default |
3635
| ------------------------------- | -------------------- | ------------------------ |
3736
| `--hyperv-boot2docker-url` | - | _Latest boot2docker url_ |
38-
| `--hyperv-boot2docker-location` | - | - |
3937
| `--hyperv-virtual-switch` | - | _first found_ |
4038
| `--hyperv-disk-size` | - | `20000` |
4139
| `--hyperv-memory` | - | `1024` |

drivers/hyperv/hyperv.go

Lines changed: 22 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
type Driver struct {
2121
*drivers.BaseDriver
2222
Boot2DockerURL string
23-
boot2DockerLoc string
2423
vSwitch string
2524
diskImage string
2625
DiskSize int
@@ -49,32 +48,27 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
4948
return []mcnflag.Flag{
5049
mcnflag.StringFlag{
5150
Name: "hyperv-boot2docker-url",
52-
Usage: "Hyper-V URL of the boot2docker image. Defaults to the latest available version.",
53-
},
54-
mcnflag.StringFlag{
55-
Name: "hyperv-boot2docker-location",
56-
Usage: "Hyper-V local boot2docker iso. Overrides URL.",
51+
Usage: "URL of the boot2docker ISO. Defaults to the latest available version.",
5752
},
5853
mcnflag.StringFlag{
5954
Name: "hyperv-virtual-switch",
60-
Usage: "Hyper-V virtual switch name. Defaults to first found.",
55+
Usage: "Virtual switch name. Defaults to first found.",
6156
},
6257
mcnflag.IntFlag{
6358
Name: "hyperv-disk-size",
64-
Usage: "Hyper-V maximum size of dynamically expanding disk in MB.",
59+
Usage: "Maximum size of dynamically expanding disk in MB.",
6560
Value: defaultDiskSize,
6661
},
6762
mcnflag.IntFlag{
6863
Name: "hyperv-memory",
69-
Usage: "Hyper-V memory size for host in MB.",
64+
Usage: "Memory size for host in MB.",
7065
Value: defaultMemory,
7166
},
7267
}
7368
}
7469

7570
func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
7671
d.Boot2DockerURL = flags.String("hyperv-boot2docker-url")
77-
d.boot2DockerLoc = flags.String("hyperv-boot2docker-location")
7872
d.vSwitch = flags.String("hyperv-virtual-switch")
7973
d.DiskSize = flags.Int("hyperv-disk-size")
8074
d.MemSize = flags.Int("hyperv-memory")
@@ -108,17 +102,12 @@ func (d *Driver) GetURL() (string, error) {
108102
}
109103

110104
func (d *Driver) GetState() (state.State, error) {
111-
command := []string{
112-
"(",
113-
"Get-VM",
114-
"-Name", d.MachineName,
115-
").state"}
116-
stdout, err := execute(command)
105+
stdout, err := cmdOut("(", "Get-VM", "-Name", d.MachineName, ").state")
117106
if err != nil {
118107
return state.None, fmt.Errorf("Failed to find the VM status")
119108
}
120109

121-
resp := parseStdout(stdout)
110+
resp := parseLines(stdout)
122111
if len(resp) < 1 {
123112
return state.None, nil
124113
}
@@ -171,40 +160,19 @@ func (d *Driver) Create() error {
171160
return err
172161
}
173162

174-
command := []string{
175-
"New-VM",
176-
"-Name", d.MachineName,
177-
"-Path", fmt.Sprintf("'%s'", d.ResolveStorePath(".")),
178-
"-MemoryStartupBytes", fmt.Sprintf("%dMB", d.MemSize)}
179-
_, err = execute(command)
180-
if err != nil {
163+
if err := cmd("New-VM", "-Name", d.MachineName, "-Path", fmt.Sprintf("'%s'", d.ResolveStorePath(".")), "-MemoryStartupBytes", fmt.Sprintf("%dMB", d.MemSize)); err != nil {
181164
return err
182165
}
183166

184-
command = []string{
185-
"Set-VMDvdDrive",
186-
"-VMName", d.MachineName,
187-
"-Path", fmt.Sprintf("'%s'", d.ResolveStorePath("boot2docker.iso"))}
188-
_, err = execute(command)
189-
if err != nil {
167+
if err := cmd("Set-VMDvdDrive", "-VMName", d.MachineName, "-Path", fmt.Sprintf("'%s'", d.ResolveStorePath("boot2docker.iso"))); err != nil {
190168
return err
191169
}
192170

193-
command = []string{
194-
"Add-VMHardDiskDrive",
195-
"-VMName", d.MachineName,
196-
"-Path", fmt.Sprintf("'%s'", d.diskImage)}
197-
_, err = execute(command)
198-
if err != nil {
171+
if err := cmd("Add-VMHardDiskDrive", "-VMName", d.MachineName, "-Path", fmt.Sprintf("'%s'", d.diskImage)); err != nil {
199172
return err
200173
}
201174

202-
command = []string{
203-
"Connect-VMNetworkAdapter",
204-
"-VMName", d.MachineName,
205-
"-SwitchName", fmt.Sprintf("'%s'", virtualSwitch)}
206-
_, err = execute(command)
207-
if err != nil {
175+
if err := cmd("Connect-VMNetworkAdapter", "-VMName", d.MachineName, "-SwitchName", fmt.Sprintf("'%s'", virtualSwitch)); err != nil {
208176
return err
209177
}
210178

@@ -217,14 +185,12 @@ func (d *Driver) chooseVirtualSwitch() (string, error) {
217185
return d.vSwitch, nil
218186
}
219187

220-
command := []string{
221-
"@(Get-VMSwitch).Name"}
222-
stdout, err := execute(command)
188+
stdout, err := cmdOut("@(Get-VMSwitch).Name")
223189
if err != nil {
224190
return "", err
225191
}
226192

227-
switches := parseStdout(stdout)
193+
switches := parseLines(stdout)
228194
if len(switches) < 1 {
229195
return "", fmt.Errorf("no vswitch found")
230196
}
@@ -250,29 +216,22 @@ func (d *Driver) wait() error {
250216
}
251217

252218
func (d *Driver) Start() error {
253-
command := []string{
254-
"Start-VM",
255-
"-Name", d.MachineName}
256-
_, err := execute(command)
257-
if err != nil {
219+
if err := cmd("Start-VM", "-Name", d.MachineName); err != nil {
258220
return err
259221
}
260222

261223
if err := d.wait(); err != nil {
262224
return err
263225
}
264226

227+
var err error
265228
d.IPAddress, err = d.GetIP()
266229

267230
return err
268231
}
269232

270233
func (d *Driver) Stop() error {
271-
command := []string{
272-
"Stop-VM",
273-
"-Name", d.MachineName}
274-
_, err := execute(command)
275-
if err != nil {
234+
if err := cmd("Stop-VM", "-Name", d.MachineName); err != nil {
276235
return err
277236
}
278237

@@ -306,12 +265,7 @@ func (d *Driver) Remove() error {
306265
}
307266
}
308267

309-
command := []string{
310-
"Remove-VM",
311-
"-Name", d.MachineName,
312-
"-Force"}
313-
_, err = execute(command)
314-
return err
268+
return cmd("Remove-VM", "-Name", d.MachineName, "-Force")
315269
}
316270

317271
func (d *Driver) Restart() error {
@@ -324,12 +278,7 @@ func (d *Driver) Restart() error {
324278
}
325279

326280
func (d *Driver) Kill() error {
327-
command := []string{
328-
"Stop-VM",
329-
"-Name", d.MachineName,
330-
"-TurnOff"}
331-
_, err := execute(command)
332-
if err != nil {
281+
if err := cmd("Stop-VM", "-Name", d.MachineName, "-TurnOff"); err != nil {
333282
return err
334283
}
335284

@@ -352,17 +301,12 @@ func (d *Driver) Kill() error {
352301
}
353302

354303
func (d *Driver) GetIP() (string, error) {
355-
command := []string{
356-
"((",
357-
"Get-VM",
358-
"-Name", d.MachineName,
359-
").networkadapters[0]).ipaddresses[0]"}
360-
stdout, err := execute(command)
304+
stdout, err := cmdOut("((", "Get-VM", "-Name", d.MachineName, ").networkadapters[0]).ipaddresses[0]")
361305
if err != nil {
362306
return "", err
363307
}
364308

365-
resp := parseStdout(stdout)
309+
resp := parseLines(stdout)
366310
if len(resp) < 1 {
367311
return "", fmt.Errorf("IP not found")
368312
}
@@ -380,13 +324,7 @@ func (d *Driver) generateDiskImage() error {
380324
fixed := d.ResolveStorePath("fixed.vhd")
381325

382326
log.Infof("Creating VHD")
383-
command := []string{
384-
"New-VHD",
385-
"-Path", fmt.Sprintf("'%s'", fixed),
386-
"-SizeBytes", "10MB",
387-
"-Fixed"}
388-
_, err := execute(command)
389-
if err != nil {
327+
if err := cmd("New-VHD", "-Path", fmt.Sprintf("'%s'", fixed), "-SizeBytes", "10MB", "-Fixed"); err != nil {
390328
return err
391329
}
392330

@@ -408,26 +346,11 @@ func (d *Driver) generateDiskImage() error {
408346
}
409347
file.Close()
410348

411-
command = []string{
412-
"Convert-VHD",
413-
"-Path", fmt.Sprintf("'%s'", fixed),
414-
"-DestinationPath", fmt.Sprintf("'%s'", d.diskImage),
415-
"-VHDType", "Dynamic"}
416-
_, err = execute(command)
417-
if err != nil {
418-
return err
419-
}
420-
421-
command = []string{
422-
"Resize-VHD",
423-
"-Path", fmt.Sprintf("'%s'", d.diskImage),
424-
"-SizeBytes", fmt.Sprintf("%dMB", d.DiskSize)}
425-
_, err = execute(command)
426-
if err != nil {
349+
if err := cmd("Convert-VHD", "-Path", fmt.Sprintf("'%s'", fixed), "-DestinationPath", fmt.Sprintf("'%s'", d.diskImage), "-VHDType", "Dynamic"); err != nil {
427350
return err
428351
}
429352

430-
return err
353+
return cmd("Resize-VHD", "-Path", fmt.Sprintf("'%s'", d.diskImage), "-SizeBytes", fmt.Sprintf("%dMB", d.DiskSize))
431354
}
432355

433356
// Make a boot2docker VM disk image.

drivers/hyperv/powershell.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func init() {
2323
}
2424
}
2525

26-
func execute(args []string) (string, error) {
26+
func cmdOut(args ...string) (string, error) {
2727
args = append([]string{"-NoProfile"}, args...)
2828
cmd := exec.Command(powershell, args...)
2929
log.Debugf("[executing ==>] : %v %v", powershell, strings.Join(args, " "))
@@ -37,7 +37,12 @@ func execute(args []string) (string, error) {
3737
return stdout.String(), err
3838
}
3939

40-
func parseStdout(stdout string) []string {
40+
func cmd(args ...string) error {
41+
_, err := cmdOut(args...)
42+
return err
43+
}
44+
45+
func parseLines(stdout string) []string {
4146
resp := []string{}
4247

4348
s := bufio.NewScanner(strings.NewReader(stdout))
@@ -49,14 +54,12 @@ func parseStdout(stdout string) []string {
4954
}
5055

5156
func hypervAvailable() error {
52-
command := []string{
53-
"@(Get-Command Get-VM).ModuleName"}
54-
stdout, err := execute(command)
57+
stdout, err := cmdOut("@(Get-Command Get-VM).ModuleName")
5558
if err != nil {
5659
return err
5760
}
5861

59-
resp := parseStdout(stdout)
62+
resp := parseLines(stdout)
6063
if resp[0] != "Hyper-V" {
6164
return fmt.Errorf("Hyper-V PowerShell Module is not available")
6265
}

0 commit comments

Comments
 (0)