Skip to content

Commit

Permalink
Merge pull request #8 from ozontech-forks/add_stopping_all_attacks
Browse files Browse the repository at this point in the history
add stopping all running attacks (only network,process,stress type)
  • Loading branch information
nikitasavchenko555 authored Nov 1, 2023
2 parents 3d42708 + a62dd82 commit 8f46a56
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release_tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
go-version: 1.18.2
id: go

- uses: actions/checkout@master
- uses: actions/checkout@v3.6.0
with:
fetch-depth: 0

Expand Down
60 changes: 52 additions & 8 deletions cmd/recover/recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,22 @@ import (
"context"
"fmt"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"go.uber.org/fx"

"github.com/pingcap/errors"
"github.com/pingcap/log"

"github.com/chaos-mesh/chaosd/cmd/server"
"github.com/chaos-mesh/chaosd/pkg/core"
"github.com/chaos-mesh/chaosd/pkg/server/chaosd"
"github.com/chaos-mesh/chaosd/pkg/utils"
)

type recoverCommand struct {
uid string
All bool
}

func NewRecoverCommand() *cobra.Command {
Expand All @@ -44,26 +47,67 @@ func NewRecoverCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "recover UID",
Short: "Recover a chaos experiment",
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: completeUid,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
utils.ExitWithMsg(utils.ExitBadArgs, "UID is required")
if len(args) == 0 && !options.All {
utils.ExitWithMsg(utils.ExitBadArgs, "UID is required, option all is false")
}
if len(args) > 0 {
options.uid = args[0]
}
options.uid = args[0]
utils.FxNewAppWithoutLog(dep, fx.Invoke(recoverCommandF)).Run()
},
}

cmd.Flags().BoolVarP(&options.All, "all", "A", false, "recover all running chaos attacks")
return cmd
}

func recoverCommandF(chaos *chaosd.Server, options *recoverCommand) {
err := chaos.RecoverAttack(options.uid)
if err != nil {
utils.ExitWithError(utils.ExitError, err)
if options.uid != "" {
err := chaos.RecoverAttack(options.uid)
if err != nil {
utils.ExitWithError(utils.ExitError, err)
}
utils.NormalExit(fmt.Sprintf("Recover %s successfully", options.uid))
} else {
exps, err := chaos.Search(&core.SearchCommand{
Asc: false,
All: false,
Status: "success",
Kind: "network,stress,process",
Limit: 0,
Offset: 0,
UID: "",
})
if err != nil {
utils.ExitWithError(utils.ExitError, err)
}
if len(exps) > 0 {
logrus.Infof("Found %d attacks for recover\n", len(exps))
} else {
utils.NormalExit(fmt.Sprintf("Found 0 attacks for recover. Exit\n"))
}

errorCount := 0
for _, attack := range exps {
fmt.Println(attack)
err := chaos.RecoverAttack(attack.Uid)
if err != nil {
logrus.Infof("%d %s", utils.ExitError, err)
errorCount++
}
logrus.Infof("Recover %s with type %s successfully\n", attack.Uid, attack.Kind)
}

if errorCount != 0 {
utils.ExitWithError(utils.ExitError, fmt.Errorf("several attack recover is failed"))
}

utils.NormalExit("All attacks is recovered successfully")

}

utils.NormalExit(fmt.Sprintf("Recover %s successfully", options.uid))
}

func completeUid(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down
5 changes: 3 additions & 2 deletions pkg/store/experiment/experiment.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package experiment
import (
"context"
"errors"
"strings"

perr "github.com/pkg/errors"
"gorm.io/gorm"
Expand Down Expand Up @@ -93,11 +94,11 @@ func (e *experimentStore) ListByConditions(_ context.Context, conds *core.Search

if !conds.All {
if len(conds.Kind) > 0 {
db = db.Where("kind = ?", conds.Kind)
db = db.Where("kind IN (?)", strings.Split(conds.Kind, ","))
}

if len(conds.Status) > 0 {
db = db.Where("status = ?", conds.Status)
db = db.Where("status = (?)", conds.Status)
}
}

Expand Down

0 comments on commit 8f46a56

Please sign in to comment.