Skip to content

Commit

Permalink
cli: add timeout option for nuke and clean commands
Browse files Browse the repository at this point in the history
closes #83

Signed-off-by: Jérôme Jutteau <[email protected]>
  • Loading branch information
jerome-jutteau committed May 24, 2022
1 parent 02d7b3a commit fc155dd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
13 changes: 13 additions & 0 deletions cmd/frieza/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"log"
"time"

. "github.com/outscale-dev/frieza/internal/common"
"github.com/teris-io/cli"
Expand Down Expand Up @@ -74,3 +75,15 @@ func disableLogs() {
log.SetFlags(0)
log.SetOutput(ioutil.Discard)
}

func timeoutRunner(timeout string, json bool) {
if timeout == "-1" {
return
}
duration, err := time.ParseDuration(timeout)
if err != nil {
cliFatalf(json, "Bad format for --timeout")
}
time.Sleep(duration)
cliFatalf(json, "Frieza reached timeout")
}
13 changes: 11 additions & 2 deletions cmd/frieza/cli_clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,27 @@ import (
func cliClean() cli.Command {
return cli.NewCommand("clean", "delete created resources since a specific snapshot").
WithOption(cli.NewOption("plan", "Only show what resource would be deleted").WithType(cli.TypeBool)).
WithOption(cli.NewOption("timeout", "Exit with error after a specific duration (ex: 30s, 5m, 1.5h)").WithType(cli.TypeString)).
WithOption(cliJson()).
WithOption(cli.NewOption("auto-approve", "Approve resource deletion without confirmation").WithType(cli.TypeBool)).
WithArg(cli.NewArg("snapshot_name", "snapshot")).
WithOption(cliConfigPath()).
WithOption(cliDebug()).
WithAction(func(args []string, options map[string]string) int {
setupDebug(options)
clean(options["config"], &args[0], options["plan"] == "true", options["auto-approve"] == "true", options["json"] == "true")
plan := options["plan"] == "true"
autoApprove := options["auto-approve"] == "true"
jsonOutput := options["json"] == "true"
timeout := "-1"
if len(options["timeout"]) > 0 {
timeout = options["timeout"]
}
clean(options["config"], &args[0], plan, autoApprove, jsonOutput, timeout)
return 0
})
}

func clean(customConfigPath string, snapshotName *string, plan bool, autoApprove bool, jsonOutput bool) {
func clean(customConfigPath string, snapshotName *string, plan bool, autoApprove bool, jsonOutput bool, timeout string) {
var configPath *string
if jsonOutput && !autoApprove {
cliFatalf(true, "Cannot use --json option without --auto-approve")
Expand Down Expand Up @@ -70,5 +78,6 @@ func clean(customConfigPath string, snapshotName *string, plan bool, autoApprove
if !confirmAction(&message, autoApprove) {
log.Fatal("Clean canceled")
}
go timeoutRunner(timeout, jsonOutput)
destroyer.run()
}
13 changes: 11 additions & 2 deletions cmd/frieza/cli_nuke.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,27 @@ import (
func cliNuke() cli.Command {
return cli.NewCommand("nuke", "delete ALL resources of specified profiles").
WithOption(cli.NewOption("plan", "Only show what resource would be deleted").WithType(cli.TypeBool)).
WithOption(cli.NewOption("timeout", "Exit with error after a specific duration (ex: 30s, 5m, 1.5h)").WithType(cli.TypeString)).
WithOption(cliJson()).
WithOption(cli.NewOption("auto-approve", "Approve resource deletion without confirmation").WithType(cli.TypeBool)).
WithOption(cliConfigPath()).
WithOption(cliDebug()).
WithArg(cli.NewArg("profile", "one or more profile").AsOptional()).
WithAction(func(args []string, options map[string]string) int {
setupDebug(options)
nuke(options["config"], args, options["plan"] == "true", options["auto-approve"] == "true", options["json"] == "true")
plan := options["plan"] == "true"
autoApprove := options["auto-approve"] == "true"
jsonOutput := options["json"] == "true"
timeout := "-1"
if len(options["timeout"]) > 0 {
timeout = options["timeout"]
}
nuke(options["config"], args, plan, autoApprove, jsonOutput, timeout)
return 0
})
}

func nuke(customConfigPath string, profiles []string, plan bool, autoApprove bool, jsonOutput bool) {
func nuke(customConfigPath string, profiles []string, plan bool, autoApprove bool, jsonOutput bool, timeout string) {
if jsonOutput && !autoApprove {
cliFatalf(true, "Cannot use --json option without --auto-approve")
}
Expand Down Expand Up @@ -72,5 +80,6 @@ func nuke(customConfigPath string, profiles []string, plan bool, autoApprove boo
if !confirmAction(&message, autoApprove) {
log.Fatal("Nuke canceled")
}
go timeoutRunner(timeout, jsonOutput)
destroyer.run()
}

0 comments on commit fc155dd

Please sign in to comment.