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

     1  // Copyright 2020 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/errors"
    26  	"go.chromium.org/luci/led/job"
    27  	"go.chromium.org/luci/led/ledcmd"
    28  )
    29  
    30  func getSwarmCmd(opts cmdBaseOptions) *subcommands.Command {
    31  	return &subcommands.Command{
    32  		UsageLine: "get-swarm <swarm task id>",
    33  		ShortDesc: "obtain a JobDefinition from a swarming task",
    34  		LongDesc:  `Obtains the task definition from swarming and produce a JobDefinition.`,
    35  
    36  		CommandRun: func() subcommands.CommandRun {
    37  			ret := &cmdGetSwarm{}
    38  			ret.initFlags(opts)
    39  			return ret
    40  		},
    41  	}
    42  }
    43  
    44  type cmdGetSwarm struct {
    45  	cmdBase
    46  
    47  	taskID       string
    48  	swarmingHost string
    49  	pinBotID     bool
    50  	priorityDiff int
    51  }
    52  
    53  func (c *cmdGetSwarm) initFlags(opts cmdBaseOptions) {
    54  	c.Flags.StringVar(&c.swarmingHost, "S", "chromium-swarm.appspot.com",
    55  		"the swarming `host` to get the task from.")
    56  
    57  	c.Flags.BoolVar(&c.pinBotID, "pin-bot-id", false,
    58  		"Pin the bot id in the generated job Definition's dimensions.")
    59  
    60  	c.Flags.IntVar(&c.priorityDiff, "adjust-priority", 10,
    61  		"Increase or decrease the priority of the generated job. Note: priority works like Unix 'niceness'; Higher values indicate lower priority.")
    62  
    63  	c.cmdBase.initFlags(opts)
    64  }
    65  
    66  func (c *cmdGetSwarm) jobInput() bool                  { return false }
    67  func (c *cmdGetSwarm) positionalRange() (min, max int) { return 1, 1 }
    68  
    69  func (c *cmdGetSwarm) validateFlags(ctx context.Context, positionals []string, env subcommands.Env) error {
    70  	c.taskID = positionals[0]
    71  	return errors.Annotate(pingHost(c.swarmingHost), "swarming host").Err()
    72  }
    73  
    74  func (c *cmdGetSwarm) execute(ctx context.Context, authClient *http.Client, _ auth.Options, inJob *job.Definition) (out any, err error) {
    75  	return ledcmd.GetFromSwarmingTask(ctx, authClient, nil, ledcmd.GetFromSwarmingTaskOpts{
    76  		Name:         fmt.Sprintf("led get-swarm %s", c.taskID),
    77  		PinBotID:     c.pinBotID,
    78  		SwarmingHost: c.swarmingHost,
    79  		TaskID:       c.taskID,
    80  		PriorityDiff: c.priorityDiff,
    81  
    82  		KitchenSupport: c.kitchenSupport,
    83  	})
    84  }
    85  
    86  func (c *cmdGetSwarm) Run(a subcommands.Application, args []string, env subcommands.Env) int {
    87  	return c.doContextExecute(a, c, args, env)
    88  }