Skip to content

Commit

Permalink
chore: support -j N syntax for parallel flag and remove default value (
Browse files Browse the repository at this point in the history
…#1524)

<!--  Thanks for sending a pull request!  Here are some tips for you:

1. If this is your first time, please read our contributor guidelines:
https://github.com/terramate-io/terramate/blob/main/CONTRIBUTING.md
2. If the PR is unfinished, mark it as draft:
https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request
3. Please update the PR title using the Conventional Commits convention:
https://www.conventionalcommits.org/en/v1.0.0/
    Example: feat: add support for XYZ.
-->

## What this PR does / why we need it:

This PR adds support for supplying a parameter to the short form of the
`--parallel`flag, i.e. `-j N`.
It also removes the default value, so the long version always needs a
value.

## Which issue(s) this PR fixes:
<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
_If PR is about `failing-tests or flakes`, please post the related
issues/tests in a comment and do not use `Fixes`_*
Keep it empty if not applicable.
-->

## Special notes for your reviewer:

## Does this PR introduce a user-facing change?
<!--
If no, just write "no" in the block below.
If yes, please explain the change and update documentation and the
CHANGELOG.md file accordingly.
-->
```
no
```
  • Loading branch information
snakster authored Mar 5, 2024
2 parents dcde4ac + 9e449be commit 6d74b99
Show file tree
Hide file tree
Showing 10 changed files with 12 additions and 51 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ Given a version number `MAJOR.MINOR.PATCH`, we increment the:
- Improve user experience when using Terramate with existing Terragrunt projects.
- Add `terramate create --all-terragrunt` option, which will automatically create Terramate stacks for each Terraform module.
- Allow to run independent stacks in parallel for faster deployments and better utilization of system resources in general.
- Add `--parallel` (short `-j`) option to `terramate run` and `terramate script run`.
- `--parallel=N` limits the number of concurrent runs to `N`, otherwise a sensible default limit is chosen.
- Add `--parallel=N` (short `-j N`) option to `terramate run` and `terramate script run` to allow running up to `N` stacks in parallel.
- Ordering constraints between stacks are still respected, i.e. `before`/`after`, parent before sub-folders.
- Add `cloud_sync_drift_status` option to `script` block commands. It allows for synchronizing the
stack drift details from script jobs.
Expand Down
42 changes: 2 additions & 40 deletions cmd/terramate/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"os"
"path"
"path/filepath"
"reflect"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -102,12 +100,6 @@ const (
// UIMode defines different modes of operation for the cli.
type UIMode int

type kongParallelFlag struct {
Value int
}

const defaultParallelRunCount = 5

type cliSpec struct {
VersionFlag bool `hidden:"true" name:"version" help:"Show Terramate version."`
Chdir string `short:"C" optional:"true" predictor:"file" help:"Set working directory."`
Expand Down Expand Up @@ -171,7 +163,7 @@ type cliSpec struct {
// Note: 0 is not the real default value here, this is just a workaround.
// Kong doesn't support having 0 as the default value in case the flag isn't set, but K in case it's set without a value.
// The K case is handled in the custom decoder.
Parallel kongParallelFlag `short:"j" optional:"true" default:"0" help:"Run independent stacks in parallel."`
Parallel int `short:"j" optional:"true" help:"Run independent stacks in parallel."`

runSafeguardsCliSpec

Expand All @@ -196,7 +188,7 @@ type cliSpec struct {
Cmds []string `arg:"" optional:"true" passthrough:"" help:"Script to execute."`

// See above comment regarding for run --parallel.
Parallel kongParallelFlag `short:"j" optional:"true" default:"0" help:"Run independent stacks in parallel"`
Parallel int `short:"j" optional:"true" help:"Run independent stacks in parallel."`

runSafeguardsCliSpec
} `cmd:"" help:"Run a Terramate Script in stacks."`
Expand Down Expand Up @@ -709,36 +701,6 @@ func (c *cli) run() {
}
}

func (s *kongParallelFlag) Decode(ctx *kong.DecodeContext) error {
if ctx.Scan.Peek().Type == kong.FlagValueToken {
t, err := ctx.Scan.PopValue("counter")
if err != nil {
return err
}

switch v := t.Value.(type) {
case string:
n, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return stdfmt.Errorf("expected a counter but got %q (%T)", t, t.Value)
}
s.Value = int(n)

case int, int8, int16, int32, int64:
t := reflect.ValueOf(v)
s.Value = int(t.Int())

default:
return stdfmt.Errorf("expected a counter but got %q (%T)", t, t.Value)
}
return nil
}

s.Value = defaultParallelRunCount

return nil
}

func (c *cli) setupSafeguards(run runSafeguardsCliSpec) {
global := c.parsedArgs.deprecatedGlobalSafeguardsCliSpec

Expand Down
2 changes: 1 addition & 1 deletion cmd/terramate/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (c *cli) runOnStacks() {
Reverse: c.parsedArgs.Run.Reverse,
ScriptRun: false,
ContinueOnError: c.parsedArgs.Run.ContinueOnError,
Parallel: c.parsedArgs.Run.Parallel.Value,
Parallel: c.parsedArgs.Run.Parallel,
})
if err != nil {
fatal("one or more commands failed", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/terramate/cli/script_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (c *cli) runScript() {
DryRun: c.parsedArgs.Script.Run.DryRun,
ScriptRun: true,
ContinueOnError: false,
Parallel: c.parsedArgs.Script.Run.Parallel.Value,
Parallel: c.parsedArgs.Script.Run.Parallel,
})
if err != nil {
fatal("one or more commands failed", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/terramate/e2etests/cloud/run_cloud_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ func TestCLIRunWithCloudSyncDeployment(t *testing.T) {
"--cloud-sync-deployment",
}
if isParallel {
runflags = append(runflags, "--parallel")
runflags = append(runflags, "--parallel", "5")
tc.want.run.IgnoreStdout = true
tc.want.run.IgnoreStderr = true
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/terramate/e2etests/cloud/run_cloud_drift_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ func TestCLIRunWithCloudSyncDriftStatus(t *testing.T) {
"--cloud-sync-drift-status",
}
if isParallel {
runflags = append(runflags, "--parallel")
runflags = append(runflags, "-j", "5")
tc.want.run.IgnoreStdout = true
tc.want.run.IgnoreStderr = true
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/terramate/e2etests/cloud/run_cloud_signal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestCLIRunWithCloudSyncDeploymentWithSignals(t *testing.T) {
"--cloud-sync-deployment",
}
if isParallel {
runflags = append(runflags, "--parallel")
runflags = append(runflags, "--parallel", "5")
tc.want.run.IgnoreStdout = true
tc.want.run.IgnoreStderr = true
}
Expand Down Expand Up @@ -220,7 +220,7 @@ func TestCLIRunWithCloudSyncDriftStatusWithSignals(t *testing.T) {
"--cloud-sync-drift-status",
}
if isParallel {
runflags = append(runflags, "--parallel")
runflags = append(runflags, "--parallel=5")
tc.want.run.IgnoreStdout = true
tc.want.run.IgnoreStderr = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func TestCLIScriptRunWithCloudSyncDeployment(t *testing.T) {

scriptArgs := []string{"--quiet", "--disable-safeguards=git-out-of-sync"}
if isParallel {
scriptArgs = append(scriptArgs, "--parallel")
scriptArgs = append(scriptArgs, "--parallel=5")
// For the parallel test, we ignore output validation, since the print order is non-deterministic.
tc.want.run.IgnoreStderr = true
tc.want.run.IgnoreStdout = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ func TestScriptRunDriftStatus(t *testing.T) {
"--quiet",
}
if isParallel {
runflags = append(runflags, "--parallel")
runflags = append(runflags, "--parallel", "5")
tc.want.run.IgnoreStdout = true
tc.want.run.IgnoreStderr = true
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/terramate/e2etests/core/run_parallel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestParallelFibonacci(t *testing.T) {

tmcli := NewCLI(t, s.RootDir())

res := tmcli.Run("run", "--quiet", "--parallel", "--", HelperPath, "fibonacci")
res := tmcli.Run("run", "--quiet", "--parallel=5", "--", HelperPath, "fibonacci")
AssertRunResult(t, res, RunExpected{})

b, err := os.ReadFile(s.RootDir() + fmt.Sprintf("/fib.%v/fib.txt", tc.FibN))
Expand Down

0 comments on commit 6d74b99

Please sign in to comment.