Skip to content

Commit

Permalink
chore: add deployment key to provisioning runs in provisioner
Browse files Browse the repository at this point in the history
  • Loading branch information
jvmakine committed Mar 4, 2025
1 parent 6988514 commit 2c1c34d
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 9 deletions.
2 changes: 2 additions & 0 deletions backend/provisioner/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ func RegistryFromConfigFile(ctx context.Context, workingDir string, file *os.Fil

return registry, nil
}

func (s *Service) HandleChangesetPrepared(ctx context.Context, req key.Changeset) error {

_, err := s.schemaClient.CommitChangeset(ctx, connect.NewRequest(&ftlv1.CommitChangesetRequest{Changeset: req.String()}))
Expand All @@ -201,6 +202,7 @@ func (s *Service) HandleChangesetPrepared(ctx context.Context, req key.Changeset
}
return nil
}

func (s *Service) HandleChangesetCommitted(ctx context.Context, req *schema.Changeset) error {
go func() {
time.Sleep(time.Second * 5)
Expand Down
2 changes: 1 addition & 1 deletion cmd/ftl-provisioner-cloudformation/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (c *CloudformationProvisioner) Provision(ctx context.Context, req *connect.
return nil, fmt.Errorf("provisioner already running: %s", stackID)
}
logger.Debugf("Starting task for module %s: %s", req.Msg.DesiredModule.Name, stackID)
task.Start(ctx, module.Name)
task.Start(ctx, module.Name, module.Runtime.Deployment.DeploymentKey)
return connect.NewResponse(&provisionerpb.ProvisionResponse{
Status: provisionerpb.ProvisionResponse_PROVISION_RESPONSE_STATUS_SUBMITTED,
ProvisioningToken: stackID,
Expand Down
6 changes: 5 additions & 1 deletion cmd/ftl-provisioner-cloudformation/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ func (c *CloudformationProvisioner) Status(ctx context.Context, req *connect.Req
// in that case, we start a new task to query the existing stack
task, loaded := c.running.LoadOrStore(token, &provisioner.Task{})
if !loaded {
task.Start(ctx, req.Msg.DesiredModule.Name)
dk, err := key.ParseDeploymentKey(token)
if err != nil {
return nil, fmt.Errorf("failed to parse deployment key: %w", err)
}
task.Start(ctx, req.Msg.DesiredModule.Name, dk)
}

if task.Err() != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/ftl-provisioner-sandbox/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (c *SandboxProvisioner) Provision(ctx context.Context, req *connect.Request
return nil, fmt.Errorf("provisioner already running: %s", token)
}
logger.Debugf("Starting task %s", token)
task.Start(ctx, module.Name)
task.Start(ctx, module.Name, module.Runtime.Deployment.DeploymentKey)
return connect.NewResponse(&provisionerpb.ProvisionResponse{
Status: provisionerpb.ProvisionResponse_PROVISION_RESPONSE_STATUS_SUBMITTED,
ProvisioningToken: token,
Expand Down
6 changes: 6 additions & 0 deletions internal/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import (
"github.com/alecthomas/atomic"
"github.com/alecthomas/types/optional"
"github.com/benbjohnson/clock"
"github.com/block/ftl/internal/key"
)

var _ Interface = (*Logger)(nil)

const scopeKey = "scope"
const moduleKey = "module"
const deploymentKey = "deployment"

type Entry struct {
Time time.Time `json:"-"`
Expand Down Expand Up @@ -54,6 +56,10 @@ func (l Logger) Module(module string) *Logger {
return l.Attrs(map[string]string{moduleKey: module})
}

func (l Logger) Deployment(deployment key.Deployment) *Logger {
return l.Attrs(map[string]string{deploymentKey: deployment.String()})
}

// Attrs creates a new logger with the given attributes.
func (l Logger) Attrs(attributes map[string]string) *Logger {
attr := make(map[string]string, len(l.attributes)+len(attributes))
Expand Down
2 changes: 1 addition & 1 deletion internal/provisioner/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestExecutorSelection(t *testing.T) {
},
}

states, err := runner.Run(ctx, "test")
states, err := runner.Run(ctx)
assert.NoError(t, err)
assert.Equal(t, 2, len(states))

Expand Down
12 changes: 7 additions & 5 deletions internal/provisioner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/alecthomas/atomic"
"golang.org/x/sync/errgroup"

"github.com/block/ftl/internal/key"
"github.com/block/ftl/internal/log"
"github.com/block/ftl/internal/provisioner/state"
)
Expand Down Expand Up @@ -41,8 +42,8 @@ type Runner struct {
Stages []RunnerStage
}

func (r *Runner) Run(ctx context.Context, module string) ([]state.State, error) {
logger := log.FromContext(ctx).Module(module)
func (r *Runner) Run(ctx context.Context) ([]state.State, error) {
logger := log.FromContext(ctx)

for _, stage := range r.Stages {
logger.Debugf("running stage %s", stage.Name)
Expand Down Expand Up @@ -120,11 +121,12 @@ func (r *Runner) execute(ctx context.Context, stage *RunnerStage) ([]state.State
return result, nil
}

func (t *Task) Start(oldCtx context.Context, module string) {
func (t *Task) Start(oldCtx context.Context, module string, deployment key.Deployment) {
ctx := context.WithoutCancel(oldCtx)
logger := log.FromContext(ctx).Module(module)
logger := log.FromContext(ctx).Module(module).Deployment(deployment)
ctx = log.ContextWithLogger(ctx, logger)
go func() {
outputs, err := t.runner.Run(ctx, module)
outputs, err := t.runner.Run(ctx)
if err != nil {
logger.Errorf(err, "failed to execute provisioner")
t.err.Store(err)
Expand Down

0 comments on commit 2c1c34d

Please sign in to comment.