Skip to content

Instantly share code, notes, and snippets.

@jeffmendoza
Created May 20, 2015 16:10
Show Gist options
  • Save jeffmendoza/356a413aa0185e6ed17a to your computer and use it in GitHub Desktop.
Save jeffmendoza/356a413aa0185e6ed17a to your computer and use it in GitHub Desktop.
Using Docker Machine's drivers package to create VMs
// Copyright 2015 Google, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"io/ioutil"
"log"
"github.com/docker/machine/drivers"
_ "github.com/docker/machine/drivers/amazonec2"
_ "github.com/docker/machine/drivers/azure"
_ "github.com/docker/machine/drivers/digitalocean"
_ "github.com/docker/machine/drivers/google"
_ "github.com/docker/machine/drivers/hyperv"
_ "github.com/docker/machine/drivers/none"
_ "github.com/docker/machine/drivers/openstack"
_ "github.com/docker/machine/drivers/rackspace"
_ "github.com/docker/machine/drivers/softlayer"
_ "github.com/docker/machine/drivers/virtualbox"
_ "github.com/docker/machine/drivers/vmwarefusion"
_ "github.com/docker/machine/drivers/vmwarevcloudair"
_ "github.com/docker/machine/drivers/vmwarevsphere"
"github.com/docker/machine/state"
)
// Drivers expects:
// type DriverOptions interface {
// String(key string) string
// Int(key string) int
// Bool(key string) bool
// }
// Wich is really codegangsta/cli.Context
type opts struct {
Bools map[string]bool
Strings map[string]string
Ints map[string]int
}
func (op opts) String(key string) string {
return op.Strings[key]
}
func (op opts) Int(key string) int {
return op.Ints[key]
}
func (op opts) Bool(key string) bool {
return op.Bools[key]
}
func main() {
fmt.Println("Machine test")
fmt.Println()
// Drivers are tightly coupled with codegangsta/cli
// returns cli.Flag
flags, err := drivers.GetCreateFlagsForDriver("google")
if err != nil {
log.Fatal(err.Error())
}
for _, flag := range flags {
fmt.Printf("%v\n", flag.String())
}
fmt.Println()
var config opts
// Instead of using cli, just manually create the options here
config.Bools = make(map[string]bool)
//config.Bools["flag"] = true
config.Strings = make(map[string]string)
//config.Strings["flag"] = "string"
config.Strings["google-project"] = "my-project"
config.Strings["google-zone"] = "us-central1-a"
config.Strings["google-machine-type"] = "f1-micro"
config.Strings["google-scopes"] = "https://www.googleapis.com/auth/devstorage.read_only,https://www.googleapis.com/auth/logging.write"
config.Strings["google-username"] = "docker-user"
config.Strings["google-disk-type"] = "pd-standard"
config.Ints = make(map[string]int)
//config.Ints["flag"] = 123
config.Ints["google-disk-size"] = 10
// Two last params are ignored by the driver
driver, err := drivers.NewDriver("google", "machinename", "tempdir", "", "")
if err != nil {
log.Fatal(err.Error())
}
if err := driver.SetConfigFromFlags(config); err != nil {
log.Fatal(err.Error())
}
if err := driver.PreCreateCheck(); err != nil {
log.Fatal(err.Error())
}
if err := driver.Create(); err != nil {
log.Fatal(err.Error())
}
fmt.Println("Machine created, waiting for running")
running := drivers.MachineInState(driver, state.Running)
for !running() {
}
// do some stuff
ip, err := driver.GetIP()
if err != nil {
log.Fatal(err.Error())
}
fmt.Printf("ip: %v\n", ip)
// uses github.com/docker/machine/ssh
output, err := drivers.RunSSHCommandFromDriver(driver, "uname -a")
if err != nil {
log.Fatal(err.Error())
}
stdout, err := ioutil.ReadAll(output.Stdout)
if err != nil {
log.Fatal(err.Error())
}
fmt.Printf("stdout: %v\n", string(stdout))
stderr, err := ioutil.ReadAll(output.Stderr)
if err != nil {
log.Fatal(err.Error())
}
fmt.Printf("stderr: %v\n", string(stderr))
// driver.Stop()
// for !drivers.MachineInState(driver, state.Stopped) {
// }
// driver.Remove()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment