2021-11-04 12:17:27 +08:00
|
|
|
package storeutil
|
|
|
|
|
|
|
|
import (
|
2021-11-04 13:41:57 +08:00
|
|
|
"bytes"
|
2021-11-04 12:17:27 +08:00
|
|
|
"os"
|
2021-11-04 13:41:57 +08:00
|
|
|
"strings"
|
2021-11-04 12:17:27 +08:00
|
|
|
|
|
|
|
"github.com/docker/buildx/store"
|
|
|
|
"github.com/docker/buildx/util/confutil"
|
2022-11-28 22:47:40 +08:00
|
|
|
"github.com/docker/buildx/util/dockerutil"
|
2021-11-04 12:17:27 +08:00
|
|
|
"github.com/docker/buildx/util/imagetools"
|
2021-11-04 13:41:57 +08:00
|
|
|
"github.com/docker/buildx/util/resolver"
|
2021-11-04 12:17:27 +08:00
|
|
|
"github.com/docker/cli/cli/command"
|
2021-11-04 13:41:57 +08:00
|
|
|
buildkitdconfig "github.com/moby/buildkit/cmd/buildkitd/config"
|
2021-11-04 12:17:27 +08:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetStore returns current builder instance store
|
|
|
|
func GetStore(dockerCli command.Cli) (*store.Txn, func(), error) {
|
2024-10-18 19:28:45 +08:00
|
|
|
s, err := store.New(confutil.NewConfig(dockerCli))
|
2021-11-04 12:17:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return s.Txn()
|
|
|
|
}
|
|
|
|
|
2022-02-18 14:21:08 +08:00
|
|
|
func GetProxyConfig(dockerCli command.Cli) map[string]string {
|
|
|
|
cfg := dockerCli.ConfigFile()
|
|
|
|
host := dockerCli.Client().DaemonHost()
|
|
|
|
|
|
|
|
proxy, ok := cfg.Proxies[host]
|
|
|
|
if !ok {
|
|
|
|
proxy = cfg.Proxies["default"]
|
|
|
|
}
|
|
|
|
|
|
|
|
m := map[string]string{}
|
|
|
|
|
|
|
|
if v := proxy.HTTPProxy; v != "" {
|
|
|
|
m["HTTP_PROXY"] = v
|
|
|
|
}
|
|
|
|
if v := proxy.HTTPSProxy; v != "" {
|
|
|
|
m["HTTPS_PROXY"] = v
|
|
|
|
}
|
|
|
|
if v := proxy.NoProxy; v != "" {
|
|
|
|
m["NO_PROXY"] = v
|
|
|
|
}
|
|
|
|
if v := proxy.FTPProxy; v != "" {
|
|
|
|
m["FTP_PROXY"] = v
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2021-11-04 12:17:27 +08:00
|
|
|
// GetCurrentInstance finds the current builder instance
|
|
|
|
func GetCurrentInstance(txn *store.Txn, dockerCli command.Cli) (*store.NodeGroup, error) {
|
2022-11-28 22:47:40 +08:00
|
|
|
ep, err := dockerutil.GetCurrentEndpoint(dockerCli)
|
2021-11-04 12:17:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ng, err := txn.Current(ep)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if ng == nil {
|
2023-04-12 00:19:11 +08:00
|
|
|
ng, err = GetNodeGroup(txn, dockerCli, dockerCli.CurrentContext())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-04 12:17:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ng, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNodeGroup returns nodegroup based on the name
|
|
|
|
func GetNodeGroup(txn *store.Txn, dockerCli command.Cli, name string) (*store.NodeGroup, error) {
|
|
|
|
ng, err := txn.NodeGroupByName(name)
|
|
|
|
if err != nil {
|
2023-06-14 18:07:31 +08:00
|
|
|
if !os.IsNotExist(errors.Cause(err)) && !store.IsErrInvalidName(err) {
|
2021-11-04 12:17:27 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ng != nil {
|
|
|
|
return ng, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
list, err := dockerCli.ContextStore().List()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, l := range list {
|
|
|
|
if l.Name == name {
|
2022-12-07 18:44:33 +08:00
|
|
|
ng = &store.NodeGroup{
|
2022-12-07 15:51:22 +08:00
|
|
|
Name: name,
|
2021-11-04 12:17:27 +08:00
|
|
|
Nodes: []store.Node{
|
|
|
|
{
|
2022-12-07 15:51:22 +08:00
|
|
|
Name: name,
|
2021-11-04 12:17:27 +08:00
|
|
|
Endpoint: name,
|
|
|
|
},
|
|
|
|
},
|
2023-02-04 04:23:29 +08:00
|
|
|
DockerContext: true,
|
2022-12-07 18:44:33 +08:00
|
|
|
}
|
|
|
|
if ng.LastActivity, err = txn.GetLastActivity(ng); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ng, nil
|
2021-11-04 12:17:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.Errorf("no builder %q found", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetImageConfig(dockerCli command.Cli, ng *store.NodeGroup) (opt imagetools.Opt, err error) {
|
|
|
|
opt.Auth = dockerCli.ConfigFile()
|
|
|
|
|
2021-11-04 13:41:57 +08:00
|
|
|
if ng == nil || len(ng.Nodes) == 0 {
|
|
|
|
return opt, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
files := ng.Nodes[0].Files
|
|
|
|
|
|
|
|
dt, ok := files["buildkitd.toml"]
|
|
|
|
if !ok {
|
|
|
|
return opt, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := buildkitdconfig.Load(bytes.NewReader(dt))
|
|
|
|
if err != nil {
|
|
|
|
return opt, err
|
|
|
|
}
|
|
|
|
|
|
|
|
regconfig := make(map[string]resolver.RegistryConfig)
|
|
|
|
|
|
|
|
for k, v := range config.Registries {
|
|
|
|
rc := resolver.RegistryConfig{
|
|
|
|
Mirrors: v.Mirrors,
|
|
|
|
PlainHTTP: v.PlainHTTP,
|
|
|
|
Insecure: v.Insecure,
|
|
|
|
}
|
|
|
|
for _, ca := range v.RootCAs {
|
|
|
|
dt, ok := files[strings.TrimPrefix(ca, confutil.DefaultBuildKitConfigDir+"/")]
|
|
|
|
if ok {
|
|
|
|
rc.RootCAs = append(rc.RootCAs, dt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kp := range v.KeyPairs {
|
|
|
|
key, keyok := files[strings.TrimPrefix(kp.Key, confutil.DefaultBuildKitConfigDir+"/")]
|
|
|
|
cert, certok := files[strings.TrimPrefix(kp.Certificate, confutil.DefaultBuildKitConfigDir+"/")]
|
|
|
|
if keyok && certok {
|
|
|
|
rc.KeyPairs = append(rc.KeyPairs, resolver.TLSKeyPair{
|
|
|
|
Key: key,
|
|
|
|
Certificate: cert,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
regconfig[k] = rc
|
|
|
|
}
|
|
|
|
|
|
|
|
opt.RegistryConfig = regconfig
|
|
|
|
|
2021-11-04 12:17:27 +08:00
|
|
|
return opt, nil
|
|
|
|
}
|