-
Notifications
You must be signed in to change notification settings - Fork 9
/
docker.go
64 lines (52 loc) · 2 KB
/
docker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package dockerfile
import (
"context"
"io/fs"
"path/filepath"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/parsing/integration/api"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/pkggraph"
)
func Register() {
api.RegisterIntegration[*schema.DockerfileIntegration, *schema.DockerfileIntegration](impl{})
}
type impl struct {
api.DefaultBinaryTestIntegration[*schema.DockerfileIntegration]
}
func (impl) ApplyToServer(ctx context.Context, env *schema.Environment, pl pkggraph.PackageLoader, pkg *pkggraph.Package, data *schema.DockerfileIntegration) error {
if pkg.Server == nil {
// Can't happen with the current syntax.
return fnerrors.NewWithLocation(pkg.Location, "docker integration requires a server")
}
binaryRef, err := api.GenerateBinaryAndAddToPackage(ctx, env, pl, pkg, pkg.Server.Name, data)
if err != nil {
return err
}
return api.SetServerBinaryRef(pkg, binaryRef)
}
func (impl) CreateBinary(ctx context.Context, env *schema.Environment, pl pkggraph.PackageLoader, loc pkggraph.Location, data *schema.DockerfileIntegration) (*schema.Binary, error) {
dockerfile := data.Src
if dockerfile == "" {
dockerfile = "Dockerfile"
}
if _, err := fs.Stat(loc.Module.ReadOnlyFS(), filepath.Join(loc.Rel(), dockerfile)); err != nil {
return nil, fnerrors.NewWithLocation(loc, "could not find %q file, please verify that the specified dockerfile path is correct: %w", dockerfile, err)
}
var config *schema.BinaryConfig
if data.WorkingDir != "" || data.Command != nil {
config = &schema.BinaryConfig{
WorkingDir: data.WorkingDir,
Command: data.Command,
}
}
return &schema.Binary{
BuildPlan: &schema.LayeredImageBuildPlan{
LayerBuildPlan: []*schema.ImageBuildPlan{{Dockerfile: dockerfile}},
},
Config: config,
}, nil
}