script to determine GCS usage by bucket
Bug: skia:
Change-Id: Ib7f0484ddea902c6efb67389c1a5be5e9de550f6
Reviewed-on: https://skia-review.googlesource.com/c/180761
Commit-Queue: Kevin Lubick <kjlubick@google.com>
Reviewed-by: Joe Gregorio <jcgregorio@google.com>
diff --git a/scripts/gcs_usage/gcs_usage.go b/scripts/gcs_usage/gcs_usage.go
new file mode 100644
index 0000000..abc4202
--- /dev/null
+++ b/scripts/gcs_usage/gcs_usage.go
@@ -0,0 +1,65 @@
+package main
+
+import (
+ "bytes"
+ "context"
+ "flag"
+ "fmt"
+ "os"
+ "strings"
+
+ "go.skia.org/infra/go/exec"
+)
+
+var (
+ project = flag.String("project", "", "[REQUIRED] The GCP project to analyze usage for.")
+ displayBytes = flag.Bool("display_bytes", false, "Show the results in bytes, not in human readable form.")
+)
+
+func main() {
+ flag.Parse()
+ if *project == "" {
+ fmt.Println("--project is required")
+ flag.PrintDefaults()
+ os.Exit(1)
+ }
+
+ fmt.Println("Fetching buckets in project")
+ output := bytes.Buffer{}
+ err := exec.Run(context.Background(), &exec.Command{
+ Name: "gsutil",
+ Args: []string{"ls", "-p", *project},
+ Stdout: &output,
+ })
+ if err != nil {
+ fmt.Printf("Could not retrieve buckets: %s\n", err)
+ os.Exit(1)
+ }
+
+ buckets := strings.Split(strings.TrimSpace(output.String()), "\n")
+ fmt.Printf("Found %d buckets\n", len(buckets))
+ fmt.Println("Tabulating total space, this may take tens of seconds for big buckets")
+
+ flags := "-hs"
+ if *displayBytes {
+ flags = "-s"
+ }
+
+ // Do them one at a time to show incremental progress, as large
+ // buckets can take >10s to tabulate.
+ for _, b := range buckets {
+ output := bytes.Buffer{}
+ // GCS buckets must be uniquely named, so no need to specify a project.
+ err := exec.Run(context.Background(), &exec.Command{
+ Name: "gsutil",
+ Args: []string{"du", flags, b},
+ Stdout: &output,
+ })
+ if err != nil {
+ fmt.Printf("Could not get size for bucket %s: %s\n", b, err)
+ continue
+ }
+ fmt.Println(strings.TrimSpace(output.String()))
+ }
+ fmt.Println("Done")
+}