2017-05-15 17:14:31 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
2018-06-08 05:26:10 -04:00
|
|
|
"context"
|
2017-05-15 17:14:31 -04:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
cliconfig "github.com/docker/cli/cli/config"
|
|
|
|
"github.com/docker/docker/api/types/versions"
|
2017-08-07 05:52:40 -04:00
|
|
|
"github.com/moby/buildkit/session"
|
2017-05-15 17:14:31 -04:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
const clientSessionRemote = "client-session"
|
|
|
|
|
2018-10-22 16:43:36 -04:00
|
|
|
func isSessionSupported(dockerCli command.Cli, forStream bool) bool {
|
|
|
|
if !forStream && versions.GreaterThanOrEqualTo(dockerCli.Client().ClientVersion(), "1.39") {
|
2018-08-21 18:51:19 -04:00
|
|
|
return true
|
|
|
|
}
|
2017-05-15 17:14:31 -04:00
|
|
|
return dockerCli.ServerInfo().HasExperimental && versions.GreaterThanOrEqualTo(dockerCli.Client().ClientVersion(), "1.31")
|
|
|
|
}
|
|
|
|
|
2018-10-22 16:43:36 -04:00
|
|
|
func trySession(dockerCli command.Cli, contextDir string, forStream bool) (*session.Session, error) {
|
2019-04-02 07:22:30 -04:00
|
|
|
if !isSessionSupported(dockerCli, forStream) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
sharedKey := getBuildSharedKey(contextDir)
|
|
|
|
s, err := session.NewSession(context.Background(), filepath.Base(contextDir), sharedKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to create session")
|
2017-05-15 17:14:31 -04:00
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2019-04-02 07:22:30 -04:00
|
|
|
func getBuildSharedKey(dir string) string {
|
2017-05-15 17:14:31 -04:00
|
|
|
// build session is hash of build dir with node based randomness
|
|
|
|
s := sha256.Sum256([]byte(fmt.Sprintf("%s:%s", tryNodeIdentifier(), dir)))
|
2019-04-02 07:22:30 -04:00
|
|
|
return hex.EncodeToString(s[:])
|
2017-05-15 17:14:31 -04:00
|
|
|
}
|
|
|
|
|
2017-10-12 11:44:03 -04:00
|
|
|
func tryNodeIdentifier() string {
|
|
|
|
out := cliconfig.Dir() // return config dir as default on permission error
|
2017-05-15 17:14:31 -04:00
|
|
|
if err := os.MkdirAll(cliconfig.Dir(), 0700); err == nil {
|
|
|
|
sessionFile := filepath.Join(cliconfig.Dir(), ".buildNodeID")
|
|
|
|
if _, err := os.Lstat(sessionFile); err != nil {
|
|
|
|
if os.IsNotExist(err) { // create a new file with stored randomness
|
|
|
|
b := make([]byte, 32)
|
|
|
|
if _, err := rand.Read(b); err != nil {
|
2017-10-12 11:44:03 -04:00
|
|
|
return out
|
2017-05-15 17:14:31 -04:00
|
|
|
}
|
2022-02-25 07:10:34 -05:00
|
|
|
if err := os.WriteFile(sessionFile, []byte(hex.EncodeToString(b)), 0600); err != nil {
|
2017-10-12 11:44:03 -04:00
|
|
|
return out
|
2017-05-15 17:14:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 07:10:34 -05:00
|
|
|
dt, err := os.ReadFile(sessionFile)
|
2017-05-15 17:14:31 -04:00
|
|
|
if err == nil {
|
|
|
|
return string(dt)
|
|
|
|
}
|
|
|
|
}
|
2017-10-12 11:44:03 -04:00
|
|
|
return out
|
2017-05-15 17:14:31 -04:00
|
|
|
}
|