From 6ec1c72825262b7e7e80a373ddba93662ebd36c9 Mon Sep 17 00:00:00 2001 From: Brad Sickles Date: Thu, 21 Nov 2024 15:18:30 -0500 Subject: [PATCH] Created fallback when we cannot find app deployment workflow. --- CHANGELOG.md | 3 +++ cmd/deploy.go | 10 ++++++++-- cmd/wait_for.go | 27 +++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56e53e4..014aaeb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 0.0.130 (Nov 21, 2024) +* Created fallback when we cannot find app deployment workflow. + # 0.0.129 (Nov 12, 2024) * Added `nullstone iac generate` command to export workspace config to an IaC file. diff --git a/cmd/deploy.go b/cmd/deploy.go index c02b3dd..5eaa61b 100644 --- a/cmd/deploy.go +++ b/cmd/deploy.go @@ -3,6 +3,7 @@ package cmd import ( "context" "fmt" + "github.com/mitchellh/colorstring" "github.com/nullstone-io/deployment-sdk/app" "github.com/nullstone-io/deployment-sdk/logging" "github.com/urfave/cli/v2" @@ -113,10 +114,12 @@ func streamDeployIntentLogs(ctx context.Context, osWriters logging.OsWriters, cf if iw, err = waitForRunningIntentWorkflow(ctx, cfg, iw); err != nil { return err } else if iw.Status == types.IntentWorkflowStatusCompleted { - fmt.Fprintln(stderr, "Deployment completed.") + colorstring.Fprintln(stderr, "[green]Deployment completed.[reset]") return nil } + colorstring.Fprintln(stderr, "[green]Deployment started.[reset]") + var wflow types.WorkspaceWorkflow for _, ww := range iw.WorkspaceWorkflows { if ww.BlockId == appDetails.App.Id && ww.EnvId == appDetails.Env.Id && ww.StackId == appDetails.App.StackId { @@ -125,7 +128,10 @@ func streamDeployIntentLogs(ctx context.Context, osWriters logging.OsWriters, cf } } if wflow.Id == 0 { - return fmt.Errorf("deployment workflow is missing") + colorstring.Fprintln(stderr, "[yellow]We cannot find the deployment workflow to report the deployment logs.[reset]") + colorstring.Fprintln(stderr, "[bold]Waiting for successful deployment without reporting deployment logs...[reset]") + iw, err = waitForCompletedIntentWorkflow(ctx, cfg, iw) + return err } activities, err := client.WorkspaceWorkflows().GetActivities(ctx, wflow.StackId, wflow.BlockId, wflow.EnvId, wflow.Id) diff --git a/cmd/wait_for.go b/cmd/wait_for.go index 8db3250..fac67f1 100644 --- a/cmd/wait_for.go +++ b/cmd/wait_for.go @@ -38,6 +38,33 @@ func waitForRunningIntentWorkflow(ctx context.Context, cfg api.Config, iw types. } } +func waitForCompletedIntentWorkflow(ctx context.Context, cfg api.Config, iw types.IntentWorkflow) (types.IntentWorkflow, error) { + client := api.Client{Config: cfg} + intentWorkflow, ch, err := client.IntentWorkflows().WatchGet(ctx, iw.StackId, iw.Id, ws.RetryInfinite(time.Second)) + if err != nil { + return iw, fmt.Errorf("error waiting for deployment: %w", err) + } else if intentWorkflow == nil { + return iw, context.Canceled + } + + cur := *intentWorkflow + for { + switch cur.Status { + case types.IntentWorkflowStatusCompleted: + return cur, nil + case types.IntentWorkflowStatusFailed: + return cur, fmt.Errorf("Deployment failed: %s", cur.StatusMessage) + case types.IntentWorkflowStatusCancelled: + return cur, fmt.Errorf("Deployment was cancelled.") + } + so := <-ch + if so.Err != nil { + return cur, fmt.Errorf("error waiting for deployment: %w", so.Err) + } + cur = so.Object.ApplyTo(cur) + } +} + func waitForWorkspaceWorkflowRun(ctx context.Context, cfg api.Config, ww types.WorkspaceWorkflow) (types.Run, error) { client := api.Client{Config: cfg} workspaceWorkflow, ch, err := client.WorkspaceWorkflows().WatchGet(ctx, ww.StackId, ww.BlockId, ww.EnvId, ww.Id, ws.RetryInfinite(time.Second))