Skip to content

Commit

Permalink
refactor: Bind stacker binary into container in SetupBuildContainerCo…
Browse files Browse the repository at this point in the history
…nfig

Previously there were 3 individual calls to bind stacker into
tools/static-stacker.  This just changes that to 1 place, so
after calling SetupBuildContainerConfig you can use stacker inside.

Signed-off-by: Scott Moser <smoser@brickies.net>
  • Loading branch information
Scott Moser authored and smoser committed Aug 22, 2023
1 parent 42c1b73 commit ff4944c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 46 deletions.
22 changes: 2 additions & 20 deletions pkg/stacker/bom.go
Expand Up @@ -38,16 +38,7 @@ func BuildLayerArtifacts(sc types.StackerConfig, storage types.Storage, l types.
return err
}

binary, err := os.Readlink("/proc/self/exe")
if err != nil {
return err
}

if err := c.BindMount(binary, "/stacker/tools/static-stacker", ""); err != nil {
return err
}

cmd := []string{"/stacker/tools/static-stacker"}
cmd := []string{insideStaticStacker}

if sc.Debug {
cmd = append(cmd, "--debug")
Expand Down Expand Up @@ -92,16 +83,7 @@ func VerifyLayerArtifacts(sc types.StackerConfig, storage types.Storage, l types
return err
}

binary, err := os.Readlink("/proc/self/exe")
if err != nil {
return err
}

if err := c.BindMount(binary, "/stacker/tools/static-stacker", ""); err != nil {
return err
}

cmd := []string{"/stacker/tools/static-stacker"}
cmd := []string{insideStaticStacker}

if sc.Debug {
cmd = append(cmd, "--debug")
Expand Down
28 changes: 14 additions & 14 deletions pkg/stacker/build.go
Expand Up @@ -21,7 +21,10 @@ import (
"stackerbuild.io/stacker/pkg/types"
)

const DefaultShell = "/bin/sh"
const (
DefaultShell = "/bin/sh"
insideStaticStacker = "/stacker/tools/static-stacker"
)

type BuildArgs struct {
Config types.StackerConfig
Expand Down Expand Up @@ -687,6 +690,16 @@ func SetupBuildContainerConfig(config types.StackerConfig, storage types.Storage
return err
}

binary, err := os.Readlink("/proc/self/exe")
if err != nil {
return err
}

// make stacker binary available inside container
if err := c.BindMount(binary, insideStaticStacker, ""); err != nil {
return err
}

rootfs, err := storage.GetLXCRootfsConfig(name)
if err != nil {
return err
Expand Down Expand Up @@ -749,19 +762,6 @@ func SetupLayerConfig(config types.StackerConfig, c *container.Container, l type
} else {
log.Debugf("not bind mounting %s into container", artifactsDir)
}

// make stacker also available to run the internal bom cmds
binary, err := os.Readlink("/proc/self/exe")
if err != nil {
return errors.Wrapf(err, "couldn't find executable for bind mount")
}

err = c.BindMount(binary, "/stacker/tools/static-stacker", "")
if err != nil {
return err
}

log.Debugf("bind mounting %s into container", binary)
}

for k, v := range env {
Expand Down
13 changes: 1 addition & 12 deletions pkg/stacker/grab.go
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"path"

"github.com/pkg/errors"
"stackerbuild.io/stacker/pkg/container"
"stackerbuild.io/stacker/pkg/types"
)
Expand All @@ -26,22 +25,12 @@ func Grab(sc types.StackerConfig, storage types.Storage, name string, source str
}
defer os.Remove(path.Join(sc.RootFSDir, name, "rootfs", "stacker"))

binary, err := os.Readlink("/proc/self/exe")
if err != nil {
return errors.Wrapf(err, "couldn't find executable for bind mount")
}

err = c.BindMount(binary, "/stacker/tools/static-stacker", "")
if err != nil {
return err
}

err = SetupBuildContainerConfig(sc, storage, c, name)
if err != nil {
return err
}

bcmd := []string{"/stacker/tools/static-stacker", "internal-go"}
bcmd := []string{insideStaticStacker, "internal-go"}
err = c.Execute(append(bcmd, "cp", source, "/stacker/"+path.Base(source)), nil)
if err != nil {
return err
Expand Down

0 comments on commit ff4944c

Please sign in to comment.