Skip to content

Commit 587c898

Browse files
csweichelaledbf
authored andcommitted
Fix darwin build
1 parent 9386054 commit 587c898

File tree

4 files changed

+152
-130
lines changed

4 files changed

+152
-130
lines changed

leeway

29.2 MB
Binary file not shown.

pkg/leeway/build.go

Lines changed: 0 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import (
2121
"time"
2222

2323
"github.com/in-toto/in-toto-golang/in_toto"
24-
"github.com/opencontainers/runc/libcontainer/specconv"
25-
"github.com/opencontainers/runtime-spec/specs-go"
2624
log "github.com/sirupsen/logrus"
2725
"golang.org/x/mod/modfile"
2826
"golang.org/x/sync/semaphore"
@@ -1607,134 +1605,6 @@ func executeCommandsForPackage(buildctx *buildContext, p *Package, wd string, co
16071605
return nil
16081606
}
16091607

1610-
func executeCommandsForPackageSafe(buildctx *buildContext, p *Package, wd string, commands [][]string) error {
1611-
tmpdir, err := os.MkdirTemp("", "leeway-*")
1612-
if err != nil {
1613-
return err
1614-
}
1615-
1616-
jc, err := json.Marshal(commands)
1617-
if err != nil {
1618-
return err
1619-
}
1620-
commandsFN := filepath.Join(tmpdir, "commands")
1621-
err = os.WriteFile(commandsFN, []byte(base64.StdEncoding.EncodeToString(jc)), 0644)
1622-
if err != nil {
1623-
return err
1624-
}
1625-
1626-
if !log.IsLevelEnabled(log.DebugLevel) {
1627-
defer os.RemoveAll(tmpdir)
1628-
}
1629-
1630-
log.WithField("tmpdir", tmpdir).WithField("package", p.FullName()).Debug("preparing build runc environment")
1631-
err = os.MkdirAll(filepath.Join(tmpdir, "rootfs"), 0755)
1632-
if err != nil {
1633-
return err
1634-
}
1635-
1636-
version, err := p.Version()
1637-
if err != nil {
1638-
return err
1639-
}
1640-
name := fmt.Sprintf("b%s", version)
1641-
1642-
spec := specconv.Example()
1643-
specconv.ToRootless(spec)
1644-
1645-
// we assemble the root filesystem from the outside world
1646-
for _, d := range []string{"home", "bin", "dev", "etc", "lib", "lib64", "opt", "sbin", "sys", "usr", "var"} {
1647-
spec.Mounts = append(spec.Mounts, specs.Mount{
1648-
Destination: "/" + d,
1649-
Source: "/" + d,
1650-
Type: "bind",
1651-
Options: []string{"rbind", "rprivate"},
1652-
})
1653-
}
1654-
1655-
spec.Mounts = append(spec.Mounts, specs.Mount{Destination: "/build", Source: wd, Type: "bind", Options: []string{"bind", "private"}})
1656-
spec.Mounts = append(spec.Mounts, specs.Mount{Destination: "/commands", Source: commandsFN, Type: "bind", Options: []string{"bind", "private"}})
1657-
1658-
for _, p := range []string{"tmp", "root"} {
1659-
fn := filepath.Join(tmpdir, p)
1660-
err = os.MkdirAll(fn, 0777)
1661-
if err != nil {
1662-
return err
1663-
}
1664-
spec.Mounts = append(spec.Mounts, specs.Mount{Destination: "/" + p, Source: fn, Type: "bind", Options: []string{"bind", "private"}})
1665-
}
1666-
1667-
buildCache, _ := buildctx.LocalCache.Location(p)
1668-
buildCache = filepath.Dir(buildCache)
1669-
spec.Mounts = append(spec.Mounts, specs.Mount{Destination: buildCache, Source: buildCache, Type: "bind", Options: []string{"bind", "private"}})
1670-
1671-
self, err := os.Executable()
1672-
if err != nil {
1673-
return err
1674-
}
1675-
spec.Mounts = append(spec.Mounts, specs.Mount{Destination: "/leeway", Source: self, Type: "bind", Options: []string{"bind", "private"}})
1676-
1677-
if p := os.Getenv("GOPATH"); p != "" {
1678-
spec.Mounts = append(spec.Mounts, specs.Mount{Destination: p, Source: p, Type: "bind", Options: []string{"bind", "private"}})
1679-
}
1680-
if p := os.Getenv("GOROOT"); p != "" {
1681-
spec.Mounts = append(spec.Mounts, specs.Mount{Destination: p, Source: p, Type: "bind", Options: []string{"bind", "private"}})
1682-
}
1683-
if p := os.Getenv("DOCKER_HOST"); strings.HasPrefix(p, "file://") {
1684-
p = strings.TrimPrefix(p, "file://")
1685-
spec.Mounts = append(spec.Mounts, specs.Mount{Destination: p, Source: p, Type: "bind", Options: []string{"bind", "private"}})
1686-
} else if _, err := os.Stat("/var/run/docker.sock"); err == nil {
1687-
p = "/var/run/docker.sock"
1688-
spec.Mounts = append(spec.Mounts, specs.Mount{Destination: p, Source: p, Type: "bind", Options: []string{"bind", "private"}})
1689-
}
1690-
1691-
var env []string
1692-
for _, e := range []string{"PATH", "TERM", "GOROOT", "GOPATH"} {
1693-
val := os.Getenv(e)
1694-
if val == "" {
1695-
continue
1696-
}
1697-
env = append(env, fmt.Sprintf("%s=%s", e, val))
1698-
}
1699-
1700-
spec.Hostname = name
1701-
spec.Process.Terminal = false
1702-
spec.Process.NoNewPrivileges = true
1703-
spec.Process.Args = []string{"/leeway", "plumbing", "exec", "/commands"}
1704-
if log.IsLevelEnabled(log.DebugLevel) {
1705-
spec.Process.Args = append(spec.Process.Args, "--verbose")
1706-
1707-
}
1708-
spec.Process.Cwd = "/build"
1709-
spec.Process.Env = env
1710-
1711-
fc, err := json.MarshalIndent(spec, "", " ")
1712-
if err != nil {
1713-
return err
1714-
}
1715-
err = os.WriteFile(filepath.Join(tmpdir, "config.json"), fc, 0644)
1716-
if err != nil {
1717-
return err
1718-
}
1719-
1720-
args := []string{
1721-
"--root", "state",
1722-
"--log-format", "json",
1723-
}
1724-
if log.IsLevelEnabled(log.DebugLevel) {
1725-
args = append(args, "--debug")
1726-
}
1727-
args = append(args,
1728-
"run", name,
1729-
)
1730-
1731-
cmd := exec.Command("runc", args...)
1732-
cmd.Dir = tmpdir
1733-
cmd.Stdout = &reporterStream{R: buildctx.Reporter, P: p, IsErr: false}
1734-
cmd.Stderr = &reporterStream{R: buildctx.Reporter, P: p, IsErr: true}
1735-
return cmd.Run()
1736-
}
1737-
17381608
func run(rep Reporter, p *Package, env []string, cwd, name string, args ...string) error {
17391609
log.WithField("package", p.FullName()).WithField("command", strings.Join(append([]string{name}, args...), " ")).Debug("running")
17401610

pkg/leeway/build_darwin.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package leeway
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func executeCommandsForPackageSafe(buildctx *buildContext, p *Package, wd string, commands [][]string) error {
8+
return fmt.Errorf("not implemented")
9+
}

pkg/leeway/build_linux.go

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package leeway
2+
3+
import (
4+
"encoding/base64"
5+
"encoding/json"
6+
"fmt"
7+
"os"
8+
"os/exec"
9+
"path/filepath"
10+
"strings"
11+
12+
"github.com/opencontainers/runc/libcontainer/specconv"
13+
"github.com/opencontainers/runtime-spec/specs-go"
14+
log "github.com/sirupsen/logrus"
15+
)
16+
17+
func executeCommandsForPackageSafe(buildctx *buildContext, p *Package, wd string, commands [][]string) error {
18+
tmpdir, err := os.MkdirTemp("", "leeway-*")
19+
if err != nil {
20+
return err
21+
}
22+
23+
jc, err := json.Marshal(commands)
24+
if err != nil {
25+
return err
26+
}
27+
commandsFN := filepath.Join(tmpdir, "commands")
28+
err = os.WriteFile(commandsFN, []byte(base64.StdEncoding.EncodeToString(jc)), 0644)
29+
if err != nil {
30+
return err
31+
}
32+
33+
if !log.IsLevelEnabled(log.DebugLevel) {
34+
defer os.RemoveAll(tmpdir)
35+
}
36+
37+
log.WithField("tmpdir", tmpdir).WithField("package", p.FullName()).Debug("preparing build runc environment")
38+
err = os.MkdirAll(filepath.Join(tmpdir, "rootfs"), 0755)
39+
if err != nil {
40+
return err
41+
}
42+
43+
version, err := p.Version()
44+
if err != nil {
45+
return err
46+
}
47+
name := fmt.Sprintf("b%s", version)
48+
49+
spec := specconv.Example()
50+
specconv.ToRootless(spec)
51+
52+
// we assemble the root filesystem from the outside world
53+
for _, d := range []string{"home", "bin", "dev", "etc", "lib", "lib64", "opt", "sbin", "sys", "usr", "var"} {
54+
spec.Mounts = append(spec.Mounts, specs.Mount{
55+
Destination: "/" + d,
56+
Source: "/" + d,
57+
Type: "bind",
58+
Options: []string{"rbind", "rprivate"},
59+
})
60+
}
61+
62+
spec.Mounts = append(spec.Mounts, specs.Mount{Destination: "/build", Source: wd, Type: "bind", Options: []string{"bind", "private"}})
63+
spec.Mounts = append(spec.Mounts, specs.Mount{Destination: "/commands", Source: commandsFN, Type: "bind", Options: []string{"bind", "private"}})
64+
65+
for _, p := range []string{"tmp", "root"} {
66+
fn := filepath.Join(tmpdir, p)
67+
err = os.MkdirAll(fn, 0777)
68+
if err != nil {
69+
return err
70+
}
71+
spec.Mounts = append(spec.Mounts, specs.Mount{Destination: "/" + p, Source: fn, Type: "bind", Options: []string{"bind", "private"}})
72+
}
73+
74+
buildCache, _ := buildctx.LocalCache.Location(p)
75+
buildCache = filepath.Dir(buildCache)
76+
spec.Mounts = append(spec.Mounts, specs.Mount{Destination: buildCache, Source: buildCache, Type: "bind", Options: []string{"bind", "private"}})
77+
78+
self, err := os.Executable()
79+
if err != nil {
80+
return err
81+
}
82+
spec.Mounts = append(spec.Mounts, specs.Mount{Destination: "/leeway", Source: self, Type: "bind", Options: []string{"bind", "private"}})
83+
84+
if p := os.Getenv("GOPATH"); p != "" {
85+
spec.Mounts = append(spec.Mounts, specs.Mount{Destination: p, Source: p, Type: "bind", Options: []string{"bind", "private"}})
86+
}
87+
if p := os.Getenv("GOROOT"); p != "" {
88+
spec.Mounts = append(spec.Mounts, specs.Mount{Destination: p, Source: p, Type: "bind", Options: []string{"bind", "private"}})
89+
}
90+
if p := os.Getenv("DOCKER_HOST"); strings.HasPrefix(p, "file://") {
91+
p = strings.TrimPrefix(p, "file://")
92+
spec.Mounts = append(spec.Mounts, specs.Mount{Destination: p, Source: p, Type: "bind", Options: []string{"bind", "private"}})
93+
} else if _, err := os.Stat("/var/run/docker.sock"); err == nil {
94+
p = "/var/run/docker.sock"
95+
spec.Mounts = append(spec.Mounts, specs.Mount{Destination: p, Source: p, Type: "bind", Options: []string{"bind", "private"}})
96+
}
97+
98+
var env []string
99+
for _, e := range []string{"PATH", "TERM", "GOROOT", "GOPATH"} {
100+
val := os.Getenv(e)
101+
if val == "" {
102+
continue
103+
}
104+
env = append(env, fmt.Sprintf("%s=%s", e, val))
105+
}
106+
107+
spec.Hostname = name
108+
spec.Process.Terminal = false
109+
spec.Process.NoNewPrivileges = true
110+
spec.Process.Args = []string{"/leeway", "plumbing", "exec", "/commands"}
111+
if log.IsLevelEnabled(log.DebugLevel) {
112+
spec.Process.Args = append(spec.Process.Args, "--verbose")
113+
114+
}
115+
spec.Process.Cwd = "/build"
116+
spec.Process.Env = env
117+
118+
fc, err := json.MarshalIndent(spec, "", " ")
119+
if err != nil {
120+
return err
121+
}
122+
err = os.WriteFile(filepath.Join(tmpdir, "config.json"), fc, 0644)
123+
if err != nil {
124+
return err
125+
}
126+
127+
args := []string{
128+
"--root", "state",
129+
"--log-format", "json",
130+
}
131+
if log.IsLevelEnabled(log.DebugLevel) {
132+
args = append(args, "--debug")
133+
}
134+
args = append(args,
135+
"run", name,
136+
)
137+
138+
cmd := exec.Command("runc", args...)
139+
cmd.Dir = tmpdir
140+
cmd.Stdout = &reporterStream{R: buildctx.Reporter, P: p, IsErr: false}
141+
cmd.Stderr = &reporterStream{R: buildctx.Reporter, P: p, IsErr: true}
142+
return cmd.Run()
143+
}

0 commit comments

Comments
 (0)