Skip to content

Commit

Permalink
feat(tmc): send interpolated command in each drift sync (#1152)
Browse files Browse the repository at this point in the history
  • Loading branch information
i4k-tm committed Sep 21, 2023
2 parents 3c25c99 + 1a62ac1 commit 3a6e6f0
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 21 deletions.
1 change: 1 addition & 0 deletions cloud/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ type (
Stack Stack `json:"stack"`
Status stack.Status `json:"drift_status"`
Metadata *DeploymentMetadata `json:"metadata,omitempty"`
Command []string `json:"command"`
}

// DriftStackPayloadRequests is a list of DriftStackPayloadRequest
Expand Down
12 changes: 6 additions & 6 deletions cmd/terramate/cli/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,28 +174,28 @@ func (c *cli) setupCloudConfig() error {
return nil
}

func (c *cli) cloudSyncBefore(s *config.Stack, _ string) {
func (c *cli) cloudSyncBefore(run ExecContext, _ string) {
if !c.cloudEnabled() || !c.parsedArgs.Run.CloudSyncDeployment {
return
}
c.doCloudSyncDeployment(s, deployment.Running)
c.doCloudSyncDeployment(run, deployment.Running)
}

func (c *cli) cloudSyncAfter(s *config.Stack, exitCode int, err error) {
func (c *cli) cloudSyncAfter(runContext ExecContext, exitCode int, err error) {
if !c.cloudEnabled() || !c.isCloudSync() {
return
}

if c.parsedArgs.Run.CloudSyncDeployment {
c.cloudSyncDeployment(s, err)
c.cloudSyncDeployment(runContext, err)
} else {
c.cloudSyncDriftStatus(s, exitCode, err)
c.cloudSyncDriftStatus(runContext, exitCode, err)
}
}

func (c *cli) cloudSyncCancelStacks(stacks []ExecContext) {
for _, run := range stacks {
c.cloudSyncAfter(run.Stack, -1, errors.E(ErrRunCanceled))
c.cloudSyncAfter(run, -1, errors.E(ErrRunCanceled))
}
}

Expand Down
14 changes: 7 additions & 7 deletions cmd/terramate/cli/cloud_sync_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/rs/zerolog/log"
"github.com/terramate-io/terramate/cloud"
"github.com/terramate-io/terramate/cloud/deployment"
"github.com/terramate-io/terramate/config"
"github.com/terramate-io/terramate/errors"
prj "github.com/terramate-io/terramate/project"
)
Expand Down Expand Up @@ -124,7 +123,7 @@ func (c *cli) createCloudDeployment(runStacks []ExecContext) {
}
}

func (c *cli) cloudSyncDeployment(s *config.Stack, err error) {
func (c *cli) cloudSyncDeployment(runContext ExecContext, err error) {
var status deployment.Status
switch {
case err == nil:
Expand All @@ -137,17 +136,18 @@ func (c *cli) cloudSyncDeployment(s *config.Stack, err error) {
panic(errors.E(errors.ErrInternal, "unexpected run status"))
}

c.doCloudSyncDeployment(s, status)
c.doCloudSyncDeployment(runContext, status)
}

func (c *cli) doCloudSyncDeployment(s *config.Stack, status deployment.Status) {
func (c *cli) doCloudSyncDeployment(runContext ExecContext, status deployment.Status) {
st := runContext.Stack
logger := log.With().
Str("organization", c.cloud.run.orgUUID).
Str("stack", s.RelPath()).
Str("stack", st.RelPath()).
Stringer("status", status).
Logger()

stackID, ok := c.cloud.run.meta2id[s.ID]
stackID, ok := c.cloud.run.meta2id[st.ID]
if !ok {
logger.Error().Msg("unable to update deployment status due to invalid API response")
return
Expand All @@ -168,7 +168,7 @@ func (c *cli) doCloudSyncDeployment(s *config.Stack, status deployment.Status) {
defer cancel()
err := c.cloud.client.UpdateDeploymentStacks(ctx, c.cloud.run.orgUUID, c.cloud.run.runUUID, payload)
if err != nil {
logger.Err(err).Str("stack_id", s.ID).Msg("failed to update deployment status for each")
logger.Err(err).Str("stack_id", st.ID).Msg("failed to update deployment status for each")
} else {
logger.Debug().Msg("deployment status synced successfully")
}
Expand Down
7 changes: 5 additions & 2 deletions cmd/terramate/cli/cloud_sync_drift.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import (
"github.com/terramate-io/terramate/cloud"
"github.com/terramate-io/terramate/cloud/stack"
"github.com/terramate-io/terramate/cmd/terramate/cli/clitest"
"github.com/terramate-io/terramate/config"
"github.com/terramate-io/terramate/errors"
)

func (c *cli) cloudSyncDriftStatus(st *config.Stack, exitCode int, err error) {
func (c *cli) cloudSyncDriftStatus(runContext ExecContext, exitCode int, err error) {
st := runContext.Stack

logger := log.With().
Str("action", "cloudSyncDriftStatus").
Stringer("stack", st.Dir).
Int("exit_code", exitCode).
Strs("command", runContext.Cmd).
Err(err).
Logger()

Expand Down Expand Up @@ -54,6 +56,7 @@ func (c *cli) cloudSyncDriftStatus(st *config.Stack, exitCode int, err error) {
},
Status: status,
Metadata: c.cloud.run.metadata,
Command: runContext.Cmd,
})

if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions cmd/terramate/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,12 @@ func (c *cli) RunAll(runStacks []ExecContext, isSuccessCode func(exitCode int) b
Stringer("stack", runContext.Stack).
Logger()

c.cloudSyncBefore(runContext.Stack, cmdStr)
c.cloudSyncBefore(runContext, cmdStr)

environ := newEnvironFrom(stackEnvs[runContext.Stack.Dir])
cmdPath, err := run.LookPath(runContext.Cmd[0], environ)
if err != nil {
c.cloudSyncAfter(runContext.Stack, -1, errors.E(ErrRunCommandNotFound, err))
c.cloudSyncAfter(runContext, -1, errors.E(ErrRunCommandNotFound, err))
errs.Append(errors.E(err, "running `%s` in stack %s", cmdStr, runContext.Stack.Dir))
if continueOnError {
continue
Expand All @@ -212,7 +212,7 @@ func (c *cli) RunAll(runStacks []ExecContext, isSuccessCode func(exitCode int) b
logger.Info().Msg("running")

if err := cmd.Start(); err != nil {
c.cloudSyncAfter(runContext.Stack, -1, errors.E(err, ErrRunFailed))
c.cloudSyncAfter(runContext, -1, errors.E(err, ErrRunFailed))
errs.Append(errors.E(err, "running %s (at stack %s)", cmd, runContext.Stack.Dir))
logger.Error().Err(err).Msg("failed to execute")
if continueOnError {
Expand Down Expand Up @@ -243,7 +243,7 @@ func (c *cli) RunAll(runStacks []ExecContext, isSuccessCode func(exitCode int) b
logger.Debug().Err(err).Msg("unable to send kill signal to child process")
}

c.cloudSyncAfter(runContext.Stack, -1, errors.E(ErrRunCanceled))
c.cloudSyncAfter(runContext, -1, errors.E(ErrRunCanceled))
c.cloudSyncCancelStacks(runStacks[i+1:])

return errors.E(ErrRunCanceled, "execution aborted by CTRL-C (3x)")
Expand All @@ -257,7 +257,7 @@ func (c *cli) RunAll(runStacks []ExecContext, isSuccessCode func(exitCode int) b
logger.Error().Err(err).Msg("failed to execute")
}

c.cloudSyncAfter(runContext.Stack, result.cmd.ProcessState.ExitCode(), err)
c.cloudSyncAfter(runContext, result.cmd.ProcessState.ExitCode(), err)
cmdIsRunning = false
}
}
Expand Down
9 changes: 8 additions & 1 deletion cmd/terramate/e2etests/run_cloud_drift_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/madlambda/spells/assert"
"github.com/terramate-io/terramate/cloud"
"github.com/terramate-io/terramate/cloud/stack"
Expand Down Expand Up @@ -276,7 +277,13 @@ func assertRunDrifts(t *testing.T, expectedDrifts cloud.DriftStackPayloadRequest
}, "GET", cloud.DriftsPath+"/"+testserver.DefaultOrgUUID, nil)
assert.NoError(t, err)

if diff := cmp.Diff(res, expectedDrifts); diff != "" {
// TODO(i4k): skip checking interpolated commands for now because of the hack
// for making the --eval work with the helper binary in a portable way.
// We can't portably predict the command because when using --eval the
// whole argument list is interpolated, including the program name, and then
// on Windows it requires a special escaped string.
// See variable `testHelperBinAsHCL`.
if diff := cmp.Diff(res, expectedDrifts, cmpopts.IgnoreFields(cloud.DriftStackPayloadRequest{}, "Command")); diff != "" {
t.Logf("want: %+v", expectedDrifts)
t.Logf("got: %+v", res)
t.Fatal(diff)
Expand Down

0 comments on commit 3a6e6f0

Please sign in to comment.