Merge pull request #817 from samalba/unify-buildkit-client

Unify buildkit clients
This commit is contained in:
Andrea Luzzardi 2021-07-14 12:19:52 +02:00 committed by GitHub
commit 2f2ede4cfc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 116 additions and 121 deletions

View File

@ -69,13 +69,13 @@ func New(ctx context.Context, host string, noCache bool) (*Client, error) {
type DoFunc func(context.Context, *environment.Environment, solver.Solver) error type DoFunc func(context.Context, *environment.Environment, solver.Solver) error
// FIXME: return completed *Route, instead of *compiler.Value // FIXME: return completed *Route, instead of *compiler.Value
func (c *Client) Do(ctx context.Context, state *state.State, fn DoFunc) (*environment.Environment, error) { func (c *Client) Do(ctx context.Context, state *state.State, fn DoFunc) error {
lg := log.Ctx(ctx) lg := log.Ctx(ctx)
eg, gctx := errgroup.WithContext(ctx) eg, gctx := errgroup.WithContext(ctx)
environment, err := environment.New(state) environment, err := environment.New(state)
if err != nil { if err != nil {
return nil, err return err
} }
// Spawn print function // Spawn print function
@ -92,7 +92,7 @@ func (c *Client) Do(ctx context.Context, state *state.State, fn DoFunc) (*enviro
return c.buildfn(gctx, state, environment, fn, events) return c.buildfn(gctx, state, environment, fn, events)
}) })
return environment, eg.Wait() return eg.Wait()
} }
func (c *Client) buildfn(ctx context.Context, st *state.State, env *environment.Environment, fn DoFunc, ch chan *bk.SolveStatus) error { func (c *Client) buildfn(ctx context.Context, st *state.State, env *environment.Environment, fn DoFunc, ch chan *bk.SolveStatus) error {

View File

@ -9,8 +9,6 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
"go.dagger.io/dagger/client" "go.dagger.io/dagger/client"
"go.dagger.io/dagger/compiler" "go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/environment"
"go.dagger.io/dagger/solver"
"go.dagger.io/dagger/state" "go.dagger.io/dagger/state"
) )
@ -83,20 +81,6 @@ func CurrentEnvironmentState(ctx context.Context, workspace *state.Workspace) *s
return environments[0] return environments[0]
} }
// Re-compute an environment (equivalent to `dagger up`).
func EnvironmentUp(ctx context.Context, cl *client.Client, state *state.State, noCache bool) *environment.Environment {
lg := log.Ctx(ctx)
result, err := cl.Do(ctx, state, func(ctx context.Context, environment *environment.Environment, s solver.Solver) error {
log.Ctx(ctx).Debug().Msg("bringing environment up")
return environment.Up(ctx, s)
})
if err != nil {
lg.Fatal().Err(err).Msg("failed to up environment")
}
return result
}
// FormatValue returns the String representation of the cue value // FormatValue returns the String representation of the cue value
func FormatValue(val *compiler.Value) string { func FormatValue(val *compiler.Value) string {
if val.HasAttr("artifact") { if val.HasAttr("artifact") {

View File

@ -1,6 +1,7 @@
package cmd package cmd
import ( import (
"context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@ -8,10 +9,11 @@ import (
"strings" "strings"
"cuelang.org/go/cue" "cuelang.org/go/cue"
"go.dagger.io/dagger/client"
"go.dagger.io/dagger/cmd/dagger/cmd/common" "go.dagger.io/dagger/cmd/dagger/cmd/common"
"go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/compiler" "go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/environment"
"go.dagger.io/dagger/solver"
"go.dagger.io/dagger/state" "go.dagger.io/dagger/state"
"go.mozilla.org/sops/v3" "go.mozilla.org/sops/v3"
"go.mozilla.org/sops/v3/decrypt" "go.mozilla.org/sops/v3/decrypt"
@ -164,24 +166,34 @@ var computeCmd = &cobra.Command{
} }
} }
cl, err := client.New(ctx, "", false) cl := common.NewClient(ctx, viper.GetBool("no-cache"))
if err != nil {
lg.Fatal().Err(err).Msg("unable to create client") err := cl.Do(ctx, st, func(ctx context.Context, env *environment.Environment, s solver.Solver) error {
// check that all inputs are set
checkInputs(ctx, env)
if err := env.Up(ctx, s); err != nil {
return err
} }
environment := common.EnvironmentUp(ctx, cl, st, viper.GetBool("no-cache"))
v := compiler.NewValue() v := compiler.NewValue()
if err := v.FillPath(cue.MakePath(), environment.Plan()); err != nil { if err := v.FillPath(cue.MakePath(), env.Plan()); err != nil {
lg.Fatal().Err(err).Msg("failed to merge") return err
} }
if err := v.FillPath(cue.MakePath(), environment.Input()); err != nil { if err := v.FillPath(cue.MakePath(), env.Input()); err != nil {
lg.Fatal().Err(err).Msg("failed to merge") return err
} }
if err := v.FillPath(cue.MakePath(), environment.Computed()); err != nil { if err := v.FillPath(cue.MakePath(), env.Computed()); err != nil {
lg.Fatal().Err(err).Msg("failed to merge") return err
} }
fmt.Println(v.JSON()) fmt.Println(v.JSON())
return nil
})
if err != nil {
lg.Fatal().Err(err).Msg("failed to up environment")
}
}, },
} }

