cmd/gomote: allow creation of multiple VMs of the same type

For example:

	$ for i in 1 2 3; do gomote create linux-amd64; done
	$ gomote list
	linux-amd64	https://146.148.41.74
	linux-amd64-1	https://146.148.66.230
	linux-amd64-2	https://23.236.58.252

Change-Id: I3c84f455a8b0293056dd399ae34df0d39ccf29be
Reviewed-on: https://go-review.googlesource.com/6806
Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
Andrew Gerrand 2015-03-05 15:56:40 +11:00
Родитель ec2973ad9c
Коммит 51edd24018
2 изменённых файлов: 33 добавлений и 3 удалений

Просмотреть файл

@ -63,7 +63,11 @@ func create(args []string) error {
}
}
instName := fmt.Sprintf("mote-%s-%s", username(), builderType)
instPrefix := fmt.Sprintf("mote-%s-", username())
instName, err := nextName(instPrefix + builderType)
if err != nil {
return err
}
client, err := buildlet.StartNewVM(projTokenSource(), instName, builderType, buildlet.VMOpts{
Zone: *zone,
ProjectID: *proj,
@ -80,6 +84,6 @@ func create(args []string) error {
if err != nil {
return fmt.Errorf("failed to create VM: %v", err)
}
fmt.Printf("%s\t%s\n", builderType, client.URL())
fmt.Printf("%s\t%s\n", strings.TrimPrefix(instName, instPrefix), client.URL())
return nil
}

Просмотреть файл

@ -34,7 +34,7 @@ func list(args []string) error {
if !strings.HasPrefix(vm.Name, prefix) {
continue
}
fmt.Printf("%s\thttps://%s\n", vm.Type, strings.TrimSuffix(vm.IPPort, ":443"))
fmt.Printf("%s\thttps://%s\n", strings.TrimPrefix(vm.Name, prefix), strings.TrimSuffix(vm.IPPort, ":443"))
}
return nil
}
@ -67,3 +67,29 @@ func namedClient(name string) (*buildlet.Client, error) {
}
return nil, fmt.Errorf("buildlet %q not running", name)
}
// nextName returns the next available numbered name or the given buildlet base
// name. For example, if the provided prefix is "linux-amd64" and there's
// already an instance named "linux-amd64", nextName will return
// "linux-amd64-1".
func nextName(prefix string) (string, error) {
vms, err := buildlet.ListVMs(projTokenSource(), *proj, *zone)
if err != nil {
return "", fmt.Errorf("error listing VMs: %v", err)
}
matches := map[string]bool{}
for _, vm := range vms {
if strings.HasPrefix(vm.Name, prefix) {
matches[vm.Name] = true
}
}
if len(matches) == 0 || !matches[prefix] {
return prefix, nil
}
for i := 1; ; i++ {
next := fmt.Sprintf("%v-%v", prefix, i)
if !matches[next] {
return next, nil
}
}
}