-
Notifications
You must be signed in to change notification settings - Fork 557
/
host.go
150 lines (135 loc) · 3.46 KB
/
host.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package core
import (
"context"
"fmt"
"path/filepath"
"github.com/containerd/containerd/labels"
specs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/vektah/gqlparser/v2/ast"
"github.com/dagger/dagger/dagql"
)
type Host struct {
Query *Query
}
func (*Host) Type() *ast.Type {
return &ast.Type{
NamedType: "Host",
NonNull: true,
}
}
func (*Host) TypeDescription() string {
return "Information about the host environment."
}
type CopyFilter struct {
Exclude []string `default:"[]"`
Include []string `default:"[]"`
}
func LoadBlob(ctx context.Context, srv *dagql.Server, desc specs.Descriptor) (i dagql.Instance[*Directory], err error) {
// Instead of directly returning a Directory, which would get "stamped" with
// an impure ID that cannot be passed between modules, we fetch the Directory
// we just uploaded by its blob, which yields a pure ID.
err = srv.Select(ctx, srv.Root(), &i, dagql.Selector{
Field: "blob",
Args: []dagql.NamedInput{
{
Name: "digest",
Value: dagql.NewString(desc.Digest.String()),
},
{
Name: "size",
Value: dagql.NewInt(desc.Size),
},
{
Name: "mediaType",
Value: dagql.NewString(desc.MediaType),
},
{
Name: "uncompressed",
Value: dagql.NewString(desc.Annotations[labels.LabelUncompressed]),
},
},
})
return
}
func (host *Host) Directory(
ctx context.Context,
srv *dagql.Server,
dirPath string,
pipelineNamePrefix string,
filter CopyFilter,
) (dagql.Instance[*Directory], error) {
var i dagql.Instance[*Directory]
// TODO: enforcement that requester session is granted access to source session at this path
// Create a sub-pipeline to group llb.Local instructions
_, desc, err := host.Query.Buildkit.LocalImport(
ctx,
host.Query.Platform.Spec(),
dirPath,
filter.Exclude,
filter.Include,
)
if err != nil {
return i, fmt.Errorf("host directory %s: %w", dirPath, err)
}
return LoadBlob(ctx, srv, desc)
}
func (host *Host) File(ctx context.Context, srv *dagql.Server, filePath string) (dagql.Instance[*File], error) {
fileDir, fileName := filepath.Split(filePath)
var i dagql.Instance[*File]
if err := srv.Select(ctx, srv.Root(), &i, dagql.Selector{
Field: "host",
}, dagql.Selector{
Field: "directory",
Args: []dagql.NamedInput{
{
Name: "path",
Value: dagql.NewString(fileDir),
},
{
Name: "include",
Value: dagql.ArrayInput[dagql.String]{dagql.NewString(fileName)},
},
},
}, dagql.Selector{
Field: "file",
Args: []dagql.NamedInput{
{
Name: "path",
Value: dagql.NewString(fileName),
},
},
}); err != nil {
return i, err
}
return i, nil
}
func (host *Host) SetSecretFile(ctx context.Context, srv *dagql.Server, secretName string, path string) (i dagql.Instance[*Secret], err error) {
accessor, err := GetLocalSecretAccessor(ctx, host.Query, secretName)
if err != nil {
return i, err
}
secretFileContent, err := host.Query.Buildkit.ReadCallerHostFile(ctx, path)
if err != nil {
return i, fmt.Errorf("read secret file: %w", err)
}
if err := host.Query.Secrets.AddSecret(ctx, accessor, secretFileContent); err != nil {
return i, err
}
err = srv.Select(ctx, srv.Root(), &i, dagql.Selector{
Field: "secret",
Args: []dagql.NamedInput{
{
Name: "name",
Value: dagql.NewString(secretName),
},
{
Name: "accessor",
Value: dagql.Opt(dagql.NewString(accessor)),
},
},
})
return
}
func (host *Host) Socket(sockPath string) *Socket {
return NewHostUnixSocket(sockPath)
}