Skip to content

Commit

Permalink
cmd/up/compute: unify buildkit sessions to use only one
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Alba <sam.alba@gmail.com>
  • Loading branch information
samalba committed Jul 13, 2021
1 parent fd942d2 commit ce7adc1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 41 deletions.
16 changes: 0 additions & 16 deletions cmd/dagger/cmd/common/common.go
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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") {
Expand Down
17 changes: 13 additions & 4 deletions cmd/dagger/cmd/compute.go
@@ -1,17 +1,19 @@
package cmd

import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"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"
Expand Down Expand Up @@ -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 {
Expand Down
38 changes: 17 additions & 21 deletions cmd/dagger/cmd/up.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
inputs, err := env.ScanInputs(ctx, true)
if err != nil {
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 inputs {
if i.IsConcreteR(cue.Optional(true)) != nil {
notConcreteInputs = append(notConcreteInputs, i)
}

return nil
})

if err != nil {
lg.Fatal().Err(err).Msg("failed to query environment")
}

for _, i := range notConcreteInputs {
Expand Down

0 comments on commit ce7adc1

Please sign in to comment.