Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for [deploy] seed_command #4139

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/appconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ type Deploy struct {
ReleaseCommand string `toml:"release_command,omitempty" json:"release_command,omitempty"`
ReleaseCommandTimeout *fly.Duration `toml:"release_command_timeout,omitempty" json:"release_command_timeout,omitempty"`
ReleaseCommandCompute *Compute `toml:"release_command_vm,omitempty" json:"release_command_vm,omitempty"`
SeedCommand string `toml:"seed_command,omitempty" json:"seed_command,omitempty"`
}

type File struct {
Expand Down
15 changes: 15 additions & 0 deletions internal/appconfig/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const (
_ contextKeyType = iota
configContextKey
nameContextKey
seedContextKey
)

// WithConfig derives a context that carries cfg from ctx.
Expand Down Expand Up @@ -39,3 +40,17 @@ func NameFromContext(ctx context.Context) string {

return ""
}

// WithSeed derives a context that carries the given seed from ctx.
func WithSeedCommand(ctx context.Context, seedCommand string) context.Context {
return context.WithValue(ctx, seedContextKey, seedCommand)
}

// SeedFromContext returns the seed ctx carries or an empty string.
func SeedCommandFromContext(ctx context.Context) string {
if seed, ok := ctx.Value(seedContextKey).(string); ok {
return seed
}

return ""
}
2 changes: 1 addition & 1 deletion internal/command/deploy/machines_deploymachinesapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ func (md *machineDeployment) deployMachinesApp(ctx context.Context) error {
defer span.End()

if !md.skipReleaseCommand {
if err := md.runReleaseCommand(ctx); err != nil {
if err := md.runReleaseCommands(ctx); err != nil {
return fmt.Errorf("release command failed - aborting deployment. %w", err)
}
}
Expand Down
44 changes: 31 additions & 13 deletions internal/command/deploy/machines_releasecommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,38 @@ import (
"golang.org/x/sync/errgroup"
)

func (md *machineDeployment) runReleaseCommand(ctx context.Context) (err error) {
ctx, span := tracing.GetTracer().Start(ctx, "run_release_cmd")
func (md *machineDeployment) runReleaseCommands(ctx context.Context) error {
err := md.runReleaseCommand(ctx, "release")

if err == nil {
seedCommand := appconfig.SeedCommandFromContext(ctx)

if seedCommand != "" {
md.appConfig.Deploy.ReleaseCommand = seedCommand
err = md.runReleaseCommand(ctx, "seed")
}
}

return err
}

func (md *machineDeployment) runReleaseCommand(ctx context.Context, commandType string) (err error) {
ctx, span := tracing.GetTracer().Start(ctx, "run_"+commandType+"_cmd")
defer func() {
if err != nil {
tracing.RecordError(span, err, "failed to run release_cmd")
tracing.RecordError(span, err, "failed to run "+commandType+"_cmd")
}
span.End()
}()

if md.appConfig.Deploy == nil || md.appConfig.Deploy.ReleaseCommand == "" {
span.AddEvent("no release command")
span.AddEvent("no " + commandType + " command")
return nil
}

fmt.Fprintf(md.io.ErrOut, "Running %s release_command: %s\n",
fmt.Fprintf(md.io.ErrOut, "Running %s %s_command: %s\n",
md.colorize.Bold(md.app.Name),
commandType,
md.appConfig.Deploy.ReleaseCommand,
)
ctx, loggerCleanup := statuslogger.SingleLine(ctx, true)
Expand All @@ -64,8 +80,8 @@ func (md *machineDeployment) runReleaseCommand(ctx context.Context) (err error)
eg.Go(func() error {
err := md.createOrUpdateReleaseCmdMachine(groupCtx)
if err != nil {
tracing.RecordError(span, err, "failed to create release cmd machine")
return fmt.Errorf("error running release_command machine: %w", err)
tracing.RecordError(span, err, "failed to create "+commandType+" cmd machine")
return fmt.Errorf("error running %s_command machine: %w", commandType, err)
}
return nil
})
Expand Down Expand Up @@ -117,32 +133,32 @@ func (md *machineDeployment) runReleaseCommand(ctx context.Context) (err error)
fmt.Fprintln(md.io.ErrOut, "Starting machine")

if err = releaseCmdMachine.Start(ctx); err != nil {
fmt.Fprintf(md.io.ErrOut, "error starting release_command machine: %v\n", err)
fmt.Fprintf(md.io.ErrOut, "error starting %s_command machine: %v\n", commandType, err)
return
}

// FIXME: consolidate this wait stuff with deploy waits? Especially once we improve the outpu
err = md.waitForReleaseCommandToFinish(ctx, releaseCmdMachine)
if err != nil {
tracing.RecordError(span, err, "failed to wait for release cmd machine")
tracing.RecordError(span, err, "failed to wait for "+commandType+" cmd machine")

return err
}
lastExitEvent, err := releaseCmdMachine.WaitForEventTypeAfterType(ctx, "exit", "start", md.releaseCmdTimeout, true)
if err != nil {
return fmt.Errorf("error finding the release_command machine %s exit event: %w", releaseCmdMachine.Machine().ID, err)
return fmt.Errorf("error finding the %s_command machine %s exit event: %w", commandType, releaseCmdMachine.Machine().ID, err)
}
exitCode, err := lastExitEvent.Request.GetExitCode()
if err != nil {
return fmt.Errorf("error get release_command machine %s exit code: %w", releaseCmdMachine.Machine().ID, err)
return fmt.Errorf("error get %s_command machine %s exit code: %w", commandType, releaseCmdMachine.Machine().ID, err)
}

if flag.GetBool(ctx, "verbose") {
waitForLogs(md, logsCtx, stream, releaseCmdMachine.Machine().ID)
}

if exitCode != 0 {
statuslogger.LogStatus(ctx, statuslogger.StatusFailure, "release_command failed")
statuslogger.LogStatus(ctx, statuslogger.StatusFailure, commandType+"_command failed")

// Preemptive cleanup of the logger so that the logs have a clean place to write to
loggerCleanup(false)
Expand All @@ -163,7 +179,8 @@ func (md *machineDeployment) runReleaseCommand(ctx context.Context) (err error)
}
statuslogger.LogfStatus(ctx,
statuslogger.StatusSuccess,
"release_command %s completed successfully",
"%s_command %s completed successfully",
commandType,
md.colorize.Bold(releaseCmdMachine.Machine().ID),
)
return nil
Expand Down Expand Up @@ -332,5 +349,6 @@ func (md *machineDeployment) waitForReleaseCommandToFinish(ctx context.Context,
err = suggestChangeWaitTimeout(err, "release-command-timeout")
return fmt.Errorf("error waiting for release_command machine %s to finish running: %w", releaseCmdMachine.Machine().ID, err)
}
md.releaseCommandMachine.RemoveMachines(ctx, []machine.LeasableMachine{releaseCmdMachine})
return nil
}
4 changes: 4 additions & 0 deletions internal/command/launch/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ func (state *launchState) Launch(ctx context.Context) error {
}

if state.sourceInfo != nil {
if state.appConfig.Deploy != nil && state.appConfig.Deploy.SeedCommand != "" {
ctx = appconfig.WithSeedCommand(ctx, state.appConfig.Deploy.SeedCommand)
}

if err := state.firstDeploy(ctx); err != nil {
return err
}
Expand Down
9 changes: 9 additions & 0 deletions internal/command/launch/launch_frameworks.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ func (state *launchState) scannerRunCallback(ctx context.Context) error {
state.sourceInfo.ReleaseCmd = cfg.Deploy.ReleaseCommand
}

if state.sourceInfo.SeedCmd == "" && cfg.Deploy != nil {
state.sourceInfo.SeedCmd = cfg.Deploy.SeedCommand
}

if len(cfg.Env) > 0 {
if len(state.sourceInfo.Env) == 0 {
state.sourceInfo.Env = cfg.Env
Expand Down Expand Up @@ -326,6 +330,11 @@ func (state *launchState) scannerSetAppconfig(ctx context.Context) error {
appConfig.SetReleaseCommand(srcInfo.ReleaseCmd)
}

if srcInfo.SeedCmd != "" {
// no V1 compatibility for this feature so bypass setters
appConfig.Deploy.SeedCommand = srcInfo.SeedCmd
}

if srcInfo.DockerCommand != "" {
appConfig.SetDockerCommand(srcInfo.DockerCommand)
}
Expand Down
1 change: 1 addition & 0 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type SourceInfo struct {
BuildArgs map[string]string
Builder string
ReleaseCmd string
SeedCmd string
DockerCommand string
DockerEntrypoint string
KillSignal string
Expand Down
Loading