golang.org/x/build@v0.0.0-20240506185731-218518f32b70/cmd/gomote/destroy.go (about) 1 // 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 5 package main 6 7 import ( 8 "context" 9 "flag" 10 "fmt" 11 "log" 12 "os" 13 14 "golang.org/x/build/internal/gomote/protos" 15 ) 16 17 func destroy(args []string) error { 18 fs := flag.NewFlagSet("destroy", flag.ContinueOnError) 19 fs.Usage = func() { 20 fmt.Fprintln(os.Stderr, "destroy usage: gomote destroy [instance]") 21 fmt.Fprintln(os.Stderr) 22 fmt.Fprintln(os.Stderr, "Destroys a single instance, or all instances in a group.") 23 fmt.Fprintln(os.Stderr, "Instance argument is optional with a group.") 24 fs.PrintDefaults() 25 if fs.NArg() == 0 { 26 // List buildlets that you might want to destroy. 27 client := gomoteServerClient(context.Background()) 28 resp, err := client.ListInstances(context.Background(), &protos.ListInstancesRequest{}) 29 if err != nil { 30 log.Fatalf("unable to list possible instances to destroy: %v", err) 31 } 32 if len(resp.GetInstances()) > 0 { 33 fmt.Printf("possible instances:\n") 34 for _, inst := range resp.GetInstances() { 35 fmt.Printf("\t%s\n", inst.GetGomoteId()) 36 } 37 } 38 } 39 os.Exit(1) 40 } 41 var destroyGroup bool 42 fs.BoolVar(&destroyGroup, "destroy-group", false, "if a group is used, destroy the group too") 43 44 fs.Parse(args) 45 46 var destroySet []string 47 if fs.NArg() == 1 { 48 destroySet = append(destroySet, fs.Arg(0)) 49 } else if activeGroup != nil { 50 for _, inst := range activeGroup.Instances { 51 destroySet = append(destroySet, inst) 52 } 53 } else { 54 fs.Usage() 55 } 56 for _, name := range destroySet { 57 fmt.Fprintf(os.Stderr, "# Destroying %s\n", name) 58 ctx := context.Background() 59 client := gomoteServerClient(ctx) 60 if _, err := client.DestroyInstance(ctx, &protos.DestroyInstanceRequest{ 61 GomoteId: name, 62 }); err != nil { 63 return fmt.Errorf("unable to destroy instance: %w", err) 64 } 65 } 66 if activeGroup != nil { 67 if destroyGroup { 68 if err := deleteGroup(activeGroup.Name); err != nil { 69 return err 70 } 71 } else { 72 activeGroup.Instances = nil 73 if err := storeGroup(activeGroup); err != nil { 74 return err 75 } 76 } 77 } 78 return nil 79 }