View File

@ -71,7 +71,7 @@ var editCmd = &cobra.Command{
st.Inputs = newState.Inputs st.Inputs = newState.Inputs
cl := common.NewClient(ctx, false) cl := common.NewClient(ctx, false)
_, err = cl.Do(ctx, st, func(ctx context.Context, env *environment.Environment, s solver.Solver) error { err = cl.Do(ctx, st, func(ctx context.Context, env *environment.Environment, s solver.Solver) error {
// check for cue errors by scanning all the inputs // check for cue errors by scanning all the inputs
_, err := env.ScanInputs(ctx, true) _, err := env.ScanInputs(ctx, true)
if err != nil { if err != nil {

View File

@ -45,7 +45,7 @@ var listCmd = &cobra.Command{
lg.Fatal().Err(err).Msg("unable to create client") lg.Fatal().Err(err).Msg("unable to create client")
} }
_, err = c.Do(ctx, st, func(ctx context.Context, env *environment.Environment, s solver.Solver) error { err = c.Do(ctx, st, func(ctx context.Context, env *environment.Environment, s solver.Solver) error {
inputs, err := env.ScanInputs(ctx, false) inputs, err := env.ScanInputs(ctx, false)
if err != nil { if err != nil {
return err return err

View File

@ -43,7 +43,7 @@ func updateEnvironmentInput(ctx context.Context, cl *client.Client, target strin
st.SetInput(target, input) st.SetInput(target, input)
_, err := cl.Do(ctx, st, func(ctx context.Context, env *environment.Environment, s solver.Solver) error { err := cl.Do(ctx, st, func(ctx context.Context, env *environment.Environment, s solver.Solver) error {
// the inputs are set, check for cue errors by scanning all the inputs // the inputs are set, check for cue errors by scanning all the inputs
_, err := env.ScanInputs(ctx, true) _, err := env.ScanInputs(ctx, true)
if err != nil { if err != nil {

View File

@ -6,12 +6,10 @@ import (
"os" "os"
"text/tabwriter" "text/tabwriter"
"go.dagger.io/dagger/client"
"go.dagger.io/dagger/cmd/dagger/cmd/common" "go.dagger.io/dagger/cmd/dagger/cmd/common"
"go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/environment" "go.dagger.io/dagger/environment"
"go.dagger.io/dagger/solver" "go.dagger.io/dagger/solver"
"go.dagger.io/dagger/state"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -36,23 +34,25 @@ var listCmd = &cobra.Command{
workspace := common.CurrentWorkspace(ctx) workspace := common.CurrentWorkspace(ctx)
st := common.CurrentEnvironmentState(ctx, workspace) st := common.CurrentEnvironmentState(ctx, workspace)
ListOutputs(ctx, st, true) cl := common.NewClient(ctx, false)
err := cl.Do(ctx, st, func(ctx context.Context, env *environment.Environment, s solver.Solver) error {
return ListOutputs(ctx, env, true)
})
if err != nil {
lg.Fatal().Err(err).Msg("failed to scan outputs")
}
}, },
} }
func ListOutputs(ctx context.Context, st *state.State, isTTY bool) { func ListOutputs(ctx context.Context, env *environment.Environment, isTTY bool) error {
lg := log.Ctx(ctx).With(). lg := log.Ctx(ctx).With().
Str("environment", st.Name). Str("environment", env.Name()).
Logger() Logger()
c, err := client.New(ctx, "", false)
if err != nil {
lg.Fatal().Err(err).Msg("unable to create client")
}
_, err = c.Do(ctx, st, func(ctx context.Context, env *environment.Environment, s solver.Solver) error {
outputs, err := env.ScanOutputs(ctx) outputs, err := env.ScanOutputs(ctx)
if err != nil { if err != nil {
lg.Error().Err(err).Msg("failed to scan outputs")
return err return err
} }
@ -78,9 +78,4 @@ func ListOutputs(ctx context.Context, st *state.State, isTTY bool) {
w.Flush() w.Flush()
return nil return nil
})
if err != nil {
lg.Fatal().Err(err).Msg("failed to query environment")
}
} }

View File

@ -1,13 +1,15 @@
package cmd package cmd
import ( import (
"context"
"fmt" "fmt"
"cuelang.org/go/cue" "cuelang.org/go/cue"
"go.dagger.io/dagger/client"
"go.dagger.io/dagger/cmd/dagger/cmd/common" "go.dagger.io/dagger/cmd/dagger/cmd/common"
"go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/compiler" "go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/environment"
"go.dagger.io/dagger/solver"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -42,29 +44,27 @@ var queryCmd = &cobra.Command{
cuePath = cue.ParsePath(args[0]) cuePath = cue.ParsePath(args[0])
} }
c, err := client.New(ctx, "", false) cl := common.NewClient(ctx, false)
if err != nil {
lg.Fatal().Err(err).Msg("unable to create client")
}
environment, err := c.Do(ctx, state, nil)
if err != nil {
lg.Fatal().Err(err).Msg("failed to query environment")
}
cueVal := compiler.NewValue() cueVal := compiler.NewValue()
err := cl.Do(ctx, state, func(ctx context.Context, env *environment.Environment, s solver.Solver) error {
if !viper.GetBool("no-plan") { if !viper.GetBool("no-plan") {
if err := cueVal.FillPath(cue.MakePath(), environment.Plan()); err != nil { if err := cueVal.FillPath(cue.MakePath(), env.Plan()); err != nil {
lg.Fatal().Err(err).Msg("failed to merge plan") return err
} }
} }
if !viper.GetBool("no-input") { if !viper.GetBool("no-input") {
if err := cueVal.FillPath(cue.MakePath(), environment.Input()); err != nil { if err := cueVal.FillPath(cue.MakePath(), env.Input()); err != nil {
lg.Fatal().Err(err).Msg("failed to merge plan with output") return err
} }
} }
return nil
})
if err != nil {
lg.Fatal().Err(err).Msg("failed to query environment")
}
if !viper.GetBool("no-computed") && state.Computed != "" { if !viper.GetBool("no-computed") && state.Computed != "" {
computed, err := compiler.DecodeJSON("", []byte(state.Computed)) computed, err := compiler.DecodeJSON("", []byte(state.Computed))

View File

@ -2,17 +2,16 @@ package cmd
import ( import (
"context" "context"
"errors"
"os" "os"
"cuelang.org/go/cue" "cuelang.org/go/cue"
"go.dagger.io/dagger/client"
"go.dagger.io/dagger/cmd/dagger/cmd/common" "go.dagger.io/dagger/cmd/dagger/cmd/common"
"go.dagger.io/dagger/cmd/dagger/cmd/output" "go.dagger.io/dagger/cmd/dagger/cmd/output"
"go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/compiler" "go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/environment" "go.dagger.io/dagger/environment"
"go.dagger.io/dagger/solver" "go.dagger.io/dagger/solver"
"go.dagger.io/dagger/state"
"golang.org/x/term" "golang.org/x/term"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -40,28 +39,38 @@ var upCmd = &cobra.Command{
cl := common.NewClient(ctx, viper.GetBool("no-cache")) cl := common.NewClient(ctx, viper.GetBool("no-cache"))
err := cl.Do(ctx, st, func(ctx context.Context, env *environment.Environment, s solver.Solver) error {
// check that all inputs are set // check that all inputs are set
checkInputs(ctx, cl, st) if err := checkInputs(ctx, env); err != nil {
return err
result := common.EnvironmentUp(ctx, cl, st, viper.GetBool("no-cache"))
st.Computed = result.Computed().JSON().PrettyString()
if err := workspace.Save(ctx, st); err != nil {
lg.Fatal().Err(err).Msg("failed to update environment")
} }
output.ListOutputs(ctx, st, term.IsTerminal(int(os.Stdout.Fd()))) if err := env.Up(ctx, s); err != nil {
return err
}
st.Computed = env.Computed().JSON().PrettyString()
if err := workspace.Save(ctx, st); err != nil {
return err
}
return output.ListOutputs(ctx, env, term.IsTerminal(int(os.Stdout.Fd())))
})
if err != nil {
lg.Fatal().Err(err).Msg("failed to up environment")
}
}, },
} }
func checkInputs(ctx context.Context, cl *client.Client, st *state.State) { func checkInputs(ctx context.Context, env *environment.Environment) error {
lg := log.Ctx(ctx) lg := log.Ctx(ctx)
warnOnly := viper.GetBool("force") warnOnly := viper.GetBool("force")
notConcreteInputs := []*compiler.Value{} notConcreteInputs := []*compiler.Value{}
_, err := cl.Do(ctx, st, func(ctx context.Context, env *environment.Environment, s solver.Solver) error {
inputs, err := env.ScanInputs(ctx, true) inputs, err := env.ScanInputs(ctx, true)
if err != nil { if err != nil {
lg.Error().Err(err).Msg("failed to scan inputs")
return err return err
} }
@ -71,13 +80,6 @@ func checkInputs(ctx context.Context, cl *client.Client, st *state.State) {
} }
} }
return nil
})
if err != nil {
lg.Fatal().Err(err).Msg("failed to query environment")
}
for _, i := range notConcreteInputs { for _, i := range notConcreteInputs {
if warnOnly { if warnOnly {
lg.Warn().Str("input", i.Path().String()).Msg("required input is missing") lg.Warn().Str("input", i.Path().String()).Msg("required input is missing")
@ -87,8 +89,10 @@ func checkInputs(ctx context.Context, cl *client.Client, st *state.State) {
} }
if !warnOnly && len(notConcreteInputs) > 0 { if !warnOnly && len(notConcreteInputs) > 0 {
lg.Fatal().Int("missing", len(notConcreteInputs)).Msg("some required inputs are not set, please re-run with `--force` if you think it's a mistake") return errors.New("some required inputs are not set, please re-run with `--force` if you think it's a mistake")
} }
return nil
} }
func init() { func init() {