|
@@ -8,6 +8,7 @@ import (
|
|
|
"os"
|
|
|
"path/filepath"
|
|
|
|
|
|
+ "github.com/docker/docker/pkg/idtools"
|
|
|
"github.com/moby/buildkit/executor"
|
|
|
"github.com/moby/buildkit/identity"
|
|
|
)
|
|
@@ -17,10 +18,10 @@ const hostsContent = `
|
|
|
::1 localhost ip6-localhost ip6-loopback
|
|
|
`
|
|
|
|
|
|
-func GetHostsFile(ctx context.Context, stateDir string, extraHosts []executor.HostIP) (string, func(), error) {
|
|
|
+func GetHostsFile(ctx context.Context, stateDir string, extraHosts []executor.HostIP, idmap *idtools.IdentityMapping) (string, func(), error) {
|
|
|
if len(extraHosts) == 0 {
|
|
|
_, err := g.Do(ctx, stateDir, func(ctx context.Context) (interface{}, error) {
|
|
|
- _, _, err := makeHostsFile(stateDir, nil)
|
|
|
+ _, _, err := makeHostsFile(stateDir, nil, idmap)
|
|
|
return nil, err
|
|
|
})
|
|
|
if err != nil {
|
|
@@ -28,10 +29,10 @@ func GetHostsFile(ctx context.Context, stateDir string, extraHosts []executor.Ho
|
|
|
}
|
|
|
return filepath.Join(stateDir, "hosts"), func() {}, nil
|
|
|
}
|
|
|
- return makeHostsFile(stateDir, extraHosts)
|
|
|
+ return makeHostsFile(stateDir, extraHosts, idmap)
|
|
|
}
|
|
|
|
|
|
-func makeHostsFile(stateDir string, extraHosts []executor.HostIP) (string, func(), error) {
|
|
|
+func makeHostsFile(stateDir string, extraHosts []executor.HostIP, idmap *idtools.IdentityMapping) (string, func(), error) {
|
|
|
p := filepath.Join(stateDir, "hosts")
|
|
|
if len(extraHosts) != 0 {
|
|
|
p += "." + identity.NewID()
|
|
@@ -56,11 +57,19 @@ func makeHostsFile(stateDir string, extraHosts []executor.HostIP) (string, func(
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if err := ioutil.WriteFile(p+".tmp", b.Bytes(), 0644); err != nil {
|
|
|
+ tmpPath := p + ".tmp"
|
|
|
+ if err := ioutil.WriteFile(tmpPath, b.Bytes(), 0644); err != nil {
|
|
|
return "", nil, err
|
|
|
}
|
|
|
|
|
|
- if err := os.Rename(p+".tmp", p); err != nil {
|
|
|
+ if idmap != nil {
|
|
|
+ root := idmap.RootPair()
|
|
|
+ if err := os.Chown(tmpPath, root.UID, root.GID); err != nil {
|
|
|
+ return "", nil, err
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := os.Rename(tmpPath, p); err != nil {
|
|
|
return "", nil, err
|
|
|
}
|
|
|
return p, func() {
|