Skip to content
This repository has been archived by the owner on Jan 21, 2023. It is now read-only.

Commit

Permalink
thangs are broke
Browse files Browse the repository at this point in the history
  • Loading branch information
blacktop committed Mar 24, 2018
1 parent 7ac750f commit 53db218
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 33 deletions.
15 changes: 15 additions & 0 deletions clients/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# clients

## Coverage

### VBoxManage

### vmrun

* [ ] listSnapshots `vmx_path`
* [ ] revertToSnapshot `vmx_path` `snapshot`
* [ ] deleteSnapshot `vmx_path` memdump
* [ ] snapshot `vmx_path` memdump
* [ ] start `vmx_path` headless
* [ ] list
* [ ] stop `vmx_path` hard
112 changes: 112 additions & 0 deletions drivers/drivers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package drivers

import (
"errors"

"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/mcnflag"
"github.com/docker/machine/libmachine/state"
)

// Driver defines how a host is created and controlled. Different types of
// driver represent different ways hosts can be created (e.g. different
// hypervisors, different cloud providers)
type Driver interface {
// Create a host using the driver's config
Create() error

// DriverName returns the name of the driver
DriverName() string

// GetCreateFlags returns the mcnflag.Flag slice representing the flags
// that can be set, their descriptions and defaults.
GetCreateFlags() []mcnflag.Flag

// GetIP returns an IP or hostname that this host is available at
// e.g. 1.2.3.4 or docker-host-d60b70a14d3a.cloudapp.net
GetIP() (string, error)

// GetMachineName returns the name of the machine
GetMachineName() string

// GetSSHHostname returns hostname for use with ssh
GetSSHHostname() (string, error)

// GetSSHKeyPath returns key path for use with ssh
GetSSHKeyPath() string

// GetSSHPort returns port for use with ssh
GetSSHPort() (int, error)

// GetSSHUsername returns username for use with ssh
GetSSHUsername() string

// GetURL returns a Docker compatible host URL for connecting to this host
// e.g. tcp://1.2.3.4:2376
GetURL() (string, error)

// GetState returns the state that the host is in (running, stopped, etc)
GetState() (state.State, error)

// Kill stops a host forcefully
Kill() error

// PreCreateCheck allows for pre-create operations to make sure a driver is ready for creation
PreCreateCheck() error

// Remove a host
Remove() error

// Restart a host. This may just call Stop(); Start() if the provider does not
// have any special restart behaviour.
Restart() error

// SetConfigFromFlags configures the driver with the object that was returned
// by RegisterCreateFlags
SetConfigFromFlags(opts DriverOptions) error

// Start a host
Start() error

// Stop a host gracefully
Stop() error

// List VMs
List() string
}

var ErrHostIsNotRunning = errors.New("Host is not running")

type DriverOptions interface {
String(key string) string
StringSlice(key string) []string
Int(key string) int
Bool(key string) bool
}

func MachineInState(d Driver, desiredState state.State) func() bool {
return func() bool {
currentState, err := d.GetState()
if err != nil {
log.Debugf("Error getting machine state: %s", err)
}
if currentState == desiredState {
return true
}
return false
}
}

// MustBeRunning will return an error if the machine is not in a running state.
func MustBeRunning(d Driver) error {
s, err := d.GetState()
if err != nil {
return err
}

if s != state.Running {
return ErrHostIsNotRunning
}

return nil
}
41 changes: 21 additions & 20 deletions drivers/vmwarefusion/fusion.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"text/template"
"time"

