Skip to content

Commit

Permalink
Updated envs up/down to use intent workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
BSick7 committed Jun 20, 2024
1 parent 0f9f64b commit 21c857e
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 60 deletions.
63 changes: 6 additions & 57 deletions cmd/envs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"gopkg.in/nullstone-io/go-api-client.v0"
"gopkg.in/nullstone-io/go-api-client.v0/find"
"gopkg.in/nullstone-io/go-api-client.v0/types"
"gopkg.in/nullstone-io/nullstone.v0/runs"
"math"
"os"
"regexp"
Expand Down Expand Up @@ -372,11 +371,6 @@ func createEnvRun(c *cli.Context, cfg api.Config, isDestroy bool) error {
stackName := c.String("stack")
envName := c.String("env")

action := "launch"
if isDestroy {
action = "destroy"
}

stack, err := client.StacksByName().Get(ctx, stackName)
if err != nil {
return fmt.Errorf("error looking for stack %q: %w", stackName, err)
Expand All @@ -391,56 +385,11 @@ func createEnvRun(c *cli.Context, cfg api.Config, isDestroy bool) error {
return fmt.Errorf("environment %q does not exist in stack %d", envName, stack.Id)
}

body := types.CreateEnvRunInput{IsDestroy: isDestroy}
newRuns, err := client.EnvRuns().Create(ctx, stack.Id, env.Id, body)
if err != nil {
return fmt.Errorf("error creating run: %w", err)
}

if len(newRuns) <= 0 {
fmt.Fprintf(os.Stdout, "no runs created to %s the %q environment\n", action, envName)
return nil
}

workspaces, err := client.Workspaces().List(ctx, stack.Id)
if err != nil {
return fmt.Errorf("error retrieving list of workspaces: %w", err)
}
blocks, err := client.Blocks().List(ctx, stack.Id)
if err != nil {
return fmt.Errorf("error retrieving list of blocks: %w", err)
input := PerformEnvRunInput{
CommitSha: "",
Stack: *stack,
Env: *env,
IsDestroy: isDestroy,
}

findWorkspace := func(run types.Run) *types.Workspace {
for _, workspace := range workspaces {
if workspace.Uid == run.WorkspaceUid {
return &workspace
}
}
return nil
}
findBlock := func(workspace *types.Workspace) *types.Block {
if workspace == nil {
return nil
}
for _, block := range blocks {
if workspace.BlockId == block.Id {
return &block
}
}
return nil
}
for _, run := range newRuns {
blockName := "(unknown)"
workspace := findWorkspace(run)
if block := findBlock(workspace); block != nil {
blockName = block.Name
}
browserUrl := ""
if workspace != nil {
browserUrl = fmt.Sprintf(" Logs: %s", runs.GetBrowserUrl(cfg, *workspace, run))
}
fmt.Fprintf(os.Stdout, "created run to %s %s and dependencies in %q environment.%s\n", action, blockName, envName, browserUrl)
}
return nil
return PerformEnvRun(ctx, cfg, input)
}
90 changes: 90 additions & 0 deletions cmd/perform_env_run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package cmd

import (
"context"
"fmt"
"gopkg.in/nullstone-io/go-api-client.v0"
"gopkg.in/nullstone-io/go-api-client.v0/types"
"gopkg.in/nullstone-io/nullstone.v0/app_urls"
"os"
)

type PerformEnvRunInput struct {
CommitSha string
Stack types.Stack
Env types.Environment
IsDestroy bool
}

func PerformEnvRun(ctx context.Context, cfg api.Config, input PerformEnvRunInput) error {
stdout := os.Stdout
action := "launch"
if input.IsDestroy {
action = "destroy"
}

client := api.Client{Config: cfg}
body := types.CreateEnvRunInput{IsDestroy: input.IsDestroy}
result, err := client.EnvRuns().Create(ctx, input.Stack.Id, input.Env.Id, body)
if err != nil {
return fmt.Errorf("error creating run: %w", err)
} else if result == nil {
fmt.Fprintf(stdout, "no runs created to %s the %q environment\n", action, input.Env.Name)
return nil
}

if result.IntentWorkflow.Intent != "" {
fmt.Fprintf(stdout, "created workflow to %s %q environment.\n", action, input.Env.Name)
fmt.Fprintln(stdout, app_urls.GetIntentWorkflow(cfg, result.IntentWorkflow))
return nil
} else if result.Runs == nil {
return fmt.Errorf("workflow to %q environment was not created", action)
}

if len(result.Runs) < 1 {
fmt.Fprintf(stdout, "no runs created to %s the %q environment\n", action, input.Env.Name)
return nil
}

workspaces, err := client.Workspaces().List(ctx, input.Env.Id)
if err != nil {
return fmt.Errorf("error retrieving list of workspaces: %w", err)
}
blocks, err := client.Blocks().List(ctx, input.Stack.Id)
if err != nil {
return fmt.Errorf("error retrieving list of blocks: %w", err)
}

findWorkspace := func(run types.Run) *types.Workspace {
for _, workspace := range workspaces {
if workspace.Uid == run.WorkspaceUid {
return &workspace
}
}
return nil
}
findBlock := func(workspace *types.Workspace) *types.Block {
if workspace == nil {
return nil
}
for _, block := range blocks {
if workspace.BlockId == block.Id {
return &block
}
}
return nil
}
for _, run := range result.Runs {
blockName := "(unknown)"
workspace := findWorkspace(run)
if block := findBlock(workspace); block != nil {
blockName = block.Name
}
browserUrl := ""
if workspace != nil {
browserUrl = fmt.Sprintf(" Logs: %s", app_urls.GetRun(cfg, *workspace, run))
}
fmt.Fprintf(os.Stdout, "created run to %s %s and dependencies in %q environment. %s\n", action, blockName, input.Env.Name, browserUrl)
}
return nil
}
3 changes: 2 additions & 1 deletion cmd/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"gopkg.in/nullstone-io/go-api-client.v0"
"gopkg.in/nullstone-io/go-api-client.v0/find"
"gopkg.in/nullstone-io/go-api-client.v0/types"
"gopkg.in/nullstone-io/nullstone.v0/app_urls"
"gopkg.in/nullstone-io/nullstone.v0/runs"
"strings"
"time"
Expand Down Expand Up @@ -98,7 +99,7 @@ func WaitForLaunch(ctx context.Context, osWriters logging.OsWriters, cfg api.Con
}

fmt.Fprintf(stderr, "Waiting for %q to launch in %q environment...\n", details.Block.Name, details.Env.Name)
fmt.Fprintf(stderr, "Watching run for launch: %s\n", runs.GetBrowserUrl(cfg, *details.Workspace, *launchRun))
fmt.Fprintf(stderr, "Watching run for launch: %s\n", app_urls.GetRun(cfg, *details.Workspace, *launchRun))
fmt.Fprintf(stderr, "Timeout = %s, Approval Timeout = %s\n", timeout, approvalTimeout)

result, err := runs.WaitForTerminalRun(ctx, osWriters, cfg, *details.Workspace, *launchRun, timeout, approvalTimeout)
Expand Down
4 changes: 2 additions & 2 deletions cmd/wait_for.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func waitForWorkspaceWorkflowRun(ctx context.Context, cfg api.Config, ww types.W
if cur.Run != nil {
return *cur.Run, nil
}
if types.IsTerminalRunStatus(cur.Run.Status) {
return types.Run{}, fmt.Errorf("workflow reached %s status before a run could be found", cur.Run.Status)
if types.IsTerminalWorkspaceWorkflow(cur.Status) {
return types.Run{}, fmt.Errorf("workflow reached %s status before a run could be found", cur.Status)
}
so := <-ch
if so.Err != nil {
Expand Down

0 comments on commit 21c857e

Please sign in to comment.