Skip to content

Commit

Permalink
Add initial support for Runner type which can perform runs in parallel
Browse files Browse the repository at this point in the history
This is part of a feature that will enable running Terraform from the TUI that
is coming shortly after this.
  • Loading branch information
jlarfors committed Jul 6, 2022
1 parent ac1cb5d commit 5112fc5
Show file tree
Hide file tree
Showing 25 changed files with 679 additions and 560 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*.dll
*.so
*.dylib
*.DS_Store

# Test binary, built with `go test -c`
*.test
Expand Down Expand Up @@ -43,5 +44,8 @@ tutorials/**/*.*
tutorials/**/*.tp.tf
tutorials/**/terraplate.tf

# Ignore any default plans that exist in a testData directory
**/testData/**/tfplan

# goreleaser build directory
dist/
24 changes: 13 additions & 11 deletions builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,21 @@ func buildTerraplate(terrafile *parser.Terrafile, config *parser.TerraConfig) er
// changes each time.
// Iterate over the sorted keys and then extract the value for that key
provMap := terrafile.TerraformBlock.RequiredProviders()
for _, name := range sortedMapKeys(provMap) {
value := provMap[name]
ctyType, typeErr := gocty.ImpliedType(value)
if typeErr != nil {
return fmt.Errorf("implying required provider to cty type for provider %s: %w", name, typeErr)
}
ctyValue, ctyErr := gocty.ToCtyValue(value, ctyType)
if ctyErr != nil {
return fmt.Errorf("converting required provider to cty value for provider %s: %w", name, ctyErr)
if len(provMap) > 0 {
for _, name := range sortedMapKeys(provMap) {
value := provMap[name]
ctyType, typeErr := gocty.ImpliedType(value)
if typeErr != nil {
return fmt.Errorf("implying required provider to cty type for provider %s: %w", name, typeErr)
}
ctyValue, ctyErr := gocty.ToCtyValue(value, ctyType)
if ctyErr != nil {
return fmt.Errorf("converting required provider to cty value for provider %s: %w", name, ctyErr)
}
provBlock.Body().SetAttributeValue(name, ctyValue)
}
provBlock.Body().SetAttributeValue(name, ctyValue)
tfBlock.Body().AppendBlock(provBlock)
}
tfBlock.Body().AppendBlock(provBlock)
// If body is not empty, write the terraform block
if isBodyEmpty(tfBlock.Body()) {
tfFile.Body().AppendBlock(tfBlock)
Expand Down
6 changes: 3 additions & 3 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ var applyCmd = &cobra.Command{
if err != nil {
return fmt.Errorf("parsing terraplate: %w", err)
}
result := runner.Run(config, runner.RunApply(), runner.Jobs(applyJobs), runner.ExtraArgs(args))
runner := runner.Run(config, runner.RunApply(), runner.Jobs(applyJobs), runner.ExtraArgs(args))

// Print log
fmt.Println(result.Log())
fmt.Println(runner.Log())

return result.Errors()
return runner.Errors()
},
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ templates and configurations detected.`,
fmt.Print(buildSuccessMessage)

if doValidate {
result := runner.Run(config, runner.RunInit(), runner.RunValidate())
runner := runner.Run(config, runner.RunInit(), runner.RunValidate())
// Print log
fmt.Println(result.Log())
fmt.Println(runner.Log())

if result.HasError() {
return result.Errors()
if runner.HasError() {
return runner.Errors()
}
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/drift.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ var driftCmd = &cobra.Command{
fmt.Print(buildSuccessMessage)
// Plan
fmt.Print(terraformStartMessage)
runOpts := []func(r *runner.TerraRun){
runOpts := []func(r *runner.TerraRunOpts){
runner.RunInit(),
runner.RunPlan(),
runner.RunShowPlan(),
runner.Jobs(planJobs),
}
runOpts = append(runOpts, runner.ExtraArgs(args))
result := runner.Run(config, runOpts...)
runner := runner.Run(config, runOpts...)

if notifyService != nil {
repo, repoErr := notify.LookupRepo(
Expand All @@ -83,7 +83,7 @@ var driftCmd = &cobra.Command{
return fmt.Errorf("looking up repository details: %w", repoErr)
}
sendErr := notifyService.Send(&notify.Data{
Result: result,
Runner: runner,
Repo: repo,
ResultsURL: notifyResultsUrl,
})
Expand All @@ -92,7 +92,7 @@ var driftCmd = &cobra.Command{
}
}

fmt.Print(result.PlanSummary())
fmt.Print(runner.PlanSummary())
return nil
},
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ var initCmd = &cobra.Command{
if err != nil {
return fmt.Errorf("parsing terraplate: %w", err)
}
result := runner.Run(config, runner.RunInit(), runner.Jobs(initJobs), runner.ExtraArgs(args))
runner := runner.Run(config, runner.RunInit(), runner.Jobs(initJobs), runner.ExtraArgs(args))
// Print log
fmt.Println(result.Log())
fmt.Println(runner.Log())

return result.Errors()
return runner.Errors()
},
}

Expand Down
10 changes: 5 additions & 5 deletions cmd/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var planCmd = &cobra.Command{
fmt.Print(buildSuccessMessage)
}
fmt.Print(terraformStartMessage)
runOpts := []func(r *runner.TerraRun){
runOpts := []func(r *runner.TerraRunOpts){
runner.RunPlan(),
runner.RunShowPlan(),
runner.Jobs(planJobs),
Expand All @@ -57,14 +57,14 @@ var planCmd = &cobra.Command{
runOpts = append(runOpts, runner.RunInit())
}
runOpts = append(runOpts, runner.ExtraArgs(args))
result := runner.Run(config, runOpts...)
runner := runner.Run(config, runOpts...)

// Print log
fmt.Println(result.Log())
fmt.Println(runner.Log())

fmt.Println(result.PlanSummary())
fmt.Println(runner.PlanSummary())

return result.Errors()
return runner.Errors()
},
}

Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/spf13/cobra v1.3.0
github.com/stretchr/testify v1.7.0
github.com/zclconf/go-cty v1.10.0
gotest.tools v2.2.0+incompatible
golang.org/x/text v0.3.7
)

require (
Expand Down Expand Up @@ -47,7 +47,6 @@ require (
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
Expand All @@ -57,8 +56,8 @@ require (
github.com/xanzy/ssh-agent v0.3.0 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d // indirect
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -654,10 +654,12 @@ golang.org/x/sys v0.0.0-20210908233432-aa78b53d3365/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d h1:FjkYO/PPp4Wi0EAUOVLxePm7qVW4r4ctbWpURyuOD0E=
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 h1:rm+CHSpPEEW2IsXUib1ThaHIjuBVZjxNgSKmBLFfD4c=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down Expand Up @@ -895,8 +897,6 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
4 changes: 2 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ func TestMain(t *testing.T) {
require.NoError(t, buildErr)

if !tc.skipTerraform {
result := runner.Run(config,
runner := runner.Run(config,
runner.RunValidate(),
runner.RunInit(),
runner.RunPlan(),
runner.RunApply())

require.NoError(t, result.Errors())
require.NoError(t, runner.Errors())
}
})
}
Expand Down
6 changes: 3 additions & 3 deletions notify/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ var (
)

type Data struct {
Result *runner.Result
Runner *runner.Runner
Repo *Repo
ResultsURL string
}

func (d Data) StatusColor() string {
switch {
case d.Result.HasError():
case d.Runner.HasError():
return statusErrorColor
case d.Result.HasDrift():
case d.Runner.HasDrift():
return statusDrftColor
}
return statusSyncColor
Expand Down
6 changes: 3 additions & 3 deletions notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ func (f NotifyFilter) IsValid() bool {
return false
}

// ShouldNotify takes a result and returns a bool whether the notification should
// ShouldNotify takes a runner and returns a bool whether the notification should
// be sent, or not
func (f NotifyFilter) ShouldNotify(result *runner.Result) bool {
func (f NotifyFilter) ShouldNotify(runner *runner.Runner) bool {
switch f {
case NotifyFilterAll:
return true
case NotifyFilterDrift:
if result.HasError() || result.HasDrift() {
if runner.HasError() || runner.HasDrift() {
return true
}
}
Expand Down
2 changes: 1 addition & 1 deletion notify/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type slackMsgJSON struct {
}

func (s *slackService) Send(data *Data) error {
if !s.NotifyFilter.ShouldNotify(data.Result) {
if !s.NotifyFilter.ShouldNotify(data.Runner) {
fmt.Print(notifyNoDriftMessage)
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion parser/terraconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (c *TerraConfig) MergeTerrafiles() error {

for _, rootTf := range rootTfs {
// Set defaults for root terrafile
if err := mergo.Merge(rootTf, defaultTerrafile); err != nil {
if err := mergo.Merge(rootTf, DefaultTerrafile); err != nil {
return fmt.Errorf("setting defaults for root terrafile %s: %w", rootTf.Path, err)
}

Expand Down
4 changes: 2 additions & 2 deletions parser/terrafile.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
ctyjson "github.com/zclconf/go-cty/cty/json"
)

// defaultTerrafile sets default values for a Terrafile that are used when
// DefaultTerrafile sets default values for a Terrafile that are used when
// parsing a new Terrafile
var defaultTerrafile = Terrafile{
var DefaultTerrafile = Terrafile{
// BuildBlock: &BuildBlock{},
ExecBlock: &ExecBlock{
PlanBlock: &ExecPlanBlock{
Expand Down
Loading

0 comments on commit 5112fc5

Please sign in to comment.