Skip to content

Commit

Permalink
cmd/gomote: add support for groups to destroy
Browse files Browse the repository at this point in the history
For golang/go#53956.

Change-Id: I0239d4de773d9e6cc1a505efbb5aba2e62170e3d
Reviewed-on: https://go-review.googlesource.com/c/build/+/418275
Run-TryBot: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
Auto-Submit: Michael Knyszek <mknyszek@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
  • Loading branch information
mknyszek authored and gopherbot committed Nov 18, 2022
1 parent add2602 commit 1b202d1
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions cmd/gomote/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,12 @@ func legacyDestroy(args []string) error {
}

func destroy(args []string) error {
if activeGroup != nil {
return fmt.Errorf("command does not yet support groups")
}

fs := flag.NewFlagSet("destroy", flag.ContinueOnError)
fs.Usage = func() {
fmt.Fprintln(os.Stderr, "destroy usage: gomote destroy <instance>")
fmt.Fprintln(os.Stderr, "destroy usage: gomote destroy [instance]")
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "Destroys a single instance, or all instances in a group.")
fmt.Fprintln(os.Stderr, "Instance argument is optional with a group.")
fs.PrintDefaults()
if fs.NArg() == 0 {
// List buildlets that you might want to destroy.
Expand All @@ -83,16 +82,32 @@ func destroy(args []string) error {
}

fs.Parse(args)
if fs.NArg() != 1 {

var destroySet []string
if fs.NArg() == 1 {
destroySet = append(destroySet, fs.Arg(0))
} else if activeGroup != nil {
for _, inst := range activeGroup.Instances {
destroySet = append(destroySet, inst)
}
} else {
fs.Usage()
}
name := fs.Arg(0)
ctx := context.Background()
client := gomoteServerClient(ctx)
if _, err := client.DestroyInstance(ctx, &protos.DestroyInstanceRequest{
GomoteId: name,
}); err != nil {
return fmt.Errorf("unable to destroy instance: %s", statusFromError(err))
for _, name := range destroySet {
fmt.Fprintf(os.Stderr, "# Destroying %s\n", name)
ctx := context.Background()
client := gomoteServerClient(ctx)
if _, err := client.DestroyInstance(ctx, &protos.DestroyInstanceRequest{
GomoteId: name,
}); err != nil {
return fmt.Errorf("unable to destroy instance: %s", statusFromError(err))
}
}
if activeGroup != nil {
activeGroup.Instances = nil
if err := storeGroup(activeGroup); err != nil {
return err
}
}
return nil
}

0 comments on commit 1b202d1

Please sign in to comment.