Skip to content

Commit 0d9d802

Browse files
committed
set "user" as default for template builds
1 parent a0c4e48 commit 0d9d802

File tree

5 files changed

+111
-3
lines changed

5 files changed

+111
-3
lines changed

packages/orchestrator/internal/template/build/builder.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/e2b-dev/infra/packages/orchestrator/internal/template/build/phases/base"
2626
"github.com/e2b-dev/infra/packages/orchestrator/internal/template/build/phases/finalize"
2727
"github.com/e2b-dev/infra/packages/orchestrator/internal/template/build/phases/steps"
28+
"github.com/e2b-dev/infra/packages/orchestrator/internal/template/build/phases/user"
2829
"github.com/e2b-dev/infra/packages/orchestrator/internal/template/build/storage/cache"
2930
"github.com/e2b-dev/infra/packages/orchestrator/internal/template/build/writer"
3031
buildcache "github.com/e2b-dev/infra/packages/orchestrator/internal/template/cache"
@@ -230,6 +231,18 @@ func runBuild(
230231
builder.proxy,
231232
)
232233

234+
userBuilder := user.New(
235+
bc,
236+
builder.sandboxFactory,
237+
builder.logger,
238+
builder.proxy,
239+
layerExecutor,
240+
commandExecutor,
241+
index,
242+
builder.metrics,
243+
config.TemplateDefaultUser,
244+
)
245+
233246
stepBuilders := steps.CreateStepPhases(
234247
bc,
235248
builder.sandboxFactory,
@@ -253,6 +266,10 @@ func runBuild(
253266
builders := []phases.BuilderPhase{
254267
baseBuilder,
255268
}
269+
// For v1 builds the default user is not set
270+
if !bc.IsV1Build {
271+
builders = append(builders, userBuilder)
272+
}
256273
builders = append(builders, stepBuilders...)
257274
builders = append(builders, postProcessingBuilder)
258275

packages/orchestrator/internal/template/build/config/config.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import (
66
"github.com/e2b-dev/infra/packages/shared/pkg/storage/header"
77
)
88

9-
const InstanceBuildPrefix = "b"
9+
const (
10+
InstanceBuildPrefix = "b"
11+
12+
// TemplateDefaultUser is the default user to use in the template to run all commands.
13+
TemplateDefaultUser = "user"
14+
)
1015

1116
type TemplateConfig struct {
1217
// TeamID is the ID of the team to build the template for.

packages/orchestrator/internal/template/build/phases/base/builder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/e2b-dev/infra/packages/shared/pkg/dockerhub"
3333
"github.com/e2b-dev/infra/packages/shared/pkg/id"
3434
"github.com/e2b-dev/infra/packages/shared/pkg/storage"
35+
"github.com/e2b-dev/infra/packages/shared/pkg/utils"
3536
)
3637

3738
func templatesDirectory() string {
@@ -338,8 +339,7 @@ func (bb *BaseBuilder) Layer(
338339

339340
// This is a compatibility for v1 template builds
340341
if bb.IsV1Build {
341-
cwd := "/home/user"
342-
cmdMeta.WorkDir = &cwd
342+
cmdMeta.WorkDir = utils.ToPtr("/home/user")
343343
}
344344

345345
meta := metadata.Template{
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package user
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"go.uber.org/zap"
8+
9+
"github.com/e2b-dev/infra/packages/orchestrator/internal/proxy"
10+
"github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox"
11+
"github.com/e2b-dev/infra/packages/orchestrator/internal/template/build/buildcontext"
12+
"github.com/e2b-dev/infra/packages/orchestrator/internal/template/build/commands"
13+
"github.com/e2b-dev/infra/packages/orchestrator/internal/template/build/layer"
14+
"github.com/e2b-dev/infra/packages/orchestrator/internal/template/build/metrics"
15+
"github.com/e2b-dev/infra/packages/orchestrator/internal/template/build/phases"
16+
"github.com/e2b-dev/infra/packages/orchestrator/internal/template/build/phases/steps"
17+
"github.com/e2b-dev/infra/packages/orchestrator/internal/template/build/storage/cache"
18+
template_manager "github.com/e2b-dev/infra/packages/shared/pkg/grpc/template-manager"
19+
)
20+
21+
type UserBuilder struct {
22+
*steps.StepBuilder
23+
24+
user string
25+
}
26+
27+
func New(
28+
buildContext buildcontext.BuildContext,
29+
sandboxFactory *sandbox.Factory,
30+
logger *zap.Logger,
31+
proxy *proxy.SandboxProxy,
32+
layerExecutor *layer.LayerExecutor,
33+
commandExecutor *commands.CommandExecutor,
34+
index cache.Index,
35+
metrics *metrics.BuildMetrics,
36+
user string,
37+
) *UserBuilder {
38+
return &UserBuilder{
39+
StepBuilder: steps.New(
40+
buildContext,
41+
sandboxFactory,
42+
logger,
43+
proxy,
44+
layerExecutor,
45+
commandExecutor,
46+
index,
47+
metrics,
48+
&template_manager.TemplateStep{
49+
Type: "USER",
50+
Args: []string{user},
51+
},
52+
// This step number shouldn't be used, but in case it does, defining as 1
53+
1,
54+
),
55+
user: user,
56+
}
57+
}
58+
59+
func (ub *UserBuilder) Prefix() string {
60+
return "base"
61+
}
62+
63+
func (ub *UserBuilder) String(ctx context.Context) (string, error) {
64+
return fmt.Sprintf("DEFAULT USER %s", ub.user), nil
65+
}
66+
67+
func (ub *UserBuilder) Metadata() phases.PhaseMeta {
68+
return phases.PhaseMeta{
69+
Phase: metrics.PhaseBase,
70+
StepType: "base",
71+
}
72+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package user
2+
3+
import (
4+
"github.com/e2b-dev/infra/packages/orchestrator/internal/template/build/phases"
5+
"github.com/e2b-dev/infra/packages/orchestrator/internal/template/build/storage/cache"
6+
)
7+
8+
func (ub *UserBuilder) Hash(sourceLayer phases.LayerResult) (string, error) {
9+
return cache.HashKeys(
10+
sourceLayer.Hash,
11+
"DEFAULT USER",
12+
ub.user,
13+
), nil
14+
}

0 commit comments

Comments
 (0)