Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SUP-1619: Agent bulk stopping #178

Merged
merged 9 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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