Skip to content

Commit

Permalink
make long-lived gateway configurable
Browse files Browse the repository at this point in the history
This helps with the REPL, too! Funny that both "loops" need it. Makes
sense.
  • Loading branch information
vito committed Apr 16, 2023
1 parent 71ee57b commit 49075e5
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 54 deletions.
2 changes: 1 addition & 1 deletion cmd/bass/bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func bump(ctx context.Context) error {
ctx, _, err := setupPool(ctx)
ctx, _, err := setupPool(ctx, true)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/bass/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

func export(ctx context.Context) error {
ctx, _, err := setupPool(ctx)
ctx, _, err := setupPool(ctx, true)
if err != nil {
return err
}
Expand Down
45 changes: 23 additions & 22 deletions cmd/bass/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,26 +105,6 @@ func main() {
}
}

var DefaultConfig = bass.Config{
Runtimes: []bass.RuntimeConfig{},
}

func init() {
var runtime string
if os.Getenv("DAGGER_SESSION_PORT") != "" || os.Getenv("_EXPERIMENTAL_DAGGER_CLI_BIN") != "" {
runtime = runtimes.DaggerName
} else {
runtime = runtimes.BuildkitName
}

DefaultConfig.Runtimes = []bass.RuntimeConfig{
{
Platform: bass.LinuxPlatform,
Runtime: runtime,
},
}
}

func root(ctx context.Context) error {
if showVersion {
printVersion(ctx)
Expand Down Expand Up @@ -204,8 +184,29 @@ func root(ctx context.Context) error {
return cli.WithProgress(ctx, run)
}

func setupPool(ctx context.Context) (context.Context, *runtimes.Pool, error) {
config, err := bass.LoadConfig(DefaultConfig)
func setupPool(ctx context.Context, oneShot bool) (context.Context, *runtimes.Pool, error) {
defaultConfig := bass.Config{
Runtimes: []bass.RuntimeConfig{},
}

var runtime string
if os.Getenv("DAGGER_SESSION_PORT") != "" || os.Getenv("_EXPERIMENTAL_DAGGER_CLI_BIN") != "" {
runtime = runtimes.DaggerName
} else {
runtime = runtimes.BuildkitName
}

defaultConfig.Runtimes = []bass.RuntimeConfig{
{
Platform: bass.LinuxPlatform,
Runtime: runtime,
Config: bass.Bindings{
"oneshot": bass.Bool(oneShot),
}.Scope(),
},
}

config, err := bass.LoadConfig(defaultConfig)
if err != nil {
cli.WriteError(ctx, err)
return nil, nil, err
Expand Down
2 changes: 1 addition & 1 deletion cmd/bass/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func prune(ctx context.Context) error {
ctx, _, err := setupPool(ctx)
ctx, _, err := setupPool(ctx, true)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/bass/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func repl(ctx context.Context) error {
ctx, _, err := setupPool(ctx)
ctx, _, err := setupPool(ctx, false)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/bass/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func run(ctx context.Context) error {
ctx, _, err := setupPool(ctx)
ctx, _, err := setupPool(ctx, true)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/bass/run_thunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func runThunk(ctx context.Context) error {
ctx, _, err := setupPool(ctx)
ctx, _, err := setupPool(ctx, true)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/bass/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var defaultKeys = []string{
}

func runnerLoop(ctx context.Context, client *runtimes.SSHClient) error {
ctx, pool, err := setupPool(ctx)
ctx, pool, err := setupPool(ctx, false)
if err != nil {
return err
}
Expand Down
53 changes: 28 additions & 25 deletions pkg/runtimes/buildkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const ociStoreName = "bass"
const ociTagAnnotation = "org.opencontainers.image.ref.name"

type BuildkitConfig struct {
Oneshot bool `json:"oneshot,omitempty"`
Debug bool `json:"debug,omitempty"`
Addr string `json:"addr,omitempty"`
Installation string `json:"installation,omitempty"`
Expand Down Expand Up @@ -190,33 +191,35 @@ func NewBuildkit(ctx context.Context, _ bass.RuntimePool, cfg *bass.Scope) (bass

solveOpt := newSolveOpt(authp, secrets, ociStore)

gwCh := make(chan gwclient.Client, 1)
gwErrCh := make(chan error, 1)
go func() {
statusProxy := forwardStatus(progrock.RecorderFromContext(ctx))
defer statusProxy.Wait()
var gw gwclient.Client
if config.Oneshot {
gwCh := make(chan gwclient.Client, 1)
gwErrCh := make(chan error, 1)
go func() {
statusProxy := forwardStatus(progrock.RecorderFromContext(ctx))
defer statusProxy.Wait()

_, err := client.Build(
ctx,
solveOpt,
buildkitProduct,
func(_ context.Context, gw gwclient.Client) (*gwclient.Result, error) {
gwCh <- gw
<-ctx.Done()
return gwclient.NewResult(), nil
},
statusProxy.Writer(),
)
if err != nil {
gwErrCh <- err
}
}()
_, err := client.Build(
ctx,
solveOpt,
buildkitProduct,
func(_ context.Context, gw gwclient.Client) (*gwclient.Result, error) {
gwCh <- gw
<-ctx.Done()
return gwclient.NewResult(), nil
},
statusProxy.Writer(),
)
if err != nil {
gwErrCh <- err
}
}()

var gw gwclient.Client
select {
case gw = <-gwCh:
case err := <-gwErrCh:
return nil, fmt.Errorf("buildkit gateway: %w", err)
select {
case gw = <-gwCh:
case err := <-gwErrCh:
return nil, fmt.Errorf("buildkit gateway: %w", err)
}
}

return &Buildkit{
Expand Down

0 comments on commit 49075e5

Please sign in to comment.