@@ -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-
17381608func 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
0 commit comments