Skip to content

Instantly share code, notes, and snippets.

@tianon

tianon/driver.go

Last active Apr 21, 2016
Embed
What would you like to do?
docker-machine-driver-template
package main
import (
"fmt"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/engine"
"github.com/docker/machine/libmachine/mcnflag"
"github.com/docker/machine/libmachine/state"
)
const (
driverName = "template"
flagPrefix = driverName + "-"
envPrefix = "TEMPLATE_"
defaultTemplateCustom1 = "foo bar"
)
var (
errUnimplemented = fmt.Errorf("UNIMPLEMENTED")
)
type Driver struct {
*drivers.BaseDriver
// custom fields (serialized to and from JSON)
TemplateCustom1 string
}
// SetConfigFromFlags configures the driver with the object that was returned by RegisterCreateFlags
func (d *Driver) SetConfigFromFlags(opts drivers.DriverOptions) error {
d.TemplateCustom1 = opts.String(flagPrefix + "custom1")
d.SetSwarmConfigFromFlags(opts)
if d.TemplateCustom1 == "" {
return fmt.Errorf("%s driver requires the --%scustom1/%sCUSTOM1 option", driverName, flagPrefix, envPrefix)
}
return nil
}
// GetCreateFlags returns the mcnflag.Flag slice representing the flags that can be set, their descriptions and defaults.
func (d *Driver) GetCreateFlags() []mcnflag.Flag {
return []mcnflag.Flag{
mcnflag.StringFlag{
EnvVar: envPrefix + "CUSTOM1",
Name: flagPrefix + "custom1",
Usage: "A simple required custom parameter",
Value: defaultTemplateCustom1,
},
}
}
func NewDriver(hostName, storePath string) Driver {
return Driver{
TemplateCustom1: defaultTemplateCustom1,
BaseDriver: &drivers.BaseDriver{
MachineName: hostName,
StorePath: storePath,
},
}
}
// https://github.com/docker/machine/blob/v0.7.0/libmachine/drivers/drivers.go
// https://github.com/docker/machine/blob/v0.7.0/libmachine/drivers/base.go
// Create a host using the driver's config
func (d *Driver) Create() error {
return errUnimplemented
}
// DriverName returns the name of the driver
func (d *Driver) DriverName() string {
return driverName
}
// GetSSHHostname returns hostname for use with ssh
func (d *Driver) GetSSHHostname() (string, error) {
return d.GetIP()
}
// GetURL returns a Docker compatible host URL for connecting to this host
// e.g. tcp://1.2.3.4:2376
func (d *Driver) GetURL() (string, error) {
if err := drivers.MustBeRunning(d); err != nil {
return "", err
}
ip, err := d.GetIP()
if err != nil {
return "", err
}
return fmt.Sprintf("tcp://%s:%d", ip, engine.DefaultPort), nil
}
// GetState returns the state that the host is in (running, stopped, etc)
func (d *Driver) GetState() (state.State, error) {
// https://github.com/docker/machine/blob/v0.7.0/libmachine/state/state.go
return state.Error, errUnimplemented
}
// Kill stops a host forcefully
func (d *Driver) Kill() error {
return errUnimplemented
}
// Remove a host
func (d *Driver) Remove() error {
return errUnimplemented
}
// Restart a host. This may just call Stop(); Start() if the provider does not have any special restart behaviour.
func (d *Driver) Restart() error {
if err := d.Stop(); err != nil {
return err
}
return d.Start()
}
// Start a host
func (d *Driver) Start() error {
return errUnimplemented
}
// Stop a host gracefully
func (d *Driver) Stop() error {
return errUnimplemented
}
package main
import (
"github.com/docker/machine/libmachine/drivers/plugin"
)
func main() {
plugin.RegisterDriver(new(Driver))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.