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"
"go.dagger.io/dagger/client"
"go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/environment"
"go.dagger.io/dagger/solver"
"go.dagger.io/dagger/state"
)
@ -83,20 +81,6 @@ func CurrentEnvironmentState(ctx context.Context, workspace *state.Workspace) *s
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
func FormatValue(val *compiler.Value) string {
if val.HasAttr("artifact") {

View File

@ -1,6 +1,7 @@
package cmd
import (
"context"
"encoding/json"
"errors"
"fmt"
@ -8,10 +9,11 @@ import (
"strings"
"cuelang.org/go/cue"
"go.dagger.io/dagger/client"
"go.dagger.io/dagger/cmd/dagger/cmd/common"
"go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/environment"
"go.dagger.io/dagger/solver"
"go.dagger.io/dagger/state"
"go.mozilla.org/sops/v3"
"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 {
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()
if err := v.FillPath(cue.MakePath(), environment.Plan()); err != nil {

View File

@ -5,14 +5,12 @@ import (
"os"
"cuelang.org/go/cue"
"go.dagger.io/dagger/client"
"go.dagger.io/dagger/cmd/dagger/cmd/common"
"go.dagger.io/dagger/cmd/dagger/cmd/output"
"go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/environment"
"go.dagger.io/dagger/solver"
"go.dagger.io/dagger/state"
"golang.org/x/term"
"github.com/rs/zerolog/log"
@ -40,10 +38,16 @@ var upCmd = &cobra.Command{
cl := common.NewClient(ctx, viper.GetBool("no-cache"))
// check that all inputs are set
checkInputs(ctx, cl, st)
result, 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)
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()
if err := workspace.Save(ctx, st); err != nil {
@ -54,28 +58,20 @@ 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)
warnOnly := viper.GetBool("force")
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)
if err != nil {
return err
}
for _, i := range inputs {
if i.IsConcreteR(cue.Optional(true)) != nil {
notConcreteInputs = append(notConcreteInputs, i)
}
}
return nil
})
inputs, err := env.ScanInputs(ctx, true)
if err != nil {
lg.Fatal().Err(err).Msg("failed to query environment")
lg.Fatal().Err(err).Msg("failed to scan inputs")
}
for _, i := range inputs {
if i.IsConcreteR(cue.Optional(true)) != nil {
notConcreteInputs = append(notConcreteInputs, i)
}
}
for _, i := range notConcreteInputs {