dashboard: start of cmd/gomote buildlet client, more packification

Change-Id: I874f4f5ef253cf7f1d6d5073d7c81e76fa1de863
Reviewed-on: https://go-review.googlesource.com/2981
Reviewed-by: Andrew Gerrand <adg@golang.org>
diff --git a/cmd/gomote/create.go b/cmd/gomote/create.go
new file mode 100644
index 0000000..bb25a8e
--- /dev/null
+++ b/cmd/gomote/create.go
@@ -0,0 +1,67 @@
+// Copyright 2015 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build extdep
+
+package main
+
+import (
+	"flag"
+	"fmt"
+	"log"
+	"os"
+	"sort"
+	"time"
+
+	"golang.org/x/tools/dashboard"
+	"golang.org/x/tools/dashboard/buildlet"
+)
+
+func create(args []string) error {
+	fs := flag.NewFlagSet("create", flag.ContinueOnError)
+	fs.Usage = func() {
+		fmt.Fprintln(os.Stderr, "create usage: gomote create [create-opts] <type>\n\n")
+		fs.PrintDefaults()
+		os.Exit(1)
+	}
+	var timeout time.Duration
+	fs.DurationVar(&timeout, "timeout", 60*time.Minute, "how long the VM will live before being deleted.")
+
+	fs.Parse(args)
+	if fs.NArg() != 1 {
+		fs.Usage()
+	}
+	builderType := fs.Arg(0)
+	conf, ok := dashboard.Builders[builderType]
+	if !ok || !conf.UsesVM() {
+		var valid []string
+		for k, conf := range dashboard.Builders {
+			if conf.UsesVM() {
+				valid = append(valid, k)
+			}
+		}
+		sort.Strings(valid)
+		return fmt.Errorf("Invalid builder type %q. Valid options include: %q", builderType, valid)
+	}
+
+	instName := fmt.Sprintf("mote-%s-%s", username(), builderType)
+	client, err := buildlet.StartNewVM(projTokenSource(), instName, builderType, buildlet.VMOpts{
+		Zone:        *zone,
+		ProjectID:   *proj,
+		TLS:         userKeyPair(),
+		DeleteIn:    timeout,
+		Description: fmt.Sprintf("gomote buildlet for %s", username()),
+		OnInstanceRequested: func() {
+			log.Printf("Sent create request. Waiting for operation.")
+		},
+		OnInstanceCreated: func() {
+			log.Printf("Instance created.")
+		},
+	})
+	if err != nil {
+		return fmt.Errorf("failed to create VM: %v", err)
+	}
+	fmt.Printf("%s\t%s\n", builderType, client.URL())
+	return nil
+}