Skip to content

Commit

Permalink
feat(deploy): add progress updates
Browse files Browse the repository at this point in the history
  • Loading branch information
asalkeld committed Feb 18, 2022
1 parent b652a9c commit 07c4748
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 80 deletions.
47 changes: 30 additions & 17 deletions pkg/cmd/deployment/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,33 +62,44 @@ nitric deployment apply -n prod-aws -s ../project/ -t prod "functions/*.ts"

s, err := stack.FromOptions()
if err != nil && len(args) > 0 {
s, err = stack.FromGlobArgs(args)
cobra.CheckErr(err)

s, err = codeconfig.Populate(s)
codeAsConfig := tasklet.Runner{
StartMsg: "Gathering configuration from code..",
Runner: func(_ output.Progress) error {
s, err = stack.FromGlobArgs(args)
if err != nil {
return err
}

s, err = codeconfig.Populate(s)
return err
},
StopMsg: "Configuration gathered",
}
tasklet.MustRun(codeAsConfig, tasklet.Opts{LogToPterm: true})
}
cobra.CheckErr(err)

p, err := provider.NewProvider(s, t)
cobra.CheckErr(err)

buildImages := tasklet.Runner{
StartMsg: "Building Images",
Runner: func(tCtx tasklet.TaskletContext) error {
Runner: func(_ output.Progress) error {
return build.Create(s, t)
},
StopMsg: "Images built!",
StopMsg: "Images built",
}
tasklet.MustRun(buildImages, tasklet.Opts{})
tasklet.MustRun(buildImages, tasklet.Opts{
LogToPterm: true,
})

deploy := tasklet.Runner{
StartMsg: "Deploying..",
Runner: func(tCtx tasklet.TaskletContext) error {
return p.Apply(deploymentName)
Runner: func(progress output.Progress) error {
return p.Apply(progress, deploymentName)
},
StopMsg: "Deployment complete!",
StopMsg: "Stack",
}
tasklet.MustRun(deploy, tasklet.Opts{})
tasklet.MustRun(deploy, tasklet.Opts{SuccessPrefix: "Deployed"})
},
Args: cobra.MinimumNArgs(0),
}
Expand All @@ -113,13 +124,15 @@ nitric deployment delete -n prod-aws -s ../project/ -t prod

deploy := tasklet.Runner{
StartMsg: "Deleting..",
Runner: func(tCtx tasklet.TaskletContext) error {
return p.Delete(deploymentName)
Runner: func(progress output.Progress) error {
return p.Delete(progress, deploymentName)
},
StopMsg: "Deployment deleted!",
StopMsg: "Deployment",
}
tasklet.MustRun(deploy, tasklet.Opts{})

tasklet.MustRun(deploy, tasklet.Opts{
LogToPterm: true,
SuccessPrefix: "Deleted",
})
},
Args: cobra.ExactArgs(0),
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"time"

"github.com/mitchellh/mapstructure"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"github.com/spf13/viper"

Expand Down Expand Up @@ -53,6 +54,10 @@ var rootCmd = &cobra.Command{
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if output.VerboseLevel > 1 {
pterm.EnableDebugMessages()
}

cobra.CheckErr(rootCmd.Execute())
}