"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/log"
log "github.com/Sirupsen/logrus"
"github.com/blacktop/vm-proxy/drivers"
"github.com/docker/machine/libmachine/mcnflag"
"github.com/docker/machine/libmachine/mcnutils"
"github.com/docker/machine/libmachine/ssh"
Expand All @@ -36,7 +36,7 @@ const (

// Driver for VMware Fusion
type Driver struct {
*drivers.BaseDriver
// *drivers.BaseDriver
Memory int
DiskSize int
CPU int
Expand Down Expand Up @@ -113,15 +113,15 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {

func NewDriver(hostName, storePath string) drivers.Driver {
return &Driver{
CPU: defaultCPU,
Memory: defaultMemory,
DiskSize: defaultDiskSize,
SSHPassword: defaultSSHPass,
BaseDriver: &drivers.BaseDriver{
SSHUser: defaultSSHUser,
MachineName: hostName,
StorePath: storePath,
},
// CPU: defaultCPU,
// Memory: defaultMemory,
// DiskSize: defaultDiskSize,
// SSHPassword: defaultSSHPass,
// BaseDriver: &drivers.BaseDriver{
// SSHUser: defaultSSHUser,
// MachineName: hostName,
// StorePath: storePath,
// },
}
}

Expand All @@ -137,6 +137,15 @@ func (d *Driver) GetSSHUsername() string {
return d.SSHUser
}

// List lists all VMs
func (d *Driver) List() (string, error) {
stdOut, _, err := vmrun("list")
if err != nil {
return "", err
}
return stdOut, nil
}

// DriverName returns the name of the driver
func (d *Driver) DriverName() string {
return "vmwarefusion"
Expand Down Expand Up @@ -220,14 +229,6 @@ func (d *Driver) GetState() (state.State, error) {
return state.Stopped, nil
}

func (d *Driver) ListVMs() (string, error) {
stdOut, _, err := vmrun("list")
if err != nil {
return "", err
}
return stdOut, nil
}

// PreCreateCheck checks that the machine creation process can be started safely.
func (d *Driver) PreCreateCheck() error {
// Downloading boot2docker to cache should be done here to make sure
Expand Down
2 changes: 1 addition & 1 deletion drivers/vmwarefusion/vmrun_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"strings"
"syscall"

"github.com/docker/machine/libmachine/log"
log "github.com/Sirupsen/logrus"
)

var (
Expand Down
15 changes: 15 additions & 0 deletions server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# server

## Coverage

### VBoxManage

### vmrun

* [ ] `/vmware/snapshot`
* [ ] `/vmware/snapshot/list/{vmx_path}`
* [ ] `/vmware/snapshot/revert/{vmx_path}`
* [ ] `/vmware/snapshot/delete/{vmx_path}`
* [ ] `/vmware/start/{vmx_path}`
* [ ] `/vmware/stop/{vmx_path}`
* [ ] `/vmware/list`
8 changes: 6 additions & 2 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,14 @@ func main() {
router.HandleFunc("/vbox/nictracefile1/{nameOrID}/{fileName}", vbox.NicTraceFile).Methods("GET")
router.HandleFunc("/vbox/nnictrace1/{nameOrID}/{stateOnOff}", vbox.NicTrace).Methods("GET")
router.HandleFunc("/vbox/debugvm/{nameOrID}/{fileName}", vbox.DumpVM).Methods("GET")
// router.HandleFunc("/vbox/{source:(?:giphy|xkcd|dilbert|default|contrib)}/{file}", updateImageKeywords).Methods("PATCH")
// vmware routes
router.HandleFunc("/vmware/list", vmware.List).Methods("GET")
// router.HandleFunc("/vmware/{source:(?:giphy|xkcd|dilbert|default|contrib)}/{file}", updateImageKeywords).Methods("PATCH")
router.HandleFunc("/vmware/snapshot", vmware.Snapshot).Methods("GET")
router.HandleFunc("/vmware/snapshot/list/{vmx_path}", vmware.SnapshotList).Methods("GET")
router.HandleFunc("/vmware/snapshot/revert/{vmx_path}", vmware.SnapshotRevert).Methods("GET")
router.HandleFunc("/vmware/snapshot/delete/{vmx_path}", vmware.SnapshotDelete).Methods("GET")
router.HandleFunc("/vmware/start/{vmx_path}", vmware.Start).Methods("GET")
router.HandleFunc("/vmware/stop/{vmx_path}", vmware.Stop).Methods("GET")

err := GenerateCerts("dockerhost")
if err != nil {
Expand Down
104 changes: 94 additions & 10 deletions server/vmware/vmware.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,108 @@ package vmware
import (
"net/http"

vmware "github.com/blacktop/vm-proxy/drivers/vmwarefusion"
"github.com/blacktop/vm-proxy/drivers/vmwarefusion"
"github.com/gorilla/mux"
)

// List route lists all VMs
func List(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.Header().Set("Content-Type", "text/plain; charset=UTF-8")

// machines, err := virtualbox.ListMachines()
// assert(err)
// for _, machine := range machines {
// fmt.Println(machine.Name)
d := vmwarefusion.NewDriver("", "")
outPut := d.List()
// if err != nil {
// w.WriteHeader(http.StatusInternalServerError)
// w.Write([]byte(err.Error()))
// }
w.WriteHeader(http.StatusOK)
w.Write([]byte(outPut))
}

func Snapshot(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=UTF-8")
d := vmwarefusion.NewDriver("", "")
outPut := d.List()
// if err != nil {
// w.WriteHeader(http.StatusInternalServerError)
// w.Write([]byte(err.Error()))
// }
w.WriteHeader(http.StatusOK)
w.Write([]byte(outPut))
}

// if err := json.NewEncoder(w).Encode(machines); err != nil {
// panic(err)
func SnapshotList(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=UTF-8")

vars := mux.Vars(r)
vmxPath := vars["vmx_path"]

d := vmwarefusion.NewDriver("", "")
outPut := d.List()
// if err != nil {
// w.WriteHeader(http.StatusInternalServerError)
// w.Write([]byte(err.Error()))
// }
d := vmware.NewDriver("", "")
outPut := d.DriverName()
w.WriteHeader(http.StatusOK)
w.Write([]byte(outPut))
}

func SnapshotRevert(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=UTF-8")

vars := mux.Vars(r)
vmxPath := vars["vmx_path"]

d := vmwarefusion.NewDriver("", "")
outPut := d.List()
// if err != nil {
// w.WriteHeader(http.StatusInternalServerError)
// w.Write([]byte(err.Error()))
// }
w.WriteHeader(http.StatusOK)
w.Write([]byte(outPut))
}

func SnapshotDelete(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=UTF-8")

vars := mux.Vars(r)
vmxPath := vars["vmx_path"]

d := vmwarefusion.NewDriver("", "")
outPut := d.List()
// if err != nil {
// w.WriteHeader(http.StatusInternalServerError)
// w.Write([]byte(err.Error()))
// }
w.WriteHeader(http.StatusOK)
w.Write([]byte(outPut))
}

func Start(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=UTF-8")

vars := mux.Vars(r)
vmxPath := vars["vmx_path"]

d := vmwarefusion.NewDriver("", "")
outPut := d.List()
// if err != nil {
// w.WriteHeader(http.StatusInternalServerError)
// w.Write([]byte(err.Error()))
// }
w.WriteHeader(http.StatusOK)
w.Write([]byte(outPut))
}

func Stop(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=UTF-8")

vars := mux.Vars(r)
vmxPath := vars["vmx_path"]

d := vmwarefusion.NewDriver("", "")
outPut := d.List()
// if err != nil {
// w.WriteHeader(http.StatusInternalServerError)
// w.Write([]byte(err.Error()))
Expand Down

0 comments on commit 53db218

Please sign in to comment.