From 15431030bab3431dc6950c446e3e6e6878bf3b34 Mon Sep 17 00:00:00 2001 From: Michael Anthony Knyszek Date: Tue, 19 Jul 2022 21:50:06 +0000 Subject: [PATCH] cmd/gomote: add -new-group flag to create This flag creates a new group for the newly-created instances. For golang/go#53956. Change-Id: Ib65a6391a554dbeb1f6f0e44c286ad5b0d221757 Reviewed-on: https://go-review.googlesource.com/c/build/+/418302 Run-TryBot: Michael Knyszek TryBot-Result: Gopher Robot Auto-Submit: Michael Knyszek Reviewed-by: Carlos Amedee --- cmd/gomote/create.go | 24 +++++++++++++++++------- cmd/gomote/group.go | 16 ++++++++-------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/cmd/gomote/create.go b/cmd/gomote/create.go index c9170daeee..03e4a8b546 100644 --- a/cmd/gomote/create.go +++ b/cmd/gomote/create.go @@ -164,6 +164,8 @@ func create(args []string) error { fs.IntVar(&count, "count", 1, "number of instances to create") var setup bool fs.BoolVar(&setup, "setup", false, "set up the instance by pushing GOROOT and building the Go toolchain") + var newGroup string + fs.StringVar(&newGroup, "new-group", "", "also create a new group and add the new instances to it") fs.Parse(args) if fs.NArg() != 1 { @@ -180,7 +182,15 @@ func create(args []string) error { } } - var activeGroupMu sync.Mutex + var groupMu sync.Mutex + group := activeGroup + if newGroup != "" { + group, err = doCreateGroup(newGroup) + if err != nil { + return err + } + } + eg, ctx := errgroup.WithContext(context.Background()) client := gomoteServerClient(ctx) for i := 0; i < count; i++ { @@ -207,10 +217,10 @@ func create(args []string) error { } } fmt.Println(inst) - if activeGroup != nil { - activeGroupMu.Lock() - activeGroup.Instances = append(activeGroup.Instances, inst) - activeGroupMu.Unlock() + if group != nil { + groupMu.Lock() + group.Instances = append(group.Instances, inst) + groupMu.Unlock() } if !setup { return nil @@ -239,8 +249,8 @@ func create(args []string) error { if err := eg.Wait(); err != nil { return err } - if activeGroup != nil { - return storeGroup(activeGroup) + if group != nil { + return storeGroup(group) } return nil } diff --git a/cmd/gomote/group.go b/cmd/gomote/group.go index e4079c01cc..b771e78484 100644 --- a/cmd/gomote/group.go +++ b/cmd/gomote/group.go @@ -57,16 +57,16 @@ func createGroup(args []string) error { if len(args) != 1 { usage() } - name := args[0] + _, err := doCreateGroup(args[0]) + return err +} + +func doCreateGroup(name string) (*groupData, error) { if _, err := loadGroup(name); err == nil { - return fmt.Errorf("group %q already exists", name) + return nil, fmt.Errorf("group %q already exists", name) } - if err := storeGroup(&groupData{ - Name: name, - }); err != nil { - return err - } - return nil + g := &groupData{Name: name} + return g, storeGroup(g) } func destroyGroup(args []string) error {