cmd/up/compute: unify buildkit sessions to use only one

Signed-off-by: Sam Alba <sam.alba@gmail.com>
This commit is contained in:
Sam Alba 2021-07-13 14:55:49 +02:00
parent fd942d201a
commit ce7adc1fa4
3 changed files with 31 additions and 42 deletions

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,11 +166,18 @@ var computeCmd = &cobra.Command{
} }
} }
cl, err := client.New(ctx, "", false) cl := common.NewClient(ctx, viper.GetBool("no-cache"))
environment, 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)
return env.Up(ctx, s)
})
if err != nil { if err != nil {
lg.Fatal().Err(err).Msg("unable to create client") lg.Fatal().Err(err).Msg("failed to up environment")
} }
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(), environment.Plan()); err != nil {

View File

@ -5,14 +5,12 @@ import (
"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,10 +38,16 @@ var upCmd = &cobra.Command{
cl := common.NewClient(ctx, viper.GetBool("no-cache")) cl := common.NewClient(ctx, viper.GetBool("no-cache"))
result, 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) checkInputs(ctx, env)
result := common.EnvironmentUp(ctx, cl, st, viper.GetBool("no-cache")) return env.Up(ctx, s)
})
if err != nil {
lg.Fatal().Err(err).Msg("failed to up environment")
}
st.Computed = result.Computed().JSON().PrettyString() st.Computed = result.Computed().JSON().PrettyString()
if err := workspace.Save(ctx, st); err != nil { if err := workspace.Save(ctx, st); err != nil {
@ -54,15 +58,14 @@ var upCmd = &cobra.Command{
}, },
} }
func checkInputs(ctx context.Context, cl *client.Client, st *state.State) { func checkInputs(ctx context.Context, env *environment.Environment) {
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 {
return err lg.Fatal().Err(err).Msg("failed to scan inputs")
} }
for _, i := range inputs { for _, i := range inputs {
@ -71,13 +74,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")