Skip to content

Commit

Permalink
feat(cdsctl): cdsctl queue stopall (#5401)
Browse files Browse the repository at this point in the history
* feat(cdsctl): cdsctl queue stopall

Signed-off-by: Yvonnick Esnault <[email protected]>
  • Loading branch information
yesnault authored Sep 2, 2020
1 parent fa02229 commit 5dec37d
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
88 changes: 87 additions & 1 deletion cli/cdsctl/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"strconv"
"strings"
"time"

Expand All @@ -28,6 +29,7 @@ var queueCmd = cli.Command{
func queue() *cobra.Command {
return cli.NewListCommand(queueCmd, queueRun, []*cobra.Command{
cli.NewCommand(queueUICmd, queueUIRun, nil, withAllCommandModifiers()...),
cli.NewCommand(queueStopAllCmd, queueStopAllRun, nil, withAllCommandModifiers()...),
})
}

Expand All @@ -36,6 +38,90 @@ var queueUICmd = cli.Command{
Short: "Show the current queue",
}

var queueStopAllCmd = cli.Command{
Name: "stopall",
Short: "Stop all job from the queue",
Example: "cdsctl queue stopall",
OptionalArgs: []cli.Arg{
{Name: _ProjectKey},
{Name: _WorkflowName},
},
Flags: []cli.Flag{
{
Name: "force",
Usage: "if true, do not ask user before stopping all workflows",
IsValid: func(s string) bool {
if s != "true" && s != "false" {
return false
}
return true
},
Default: "false",
Type: cli.FlagBool,
},
},
}

func queueStopAllRun(v cli.Values) error {
wantToStopAll := v.GetBool("force") || cli.AskConfirm("Are you sure to want to stop all jobs in the queue?")
if !wantToStopAll {
return nil
}

jobs, err := client.QueueWorkflowNodeJobRun(sdk.StatusWaiting, sdk.StatusBuilding)
if err != nil {
return err
}

var nbToStop int64
for _, jr := range jobs {
projectKey := getVarsInPbj("cds.project", jr.Parameters)
workflowName := getVarsInPbj("cds.workflow", jr.Parameters)

if v.GetString(_ProjectKey) != "" && projectKey != v.GetString(_ProjectKey) {
continue
}
if v.GetString(_WorkflowName) != "" && workflowName != v.GetString(_WorkflowName) {
continue
}
nbToStop++
}

wantToStopAllSure := v.GetBool("force") || cli.AskConfirm(fmt.Sprintf("There are %d worfklows to stop, confirm stopping workflows?", nbToStop))
if !wantToStopAllSure {
return nil
}

var stopped int64
for _, jr := range jobs {
run := getVarsInPbj("cds.run.number", jr.Parameters)
projectKey := getVarsInPbj("cds.project", jr.Parameters)
workflowName := getVarsInPbj("cds.workflow", jr.Parameters)

if v.GetString(_ProjectKey) != "" && projectKey != v.GetString(_ProjectKey) {
continue
}
if v.GetString(_WorkflowName) != "" && workflowName != v.GetString(_WorkflowName) {
continue
}

runNumber, err := strconv.ParseInt(run, 10, 64)
if err != nil {
return fmt.Errorf("%s invalid: not a integer for a workflow run. err: %v", run, err)
}

w, err := client.WorkflowStop(projectKey, workflowName, runNumber)
if err != nil {
fmt.Printf("ERROR while stopping Workflow %s #%d: %v\n", v.GetString(_WorkflowName), w.Number, err)
continue
}
fmt.Printf("Workflow %s #%d has been stopped\n", v.GetString(_WorkflowName), w.Number)
stopped++
}
fmt.Printf("Nb workflows stopped: %d\n", stopped)
return nil
}

func queueRun(v cli.Values) (cli.ListResult, error) {
jobList, err := getJobQueue(sdk.StatusWaiting, sdk.StatusBuilding)
if err != nil {
Expand Down Expand Up @@ -78,7 +164,7 @@ func getJobQueue(status ...string) ([]jobCLI, error) {
NodeName: getVarsInPbj("cds.node", jr.Parameters),
Status: jr.Status,
URL: generateQueueJobURL(baseURL, jr.Parameters),
Since: fmt.Sprintf(sdk.Round(time.Since(jr.Queued), time.Second).String()),
Since: sdk.Round(time.Since(jr.Queued), time.Second).String(),
Duration: time.Since(jr.Queued),
BookedBy: jr.BookedBy.Name,
TriggeredBy: getVarsInPbj("cds.triggered_by.username", jr.Parameters),
Expand Down
6 changes: 6 additions & 0 deletions tests/01_queue_stopall.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: Queue stop all
version: "2"
testcases:
- name: cdsctl queue stopall
steps:
- script: {{.cdsctl}} -f {{.cdsctl.config}} queue stopall --force
11 changes: 11 additions & 0 deletions tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ initialization_tests() {
${CMD} >01_signup_user.yml.output 2>&1

check_failure $? 01_signup_user.yml.output

CMD="${VENOM} run ${VENOM_OPTS} 01_queue_stopall.yml --var cdsctl.config=${CDSCTL_CONFIG}_admin --var cdsctl=${CDSCTL} --var api.url=${CDS_API_URL}"
echo -e " ${YELLOW}01_queue_stopall.yml ${DARKGRAY}[${CMD}]${NOCOLOR}"
${CMD} >01_queue_stopall.yml.output 2>&1
check_failure $? 01_queue_stopall.yml.output
}

smoke_tests_services() {
Expand Down Expand Up @@ -146,6 +151,12 @@ workflow_with_integration_tests() {
}

workflow_with_third_parties() {
echo "Stopping all jobs in queue:"
CMD="${VENOM} run ${VENOM_OPTS} 01_queue_stopall.yml --var cdsctl.config=${CDSCTL_CONFIG}_admin --var cdsctl=${CDSCTL} --var api.url=${CDS_API_URL}"
echo -e " ${YELLOW}01_queue_stopall.yml ${DARKGRAY}[${CMD}]${NOCOLOR}"
${CMD} >01_queue_stopall.yml.output 2>&1
check_failure $? 01_queue_stopall.yml.output

if [ -z "$CDS_MODEL_REQ" ]; then echo "missing CDS_MODEL_REQ variable"; exit 1; fi
if [ -z "$CDS_REGION_REQ" ]; then echo "missing CDS_REGION_REQ variable"; exit 1; fi
echo "Running Workflow with third parties:"
Expand Down

0 comments on commit 5dec37d

Please sign in to comment.