Skip to content

Commit

Permalink
[cli] Add the ability to control auto-refresh of stacks by Pulumi.yaml
Browse files Browse the repository at this point in the history
Fixes: #8058

A user can now set `autoRefresh: true` in Pulumi.yaml to make all
derivative stacks to refresh by default when running an update, preview
or destroy command. We also introduce a new `--skip-refresh` variable
to be used in conjunction with the Pulumi.yaml update

the specific CLI commands still override this argument. Therefore:

* If a user sets autoRefresh: true, they can override this behaviour with `--skip-refresh`
* if a user sets `--refresh` then it will use that by default
* if no cli args are passed but `autoRefresh: true` then we will perform a refresh
* the default behaviour still exists of no refresh
  • Loading branch information
stack72 committed Sep 27, 2021
1 parent 6117cde commit 54b7eb3
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
- [CLI] Enable output values in the engine by default.
[#8014](https://github.com/pulumi/pulumi/pull/8014)

- [CLI] Adding the ability to set `autoRefresh` at a Pulumi.yaml level
to allow a user to be able to auto refresh their derivative stacks by default
[#8071](https://github.com/pulumi/pulumi/pull/8071)

### Bug Fixes

- [automation/python] Fix a bug in printing `Stack` if no program is provided.
Expand Down
7 changes: 6 additions & 1 deletion pkg/cmd/pulumi/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func newDestroyCmd() *cobra.Command {
var eventLogPath string
var parallel int
var refresh bool
var skipRefresh bool
var showConfig bool
var showReplacementSteps bool
var showSames bool
Expand Down Expand Up @@ -146,7 +147,7 @@ func newDestroyCmd() *cobra.Command {
opts.Engine = engine.UpdateOptions{
Parallel: parallel,
Debug: debug,
Refresh: refresh,
Refresh: getRefreshOption(proj, refresh, skipRefresh),
DestroyTargets: targetUrns,
TargetDependents: targetDependents,
UseLegacyDiff: useLegacyDiff(),
Expand Down Expand Up @@ -207,6 +208,10 @@ func newDestroyCmd() *cobra.Command {
cmd.PersistentFlags().BoolVarP(
&refresh, "refresh", "r", false,
"Refresh the state of the stack's resources before this update")
cmd.PersistentFlags().BoolVar(
&skipRefresh, "skip-refresh", false,
"Skip the refreshing of the state of the stack's resources before this update. Typically used "+
"when overriding a Pulumi project setting")
cmd.PersistentFlags().BoolVar(
&showConfig, "show-config", false,
"Show configuration keys and variables")
Expand Down
7 changes: 6 additions & 1 deletion pkg/cmd/pulumi/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func newPreviewCmd() *cobra.Command {
var eventLogPath string
var parallel int
var refresh bool
var skipRefresh bool
var showConfig bool
var showReplacementSteps bool
var showSames bool
Expand Down Expand Up @@ -165,7 +166,7 @@ func newPreviewCmd() *cobra.Command {
LocalPolicyPacks: engine.MakeLocalPolicyPacks(policyPackPaths, policyPackConfigPaths),
Parallel: parallel,
Debug: debug,
Refresh: refresh,
Refresh: getRefreshOption(proj, refresh, skipRefresh),
ReplaceTargets: replaceURNs,
UseLegacyDiff: useLegacyDiff(),
DisableProviderPreview: disableProviderPreview(),
Expand Down Expand Up @@ -259,6 +260,10 @@ func newPreviewCmd() *cobra.Command {
cmd.PersistentFlags().BoolVarP(
&refresh, "refresh", "r", false,
"Refresh the state of the stack's resources before this update")
cmd.PersistentFlags().BoolVar(
&skipRefresh, "skip-refresh", false,
"Skip the refreshing of the state of the stack's resources before this update. Typically used "+
"when overriding a Pulumi project setting")
cmd.PersistentFlags().BoolVar(
&showConfig, "show-config", false,
"Show configuration keys and variables")
Expand Down
9 changes: 7 additions & 2 deletions pkg/cmd/pulumi/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func newUpCmd() *cobra.Command {
var eventLogPath string
var parallel int
var refresh bool
var skipRefresh bool
var showConfig bool
var showReplacementSteps bool
var showSames bool
Expand Down Expand Up @@ -126,7 +127,7 @@ func newUpCmd() *cobra.Command {
LocalPolicyPacks: engine.MakeLocalPolicyPacks(policyPackPaths, policyPackConfigPaths),
Parallel: parallel,
Debug: debug,
Refresh: refresh,
Refresh: getRefreshOption(proj, refresh, skipRefresh),
RefreshTargets: targetURNs,
ReplaceTargets: replaceURNs,
UseLegacyDiff: useLegacyDiff(),
Expand Down Expand Up @@ -291,7 +292,7 @@ func newUpCmd() *cobra.Command {
LocalPolicyPacks: engine.MakeLocalPolicyPacks(policyPackPaths, policyPackConfigPaths),
Parallel: parallel,
Debug: debug,
Refresh: refresh,
Refresh: getRefreshOption(proj, refresh, skipRefresh),
}

// TODO for the URL case:
Expand Down Expand Up @@ -461,6 +462,10 @@ func newUpCmd() *cobra.Command {
cmd.PersistentFlags().BoolVarP(
&refresh, "refresh", "r", false,
"Refresh the state of the stack's resources before this update")
cmd.PersistentFlags().BoolVar(
&skipRefresh, "skip-refresh", false,
"Skip the refreshing of the state of the stack's resources before this update. Typically used "+
"when overriding a Pulumi project setting")
cmd.PersistentFlags().BoolVar(
&showConfig, "show-config", false,
"Show configuration keys and variables")
Expand Down
20 changes: 20 additions & 0 deletions pkg/cmd/pulumi/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,3 +839,23 @@ func checkDeploymentVersionError(err error, stackName string) error {
}
return errors.Wrap(err, "could not deserialize deployment")
}

func getRefreshOption(proj *workspace.Project, refresh, skipRefresh bool) bool {
// the user has specified `--refresh` so this is the most specific action
if refresh {
return true
}

// the user has specified `--skip-refresh`
if skipRefresh {
return false
}

// the user has not specifically asked for a refresh or to skip-refresh
if proj.AutoRefresh != nil {
return *proj.AutoRefresh
}

// the default functionality right now is to always skip a refresh
return false
}
60 changes: 59 additions & 1 deletion pkg/cmd/pulumi/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import (
"os"
"testing"

"github.com/stretchr/testify/assert"

"github.com/pulumi/pulumi/pkg/v3/backend"
pul_testing "github.com/pulumi/pulumi/sdk/v3/go/common/testing"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/gitutil"
"github.com/stretchr/testify/assert"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
)

// assertEnvValue assert the update metadata's Environment map contains the given value.
Expand Down Expand Up @@ -264,3 +266,59 @@ func Test_makeJSONString(t *testing.T) {
})
}
}

func TestGetRefreshOption(t *testing.T) {
x := true
tests := []struct {
name string
refresh bool
skipRefresh bool
project workspace.Project
expectedRefreshState bool
}{
{
"No options specified means no refresh",
false,
false,
workspace.Project{},
false,
},
{
"Passing --refresh causes a refresh",
true,
false,
workspace.Project{},
true,
},
{
"Setting Refresh at a project level via Pulumi.yaml and no CLI args",
false,
false,
workspace.Project{
Name: "auto-refresh",
Runtime: workspace.ProjectRuntimeInfo{},
AutoRefresh: &x,
},
true,
},
{
"Setting Refresh at a project level via Pulumi.yaml and --skip-refresh",
false,
true,
workspace.Project{
Name: "auto-refresh",
Runtime: workspace.ProjectRuntimeInfo{},
AutoRefresh: &x,
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
shouldRefresh := getRefreshOption(&tt.project, tt.refresh, tt.skipRefresh)
if shouldRefresh != tt.expectedRefreshState {
t.Errorf("getRefreshOption got = %t, expected %vt", shouldRefresh, tt.expectedRefreshState)
}
})
}
}
3 changes: 3 additions & 0 deletions sdk/go/common/workspace/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ type Project struct {

// Backend is an optional backend configuration
Backend *ProjectBackend `json:"backend,omitempty" yaml:"backend,omitempty"`

// AutoRefresh is the ability to always run a refresh as part of a pulumi update / preview / destroy
AutoRefresh *bool `json:"autoRefresh,omitempty" yaml:"autoRefresh,omitempty"`
}

func (proj *Project) Validate() error {
Expand Down

0 comments on commit 54b7eb3

Please sign in to comment.