Expand Down Expand Up @@ -136,7 +141,7 @@ func ensureConfigDefaults() {
if needsWrite {
tasklet.MustRun(tasklet.Runner{
StartMsg: "Updating configfile to include defaults",
Runner: func(_ tasklet.TaskletContext) error {
Runner: func(_ output.Progress) error {
// ensure .config/nitric exists
err := os.MkdirAll(utils.NitricConfigDir(), os.ModePerm)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/cmd/run/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var runCmd = &cobra.Command{

createBaseImage := tasklet.Runner{
StartMsg: "Creating Dev Image",
Runner: func(tCtx tasklet.TaskletContext) error {
Runner: func(_ output.Progress) error {
return build.CreateBaseDev(s)
},
StopMsg: "Created Dev Image!",
Expand All @@ -71,7 +71,7 @@ var runCmd = &cobra.Command{

startLocalServices := tasklet.Runner{
StartMsg: "Starting Local Services",
Runner: func(tCtx tasklet.TaskletContext) error {
Runner: func(progress output.Progress) error {
go func(errch chan error) {
errch <- ls.Start()
}(memerr)
Expand All @@ -88,7 +88,7 @@ var runCmd = &cobra.Command{
if ls.Running() {
break
}
tCtx.Spinner().UpdateText("Waiting for Local Services to be ready")
progress.Busyf("Waiting for Local Services to be ready")
time.Sleep(time.Second)
}
return nil
Expand All @@ -103,7 +103,7 @@ var runCmd = &cobra.Command{

startFunctions := tasklet.Runner{
StartMsg: "Starting Functions",
Runner: func(tCtx tasklet.TaskletContext) error {
Runner: func(_ output.Progress) error {
functions, err = run.FunctionsFromHandlers(s)
if err != nil {
return err
Expand Down
8 changes: 4 additions & 4 deletions pkg/codeconfig/codeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package codeconfig

import (
"fmt"
"log"
"net"
"os"
"regexp"
osruntime "runtime"
"strings"
Expand All @@ -31,6 +31,7 @@ import (
"github.com/imdario/mergo"
"github.com/moby/moby/pkg/stdcopy"
"github.com/pkg/errors"
"github.com/pterm/pterm"
"google.golang.org/grpc"

"github.com/nitrictech/cli/pkg/build"
Expand Down Expand Up @@ -296,9 +297,8 @@ func (c *codeConfig) collectOne(handler string) error {
return err
}

pterm.Debug.Println(containerengine.Cli(cc, hostConfig))
if output.VerboseLevel > 1 {
fmt.Println(containerengine.Cli(cc, hostConfig))

logreader, err := ce.ContainerLogs(cID, types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Expand All @@ -308,7 +308,7 @@ func (c *codeConfig) collectOne(handler string) error {
return err
}
go func() {
_, _ = stdcopy.StdCopy(os.Stdout, os.Stderr, logreader)
_, _ = stdcopy.StdCopy(log.Writer(), log.Writer(), logreader)
}()
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/containerengine/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"os/exec"
"strings"
"time"
Expand All @@ -36,6 +35,7 @@ import (
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/idtools"
"github.com/pkg/errors"
"github.com/pterm/pterm"

"github.com/nitrictech/cli/pkg/utils"
)
Expand Down Expand Up @@ -144,7 +144,7 @@ func print(rd io.Reader) error {
return err
}
if len(line.Stream) > 0 {
log.Default().Print(line.Stream)
pterm.Debug.Print(line.Stream)
}
}

Expand Down
27 changes: 26 additions & 1 deletion pkg/output/verbose.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,29 @@

package output

var VerboseLevel int
import (
"io"
)

var (
VerboseLevel int
)

type Progress interface {
Debugf(format string, a ...interface{})
Busyf(format string, a ...interface{})
Successf(format string, a ...interface{})
Failf(format string, a ...interface{})
}

func StdoutToPtermDebug(b io.ReadCloser, p Progress, prefix string) {
defer b.Close()
buf := make([]byte, 1024)
for {
n, err := b.Read(buf)
if err != nil {
break
}
p.Debugf("%s %v", prefix, string(buf[:n]))
}
}
16 changes: 5 additions & 11 deletions pkg/provider/pulumi/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (

"github.com/pkg/errors"
"github.com/pulumi/pulumi/sdk/v3/go/auto"
"github.com/pulumi/pulumi/sdk/v3/go/auto/debug"
"github.com/pulumi/pulumi/sdk/v3/go/auto/optup"
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"

Expand Down Expand Up @@ -100,18 +98,13 @@ func (p *pulumiDeployment) load(name string) (*auto.Stack, error) {
return &s, errors.WithMessage(err, "Refresh")
}

func (p *pulumiDeployment) Apply(name string) error {
func (p *pulumiDeployment) Apply(log output.Progress, name string) error {
s, err := p.load(name)
if err != nil {
return errors.WithMessage(err, "loading pulumi stack")
}

var loglevel uint = uint(output.VerboseLevel)
dbg := optup.DebugLogging(debug.LoggingOptions{
LogLevel: &loglevel,
LogToStdErr: true})

res, err := s.Up(context.Background(), dbg)
res, err := s.Up(context.Background(), updateLoggingOpts(log)...)
defer p.p.CleanUp()
if err != nil {
return errors.WithMessage(err, "Updating pulumi stack "+res.Summary.Message)
Expand All @@ -136,12 +129,13 @@ func (p *pulumiDeployment) List() (interface{}, error) {
return ws.ListStacks(context.Background())
}

func (a *pulumiDeployment) Delete(name string) error {
func (a *pulumiDeployment) Delete(log output.Progress, name string) error {
s, err := a.load(name)
if err != nil {
return err
}
res, err := s.Destroy(context.Background())

res, err := s.Destroy(context.Background(), destroyLoggingOpts(log)...)
if err != nil {
return errors.WithMessage(err, res.Summary.Message)
}
Expand Down
Loading

0 comments on commit 07c4748

Please sign in to comment.