go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/swarming/server/testing/getreservation/main.go (about)

     1  // Copyright 2023 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Command getreservation fetches reservation status from RBE.
    16  package main
    17  
    18  import (
    19  	"context"
    20  	"flag"
    21  	"fmt"
    22  	"os"
    23  
    24  	"google.golang.org/grpc"
    25  	"google.golang.org/grpc/credentials"
    26  	"google.golang.org/protobuf/encoding/prototext"
    27  
    28  	"go.chromium.org/luci/auth"
    29  	"go.chromium.org/luci/common/errors"
    30  	"go.chromium.org/luci/common/logging/gologger"
    31  	"go.chromium.org/luci/hardcoded/chromeinfra"
    32  
    33  	"go.chromium.org/luci/swarming/internal/remoteworkers"
    34  )
    35  
    36  var (
    37  	wait = flag.Duration("wait", 0, "How long to wait for completion")
    38  )
    39  
    40  func main() {
    41  	flag.Parse()
    42  	ctx := gologger.StdConfig.Use(context.Background())
    43  	if err := run(ctx, flag.Args()); err != nil {
    44  		errors.Log(ctx, err)
    45  		os.Exit(1)
    46  	}
    47  }
    48  
    49  func run(ctx context.Context, args []string) error {
    50  	if len(args) != 1 {
    51  		return errors.Reason("need one positional argument with full reservation name").Err()
    52  	}
    53  	reservationName := args[0]
    54  
    55  	creds, err := auth.NewAuthenticator(ctx, auth.SilentLogin, chromeinfra.SetDefaultAuthOptions(auth.Options{
    56  		Scopes: []string{
    57  			"https://www.googleapis.com/auth/cloud-platform",
    58  			"https://www.googleapis.com/auth/userinfo.email",
    59  		},
    60  	})).PerRPCCredentials()
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	cc, err := grpc.DialContext(ctx, "remotebuildexecution.googleapis.com:443",
    66  		grpc.WithTransportCredentials(credentials.NewTLS(nil)),
    67  		grpc.WithPerRPCCredentials(creds),
    68  		grpc.WithBlock(),
    69  	)
    70  	if err != nil {
    71  		return err
    72  	}
    73  	rbe := remoteworkers.NewReservationsClient(cc)
    74  
    75  	reservation, err := rbe.GetReservation(ctx, &remoteworkers.GetReservationRequest{
    76  		Name:        reservationName,
    77  		WaitSeconds: int64(wait.Seconds()),
    78  	})
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	text, err := (prototext.MarshalOptions{Multiline: true, Indent: "  "}).Marshal(reservation)
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	fmt.Printf("%s\n", text)
    89  	return nil
    90  }