-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
89 lines (73 loc) 路 2.01 KB
/
main.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
package main
import (
"context"
_ "embed"
"encoding/json"
"fmt"
"os"
"strings"
"github.com/Azure/dalec/frontend"
"github.com/moby/buildkit/client/llb"
gwclient "github.com/moby/buildkit/frontend/gateway/client"
"github.com/moby/buildkit/frontend/gateway/grpcclient"
"github.com/moby/buildkit/util/appcontext"
"github.com/moby/buildkit/util/bklog"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/grpclog"
)
func main() {
bklog.L.Logger.SetOutput(os.Stderr)
grpclog.SetLoggerV2(grpclog.NewLoggerV2WithVerbosity(bklog.L.WriterLevel(logrus.InfoLevel), bklog.L.WriterLevel(logrus.WarnLevel), bklog.L.WriterLevel(logrus.ErrorLevel), 1))
ctx := appcontext.Context()
if err := grpcclient.RunFromEnvironment(ctx, func(ctx context.Context, c gwclient.Client) (*gwclient.Result, error) {
bopts := c.BuildOpts().Opts
target := bopts["dalec.target"]
inputs, err := c.Inputs(ctx)
if err != nil {
return nil, err
}
type config struct {
OS string
}
cfg := config{}
switch target {
case "windowscross", "windows":
cfg.OS = "windows"
default:
cfg.OS = "linux"
}
curFrontend, ok := c.(frontend.CurrentFrontend)
if !ok {
return nil, fmt.Errorf("cast to currentFrontend failed")
}
basePtr, err := curFrontend.CurrentFrontend()
if err != nil || basePtr == nil {
if err == nil {
err = fmt.Errorf("base frontend ptr was nil")
}
return nil, err
}
inputId := strings.TrimPrefix(bopts["context"], "input:")
_, ok = inputs[inputId]
if !ok {
return nil, fmt.Errorf("no artifact state provided to signer")
}
configBytes, err := json.Marshal(&cfg)
if err != nil {
return nil, err
}
output := llb.Scratch().
File(llb.Mkfile("/target", 0o600, []byte(target))).
File(llb.Mkfile("/config.json", 0o600, configBytes))
def, err := output.Marshal(ctx)
if err != nil {
return nil, err
}
return c.Solve(ctx, gwclient.SolveRequest{
Definition: def.ToPB(),
})
}); err != nil {
bklog.L.WithError(err).Fatal("error running frontend")
os.Exit(137)
}
}