Skip to content

Commit

Permalink
Merge pull request #2576 from taliesins/HyperV
Browse files Browse the repository at this point in the history
Add Hyper-V builder
  • Loading branch information
mwhooker authored Dec 13, 2016
2 parents d632f3b + e50fe9f commit c9fcaf4
Show file tree
Hide file tree
Showing 88 changed files with 14,587 additions and 72 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

common/test-fixtures/root/* eol=lf
65 changes: 65 additions & 0 deletions builder/hyperv/common/artifact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package common

import (
"fmt"
"os"
"path/filepath"

"github.com/mitchellh/packer/packer"
)

// This is the common builder ID to all of these artifacts.
const BuilderId = "MSOpenTech.hyperv"

// Artifact is the result of running the hyperv builder, namely a set
// of files associated with the resulting machine.
type artifact struct {
dir string
f []string
}

// NewArtifact returns a hyperv artifact containing the files
// in the given directory.
func NewArtifact(dir string) (packer.Artifact, error) {
files := make([]string, 0, 5)
visit := func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
files = append(files, path)
}

return err
}

if err := filepath.Walk(dir, visit); err != nil {
return nil, err
}

return &artifact{
dir: dir,
f: files,
}, nil
}

func (*artifact) BuilderId() string {
return BuilderId
}

func (a *artifact) Files() []string {
return a.f
}

func (*artifact) Id() string {
return "VM"
}

func (a *artifact) String() string {
return fmt.Sprintf("VM files in directory: %s", a.dir)
}

func (a *artifact) State(name string) interface{} {
return nil
}

func (a *artifact) Destroy() error {
return os.RemoveAll(a.dir)
}
43 changes: 43 additions & 0 deletions builder/hyperv/common/artifact_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package common

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/mitchellh/packer/packer"
)

func TestArtifact_impl(t *testing.T) {
var _ packer.Artifact = new(artifact)
}

func TestNewArtifact(t *testing.T) {
td, err := ioutil.TempDir("", "packer")
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.RemoveAll(td)

err = ioutil.WriteFile(filepath.Join(td, "a"), []byte("foo"), 0644)
if err != nil {
t.Fatalf("err: %s", err)
}

if err := os.Mkdir(filepath.Join(td, "b"), 0755); err != nil {
t.Fatalf("err: %s", err)
}

a, err := NewArtifact(td)
if err != nil {
t.Fatalf("err: %s", err)
}

if a.BuilderId() != BuilderId {
t.Fatalf("bad: %#v", a.BuilderId())
}
if len(a.Files()) != 1 {
t.Fatalf("should length 1: %d", len(a.Files()))
}
}
11 changes: 11 additions & 0 deletions builder/hyperv/common/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package common

import (
"testing"

"github.com/mitchellh/packer/template/interpolate"
)

func testConfigTemplate(t *testing.T) *interpolate.Context {
return &interpolate.Context{}
}
104 changes: 104 additions & 0 deletions builder/hyperv/common/driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package common

// A driver is able to talk to HyperV and perform certain
// operations with it. Some of the operations on here may seem overly
// specific, but they were built specifically in mind to handle features
// of the HyperV builder for Packer, and to abstract differences in
// versions out of the builder steps, so sometimes the methods are
// extremely specific.
type Driver interface {

// Checks if the VM named is running.
IsRunning(string) (bool, error)

// Checks if the VM named is off.
IsOff(string) (bool, error)

//How long has VM been on
Uptime(vmName string) (uint64, error)

// Start starts a VM specified by the name given.
Start(string) error

// Stop stops a VM specified by the name given.
Stop(string) error

// Verify checks to make sure that this driver should function
// properly. If there is any indication the driver can't function,
// this will return an error.
Verify() error

// Finds the MAC address of the NIC nic0
Mac(string) (string, error)

// Finds the IP address of a VM connected that uses DHCP by its MAC address
IpAddress(string) (string, error)

// Finds the hostname for the ip address
GetHostName(string) (string, error)

// Finds the IP address of a host adapter connected to switch
GetHostAdapterIpAddressForSwitch(string) (string, error)

// Type scan codes to virtual keyboard of vm
TypeScanCodes(string, string) error

//Get the ip address for network adaptor
GetVirtualMachineNetworkAdapterAddress(string) (string, error)

//Set the vlan to use for switch
SetNetworkAdapterVlanId(string, string) error

//Set the vlan to use for machine
SetVirtualMachineVlanId(string, string) error

UntagVirtualMachineNetworkAdapterVlan(string, string) error

CreateExternalVirtualSwitch(string, string) error

GetVirtualMachineSwitchName(string) (string, error)

ConnectVirtualMachineNetworkAdapterToSwitch(string, string) error

CreateVirtualSwitch(string, string) (bool, error)

DeleteVirtualSwitch(string) error

CreateVirtualMachine(string, string, int64, int64, string, uint) error

DeleteVirtualMachine(string) error

SetVirtualMachineCpuCount(string, uint) error

SetVirtualMachineMacSpoofing(string, bool) error

SetVirtualMachineDynamicMemory(string, bool) error

SetVirtualMachineSecureBoot(string, bool) error

SetVirtualMachineVirtualizationExtensions(string, bool) error

EnableVirtualMachineIntegrationService(string, string) error

ExportVirtualMachine(string, string) error

CompactDisks(string, string) error

CopyExportedVirtualMachine(string, string, string, string) error

RestartVirtualMachine(string) error

CreateDvdDrive(string, string, uint) (uint, uint, error)

MountDvdDrive(string, string, uint, uint) error

SetBootDvdDrive(string, uint, uint, uint) error

UnmountDvdDrive(string, uint, uint) error

DeleteDvdDrive(string, uint, uint) error

MountFloppyDrive(string, string) error

UnmountFloppyDrive(string) error
}
Loading

0 comments on commit c9fcaf4

Please sign in to comment.