Skip to content

Commit

Permalink
Sets and gets information from a VMX file
Browse files Browse the repository at this point in the history
The information stored and retrieved is limited to the needs of this project.
  • Loading branch information
c4milo committed Jan 9, 2015
1 parent 7e7e67d commit 5f597ce
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 23 deletions.
64 changes: 57 additions & 7 deletions pkg/vmware/fusion7.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"os/exec"
"strconv"
"strings"
)

Expand Down Expand Up @@ -68,20 +69,69 @@ func (v *Fusion7VM) Info() (*VMInfo, error) {
return nil, err
}

// info := new(VMInfo)
// info.Annotation
// info.CPUs
// info.MemorySize
// info.Name
// info.NetworkAdapters
vmx, err := readvmx(v.vmxPath)
if err != nil {
return nil, err
}

info := new(VMInfo)
info.Name = vmx["displayname"]
info.Annotation = vmx["annotation"]

numcpus, err := strconv.ParseInt(vmx["numvcpus"], 0, 0)
if err != nil {
return nil, err
}

return nil, nil
memsize, err := strconv.ParseInt(vmx["memsize"], 0, 0)
if err != nil {
return nil, err
}

info.CPUs = int(numcpus)
info.MemorySize = int(memsize)
info.NetworkType = NetworkType(vmx["ethernet0.connectiontype"])
info.GuestOS = vmx["guestos"]

return info, nil
}

func (v *Fusion7VM) SetInfo(info *VMInfo) error {
if err := v.verifyVMXPath(); err != nil {
return err
}

vmx, err := readvmx(v.vmxPath)
if err != nil {
return err
}

vmx["displayname"] = info.Name
vmx["annotation"] = info.Annotation
vmx["numcpus"] = strconv.Itoa(info.CPUs)
vmx["memsize"] = strconv.Itoa(info.MemorySize)

// This is to make sure to auto answer popups windows in the GUI. This is
// especially helpful when running in headless mode
vmx["msg.autoanswer"] = "true"

// Deletes all network adapters. For the simplicity's sake
// we are going to deliberately use only one network adapter.
for k, _ := range vmx {
if strings.HasPrefix(k, "ethernet") {
delete(vmx, k)
}
}

vmx["ethernet0.present"] = "true"
vmx["ethernet0.startconnected"] = "true"
vmx["ethernet0.virtualdev"] = "e1000"
vmx["ethernet0.connectiontype"] = string(info.NetworkType)

if err := writevmx(v.vmxPath, vmx); err != nil {
return err
}

return nil
}

Expand Down
15 changes: 6 additions & 9 deletions pkg/vmware/vmware.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@ const (
CloneLinked CloneType = "linked"
)

type NetworkAdapter struct {
NetType NetworkType
}

type VMInfo struct {
Name string
Annotation string
MemorySize int
CPUs int
NetworkAdapters []NetworkAdapter
Name string
Annotation string
MemorySize int
CPUs int
GuestOS string
NetworkType NetworkType
}

type VirtualMachine interface {
Expand Down
61 changes: 61 additions & 0 deletions pkg/vmware/vmx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package vmware

import (
"bytes"
"io"
"io/ioutil"
"os"
"sort"
"strings"
)

func readvmx(vmxpath string) (map[string]string, error) {
data, err := ioutil.ReadFile(vmxpath)
if err != nil {
return nil, err
}

vmx := make(map[string]string)
for _, line := range strings.Split(string(data), "\n") {
values := strings.Split(line, "=")
if len(values) != 2 {
continue
}

k := strings.TrimSpace(values[0])
v := strings.TrimSpace(values[1])
vmx[strings.ToLower(k)] = strings.Trim(v, `"`)
}

return vmx, nil
}

func writevmx(vmxpath string, vmx map[string]string) error {
f, err := os.Create(vmxpath)
if err != nil {
return err
}

defer f.Close()

i := 0
keys := make([]string, len(vmx))
for k := range vmx {
keys[i] = k
i++
}

sort.Strings(keys)

var buf bytes.Buffer
for _, key := range keys {
buf.WriteString(key + " = " + `"` + vmx[key] + `"`)
buf.WriteString("\n")
}

if _, err = io.Copy(f, &buf); err != nil {
return err
}

return nil
}
9 changes: 2 additions & 7 deletions vms/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ func (v *VM) Update() error {
info.Annotation = base64.StdEncoding.EncodeToString(imageJSON)

log.Printf("[DEBUG] Adding network adapter...")
info.NetworkAdapters = []vmware.NetworkAdapter{
vmware.NetworkAdapter{NetType: v.Network},
}
info.NetworkType = v.Network

err = v.vmwareVM.SetInfo(info)
if err != nil {
Expand Down Expand Up @@ -260,10 +258,7 @@ func (v *VM) Refresh() error {
return err
}
v.OSImage = image

if len(info.NetworkAdapters) > 0 {
v.Network = info.NetworkAdapters[0].NetType
}
v.Network = info.NetworkType

running, err := v.vmwareVM.IsRunning()
if err != nil {
Expand Down

0 comments on commit 5f597ce

Please sign in to comment.