go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/led/ledcli/edit_payload.go (about)

     1  // Copyright 2022 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  package ledcli
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"net/http"
    21  
    22  	"github.com/maruel/subcommands"
    23  
    24  	"go.chromium.org/luci/auth"
    25  	"go.chromium.org/luci/common/data/text"
    26  	"go.chromium.org/luci/common/errors"
    27  	swarmingpb "go.chromium.org/luci/swarming/proto/api_v2"
    28  
    29  	"go.chromium.org/luci/led/job"
    30  	"go.chromium.org/luci/led/ledcmd"
    31  )
    32  
    33  func editPayloadCmd(opts cmdBaseOptions) *subcommands.Command {
    34  	return &subcommands.Command{
    35  		UsageLine: "edit-payload [options]",
    36  		ShortDesc: "edit the job payload with provided RBE-CAS reference or CIPD info",
    37  		LongDesc: `edit the job payload with provided RBE-CAS reference or CIPD info.
    38  By default, the provided payload will be used as the led job's user payload.
    39  If the -property-only flag is passed or the builder has the "led_builder_is_bootstrapped"
    40  property set to true, the "led_cas_recipe_bundle" property will be set with the CAS digest so
    41  that the build's bootstrapper executable can launch the bundled recipes.
    42  `,
    43  
    44  		CommandRun: func() subcommands.CommandRun {
    45  			ret := &cmdEditPayload{}
    46  			ret.initFlags(opts)
    47  			return ret
    48  		},
    49  	}
    50  }
    51  
    52  type cmdEditPayload struct {
    53  	cmdBase
    54  
    55  	propertyOnly bool
    56  
    57  	casRef    string
    58  	casDigest *swarmingpb.Digest
    59  
    60  	cipdPkg string
    61  	cipdVer string
    62  }
    63  
    64  func (c *cmdEditPayload) initFlags(opts cmdBaseOptions) {
    65  	c.Flags.BoolVar(&c.propertyOnly, "property-only", false,
    66  		fmt.Sprintf("Pass the CAS reference via the %q property and "+
    67  			"preserve the executable of the input job rather than overwriting it. This "+
    68  			"is useful for when `exe` is actually a bootstrap program that you don't "+
    69  			"want to change. The same behavior can be enabled for a build without this "+
    70  			"flag by setting the \"led_builder_is_bootstrapped\" property to true.",
    71  			ledcmd.CASRecipeBundleProperty))
    72  
    73  	c.Flags.StringVar(&c.casRef, "cas-ref", "", text.Doc(`
    74  		The RBE-CAS reference of the payload, in the format of "hash/size",
    75  		e.g. "dead...beef/1234".
    76  	`))
    77  
    78  	c.Flags.StringVar(&c.cipdPkg, "cipd-pkg", "",
    79  		"Name of the CIPD package to be used as user payload.")
    80  	c.Flags.StringVar(&c.cipdVer, "cipd-ver", "",
    81  		"Version of the CIPD package to be used as user payload.")
    82  
    83  	c.cmdBase.initFlags(opts)
    84  }
    85  
    86  func (c *cmdEditPayload) jobInput() bool                  { return true }
    87  func (c *cmdEditPayload) positionalRange() (min, max int) { return 0, 0 }
    88  
    89  func (c *cmdEditPayload) validateFlags(ctx context.Context, _ []string, _ subcommands.Env) (err error) {
    90  	if c.casRef != "" {
    91  		if c.casDigest, err = job.ToCasDigest(c.casRef); err != nil {
    92  			return
    93  		}
    94  	}
    95  	if c.propertyOnly && (c.cipdPkg != "" || c.cipdVer != "") {
    96  		return errors.Reason("cannot use payload from CIPD in -property-only mode").Err()
    97  	}
    98  	return
    99  }
   100  
   101  func (c *cmdEditPayload) execute(ctx context.Context, _ *http.Client, _ auth.Options, inJob *job.Definition) (out any, err error) {
   102  	return inJob, ledcmd.EditPayload(ctx, inJob, &ledcmd.EditPayloadOpts{
   103  		PropertyOnly: c.propertyOnly,
   104  		CasDigest:    c.casDigest,
   105  		CIPDPkg:      c.cipdPkg,
   106  		CIPDVer:      c.cipdVer,
   107  	})
   108  }
   109  
   110  func (c *cmdEditPayload) Run(a subcommands.Application, args []string, env subcommands.Env) int {
   111  	return c.doContextExecute(a, c, args, env)
   112  }