Skip to content

Commit

Permalink
SUP-1619: Agent bulk stopping (#178)
Browse files Browse the repository at this point in the history
* Early functional arbitrary agent stopping (ArbitraryArgs)

* Move to a swtich/case statement

* Tea output/stopAgent shared func

* Some formatting and cleanup

* Newline lint?

* Silenced ugage on run (agent stop with no args etc)

* Better error joining

* More linting required

* Switch statement simplified
  • Loading branch information
james2791 authored Dec 22, 2023
1 parent 304b702 commit 09fd315
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
50 changes: 36 additions & 14 deletions pkg/cmd/agent/stop.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package agent

import (
"errors"
"fmt"

"github.com/MakeNowJust/heredoc"
"github.com/buildkite/cli/v3/internal/io"
"github.com/buildkite/cli/v3/pkg/cmd/factory"
Expand All @@ -14,7 +17,7 @@ func NewCmdAgentStop(f *factory.Factory) *cobra.Command {
cmd := cobra.Command{
DisableFlagsInUseLine: true,
Use: "stop <agent> [--force]",
Args: cobra.ExactArgs(1),
Args: cobra.ArbitraryArgs,
Short: "Stop an agent",
Long: heredoc.Doc(`
Instruct an agent to stop accepting new build jobs and shut itself down.
Expand All @@ -25,24 +28,43 @@ func NewCmdAgentStop(f *factory.Factory) *cobra.Command {
If the agent is already stopped the command returns an error.
`),
RunE: func(cmd *cobra.Command, args []string) error {
// create a bubbletea program to manage the output of this command
l := io.NewPendingCommand(func() tea.Msg {
org, agent := parseAgentArg(args[0], f.Config)
_, err := f.RestAPIClient.Agents.Stop(org, agent, force)
if err != nil {
return err
switch agents := len(args); {
case agents == 0:
// No agents slug/UUID passed in, return an error
return errors.New("Please specify at least one agent to stop.")
case agents >= 1:
// Construct an agentStopErrors variable to construct errors
var agentStopErrors error
for _, agent := range args {
err := stopAgent(agent, f, force)
// Append to agentStopErrors if there was an error stopping an agent
if err != nil {
agentStopErrors = errors.Join(agentStopErrors, err)
}
}
return io.PendingOutput("Agent stopped")
}, "Stopping agent")

p := tea.NewProgram(l)
_, err := p.Run()

return err
return agentStopErrors
}
return nil
},
}

cmd.Flags().BoolVar(&force, "force", false, "Force stop the agent. Terminating any jobs in progress")

return &cmd
}

func stopAgent(agent string, f *factory.Factory, force bool) error {
l := io.NewPendingCommand(func() tea.Msg {
org, agent := parseAgentArg(agent, f.Config)
_, err := f.RestAPIClient.Agents.Stop(org, agent, force)
if err != nil {
return err
}
return io.PendingOutput(fmt.Sprintf("Stopped agent %s", agent))
}, fmt.Sprintf("Stopping agent %s", agent))

p := tea.NewProgram(l)
_, err := p.Run()

return err
}
1 change: 1 addition & 0 deletions pkg/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func NewCmdRoot(f *factory.Factory) (*cobra.Command, error) {
Annotations: map[string]string{
"versionInfo": versionCmd.Format(f.Version),
},
SilenceUsage: true,
}

cmd.AddCommand(configureCmd.NewCmdConfigure(f))
Expand Down

0 comments on commit 09fd315

Please sign in to comment.