blob: 86fdcd8c42045c9a256775e06103b640def4a398 [file] [log] [blame]
Brad Fitzpatrick874c0832015-01-16 12:59:14 -08001// Copyright 2015 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Brad Fitzpatrick874c0832015-01-16 12:59:14 -08005package main
6
7import (
8 "flag"
9 "fmt"
Brad Fitzpatrick874c0832015-01-16 12:59:14 -080010 "os"
11 "sort"
Brad Fitzpatrickb35ba9f2015-01-19 20:53:34 -080012 "strings"
Brad Fitzpatrick874c0832015-01-16 12:59:14 -080013
Andrew Gerrandfa8373a2015-01-21 17:25:37 +110014 "golang.org/x/build/dashboard"
Brad Fitzpatrick874c0832015-01-16 12:59:14 -080015)
16
Brad Fitzpatrick45156e82015-02-05 13:37:19 -080017func vmTypes() (s []string) {
Brad Fitzpatrick4ad44352015-02-23 09:14:46 -080018 for k := range dashboard.Builders {
19 s = append(s, k)
Brad Fitzpatrick45156e82015-02-05 13:37:19 -080020 }
21 sort.Strings(s)
22 return
23}
24
Brad Fitzpatrick874c0832015-01-16 12:59:14 -080025func create(args []string) error {
26 fs := flag.NewFlagSet("create", flag.ContinueOnError)
Brad Fitzpatrick45156e82015-02-05 13:37:19 -080027
Brad Fitzpatrick874c0832015-01-16 12:59:14 -080028 fs.Usage = func() {
Andrew Gerrandbf8575d2015-05-08 13:34:07 +100029 fmt.Fprintln(os.Stderr, "create usage: gomote create [create-opts] <type>")
Brad Fitzpatrick874c0832015-01-16 12:59:14 -080030 fs.PrintDefaults()
Andrew Gerrandbf8575d2015-05-08 13:34:07 +100031 fmt.Fprintln(os.Stderr, "\nValid types:")
Brad Fitzpatrick45156e82015-02-05 13:37:19 -080032 for _, t := range vmTypes() {
33 fmt.Fprintf(os.Stderr, " * %s\n", t)
34 }
Brad Fitzpatrick874c0832015-01-16 12:59:14 -080035 os.Exit(1)
36 }
Brad Fitzpatrick6a12af62015-07-19 10:26:22 -070037 // TODO(bradfitz): restore this option, and send it to the coordinator:
38 // For now, comment it out so it's not misleading.
39 // var timeout time.Duration
40 // fs.DurationVar(&timeout, "timeout", 60*time.Minute, "how long the VM will live before being deleted.")
Brad Fitzpatrick874c0832015-01-16 12:59:14 -080041
42 fs.Parse(args)
43 if fs.NArg() != 1 {
44 fs.Usage()
45 }
46 builderType := fs.Arg(0)
Brad Fitzpatrick4ad44352015-02-23 09:14:46 -080047 _, ok := dashboard.Builders[builderType]
48 if !ok {
Brad Fitzpatrick874c0832015-01-16 12:59:14 -080049 var valid []string
Brad Fitzpatrickb35ba9f2015-01-19 20:53:34 -080050 var prefixMatch []string
Brad Fitzpatrick4ad44352015-02-23 09:14:46 -080051 for k := range dashboard.Builders {
52 valid = append(valid, k)
53 if strings.HasPrefix(k, builderType) {
54 prefixMatch = append(prefixMatch, k)
Brad Fitzpatrick874c0832015-01-16 12:59:14 -080055 }
56 }
Brad Fitzpatrickb35ba9f2015-01-19 20:53:34 -080057 if len(prefixMatch) == 1 {
58 builderType = prefixMatch[0]
Brad Fitzpatrickb35ba9f2015-01-19 20:53:34 -080059 } else {
60 sort.Strings(valid)
61 return fmt.Errorf("Invalid builder type %q. Valid options include: %q", builderType, valid)
62 }
Brad Fitzpatrick874c0832015-01-16 12:59:14 -080063 }
64
Brad Fitzpatrick6a12af62015-07-19 10:26:22 -070065 cc := coordinatorClient()
66 client, err := cc.CreateBuildlet(builderType)
67 if err != nil {
68 return fmt.Errorf("failed to create buildlet: %v", err)
Andrew Gerrand51edd242015-03-05 15:56:40 +110069 }
Brad Fitzpatrick6a12af62015-07-19 10:26:22 -070070 fmt.Println(client.RemoteName())
Brad Fitzpatrick874c0832015-01-16 12:59:14 -080071 return nil
72}