Skip to content

Commit

Permalink
Add support for specific scenarios in clean command and Engine's Clea…
Browse files Browse the repository at this point in the history
…n method

- Enhance `clean` to support individual scenario cleaning based on arguments.
- Modify `Engine.CleanAll` to log failed scenario cleanups more descriptively.
- Update `Engine.Clean` to be externally callable
- Introduce table rendering for the `clean` command
  • Loading branch information
noamsdahan committed Aug 20, 2023
1 parent 306935b commit a04ed95
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
44 changes: 39 additions & 5 deletions cmd/commands/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
cnappgoat "github.com/ermetic-research/CNAPPgoat"
"github.com/ermetic-research/CNAPPgoat/cmd/commands/common"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
Expand All @@ -11,16 +12,49 @@ var CleanCommand = &cli.Command{
Name: "clean",
Usage: "clean and remove all created resources and delete all scenarios and any related files",
Flags: common.CommandFlags(),
Before: common.CommandBefore,
Before: common.CommandUpdateBefore,
Action: func(c *cli.Context) error {
moduleTable := c.Context.Value("CNAPPgoatModuleTable").(table.Writer)
engine := c.Context.Value("CNAPPgoatEngine").(*cnappgoat.Engine)

if err := engine.CleanAll(c.Context, c.Bool("force")); err != nil {
reg := c.Context.Value("CNAPPgoatModuleRegistry").(*cnappgoat.Registry)
if c.Args().Len() == 0 {
if err := engine.CleanAll(c.Context, c.Bool("force")); err != nil {
return err
}
logrus.Infof("successfully cleaned all scenarios, and removed all created resources")
logrus.Infof("goodbye!")
return nil
}
scenarios, err :=
common.GetScenarios(
c.Args(),
reg,
c.String("module"),
c.String("platform"),
c.String("state"))
if err != nil {
return err
}
if len(scenarios) >= 3 {
ok, err := common.ConfirmForAllScenarios("clean", len(scenarios))
if err != nil {
return err
}
if !ok {
return nil
}
}

for scenarioIndex, scenario := range scenarios {
logrus.Infof("destroying scenario: %s", scenario.Name)
if err := engine.Clean(c.Context, scenario, c.Bool("force")); err != nil {
logrus.WithError(err).Error("failed to clean scenario")
}

common.AppendScenarioToTable(scenario, moduleTable, scenarioIndex)
}

logrus.Infof("successfully cleaned all scenarios, and removed all created resources")
logrus.Infof("goodbye!")
moduleTable.Render()
return nil
},
}
15 changes: 9 additions & 6 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ func NewEngine(registry *Registry, storage *LocalStorage) *Engine {
func (e *Engine) CleanAll(ctx context.Context, force bool) error {
cleanSuccessful := true
for _, scenario := range e.Registry.ListScenarios() {
if err := e.clean(ctx, scenario, force); err != nil {
logrus.WithError(err).Errorf("failed to clean scenario %s", scenario.ScenarioParams.ID)
if err := e.Clean(ctx, scenario, force); err != nil {
logrus.WithError(err).Errorf("failed to Clean scenario %s", scenario.ScenarioParams.ID)
cleanSuccessful = false
}
}
if !cleanSuccessful {
return errors.New("failed to clean all scenarios")
return errors.New("failed to Clean all scenarios")
}
return e.Storage.DeleteWorkingDir()
}
Expand Down Expand Up @@ -230,17 +230,20 @@ func (e *Engine) setStackConfigurationFromProjectFile(ctx context.Context, scena
return nil
}

func (e *Engine) clean(ctx context.Context, scenario *Scenario, force bool) error {
func (e *Engine) Clean(ctx context.Context, scenario *Scenario, force bool) error {
if scenario.State.State != NotDeployed && scenario.State.State != Destroyed {
if err := e.Destroy(ctx, scenario, force); err != nil {
return fmt.Errorf("failed to destroy scenario %s: %v", scenario.ScenarioParams.ID, err)
}
}

logrus.Info("removing stack")
if err := e.removeStack(ctx, scenario); err != nil {
return fmt.Errorf("failed to remove stack %s: %v", scenario.ScenarioParams.ID, err)
}

logrus.Infof("successfully cleaned scenario %s", scenario.ScenarioParams.ID)
if err := e.Registry.SetState(scenario, State{State: NotDeployed}); err != nil {
return err
}
return nil
}

Expand Down

0 comments on commit a04ed95

Please sign in to comment.