Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
c4milo committed Jan 9, 2015
1 parent cb7086d commit e35b883
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 326 deletions.
5 changes: 1 addition & 4 deletions osx-builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,5 @@ func init() {

func main() {
address := ":" + config.Port
err := http.ListenAndServe(address, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
log.Fatal(http.ListenAndServe(address, nil))
}
62 changes: 62 additions & 0 deletions pkg/vmware/fusion7.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package vmware

import (
"fmt"
"os"
)

type Fusion7 struct{}

func (v *Fusion7) vmrunPath() (string, error) {
vmrun := os.Getenv("VMWARE_VMRUN_PATH")

if vmrun != "" {
return vmrun, nil
}

// Guess it is in the default installation path
vmrun = "/Applications/VMware Fusion.app/Contents/Library/vmrun"
if _, err := os.Stat(vmrun); err != nil {
if os.IsNotExist(err) {
return "", fmt.Errorf("VMWare vmrun not found at path: %s", vmrun)
}
}

return vmrun, nil
}

func (v *Fusion7) Info(vmxfile string) (*VMInfo, error) {
return nil, nil
}

func (v *Fusion7) SetInfo(info *VMInfo) error {
return nil
}

func (v *Fusion7) Clone(vmxfile, dstfile string, ctype CloneType) error {
return nil
}

func (v *Fusion7) Start(vmxfile string, gui bool) error {
return nil
}

func (v *Fusion7) Stop(vmxfile string) error {
return nil
}

func (v *Fusion7) Delete(vmxfile string) error {
return nil
}

func (v *Fusion7) IsRunning(vmxfile string) (bool, error) {
return false, nil
}

func (v *Fusion7) HasToolsInstalled(vmxfile string) (bool, error) {
return false, nil
}

func (v *Fusion7) IPAddress(vmxfile string) (string, error) {
return "", nil
}
81 changes: 81 additions & 0 deletions pkg/vmware/vmware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package vmware

import (
"bytes"
"fmt"
"log"
"os/exec"
"strings"
)

type NetworkType string

const (
NetworkHostOnly NetworkType = "hostonly"
NetworkNAT NetworkType = "nat"
NetworkBridged NetworkType = "bridged"
)

type CloneType string

const (
CloneFull CloneType = "full"
CloneLinked CloneType = "linked"
)

type NetworkAdapter struct {
NetType NetworkType
}

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

type VMManager interface {
vmrunPath() (string, error)
Info(vmxfile string) (*VMInfo, error)
SetInfo(info *VMInfo) error
Clone(vmxfile, dstfile string, ctype CloneType) error
Start(vmxfile string, gui bool) error
Stop(vmxfile string) error
Delete(vmxfile string) error
IsRunning(vmxfile string) (bool, error)
HasToolsInstalled(vmxfile string) (bool, error)
IPAddress(vmxfile string) (string, error)
}

// Borrowed from https://github.com/mitchellh/packer/blob/master/builder/vmware/common/driver.go
func runAndLog(cmd *exec.Cmd) (string, string, error) {
var stdout, stderr bytes.Buffer

log.Printf("Executing: %s %v", cmd.Path, cmd.Args[1:])
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()

stdoutString := strings.TrimSpace(stdout.String())
stderrString := strings.TrimSpace(stderr.String())

if _, ok := err.(*exec.ExitError); ok {
message := stderrString
if message == "" {
message = stdoutString
}

err = fmt.Errorf("VMware error: %s", message)
}

log.Printf("stdout: %s", stdoutString)
log.Printf("stderr: %s", stderrString)

// Replace these for Windows, we only want to deal with Unix
// style line endings.
returnStdout := strings.Replace(stdout.String(), "\r\n", "\n", -1)
returnStderr := strings.Replace(stderr.String(), "\r\n", "\n", -1)

return returnStdout, returnStderr, err
}
2 changes: 1 addition & 1 deletion vms/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package vms
import (
"net/http"

"github.com/c4milo/go-osx-builder/apperror"
"github.com/c4milo/osx-builder/apperror"
)

var ErrInternal = apperror.Error{
Expand Down
7 changes: 4 additions & 3 deletions vms/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/sha256"
"crypto/sha512"
"crypto/tls"
"errors"
"fmt"
"hash"
"io"
Expand Down Expand Up @@ -37,15 +38,15 @@ type Image struct {
// Downloads and a virtual machine image
func (img *Image) Download(destPath string) error {
if img.URL == "" {
panic("URL is required")
return errors.New("Image URL is required")
}

if img.Checksum == "" {
panic("Checksum is required")
return errors.New("Image checksum is required")
}

if img.ChecksumType == "" {
panic("Checksum type is required")
return errors.New("Image checksum type is required")
}

if destPath == "" {
Expand Down
Loading

0 comments on commit e35b883

Please sign in to comment.