2020-09-21 08:44:16 +08:00
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-03-10 08:46:06 +08:00
|
|
|
"os"
|
2020-09-21 08:44:16 +08:00
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/docker/buildx/driver"
|
|
|
|
"github.com/docker/buildx/util/progress"
|
|
|
|
"github.com/moby/buildkit/client"
|
|
|
|
"github.com/moby/buildkit/client/llb"
|
|
|
|
gwclient "github.com/moby/buildkit/frontend/gateway/client"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2023-06-13 08:39:09 +08:00
|
|
|
func createTempDockerfileFromURL(ctx context.Context, d *driver.DriverHandle, url string, pw progress.Writer) (string, error) {
|
2021-08-20 11:36:24 +08:00
|
|
|
c, err := driver.Boot(ctx, ctx, d, pw)
|
2020-09-21 08:44:16 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
var out string
|
2020-10-17 13:49:29 +08:00
|
|
|
ch, done := progress.NewChannel(pw)
|
|
|
|
defer func() { <-done }()
|
2023-07-19 08:46:15 +08:00
|
|
|
_, err = c.Build(ctx, client.SolveOpt{Internal: true}, "buildx", func(ctx context.Context, c gwclient.Client) (*gwclient.Result, error) {
|
2020-09-21 08:44:16 +08:00
|
|
|
def, err := llb.HTTP(url, llb.Filename("Dockerfile"), llb.WithCustomNamef("[internal] load %s", url)).Marshal(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := c.Solve(ctx, gwclient.SolveRequest{
|
|
|
|
Definition: def.ToPB(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ref, err := res.SingleRef()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
stat, err := ref.StatFile(ctx, gwclient.StatRequest{
|
|
|
|
Path: "Dockerfile",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if stat.Size() > 512*1024 {
|
|
|
|
return nil, errors.Errorf("Dockerfile %s bigger than allowed max size", url)
|
|
|
|
}
|
|
|
|
|
|
|
|
dt, err := ref.ReadFile(ctx, gwclient.ReadRequest{
|
|
|
|
Filename: "Dockerfile",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-10 08:46:06 +08:00
|
|
|
dir, err := os.MkdirTemp("", "buildx")
|
2020-09-21 08:44:16 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-10 08:46:06 +08:00
|
|
|
if err := os.WriteFile(filepath.Join(dir, "Dockerfile"), dt, 0600); err != nil {
|
2020-09-21 08:44:16 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
out = dir
|
|
|
|
return nil, nil
|
2020-10-17 13:49:29 +08:00
|
|
|
}, ch)
|
2020-09-21 08:44:16 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|