github.com/distbuild/reclient@v0.0.0-20240401075343-3de72e395564/examples/remoteecho/main.go (about)

     1  // Copyright 2023 Google LLC
     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  // sample sends an echo command to execute remotely.
    16  // Several important reflags are:
    17  // * --echostr: the string to echo
    18  // * --service: the RE service to connect to, for RBE set to: "remotebuildexecution.googleapis.com:443"
    19  // * --instance: the RE instance to use, for testing set to: "projects/foundry-x-experiments/instances/default_instance"
    20  // * --credential_file: the credential file to use, set to something like: "$HOME/.config/foundry/keys/dev-foundry.json"
    21  package main
    22  
    23  import (
    24  	"context"
    25  	"flag"
    26  
    27  	"github.com/bazelbuild/remote-apis-sdks/go/pkg/command"
    28  	"github.com/bazelbuild/remote-apis-sdks/go/pkg/filemetadata"
    29  	"github.com/bazelbuild/remote-apis-sdks/go/pkg/moreflag"
    30  	"github.com/bazelbuild/remote-apis-sdks/go/pkg/outerr"
    31  	"github.com/bazelbuild/remote-apis-sdks/go/pkg/rexec"
    32  
    33  	reflags "github.com/bazelbuild/remote-apis-sdks/go/pkg/flags"
    34  	log "github.com/golang/glog"
    35  )
    36  
    37  const (
    38  	// The standard RBE Ubuntu container.
    39  	dockerImage = "docker://gcr.io/cloud-marketplace/google/rbe-ubuntu16-04@sha256:94d7d8552902d228c32c8c148cc13f0effc2b4837757a6e95b73fdc5c5e4b07b"
    40  )
    41  
    42  var (
    43  	echoStr = flag.String("echostr", "Hello world", "The string to echo remotely")
    44  )
    45  
    46  func main() {
    47  	moreflag.Parse()
    48  	ctx := context.Background()
    49  	grpcClient, err := reflags.NewClientFromFlags(ctx)
    50  	if err != nil {
    51  		log.Exitf("error connecting to remote execution client: %v", err)
    52  	}
    53  	defer grpcClient.Close()
    54  	c := &rexec.Client{
    55  		FileMetadataCache: filemetadata.NewNoopCache(),
    56  		GrpcClient:        grpcClient,
    57  	}
    58  
    59  	cmd := &command.Command{
    60  		ExecRoot: ".",
    61  		Args:     []string{"echo", *echoStr},
    62  		Platform: map[string]string{"container-image": dockerImage},
    63  	}
    64  	res, _ := c.Run(ctx, cmd, command.DefaultExecutionOptions(), outerr.SystemOutErr)
    65  	if !res.IsOk() {
    66  		log.Exitf("Error executing action: %v", res)
    67  	}
    68  	log.V(2).Info("Successfully executed action.")
    69  }